diff --git a/crates/hir-def/src/data.rs b/crates/hir-def/src/data.rs index f84852b262..b815c9b73e 100644 --- a/crates/hir-def/src/data.rs +++ b/crates/hir-def/src/data.rs @@ -715,7 +715,7 @@ impl<'a> AssocItemCollector<'a> { } AssocItem::MacroCall(call) => { let file_id = self.expander.current_file_id(); - let MacroCall { ast_id, expand_to, call_site, ref path } = item_tree[call]; + let MacroCall { ast_id, expand_to, ctxt, ref path } = item_tree[call]; let module = self.expander.module.local_id; let resolver = |path| { @@ -734,7 +734,7 @@ impl<'a> AssocItemCollector<'a> { match macro_call_as_call_id( self.db.upcast(), &AstIdWithPath::new(file_id, ast_id, Clone::clone(path)), - call_site, + ctxt, expand_to, self.expander.module.krate(), resolver, diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs index eb665f1941..585e93ce21 100644 --- a/crates/hir-def/src/item_tree.rs +++ b/crates/hir-def/src/item_tree.rs @@ -49,7 +49,7 @@ use intern::Interned; use la_arena::{Arena, Idx, IdxRange, RawIdx}; use rustc_hash::FxHashMap; use smallvec::SmallVec; -use span::{AstIdNode, FileAstId, Span}; +use span::{AstIdNode, FileAstId, SyntaxContextId}; use stdx::never; use syntax::{ast, match_ast, SyntaxKind}; use triomphe::Arc; @@ -790,7 +790,7 @@ pub struct MacroCall { pub path: Interned, pub ast_id: FileAstId, pub expand_to: ExpandTo, - pub call_site: Span, + pub ctxt: SyntaxContextId, } #[derive(Debug, Clone, Eq, PartialEq)] diff --git a/crates/hir-def/src/item_tree/lower.rs b/crates/hir-def/src/item_tree/lower.rs index 350593d7dc..f02163cbe4 100644 --- a/crates/hir-def/src/item_tree/lower.rs +++ b/crates/hir-def/src/item_tree/lower.rs @@ -567,7 +567,7 @@ impl<'a> Ctx<'a> { })?); let ast_id = self.source_ast_id_map.ast_id(m); let expand_to = hir_expand::ExpandTo::from_call_site(m); - let res = MacroCall { path, ast_id, expand_to, call_site: span_map.span_for_range(range) }; + let res = MacroCall { path, ast_id, expand_to, ctxt: span_map.span_for_range(range).ctx }; Some(id(self.data().macro_calls.alloc(res))) } diff --git a/crates/hir-def/src/item_tree/pretty.rs b/crates/hir-def/src/item_tree/pretty.rs index 87c90a4c6a..953bf6b85d 100644 --- a/crates/hir-def/src/item_tree/pretty.rs +++ b/crates/hir-def/src/item_tree/pretty.rs @@ -487,12 +487,12 @@ impl Printer<'_> { } } ModItem::MacroCall(it) => { - let MacroCall { path, ast_id, expand_to, call_site } = &self.tree[it]; + let MacroCall { path, ast_id, expand_to, ctxt } = &self.tree[it]; let _ = writeln!( self, - "// AstId: {:?}, Span: {}, ExpandTo: {:?}", + "// AstId: {:?}, SyntaxContext: {}, ExpandTo: {:?}", ast_id.erase().into_raw(), - call_site, + ctxt, expand_to ); wln!(self, "{}!(...);", path.display(self.db.upcast())); diff --git a/crates/hir-def/src/item_tree/tests.rs b/crates/hir-def/src/item_tree/tests.rs index b294d288ac..48da876ac1 100644 --- a/crates/hir-def/src/item_tree/tests.rs +++ b/crates/hir-def/src/item_tree/tests.rs @@ -278,7 +278,7 @@ m!(); // AstId: 2 pub macro m2 { ... } - // AstId: 3, Span: 0:3@0..1#0, ExpandTo: Items + // AstId: 3, SyntaxContext: 0, ExpandTo: Items m!(...); "#]], ); diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs index 6ff350c75a..828842de7e 100644 --- a/crates/hir-def/src/lib.rs +++ b/crates/hir-def/src/lib.rs @@ -90,7 +90,7 @@ use hir_expand::{ use item_tree::ExternBlock; use la_arena::Idx; use nameres::DefMap; -use span::{AstIdNode, FileAstId, FileId, Span}; +use span::{AstIdNode, FileAstId, FileId, SyntaxContextId}; use stdx::impl_from; use syntax::{ast, AstNode}; @@ -1357,7 +1357,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> { macro_call_as_call_id_with_eager( db, &AstIdWithPath::new(ast_id.file_id, ast_id.value, path), - call_site, + call_site.ctx, expands_to, krate, resolver, @@ -1382,7 +1382,7 @@ impl AstIdWithPath { fn macro_call_as_call_id( db: &dyn ExpandDatabase, call: &AstIdWithPath, - call_site: Span, + call_site: SyntaxContextId, expand_to: ExpandTo, krate: CrateId, resolver: impl Fn(path::ModPath) -> Option + Copy, @@ -1394,7 +1394,7 @@ fn macro_call_as_call_id( fn macro_call_as_call_id_with_eager( db: &dyn ExpandDatabase, call: &AstIdWithPath, - call_site: Span, + call_site: SyntaxContextId, expand_to: ExpandTo, krate: CrateId, resolver: impl FnOnce(path::ModPath) -> Option, diff --git a/crates/hir-def/src/macro_expansion_tests/mbe/matching.rs b/crates/hir-def/src/macro_expansion_tests/mbe/matching.rs index 63f211022c..23d8b023b8 100644 --- a/crates/hir-def/src/macro_expansion_tests/mbe/matching.rs +++ b/crates/hir-def/src/macro_expansion_tests/mbe/matching.rs @@ -33,7 +33,7 @@ m!(&k"); "#, expect![[r#" macro_rules! m { ($i:literal) => {}; } -/* error: mismatched delimiters */"#]], +/* error: expected literal */"#]], ); } diff --git a/crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs b/crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs index 362c189f6a..fb5797d6e5 100644 --- a/crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs +++ b/crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs @@ -98,7 +98,7 @@ macro_rules! m1 { ($x:ident) => { ($x } } macro_rules! m2 { ($x:ident) => {} } /* error: macro definition has parse errors */ -/* error: mismatched delimiters */ +/* error: expected ident */ "#]], ) } diff --git a/crates/hir-def/src/nameres/attr_resolution.rs b/crates/hir-def/src/nameres/attr_resolution.rs index 25744e9570..662c80edf3 100644 --- a/crates/hir-def/src/nameres/attr_resolution.rs +++ b/crates/hir-def/src/nameres/attr_resolution.rs @@ -5,7 +5,7 @@ use hir_expand::{ attrs::{Attr, AttrId, AttrInput}, MacroCallId, MacroCallKind, MacroDefId, }; -use span::Span; +use span::SyntaxContextId; use syntax::{ast, SmolStr}; use triomphe::Arc; @@ -109,7 +109,7 @@ pub(super) fn attr_macro_as_call_id( let arg = match macro_attr.input.as_deref() { Some(AttrInput::TokenTree(tt)) => { let mut tt = tt.as_ref().clone(); - tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span); + tt.delimiter.kind = tt::DelimiterKind::Invisible; Some(tt) } @@ -124,7 +124,7 @@ pub(super) fn attr_macro_as_call_id( attr_args: arg.map(Arc::new), invoc_attr_index: macro_attr.id, }, - macro_attr.span, + macro_attr.ctxt, ) } @@ -133,7 +133,7 @@ pub(super) fn derive_macro_as_call_id( item_attr: &AstIdWithPath, derive_attr_index: AttrId, derive_pos: u32, - call_site: Span, + call_site: SyntaxContextId, krate: CrateId, resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>, ) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> { diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index 593a88af69..3d026447fb 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -230,13 +230,13 @@ enum MacroDirectiveKind { FnLike { ast_id: AstIdWithPath, expand_to: ExpandTo, - call_site: Span, + ctxt: SyntaxContextId, }, Derive { ast_id: AstIdWithPath, derive_attr: AttrId, derive_pos: usize, - call_site: Span, + ctxt: SyntaxContextId, }, Attr { ast_id: AstIdWithPath, @@ -1126,7 +1126,7 @@ impl DefCollector<'_> { let resolver_def_id = |path| resolver(path).map(|(_, it)| it); match &directive.kind { - MacroDirectiveKind::FnLike { ast_id, expand_to, call_site } => { + MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => { let call_id = macro_call_as_call_id( self.db.upcast(), ast_id, @@ -1146,7 +1146,7 @@ impl DefCollector<'_> { return Resolved::Yes; } } - MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, call_site } => { + MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, ctxt: call_site } => { let id = derive_macro_as_call_id( self.db, ast_id, @@ -1266,7 +1266,7 @@ impl DefCollector<'_> { ast_id, derive_attr: attr.id, derive_pos: idx, - call_site, + ctxt: call_site.ctx, }, container: directive.container, }); @@ -1428,7 +1428,7 @@ impl DefCollector<'_> { for directive in &self.unresolved_macros { match &directive.kind { - MacroDirectiveKind::FnLike { ast_id, expand_to, call_site } => { + MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => { // FIXME: we shouldn't need to re-resolve the macro here just to get the unresolved error! let macro_call_as_call_id = macro_call_as_call_id( self.db.upcast(), @@ -1460,7 +1460,7 @@ impl DefCollector<'_> { )); } } - MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, call_site: _ } => { + MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, ctxt: _ } => { self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call( directive.module_id, MacroCallKind::Derive { @@ -2289,7 +2289,7 @@ impl ModCollector<'_, '_> { fn collect_macro_call( &mut self, - &MacroCall { ref path, ast_id, expand_to, call_site }: &MacroCall, + &MacroCall { ref path, ast_id, expand_to, ctxt }: &MacroCall, container: ItemContainerId, ) { let ast_id = AstIdWithPath::new(self.file_id(), ast_id, ModPath::clone(path)); @@ -2303,7 +2303,7 @@ impl ModCollector<'_, '_> { if let Ok(res) = macro_call_as_call_id_with_eager( db.upcast(), &ast_id, - call_site, + ctxt, expand_to, self.def_collector.def_map.krate, |path| { @@ -2361,7 +2361,7 @@ impl ModCollector<'_, '_> { self.def_collector.unresolved_macros.push(MacroDirective { module_id: self.module_id, depth: self.macro_depth + 1, - kind: MacroDirectiveKind::FnLike { ast_id, expand_to, call_site }, + kind: MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt }, container, }); } diff --git a/crates/hir-expand/src/attrs.rs b/crates/hir-expand/src/attrs.rs index 686a3ad192..af3ecdcd5e 100644 --- a/crates/hir-expand/src/attrs.rs +++ b/crates/hir-expand/src/attrs.rs @@ -7,7 +7,7 @@ use either::Either; use intern::Interned; use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct}; use smallvec::{smallvec, SmallVec}; -use span::Span; +use span::{Span, SyntaxContextId}; use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode}; use triomphe::Arc; @@ -53,7 +53,7 @@ impl RawAttrs { id, input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))), path: Interned::new(ModPath::from(crate::name!(doc))), - span: span_map.span_for_range(comment.syntax().text_range()), + ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx, }), }); let entries: Arc<[Attr]> = Arc::from_iter(entries); @@ -173,7 +173,7 @@ pub struct Attr { pub id: AttrId, pub path: Interned, pub input: Option>, - pub span: Span, + pub ctxt: SyntaxContextId, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -219,11 +219,11 @@ impl Attr { } else { None }; - Some(Attr { id, path, input, span }) + Some(Attr { id, path, input, ctxt: span.ctx }) } fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option { - let span = tt.first()?.first_span(); + let ctxt = tt.first()?.first_span().ctx; let path_end = tt .iter() .position(|tt| { @@ -255,7 +255,7 @@ impl Attr { } _ => None, }; - Some(Attr { id, path, input, span }) + Some(Attr { id, path, input, ctxt }) } pub fn path(&self) -> &ModPath { diff --git a/crates/hir-expand/src/builtin_attr_macro.rs b/crates/hir-expand/src/builtin_attr_macro.rs index 64295f64dc..9ff29b484d 100644 --- a/crates/hir-expand/src/builtin_attr_macro.rs +++ b/crates/hir-expand/src/builtin_attr_macro.rs @@ -11,7 +11,7 @@ macro_rules! register_builtin { } impl BuiltinAttrExpander { - pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree) -> ExpandResult { + pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree, Span) -> ExpandResult { match *self { $( BuiltinAttrExpander::$variant => $expand, )* } @@ -34,8 +34,9 @@ impl BuiltinAttrExpander { db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, + span: Span, ) -> ExpandResult { - self.expander()(db, id, tt) + self.expander()(db, id, tt, span) } pub fn is_derive(self) -> bool { @@ -71,6 +72,7 @@ fn dummy_attr_expand( _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, + _span: Span, ) -> ExpandResult { ExpandResult::ok(tt.clone()) } @@ -100,6 +102,7 @@ fn derive_expand( db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, + span: Span, ) -> ExpandResult { let loc = db.lookup_intern_macro_call(id); let derives = match &loc.kind { @@ -107,13 +110,10 @@ fn derive_expand( attr_args } _ => { - return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan { - open: loc.call_site, - close: loc.call_site, - })) + return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan { open: span, close: span })) } }; - pseudo_derive_attr_expansion(tt, derives, loc.call_site) + pseudo_derive_attr_expansion(tt, derives, span) } pub fn pseudo_derive_attr_expansion( diff --git a/crates/hir-expand/src/builtin_derive_macro.rs b/crates/hir-expand/src/builtin_derive_macro.rs index 66dec7d89e..528038a9cc 100644 --- a/crates/hir-expand/src/builtin_derive_macro.rs +++ b/crates/hir-expand/src/builtin_derive_macro.rs @@ -50,8 +50,8 @@ impl BuiltinDeriveExpander { db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, + span: Span, ) -> ExpandResult { - let span = db.lookup_intern_macro_call(id).call_site; let span = span_with_def_site_ctxt(db, span, id); self.expander()(span, tt) } diff --git a/crates/hir-expand/src/builtin_fn_macro.rs b/crates/hir-expand/src/builtin_fn_macro.rs index 6f3c01ba8c..9fb6a0b234 100644 --- a/crates/hir-expand/src/builtin_fn_macro.rs +++ b/crates/hir-expand/src/builtin_fn_macro.rs @@ -62,8 +62,8 @@ impl BuiltinFnLikeExpander { db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, + span: Span, ) -> ExpandResult { - let span = db.lookup_intern_macro_call(id).call_site; let span = span_with_def_site_ctxt(db, span, id); self.expander()(db, id, tt, span) } @@ -75,8 +75,8 @@ impl EagerExpander { db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, + span: Span, ) -> ExpandResult { - let span = db.lookup_intern_macro_call(id).call_site; let span = span_with_def_site_ctxt(db, span, id); self.expander()(db, id, tt, span) } diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index a7d12f11bc..ec68f2f96e 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -98,7 +98,7 @@ pub trait ExpandDatabase: SourceDatabase { /// Lowers syntactic macro call to a token tree representation. That's a firewall /// query, only typing in the macro call itself changes the returned /// subtree. - fn macro_arg(&self, id: MacroCallId) -> (Arc, SyntaxFixupUndoInfo); + fn macro_arg(&self, id: MacroCallId) -> (Arc, SyntaxFixupUndoInfo, Span); /// Fetches the expander for this macro. #[salsa::transparent] #[salsa::invoke(TokenExpander::macro_expander)] @@ -144,14 +144,16 @@ pub fn expand_speculative( let span_map = RealSpanMap::absolute(FileId::BOGUS); let span_map = SpanMapRef::RealSpanMap(&span_map); + let (_, _, span) = db.macro_arg(actual_macro_call); + // Build the subtree and token mapping for the speculative args let (mut tt, undo_info) = match loc.kind { MacroCallKind::FnLike { .. } => ( - mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site), + mbe::syntax_node_to_token_tree(speculative_args, span_map, span), SyntaxFixupUndoInfo::NONE, ), MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => ( - mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site), + mbe::syntax_node_to_token_tree(speculative_args, span_map, span), SyntaxFixupUndoInfo::NONE, ), MacroCallKind::Derive { derive_attr_index: index, .. } @@ -159,12 +161,15 @@ pub fn expand_speculative( let censor = if let MacroCallKind::Derive { .. } = loc.kind { censor_derive_input(index, &ast::Adt::cast(speculative_args.clone())?) } else { - censor_attr_input(index, &ast::Item::cast(speculative_args.clone())?) + attr_source(index, &ast::Item::cast(speculative_args.clone())?) + .into_iter() + .map(|it| it.syntax().clone().into()) + .collect() }; let censor_cfg = cfg_process::process_cfg_attrs(speculative_args, &loc, db).unwrap_or_default(); - let mut fixups = fixup::fixup_syntax(span_map, speculative_args, loc.call_site); + let mut fixups = fixup::fixup_syntax(span_map, speculative_args, span); fixups.append.retain(|it, _| match it { syntax::NodeOrToken::Token(_) => true, it => !censor.contains(it) && !censor_cfg.contains(it), @@ -178,7 +183,7 @@ pub fn expand_speculative( span_map, fixups.append, fixups.remove, - loc.call_site, + span, ), fixups.undo_info, ) @@ -200,9 +205,8 @@ pub fn expand_speculative( }?; match attr.token_tree() { Some(token_tree) => { - let mut tree = - syntax_node_to_token_tree(token_tree.syntax(), span_map, loc.call_site); - tree.delimiter = tt::Delimiter::invisible_spanned(loc.call_site); + let mut tree = syntax_node_to_token_tree(token_tree.syntax(), span_map, span); + tree.delimiter = tt::Delimiter::invisible_spanned(span); Some(tree) } @@ -216,8 +220,8 @@ pub fn expand_speculative( // Otherwise the expand query will fetch the non speculative attribute args and pass those instead. let mut speculative_expansion = match loc.def.kind { MacroDefKind::ProcMacro(expander, _, ast) => { - tt.delimiter = tt::Delimiter::invisible_spanned(loc.call_site); let span = db.proc_macro_span(ast); + tt.delimiter = tt::Delimiter::invisible_spanned(span); expander.expand( db, loc.def.krate, @@ -230,22 +234,21 @@ pub fn expand_speculative( ) } MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => { - pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, loc.call_site) + pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, span) + } + MacroDefKind::Declarative(it) => { + db.decl_macro_expander(loc.krate, it).expand_unhygienic(db, tt, loc.def.krate, span) + } + MacroDefKind::BuiltIn(it, _) => { + it.expand(db, actual_macro_call, &tt, span).map_err(Into::into) } - MacroDefKind::Declarative(it) => db.decl_macro_expander(loc.krate, it).expand_unhygienic( - db, - tt, - loc.def.krate, - loc.call_site, - ), - MacroDefKind::BuiltIn(it, _) => it.expand(db, actual_macro_call, &tt).map_err(Into::into), MacroDefKind::BuiltInDerive(it, ..) => { - it.expand(db, actual_macro_call, &tt).map_err(Into::into) + it.expand(db, actual_macro_call, &tt, span).map_err(Into::into) } MacroDefKind::BuiltInEager(it, _) => { - it.expand(db, actual_macro_call, &tt).map_err(Into::into) + it.expand(db, actual_macro_call, &tt, span).map_err(Into::into) } - MacroDefKind::BuiltInAttr(it, _) => it.expand(db, actual_macro_call, &tt), + MacroDefKind::BuiltInAttr(it, _) => it.expand(db, actual_macro_call, &tt, span), }; let expand_to = loc.expand_to(); @@ -338,7 +341,10 @@ pub(crate) fn parse_with_map( // FIXME: for derive attributes, this will return separate copies of the same structures! Though // they may differ in spans due to differing call sites... -fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc, SyntaxFixupUndoInfo) { +fn macro_arg( + db: &dyn ExpandDatabase, + id: MacroCallId, +) -> (Arc, SyntaxFixupUndoInfo, Span) { let loc = db.lookup_intern_macro_call(id); if let MacroCallLoc { @@ -347,29 +353,31 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc, Syn .. } = &loc { - return (eager.arg.clone(), SyntaxFixupUndoInfo::NONE); + return (eager.arg.clone(), SyntaxFixupUndoInfo::NONE, eager.span); } let (parse, map) = parse_with_map(db, loc.kind.file_id()); let root = parse.syntax_node(); - let (censor, item_node) = match loc.kind { + let (censor, item_node, span) = match loc.kind { MacroCallKind::FnLike { ast_id, .. } => { + let node = &ast_id.to_ptr(db).to_node(&root); + let path_range = node + .path() + .map_or_else(|| node.syntax().text_range(), |path| path.syntax().text_range()); + let span = map.span_for_range(path_range); + let dummy_tt = |kind| { ( Arc::new(tt::Subtree { - delimiter: tt::Delimiter { - open: loc.call_site, - close: loc.call_site, - kind, - }, + delimiter: tt::Delimiter { open: span, close: span, kind }, token_trees: Box::default(), }), SyntaxFixupUndoInfo::default(), + span, ) }; - let node = &ast_id.to_ptr(db).to_node(&root); let Some(tt) = node.token_tree() else { return dummy_tt(tt::DelimiterKind::Invisible); }; @@ -399,27 +407,43 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc, Syn return dummy_tt(kind); } - let mut tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), loc.call_site); + let mut tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), span); if loc.def.is_proc_macro() { // proc macros expect their inputs without parentheses, MBEs expect it with them included tt.delimiter.kind = tt::DelimiterKind::Invisible; } - return (Arc::new(tt), SyntaxFixupUndoInfo::NONE); + return (Arc::new(tt), SyntaxFixupUndoInfo::NONE, span); } MacroCallKind::Derive { ast_id, derive_attr_index, .. } => { let node = ast_id.to_ptr(db).to_node(&root); - (censor_derive_input(derive_attr_index, &node), node.into()) + let censor_derive_input = censor_derive_input(derive_attr_index, &node); + let item_node = node.into(); + let attr_source = attr_source(derive_attr_index, &item_node); + // FIXME: This is wrong, this should point to the path of the derive attribute` + let span = + map.span_for_range(attr_source.as_ref().and_then(|it| it.path()).map_or_else( + || item_node.syntax().text_range(), + |it| it.syntax().text_range(), + )); + (censor_derive_input, item_node, span) } MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => { let node = ast_id.to_ptr(db).to_node(&root); - (censor_attr_input(invoc_attr_index, &node), node) + let attr_source = attr_source(invoc_attr_index, &node); + let span = map.span_for_range( + attr_source + .as_ref() + .and_then(|it| it.path()) + .map_or_else(|| node.syntax().text_range(), |it| it.syntax().text_range()), + ); + (attr_source.into_iter().map(|it| it.syntax().clone().into()).collect(), node, span) } }; let (mut tt, undo_info) = { let syntax = item_node.syntax(); let censor_cfg = cfg_process::process_cfg_attrs(syntax, &loc, db).unwrap_or_default(); - let mut fixups = fixup::fixup_syntax(map.as_ref(), syntax, loc.call_site); + let mut fixups = fixup::fixup_syntax(map.as_ref(), syntax, span); fixups.append.retain(|it, _| match it { syntax::NodeOrToken::Token(_) => true, it => !censor.contains(it) && !censor_cfg.contains(it), @@ -433,7 +457,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc, Syn map, fixups.append, fixups.remove, - loc.call_site, + span, ), fixups.undo_info, ) @@ -444,11 +468,11 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc, Syn tt.delimiter.kind = tt::DelimiterKind::Invisible; } - (Arc::new(tt), undo_info) + (Arc::new(tt), undo_info, span) } // FIXME: Censoring info should be calculated by the caller! Namely by name resolution -/// Derives expect all `#[derive(..)]` invocations up to the currently invoked one to be stripped +/// Derives expect all `#[derive(..)]` invocations up to (and including) the currently invoked one to be stripped fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet { // FIXME: handle `cfg_attr` cov_mark::hit!(derive_censoring); @@ -465,16 +489,11 @@ fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet< .collect() } -/// Attributes expect the invoking attribute to be stripped\ -fn censor_attr_input(invoc_attr_index: AttrId, node: &ast::Item) -> FxHashSet { +/// Attributes expect the invoking attribute to be stripped +fn attr_source(invoc_attr_index: AttrId, node: &ast::Item) -> Option { // FIXME: handle `cfg_attr` cov_mark::hit!(attribute_macro_attr_censoring); - collect_attrs(node) - .nth(invoc_attr_index.ast_index()) - .and_then(|(_, attr)| Either::left(attr)) - .map(|attr| attr.syntax().clone().into()) - .into_iter() - .collect() + collect_attrs(node).nth(invoc_attr_index.ast_index()).and_then(|(_, attr)| Either::left(attr)) } impl TokenExpander { @@ -504,53 +523,54 @@ fn macro_expand( ) -> ExpandResult> { let _p = tracing::span!(tracing::Level::INFO, "macro_expand").entered(); - let ExpandResult { value: tt, err } = match loc.def.kind { + let (ExpandResult { value: tt, err }, span) = match loc.def.kind { MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id).map(CowArc::Arc), _ => { - let (macro_arg, undo_info) = db.macro_arg(macro_call_id); + let (macro_arg, undo_info, span) = db.macro_arg(macro_call_id); let arg = &*macro_arg; - let res = match loc.def.kind { - MacroDefKind::Declarative(id) => { - db.decl_macro_expander(loc.def.krate, id).expand(db, arg.clone(), macro_call_id) - } - MacroDefKind::BuiltIn(it, _) => { - it.expand(db, macro_call_id, arg).map_err(Into::into) - } - MacroDefKind::BuiltInDerive(it, _) => { - it.expand(db, macro_call_id, arg).map_err(Into::into) - } - MacroDefKind::BuiltInEager(it, _) => { - // This might look a bit odd, but we do not expand the inputs to eager macros here. - // Eager macros inputs are expanded, well, eagerly when we collect the macro calls. - // That kind of expansion uses the ast id map of an eager macros input though which goes through - // the HirFileId machinery. As eager macro inputs are assigned a macro file id that query - // will end up going through here again, whereas we want to just want to inspect the raw input. - // As such we just return the input subtree here. - let eager = match &loc.kind { - MacroCallKind::FnLike { eager: None, .. } => { - return ExpandResult::ok(CowArc::Arc(macro_arg.clone())); - } - MacroCallKind::FnLike { eager: Some(eager), .. } => Some(&**eager), - _ => None, - }; - - let mut res = it.expand(db, macro_call_id, arg).map_err(Into::into); - - if let Some(EagerCallInfo { error, .. }) = eager { - // FIXME: We should report both errors! - res.err = error.clone().or(res.err); + let res = + match loc.def.kind { + MacroDefKind::Declarative(id) => db + .decl_macro_expander(loc.def.krate, id) + .expand(db, arg.clone(), macro_call_id, span), + MacroDefKind::BuiltIn(it, _) => { + it.expand(db, macro_call_id, arg, span).map_err(Into::into) } - res - } - MacroDefKind::BuiltInAttr(it, _) => { - let mut res = it.expand(db, macro_call_id, arg); - fixup::reverse_fixups(&mut res.value, &undo_info); - res - } - _ => unreachable!(), - }; - ExpandResult { value: res.value, err: res.err } + MacroDefKind::BuiltInDerive(it, _) => { + it.expand(db, macro_call_id, arg, span).map_err(Into::into) + } + MacroDefKind::BuiltInEager(it, _) => { + // This might look a bit odd, but we do not expand the inputs to eager macros here. + // Eager macros inputs are expanded, well, eagerly when we collect the macro calls. + // That kind of expansion uses the ast id map of an eager macros input though which goes through + // the HirFileId machinery. As eager macro inputs are assigned a macro file id that query + // will end up going through here again, whereas we want to just want to inspect the raw input. + // As such we just return the input subtree here. + let eager = match &loc.kind { + MacroCallKind::FnLike { eager: None, .. } => { + return ExpandResult::ok(CowArc::Arc(macro_arg.clone())); + } + MacroCallKind::FnLike { eager: Some(eager), .. } => Some(&**eager), + _ => None, + }; + + let mut res = it.expand(db, macro_call_id, arg, span).map_err(Into::into); + + if let Some(EagerCallInfo { error, .. }) = eager { + // FIXME: We should report both errors! + res.err = error.clone().or(res.err); + } + res + } + MacroDefKind::BuiltInAttr(it, _) => { + let mut res = it.expand(db, macro_call_id, arg, span); + fixup::reverse_fixups(&mut res.value, &undo_info); + res + } + _ => unreachable!(), + }; + (ExpandResult { value: res.value, err: res.err }, span) } }; @@ -560,7 +580,7 @@ fn macro_expand( if let Err(value) = check_tt_count(&tt) { return value.map(|()| { CowArc::Owned(tt::Subtree { - delimiter: tt::Delimiter::invisible_spanned(loc.call_site), + delimiter: tt::Delimiter::invisible_spanned(span), token_trees: Box::new([]), }) }); @@ -583,7 +603,7 @@ fn proc_macro_span(db: &dyn ExpandDatabase, ast: AstId) -> Span { fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult> { let loc = db.lookup_intern_macro_call(id); - let (macro_arg, undo_info) = db.macro_arg(id); + let (macro_arg, undo_info, span) = db.macro_arg(id); let (expander, ast) = match loc.def.kind { MacroDefKind::ProcMacro(expander, _, ast) => (expander, ast), @@ -595,23 +615,25 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult None, }; - let span = db.proc_macro_span(ast); - let ExpandResult { value: mut tt, err } = expander.expand( - db, - loc.def.krate, - loc.krate, - ¯o_arg, - attr_arg, - span_with_def_site_ctxt(db, span, id), - span_with_call_site_ctxt(db, span, id), - span_with_mixed_site_ctxt(db, span, id), - ); + let ExpandResult { value: mut tt, err } = { + let span = db.proc_macro_span(ast); + expander.expand( + db, + loc.def.krate, + loc.krate, + ¯o_arg, + attr_arg, + span_with_def_site_ctxt(db, span, id), + span_with_call_site_ctxt(db, span, id), + span_with_mixed_site_ctxt(db, span, id), + ) + }; // Set a hard limit for the expanded tt if let Err(value) = check_tt_count(&tt) { return value.map(|()| { Arc::new(tt::Subtree { - delimiter: tt::Delimiter::invisible_spanned(loc.call_site), + delimiter: tt::Delimiter::invisible_spanned(span), token_trees: Box::new([]), }) }); diff --git a/crates/hir-expand/src/declarative.rs b/crates/hir-expand/src/declarative.rs index 6874336cd2..33643c0272 100644 --- a/crates/hir-expand/src/declarative.rs +++ b/crates/hir-expand/src/declarative.rs @@ -29,6 +29,7 @@ impl DeclarativeMacroExpander { db: &dyn ExpandDatabase, tt: tt::Subtree, call_id: MacroCallId, + span: Span, ) -> ExpandResult { let loc = db.lookup_intern_macro_call(call_id); let toolchain = db.toolchain(loc.def.krate); @@ -45,7 +46,7 @@ impl DeclarativeMacroExpander { }); match self.mac.err() { Some(_) => ExpandResult::new( - tt::Subtree::empty(tt::DelimSpan { open: loc.call_site, close: loc.call_site }), + tt::Subtree::empty(tt::DelimSpan { open: span, close: span }), ExpandError::MacroDefinition, ), None => self @@ -54,7 +55,7 @@ impl DeclarativeMacroExpander { &tt, |s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency), new_meta_vars, - loc.call_site, + span, ) .map_err(Into::into), } diff --git a/crates/hir-expand/src/eager.rs b/crates/hir-expand/src/eager.rs index 4524463e63..8b147c88c1 100644 --- a/crates/hir-expand/src/eager.rs +++ b/crates/hir-expand/src/eager.rs @@ -19,7 +19,7 @@ //! //! See the full discussion : use base_db::CrateId; -use span::Span; +use span::SyntaxContextId; use syntax::{ted, Parse, SyntaxElement, SyntaxNode, TextSize, WalkEvent}; use triomphe::Arc; @@ -37,7 +37,7 @@ pub fn expand_eager_macro_input( macro_call: &ast::MacroCall, ast_id: AstId, def: MacroDefId, - call_site: Span, + call_site: SyntaxContextId, resolver: &dyn Fn(ModPath) -> Option, ) -> ExpandResult> { let expand_to = ExpandTo::from_call_site(macro_call); @@ -50,9 +50,10 @@ pub fn expand_eager_macro_input( def, krate, kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None }, - call_site, + ctxt: call_site, } .intern(db); + let (_, _, span) = db.macro_arg(arg_id); let ExpandResult { value: (arg_exp, arg_exp_map), err: parse_err } = db.parse_macro_expansion(arg_id.as_macro_file()); @@ -79,7 +80,7 @@ pub fn expand_eager_macro_input( return ExpandResult { value: None, err }; }; - let mut subtree = mbe::syntax_node_to_token_tree(&expanded_eager_input, arg_map, call_site); + let mut subtree = mbe::syntax_node_to_token_tree(&expanded_eager_input, arg_map, span); subtree.delimiter.kind = crate::tt::DelimiterKind::Invisible; @@ -93,9 +94,10 @@ pub fn expand_eager_macro_input( arg: Arc::new(subtree), arg_id, error: err.clone(), + span, })), }, - call_site, + ctxt: call_site, }; ExpandResult { value: Some(loc.intern(db)), err } @@ -107,7 +109,7 @@ fn lazy_expand( macro_call: &ast::MacroCall, ast_id: AstId, krate: CrateId, - call_site: Span, + call_site: SyntaxContextId, ) -> ExpandResult<(InFile>, Arc)> { let expand_to = ExpandTo::from_call_site(macro_call); let id = def.make_call( @@ -129,7 +131,7 @@ fn eager_macro_recur( mut offset: TextSize, curr: InFile, krate: CrateId, - call_site: Span, + call_site: SyntaxContextId, macro_resolver: &dyn Fn(ModPath) -> Option, ) -> ExpandResult> { let original = curr.value.clone_for_update(); diff --git a/crates/hir-expand/src/hygiene.rs b/crates/hir-expand/src/hygiene.rs index ac2bab280d..097e760c70 100644 --- a/crates/hir-expand/src/hygiene.rs +++ b/crates/hir-expand/src/hygiene.rs @@ -65,7 +65,7 @@ pub(super) fn apply_mark( return apply_mark_internal(db, ctxt, call_id, transparency); } - let call_site_ctxt = db.lookup_intern_macro_call(call_id).call_site.ctx; + let call_site_ctxt = db.lookup_intern_macro_call(call_id).ctxt; let mut call_site_ctxt = if transparency == Transparency::SemiTransparent { call_site_ctxt.normalize_to_macros_2_0(db) } else { @@ -205,11 +205,10 @@ pub(crate) fn dump_syntax_contexts(db: &dyn ExpandDatabase) -> String { let id = e.key; let expn_data = e.value.as_ref().unwrap(); s.push_str(&format!( - "\n{:?}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}", + "\n{:?}: parent: {:?}, call_site_ctxt: {:?}, kind: {:?}", id, expn_data.kind.file_id(), - expn_data.call_site, - SyntaxContextId::ROOT, // FIXME expn_data.def_site, + expn_data.ctxt, expn_data.kind.descr(), )); } diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index 22bc1a70e0..5d4f7dc146 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -171,8 +171,7 @@ pub struct MacroCallLoc { pub def: MacroDefId, pub krate: CrateId, pub kind: MacroCallKind, - // FIXME: Spans while relative to an anchor, are still rather unstable - pub call_site: Span, + pub ctxt: SyntaxContextId, } impl_intern_value_trivial!(MacroCallLoc); @@ -202,6 +201,8 @@ pub struct EagerCallInfo { /// Call id of the eager macro's input file (this is the macro file for its fully expanded input). arg_id: MacroCallId, error: Option, + /// TODO: Doc + span: Span, } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -429,9 +430,9 @@ impl MacroDefId { db: &dyn ExpandDatabase, krate: CrateId, kind: MacroCallKind, - call_site: Span, + ctxt: SyntaxContextId, ) -> MacroCallId { - MacroCallLoc { def: self, krate, kind, call_site }.intern(db) + MacroCallLoc { def: self, krate, kind, ctxt }.intern(db) } pub fn definition_range(&self, db: &dyn ExpandDatabase) -> InFile { @@ -805,7 +806,7 @@ impl ExpansionInfo { let (parse, exp_map) = db.parse_macro_expansion(macro_file).value; let expanded = InMacroFile { file_id: macro_file, value: parse.syntax_node() }; - let (macro_arg, _) = db.macro_arg(macro_file.macro_call_id); + let (macro_arg, _, _) = db.macro_arg(macro_file.macro_call_id); let def = loc.def.ast_id().left().and_then(|id| { let def_tt = match id.to_node(db) {