mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-02 15:56:09 +03:00
Merge #711
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:
@@ -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 @@ pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMappin
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
mod scope;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use ra_syntax::ast::{self, NameOwner};
|
||||
@@ -11,8 +9,6 @@
|
||||
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)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
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>;
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
};
|
||||
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);
|
||||
|
||||
+21
-10
@@ -9,14 +9,18 @@
|
||||
};
|
||||
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 @@ fn scope_for(&self, expr: ExprId) -> Option<ScopeId> {
|
||||
#[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 @@ fn do_check(code: &str, expected: &[&str]) {
|
||||
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 @@ fn do_check_local_name(code: &str, expected_offset: u32) {
|
||||
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),
|
||||
@@ -57,9 +57,9 @@ fn from(it: $v) -> $e {
|
||||
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::{
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
|
||||
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 @@ fn index(&self, pat: PatId) -> &Ty {
|
||||
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 @@ fn infer_body(&mut self) {
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user