Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/body.rs')
-rw-r--r--crates/hir-def/src/body.rs31
1 files changed, 23 insertions, 8 deletions
diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/body.rs
index ca4a3f5217..d3c134f326 100644
--- a/crates/hir-def/src/body.rs
+++ b/crates/hir-def/src/body.rs
@@ -6,13 +6,14 @@ pub mod scope;
#[cfg(test)]
mod tests;
-use std::ops::Index;
+use std::ops::{Deref, Index};
use base_db::CrateId;
use cfg::{CfgExpr, CfgOptions};
-use hir_expand::{name::Name, InFile};
-use la_arena::{Arena, ArenaMap};
+use hir_expand::{name::Name, ExpandError, InFile};
+use la_arena::{Arena, ArenaMap, Idx, RawIdx};
use rustc_hash::FxHashMap;
+use smallvec::SmallVec;
use span::MacroFileId;
use syntax::{ast, AstPtr, SyntaxNodePtr};
use triomphe::Arc;
@@ -23,6 +24,7 @@ use crate::{
hir::{
dummy_expr_id, Binding, BindingId, Expr, ExprId, Label, LabelId, Pat, PatId, RecordFieldPat,
},
+ item_tree::AttrOwner,
nameres::DefMap,
path::{ModPath, Path},
src::HasSource,
@@ -91,6 +93,7 @@ pub struct BodySourceMap {
label_map_back: ArenaMap<LabelId, LabelSource>,
self_param: Option<InFile<AstPtr<ast::SelfParam>>>,
+ binding_definitions: FxHashMap<BindingId, SmallVec<[PatId; 4]>>,
/// We don't create explicit nodes for record fields (`S { record_field: 92 }`).
/// Instead, we use id of expression (`92`) to identify the field.
@@ -112,8 +115,7 @@ pub struct SyntheticSyntax;
#[derive(Debug, Eq, PartialEq)]
pub enum BodyDiagnostic {
InactiveCode { node: InFile<SyntaxNodePtr>, cfg: CfgExpr, opts: CfgOptions },
- MacroError { node: InFile<AstPtr<ast::MacroCall>>, message: String },
- UnresolvedProcMacro { node: InFile<AstPtr<ast::MacroCall>>, krate: CrateId },
+ MacroError { node: InFile<AstPtr<ast::MacroCall>>, err: ExpandError },
UnresolvedMacroCall { node: InFile<AstPtr<ast::MacroCall>>, path: ModPath },
UnreachableLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name },
UndeclaredLabel { node: InFile<AstPtr<ast::Lifetime>>, name: Name },
@@ -134,16 +136,23 @@ impl Body {
let data = db.function_data(f);
let f = f.lookup(db);
let src = f.source(db);
- params = src.value.param_list().map(|param_list| {
+ params = src.value.param_list().map(move |param_list| {
let item_tree = f.id.item_tree(db);
let func = &item_tree[f.id.value];
let krate = f.container.module(db).krate;
let crate_graph = db.crate_graph();
(
param_list,
- func.params.clone().map(move |param| {
+ (0..func.params.len()).map(move |idx| {
item_tree
- .attrs(db, krate, param.into())
+ .attrs(
+ db,
+ krate,
+ AttrOwner::Param(
+ f.id.value,
+ Idx::from_raw(RawIdx::from(idx as u32)),
+ ),
+ )
.is_cfg_enabled(&crate_graph[krate].cfg_options)
}),
)
@@ -377,6 +386,10 @@ impl BodySourceMap {
self.label_map_back[label]
}
+ pub fn patterns_for_binding(&self, binding: BindingId) -> &[PatId] {
+ self.binding_definitions.get(&binding).map_or(&[], Deref::deref)
+ }
+
pub fn node_label(&self, node: InFile<&ast::Label>) -> Option<LabelId> {
let src = node.map(AstPtr::new);
self.label_map.get(&src).cloned()
@@ -428,6 +441,7 @@ impl BodySourceMap {
expansions,
format_args_template_map,
diagnostics,
+ binding_definitions,
} = self;
format_args_template_map.shrink_to_fit();
expr_map.shrink_to_fit();
@@ -440,5 +454,6 @@ impl BodySourceMap {
pat_field_map_back.shrink_to_fit();
expansions.shrink_to_fit();
diagnostics.shrink_to_fit();
+ binding_definitions.shrink_to_fit();
}
}