711: Rename FnScopes and move them below the expr module r=matklad a=flodiebold

Extracted from #693 to reduce the diff and make rebasing easier for me 😉 

The scopes belong to a body, which could be that of a function, but also a constant, static or array size. So this moves them to a submodule of `expr`.

Also move the `expr_scopes` query from `query_definitions` to that module.

Co-authored-by: Florian Diebold <flodiebold@gmail.com>
This commit is contained in:
bors[bot] 2019-01-30 20:45:35 +00:00
commit db17e06c2e
8 changed files with 38 additions and 32 deletions

View file

@ -396,7 +396,7 @@ pub struct Function {
pub(crate) id: FunctionId,
}
pub use crate::code_model_impl::function::ScopeEntryWithSyntax;
pub use crate::expr::ScopeEntryWithSyntax;
/// The declared signature of a function.
#[derive(Debug, Clone, PartialEq, Eq)]
@ -447,7 +447,7 @@ impl Function {
}
pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping {
let scopes = db.fn_scopes(*self);
let scopes = db.expr_scopes(*self);
let syntax_mapping = db.body_syntax_mapping(*self);
ScopesWithSyntaxMapping {
scopes,

View file

@ -1,5 +1,3 @@
mod scope;
use std::sync::Arc;
use ra_syntax::ast::{self, NameOwner};
@ -11,8 +9,6 @@ use crate::{
impl_block::ImplBlock,
};
pub use self::scope::{FnScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
impl Function {
pub(crate) fn body(&self, db: &impl HirDatabase) -> Arc<Body> {
db.body_hir(*self)

View file

@ -7,7 +7,7 @@ use crate::{
MacroCallId, HirFileId,
SourceFileItems, SourceItemId, Crate, Module, HirInterner,
query_definitions,
Function, FnSignature, FnScopes,
Function, FnSignature, ExprScopes,
Struct, Enum, StructField,
macros::MacroExpansion,
module_tree::ModuleTree,
@ -27,8 +27,8 @@ pub trait HirDatabase: SourceDatabase + AsRef<HirInterner> {
#[salsa::invoke(crate::macros::expand_macro_invocation)]
fn expand_macro_invocation(&self, invoc: MacroCallId) -> Option<Arc<MacroExpansion>>;
#[salsa::invoke(query_definitions::fn_scopes)]
fn fn_scopes(&self, func: Function) -> Arc<FnScopes>;
#[salsa::invoke(ExprScopes::expr_scopes_query)]
fn expr_scopes(&self, func: Function) -> Arc<ExprScopes>;
#[salsa::invoke(crate::adt::StructData::struct_data_query)]
fn struct_data(&self, s: Struct) -> Arc<StructData>;

View file

@ -16,6 +16,10 @@ use crate::{
};
use crate::ty::primitive::{UintTy, UncertainIntTy, UncertainFloatTy};
pub use self::scope::{ExprScopes, ScopesWithSyntaxMapping, ScopeEntryWithSyntax};
mod scope;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ExprId(RawId);
impl_arena_id!(ExprId);

View file

@ -9,14 +9,18 @@ use ra_syntax::{
};
use ra_arena::{Arena, RawId, impl_arena_id};
use crate::{Name, AsName, expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySyntaxMapping}};
use crate::{
Name, AsName, Function,
expr::{PatId, ExprId, Pat, Expr, Body, Statement, BodySyntaxMapping},
db::HirDatabase,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ScopeId(RawId);
impl_arena_id!(ScopeId);
#[derive(Debug, PartialEq, Eq)]
pub struct FnScopes {
pub struct ExprScopes {
body: Arc<Body>,
scopes: Arena<ScopeId, ScopeData>,
scope_for: FxHashMap<ExprId, ScopeId>,
@ -34,9 +38,16 @@ pub struct ScopeData {
entries: Vec<ScopeEntry>,
}
impl FnScopes {
pub(crate) fn new(body: Arc<Body>) -> FnScopes {
let mut scopes = FnScopes {
impl ExprScopes {
// TODO: This should take something more general than Function
pub(crate) fn expr_scopes_query(db: &impl HirDatabase, function: Function) -> Arc<ExprScopes> {
let body = db.body_hir(function);
let res = ExprScopes::new(body);
Arc::new(res)
}
fn new(body: Arc<Body>) -> ExprScopes {
let mut scopes = ExprScopes {
body: body.clone(),
scopes: Arena::default(),
scope_for: FxHashMap::default(),
@ -119,7 +130,7 @@ impl FnScopes {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScopesWithSyntaxMapping {
pub syntax_mapping: Arc<BodySyntaxMapping>,
pub scopes: Arc<FnScopes>,
pub scopes: Arc<ExprScopes>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@ -249,7 +260,7 @@ fn compute_block_scopes(
statements: &[Statement],
tail: Option<ExprId>,
body: &Body,
scopes: &mut FnScopes,
scopes: &mut ExprScopes,
mut scope: ScopeId,
) {
for stmt in statements {
@ -275,7 +286,7 @@ fn compute_block_scopes(
}
}
fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut FnScopes, scope: ScopeId) {
fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: ScopeId) {
scopes.set_scope(expr, scope);
match &body[expr] {
Expr::Block { statements, tail } => {
@ -344,7 +355,7 @@ mod tests {
let marker: &ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
let fn_def: &ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
let body_hir = expr::collect_fn_body_syntax(fn_def);
let scopes = FnScopes::new(Arc::clone(body_hir.body()));
let scopes = ExprScopes::new(Arc::clone(body_hir.body()));
let scopes = ScopesWithSyntaxMapping {
scopes: Arc::new(scopes),
syntax_mapping: Arc::new(body_hir),
@ -444,7 +455,7 @@ mod tests {
let name_ref: &ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();
let body_hir = expr::collect_fn_body_syntax(fn_def);
let scopes = FnScopes::new(Arc::clone(body_hir.body()));
let scopes = ExprScopes::new(Arc::clone(body_hir.body()));
let scopes = ScopesWithSyntaxMapping {
scopes: Arc::new(scopes),
syntax_mapping: Arc::new(body_hir),

View file

@ -57,9 +57,9 @@ pub use self::{
nameres::{ItemMap, PerNs, Namespace, Resolution},
ty::Ty,
impl_block::{ImplBlock, ImplItem},
code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping},
docs::{Docs, Documentation},
adt::AdtDef,
expr::{ExprScopes, ScopesWithSyntaxMapping},
};
pub use self::code_model_api::{

View file

@ -1,19 +1,14 @@
use std::sync::Arc;
use ra_syntax::{SyntaxNode, TreeArc};
use ra_syntax::{
SyntaxNode, TreeArc,
};
use crate::{
SourceFileItems, SourceItemId, HirFileId,
Function, FnScopes,
db::HirDatabase,
};
pub(super) fn fn_scopes(db: &impl HirDatabase, func: Function) -> Arc<FnScopes> {
let body = db.body_hir(func);
let res = FnScopes::new(body);
Arc::new(res)
}
pub(super) fn file_items(db: &impl HirDatabase, file_id: HirFileId) -> Arc<SourceFileItems> {
let source_file = db.hir_parse(file_id);
let res = SourceFileItems::new(file_id, &source_file);

View file

@ -34,7 +34,7 @@ use test_utils::tested_by;
use crate::{
Module, Function, Struct, StructField, Enum, EnumVariant, Path, Name, ImplBlock,
FnSignature, FnScopes, ModuleDef, AdtDef,
FnSignature, ExprScopes, ModuleDef, AdtDef,
db::HirDatabase,
type_ref::{TypeRef, Mutability},
name::KnownName,
@ -814,7 +814,7 @@ impl Index<PatId> for InferenceResult {
struct InferenceContext<'a, D: HirDatabase> {
db: &'a D,
body: Arc<Body>,
scopes: Arc<FnScopes>,
scopes: Arc<ExprScopes>,
module: Module,
impl_block: Option<ImplBlock>,
var_unification_table: InPlaceUnificationTable<TypeVarId>,
@ -908,7 +908,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
fn new(
db: &'a D,
body: Arc<Body>,
scopes: Arc<FnScopes>,
scopes: Arc<ExprScopes>,
module: Module,
impl_block: Option<ImplBlock>,
) -> Self {
@ -1720,7 +1720,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
pub fn infer(db: &impl HirDatabase, func: Function) -> Arc<InferenceResult> {
db.check_canceled();
let body = func.body(db);
let scopes = db.fn_scopes(func);
let scopes = db.expr_scopes(func);
let module = func.module(db);
let impl_block = func.impl_block(db);
let mut ctx = InferenceContext::new(db, body, scopes, module, impl_block);