From 3fcdd1bcdf8a93769f74b7b4ca5ba043ad316e46 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 8 Apr 2021 19:44:21 +0200 Subject: Add `AttrId` to track attribute sources --- crates/hir_def/src/attr.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index d9df7564d..1549024c1 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -9,7 +9,7 @@ use std::{ use base_db::CrateId; use cfg::{CfgExpr, CfgOptions}; use either::Either; -use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile}; +use hir_expand::{hygiene::Hygiene, name::AsName, AstId, AttrId, InFile}; use itertools::Itertools; use la_arena::ArenaMap; use mbe::ast_to_token_tree; @@ -98,13 +98,16 @@ impl RawAttrs { pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self { let entries = collect_attrs(owner) .enumerate() - .flat_map(|(i, attr)| match attr { - Either::Left(attr) => Attr::from_src(attr, hygiene, i as u32), - Either::Right(comment) => comment.doc_comment().map(|doc| Attr { - index: i as u32, - input: Some(AttrInput::Literal(SmolStr::new(doc))), - path: Interned::new(ModPath::from(hir_expand::name!(doc))), - }), + .flat_map(|(i, attr)| { + let index = AttrId(i as u32); + match attr { + Either::Left(attr) => Attr::from_src(attr, hygiene, index), + Either::Right(comment) => comment.doc_comment().map(|doc| Attr { + index, + input: Some(AttrInput::Literal(SmolStr::new(doc))), + path: Interned::new(ModPath::from(hir_expand::name!(doc))), + }), + } }) .collect::>(); @@ -560,8 +563,8 @@ impl AttrSourceMap { /// the attribute represented by `Attr`. pub fn source_of(&self, attr: &Attr) -> InFile<&Either> { self.attrs - .get(attr.index as usize) - .unwrap_or_else(|| panic!("cannot find `Attr` at index {}", attr.index)) + .get(attr.index.0 as usize) + .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.index)) .as_ref() } } @@ -572,7 +575,7 @@ pub struct DocsRangeMap { // (docstring-line-range, attr_index, attr-string-range) // a mapping from the text range of a line of the [`Documentation`] to the attribute index and // the original (untrimmed) syntax doc line - mapping: Vec<(TextRange, u32, TextRange)>, + mapping: Vec<(TextRange, AttrId, TextRange)>, } impl DocsRangeMap { @@ -585,7 +588,7 @@ impl DocsRangeMap { let relative_range = range - line_docs_range.start(); - let &InFile { file_id, value: ref source } = &self.source[idx as usize]; + let &InFile { file_id, value: ref source } = &self.source[idx.0 as usize]; match source { Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here // as well as for whats done in syntax highlight doc injection @@ -606,7 +609,7 @@ impl DocsRangeMap { #[derive(Debug, Clone, PartialEq, Eq)] pub struct Attr { - index: u32, + pub(crate) index: AttrId, pub(crate) path: Interned, pub(crate) input: Option, } @@ -620,7 +623,7 @@ pub enum AttrInput { } impl Attr { - fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option { + fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: AttrId) -> Option { let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { let value = match lit.kind() { -- cgit v1.2.3 From 546da15972312f62be8a64a409d39a8d96ed53a6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 9 Apr 2021 13:36:22 +0200 Subject: Rename `Attr`s `index` field to `id` --- crates/hir_def/src/attr.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 1549024c1..dd99c8d15 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -103,7 +103,7 @@ impl RawAttrs { match attr { Either::Left(attr) => Attr::from_src(attr, hygiene, index), Either::Right(comment) => comment.doc_comment().map(|doc| Attr { - index, + id: index, input: Some(AttrInput::Literal(SmolStr::new(doc))), path: Interned::new(ModPath::from(hir_expand::name!(doc))), }), @@ -164,7 +164,7 @@ impl RawAttrs { let cfg = parts.next().unwrap(); let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() }; let cfg = CfgExpr::parse(&cfg); - let index = attr.index; + let index = attr.id; let attrs = parts.filter(|a| !a.is_empty()).filter_map(|attr| { let tree = Subtree { delimiter: None, token_trees: attr.to_vec() }; let attr = ast::Attr::parse(&format!("#[{}]", tree)).ok()?; @@ -471,7 +471,7 @@ impl AttrsWithOwner { ) -> Option<(Documentation, DocsRangeMap)> { // FIXME: code duplication in `docs` above let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { - AttrInput::Literal(s) => Some((s, attr.index)), + AttrInput::Literal(s) => Some((s, attr.id)), AttrInput::TokenTree(_) => None, }); let indent = docs @@ -563,8 +563,8 @@ impl AttrSourceMap { /// the attribute represented by `Attr`. pub fn source_of(&self, attr: &Attr) -> InFile<&Either> { self.attrs - .get(attr.index.0 as usize) - .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.index)) + .get(attr.id.0 as usize) + .unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.id)) .as_ref() } } @@ -609,7 +609,7 @@ impl DocsRangeMap { #[derive(Debug, Clone, PartialEq, Eq)] pub struct Attr { - pub(crate) index: AttrId, + pub(crate) id: AttrId, pub(crate) path: Interned, pub(crate) input: Option, } @@ -623,7 +623,7 @@ pub enum AttrInput { } impl Attr { - fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: AttrId) -> Option { + fn from_src(ast: ast::Attr, hygiene: &Hygiene, id: AttrId) -> Option { let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?); let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() { let value = match lit.kind() { @@ -636,7 +636,7 @@ impl Attr { } else { None }; - Some(Attr { index, path, input }) + Some(Attr { id, path, input }) } /// Parses this attribute as a `#[derive]`, returns an iterator that yields all contained paths -- cgit v1.2.3 From c0dd36fd425416f5465abf7f12f5d3a14b35751d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 9 Apr 2021 13:38:01 +0200 Subject: Store `#[derive]` attribute ID along macro invoc --- crates/hir_def/src/lib.rs | 4 +++- crates/hir_def/src/nameres.rs | 2 +- crates/hir_def/src/nameres/collector.rs | 18 +++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index b72884925..d69116d51 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -62,7 +62,7 @@ use hir_expand::{ ast_id_map::FileAstId, eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, hygiene::Hygiene, - AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, + AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use la_arena::Idx; use nameres::DefMap; @@ -699,6 +699,7 @@ fn macro_call_as_call_id( fn derive_macro_as_call_id( item_attr: &AstIdWithPath, + derive_attr: AttrId, db: &dyn db::DefDatabase, krate: CrateId, resolver: impl Fn(path::ModPath) -> Option, @@ -712,6 +713,7 @@ fn derive_macro_as_call_id( MacroCallKind::Derive { ast_id: item_attr.ast_id, derive_name: last_segment.to_string(), + derive_attr, }, ) .into(); diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index d966fc239..9e181751c 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -617,7 +617,7 @@ mod diagnostics { let node = ast_id.to_node(db.upcast()); (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) } - MacroCallKind::Derive { ast_id, derive_name } => { + MacroCallKind::Derive { ast_id, derive_name, .. } => { let node = ast_id.to_node(db.upcast()); // Compute the precise location of the macro name's token in the derive diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index f431da3f2..d13d7be27 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -13,7 +13,7 @@ use hir_expand::{ builtin_macro::find_builtin_macro, name::{AsName, Name}, proc_macro::ProcMacroExpander, - HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, + AttrId, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use hir_expand::{InFile, MacroCallLoc}; use rustc_hash::{FxHashMap, FxHashSet}; @@ -216,7 +216,7 @@ struct MacroDirective { #[derive(Clone, Debug, Eq, PartialEq)] enum MacroDirectiveKind { FnLike { ast_id: AstIdWithPath }, - Derive { ast_id: AstIdWithPath }, + Derive { ast_id: AstIdWithPath, derive_attr: AttrId }, } struct DefData<'a> { @@ -831,10 +831,14 @@ impl DefCollector<'_> { Err(UnresolvedMacro) | Ok(Err(_)) => {} } } - MacroDirectiveKind::Derive { ast_id } => { - match derive_macro_as_call_id(ast_id, self.db, self.def_map.krate, |path| { - self.resolve_derive_macro(directive.module_id, &path) - }) { + MacroDirectiveKind::Derive { ast_id, derive_attr } => { + match derive_macro_as_call_id( + ast_id, + *derive_attr, + self.db, + self.def_map.krate, + |path| self.resolve_derive_macro(directive.module_id, &path), + ) { Ok(call_id) => { resolved.push((directive.module_id, call_id, directive.depth)); res = ReachedFixedPoint::No; @@ -1368,7 +1372,7 @@ impl ModCollector<'_, '_> { self.def_collector.unexpanded_macros.push(MacroDirective { module_id: self.module_id, depth: self.macro_depth + 1, - kind: MacroDirectiveKind::Derive { ast_id }, + kind: MacroDirectiveKind::Derive { ast_id, derive_attr: derive.id }, }); } } -- cgit v1.2.3 From 4ea5f690bc2692699208999f46a85a9872cbfc73 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 9 Apr 2021 14:46:52 +0200 Subject: Undo path resolution hack for extern prelude We don't populate the extern prelude for block DefMaps anymore, so this is unnecessary --- crates/hir_def/src/nameres/path_resolution.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) (limited to 'crates/hir_def') diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 60471937c..a9cf651d2 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs @@ -384,15 +384,10 @@ impl DefMap { } } }; - // Give precedence to names in outer `DefMap`s over the extern prelude; only check prelude - // from the crate DefMap. - let from_extern_prelude = match self.block { - Some(_) => PerNs::none(), - None => self - .extern_prelude - .get(name) - .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)), - }; + let from_extern_prelude = self + .extern_prelude + .get(name) + .map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public)); let from_prelude = self.resolve_in_prelude(db, name); -- cgit v1.2.3