Repalce Span with SyntaxContextId in MacroCallLoc
This commit is contained in:
parent
08327e0e5d
commit
b59c8c76db
@ -715,7 +715,7 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
}
|
}
|
||||||
AssocItem::MacroCall(call) => {
|
AssocItem::MacroCall(call) => {
|
||||||
let file_id = self.expander.current_file_id();
|
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 module = self.expander.module.local_id;
|
||||||
|
|
||||||
let resolver = |path| {
|
let resolver = |path| {
|
||||||
@ -734,7 +734,7 @@ impl<'a> AssocItemCollector<'a> {
|
|||||||
match macro_call_as_call_id(
|
match macro_call_as_call_id(
|
||||||
self.db.upcast(),
|
self.db.upcast(),
|
||||||
&AstIdWithPath::new(file_id, ast_id, Clone::clone(path)),
|
&AstIdWithPath::new(file_id, ast_id, Clone::clone(path)),
|
||||||
call_site,
|
ctxt,
|
||||||
expand_to,
|
expand_to,
|
||||||
self.expander.module.krate(),
|
self.expander.module.krate(),
|
||||||
resolver,
|
resolver,
|
||||||
|
@ -49,7 +49,7 @@ use intern::Interned;
|
|||||||
use la_arena::{Arena, Idx, IdxRange, RawIdx};
|
use la_arena::{Arena, Idx, IdxRange, RawIdx};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use span::{AstIdNode, FileAstId, Span};
|
use span::{AstIdNode, FileAstId, SyntaxContextId};
|
||||||
use stdx::never;
|
use stdx::never;
|
||||||
use syntax::{ast, match_ast, SyntaxKind};
|
use syntax::{ast, match_ast, SyntaxKind};
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
@ -790,7 +790,7 @@ pub struct MacroCall {
|
|||||||
pub path: Interned<ModPath>,
|
pub path: Interned<ModPath>,
|
||||||
pub ast_id: FileAstId<ast::MacroCall>,
|
pub ast_id: FileAstId<ast::MacroCall>,
|
||||||
pub expand_to: ExpandTo,
|
pub expand_to: ExpandTo,
|
||||||
pub call_site: Span,
|
pub ctxt: SyntaxContextId,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
@ -567,7 +567,7 @@ impl<'a> Ctx<'a> {
|
|||||||
})?);
|
})?);
|
||||||
let ast_id = self.source_ast_id_map.ast_id(m);
|
let ast_id = self.source_ast_id_map.ast_id(m);
|
||||||
let expand_to = hir_expand::ExpandTo::from_call_site(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)))
|
Some(id(self.data().macro_calls.alloc(res)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -487,12 +487,12 @@ impl Printer<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ModItem::MacroCall(it) => {
|
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!(
|
let _ = writeln!(
|
||||||
self,
|
self,
|
||||||
"// AstId: {:?}, Span: {}, ExpandTo: {:?}",
|
"// AstId: {:?}, SyntaxContext: {}, ExpandTo: {:?}",
|
||||||
ast_id.erase().into_raw(),
|
ast_id.erase().into_raw(),
|
||||||
call_site,
|
ctxt,
|
||||||
expand_to
|
expand_to
|
||||||
);
|
);
|
||||||
wln!(self, "{}!(...);", path.display(self.db.upcast()));
|
wln!(self, "{}!(...);", path.display(self.db.upcast()));
|
||||||
|
@ -278,7 +278,7 @@ m!();
|
|||||||
// AstId: 2
|
// AstId: 2
|
||||||
pub macro m2 { ... }
|
pub macro m2 { ... }
|
||||||
|
|
||||||
// AstId: 3, Span: 0:3@0..1#0, ExpandTo: Items
|
// AstId: 3, SyntaxContext: 0, ExpandTo: Items
|
||||||
m!(...);
|
m!(...);
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -90,7 +90,7 @@ use hir_expand::{
|
|||||||
use item_tree::ExternBlock;
|
use item_tree::ExternBlock;
|
||||||
use la_arena::Idx;
|
use la_arena::Idx;
|
||||||
use nameres::DefMap;
|
use nameres::DefMap;
|
||||||
use span::{AstIdNode, FileAstId, FileId, Span};
|
use span::{AstIdNode, FileAstId, FileId, SyntaxContextId};
|
||||||
use stdx::impl_from;
|
use stdx::impl_from;
|
||||||
use syntax::{ast, AstNode};
|
use syntax::{ast, AstNode};
|
||||||
|
|
||||||
@ -1357,7 +1357,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
|
|||||||
macro_call_as_call_id_with_eager(
|
macro_call_as_call_id_with_eager(
|
||||||
db,
|
db,
|
||||||
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
|
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
|
||||||
call_site,
|
call_site.ctx,
|
||||||
expands_to,
|
expands_to,
|
||||||
krate,
|
krate,
|
||||||
resolver,
|
resolver,
|
||||||
@ -1382,7 +1382,7 @@ impl<T: AstIdNode> AstIdWithPath<T> {
|
|||||||
fn macro_call_as_call_id(
|
fn macro_call_as_call_id(
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
call: &AstIdWithPath<ast::MacroCall>,
|
call: &AstIdWithPath<ast::MacroCall>,
|
||||||
call_site: Span,
|
call_site: SyntaxContextId,
|
||||||
expand_to: ExpandTo,
|
expand_to: ExpandTo,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
|
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
|
||||||
@ -1394,7 +1394,7 @@ fn macro_call_as_call_id(
|
|||||||
fn macro_call_as_call_id_with_eager(
|
fn macro_call_as_call_id_with_eager(
|
||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
call: &AstIdWithPath<ast::MacroCall>,
|
call: &AstIdWithPath<ast::MacroCall>,
|
||||||
call_site: Span,
|
call_site: SyntaxContextId,
|
||||||
expand_to: ExpandTo,
|
expand_to: ExpandTo,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
|
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
|
||||||
|
@ -33,7 +33,7 @@ m!(&k");
|
|||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
macro_rules! m { ($i:literal) => {}; }
|
macro_rules! m { ($i:literal) => {}; }
|
||||||
/* error: mismatched delimiters */"#]],
|
/* error: expected literal */"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ macro_rules! m1 { ($x:ident) => { ($x } }
|
|||||||
macro_rules! m2 { ($x:ident) => {} }
|
macro_rules! m2 { ($x:ident) => {} }
|
||||||
|
|
||||||
/* error: macro definition has parse errors */
|
/* error: macro definition has parse errors */
|
||||||
/* error: mismatched delimiters */
|
/* error: expected ident */
|
||||||
"#]],
|
"#]],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ use hir_expand::{
|
|||||||
attrs::{Attr, AttrId, AttrInput},
|
attrs::{Attr, AttrId, AttrInput},
|
||||||
MacroCallId, MacroCallKind, MacroDefId,
|
MacroCallId, MacroCallKind, MacroDefId,
|
||||||
};
|
};
|
||||||
use span::Span;
|
use span::SyntaxContextId;
|
||||||
use syntax::{ast, SmolStr};
|
use syntax::{ast, SmolStr};
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ pub(super) fn attr_macro_as_call_id(
|
|||||||
let arg = match macro_attr.input.as_deref() {
|
let arg = match macro_attr.input.as_deref() {
|
||||||
Some(AttrInput::TokenTree(tt)) => {
|
Some(AttrInput::TokenTree(tt)) => {
|
||||||
let mut tt = tt.as_ref().clone();
|
let mut tt = tt.as_ref().clone();
|
||||||
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
|
tt.delimiter.kind = tt::DelimiterKind::Invisible;
|
||||||
Some(tt)
|
Some(tt)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ pub(super) fn attr_macro_as_call_id(
|
|||||||
attr_args: arg.map(Arc::new),
|
attr_args: arg.map(Arc::new),
|
||||||
invoc_attr_index: macro_attr.id,
|
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<ast::Adt>,
|
item_attr: &AstIdWithPath<ast::Adt>,
|
||||||
derive_attr_index: AttrId,
|
derive_attr_index: AttrId,
|
||||||
derive_pos: u32,
|
derive_pos: u32,
|
||||||
call_site: Span,
|
call_site: SyntaxContextId,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
|
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
|
||||||
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
|
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
|
||||||
|
@ -230,13 +230,13 @@ enum MacroDirectiveKind {
|
|||||||
FnLike {
|
FnLike {
|
||||||
ast_id: AstIdWithPath<ast::MacroCall>,
|
ast_id: AstIdWithPath<ast::MacroCall>,
|
||||||
expand_to: ExpandTo,
|
expand_to: ExpandTo,
|
||||||
call_site: Span,
|
ctxt: SyntaxContextId,
|
||||||
},
|
},
|
||||||
Derive {
|
Derive {
|
||||||
ast_id: AstIdWithPath<ast::Adt>,
|
ast_id: AstIdWithPath<ast::Adt>,
|
||||||
derive_attr: AttrId,
|
derive_attr: AttrId,
|
||||||
derive_pos: usize,
|
derive_pos: usize,
|
||||||
call_site: Span,
|
ctxt: SyntaxContextId,
|
||||||
},
|
},
|
||||||
Attr {
|
Attr {
|
||||||
ast_id: AstIdWithPath<ast::Item>,
|
ast_id: AstIdWithPath<ast::Item>,
|
||||||
@ -1126,7 +1126,7 @@ impl DefCollector<'_> {
|
|||||||
let resolver_def_id = |path| resolver(path).map(|(_, it)| it);
|
let resolver_def_id = |path| resolver(path).map(|(_, it)| it);
|
||||||
|
|
||||||
match &directive.kind {
|
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(
|
let call_id = macro_call_as_call_id(
|
||||||
self.db.upcast(),
|
self.db.upcast(),
|
||||||
ast_id,
|
ast_id,
|
||||||
@ -1146,7 +1146,7 @@ impl DefCollector<'_> {
|
|||||||
return Resolved::Yes;
|
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(
|
let id = derive_macro_as_call_id(
|
||||||
self.db,
|
self.db,
|
||||||
ast_id,
|
ast_id,
|
||||||
@ -1266,7 +1266,7 @@ impl DefCollector<'_> {
|
|||||||
ast_id,
|
ast_id,
|
||||||
derive_attr: attr.id,
|
derive_attr: attr.id,
|
||||||
derive_pos: idx,
|
derive_pos: idx,
|
||||||
call_site,
|
ctxt: call_site.ctx,
|
||||||
},
|
},
|
||||||
container: directive.container,
|
container: directive.container,
|
||||||
});
|
});
|
||||||
@ -1428,7 +1428,7 @@ impl DefCollector<'_> {
|
|||||||
|
|
||||||
for directive in &self.unresolved_macros {
|
for directive in &self.unresolved_macros {
|
||||||
match &directive.kind {
|
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!
|
// 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(
|
let macro_call_as_call_id = macro_call_as_call_id(
|
||||||
self.db.upcast(),
|
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(
|
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
|
||||||
directive.module_id,
|
directive.module_id,
|
||||||
MacroCallKind::Derive {
|
MacroCallKind::Derive {
|
||||||
@ -2289,7 +2289,7 @@ impl ModCollector<'_, '_> {
|
|||||||
|
|
||||||
fn collect_macro_call(
|
fn collect_macro_call(
|
||||||
&mut self,
|
&mut self,
|
||||||
&MacroCall { ref path, ast_id, expand_to, call_site }: &MacroCall,
|
&MacroCall { ref path, ast_id, expand_to, ctxt }: &MacroCall,
|
||||||
container: ItemContainerId,
|
container: ItemContainerId,
|
||||||
) {
|
) {
|
||||||
let ast_id = AstIdWithPath::new(self.file_id(), ast_id, ModPath::clone(path));
|
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(
|
if let Ok(res) = macro_call_as_call_id_with_eager(
|
||||||
db.upcast(),
|
db.upcast(),
|
||||||
&ast_id,
|
&ast_id,
|
||||||
call_site,
|
ctxt,
|
||||||
expand_to,
|
expand_to,
|
||||||
self.def_collector.def_map.krate,
|
self.def_collector.def_map.krate,
|
||||||
|path| {
|
|path| {
|
||||||
@ -2361,7 +2361,7 @@ impl ModCollector<'_, '_> {
|
|||||||
self.def_collector.unresolved_macros.push(MacroDirective {
|
self.def_collector.unresolved_macros.push(MacroDirective {
|
||||||
module_id: self.module_id,
|
module_id: self.module_id,
|
||||||
depth: self.macro_depth + 1,
|
depth: self.macro_depth + 1,
|
||||||
kind: MacroDirectiveKind::FnLike { ast_id, expand_to, call_site },
|
kind: MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt },
|
||||||
container,
|
container,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use either::Either;
|
|||||||
use intern::Interned;
|
use intern::Interned;
|
||||||
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
|
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use span::Span;
|
use span::{Span, SyntaxContextId};
|
||||||
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
|
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
|
|
||||||
@ -53,7 +53,7 @@ impl RawAttrs {
|
|||||||
id,
|
id,
|
||||||
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
|
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
|
||||||
path: Interned::new(ModPath::from(crate::name!(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);
|
let entries: Arc<[Attr]> = Arc::from_iter(entries);
|
||||||
@ -173,7 +173,7 @@ pub struct Attr {
|
|||||||
pub id: AttrId,
|
pub id: AttrId,
|
||||||
pub path: Interned<ModPath>,
|
pub path: Interned<ModPath>,
|
||||||
pub input: Option<Interned<AttrInput>>,
|
pub input: Option<Interned<AttrInput>>,
|
||||||
pub span: Span,
|
pub ctxt: SyntaxContextId,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
@ -219,11 +219,11 @@ impl Attr {
|
|||||||
} else {
|
} else {
|
||||||
None
|
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<Attr> {
|
fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> {
|
||||||
let span = tt.first()?.first_span();
|
let ctxt = tt.first()?.first_span().ctx;
|
||||||
let path_end = tt
|
let path_end = tt
|
||||||
.iter()
|
.iter()
|
||||||
.position(|tt| {
|
.position(|tt| {
|
||||||
@ -255,7 +255,7 @@ impl Attr {
|
|||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
Some(Attr { id, path, input, span })
|
Some(Attr { id, path, input, ctxt })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn path(&self) -> &ModPath {
|
pub fn path(&self) -> &ModPath {
|
||||||
|
@ -11,7 +11,7 @@ macro_rules! register_builtin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BuiltinAttrExpander {
|
impl BuiltinAttrExpander {
|
||||||
pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree) -> ExpandResult<tt::Subtree> {
|
pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree, Span) -> ExpandResult<tt::Subtree> {
|
||||||
match *self {
|
match *self {
|
||||||
$( BuiltinAttrExpander::$variant => $expand, )*
|
$( BuiltinAttrExpander::$variant => $expand, )*
|
||||||
}
|
}
|
||||||
@ -34,8 +34,9 @@ impl BuiltinAttrExpander {
|
|||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
|
span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
self.expander()(db, id, tt)
|
self.expander()(db, id, tt, span)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_derive(self) -> bool {
|
pub fn is_derive(self) -> bool {
|
||||||
@ -71,6 +72,7 @@ fn dummy_attr_expand(
|
|||||||
_db: &dyn ExpandDatabase,
|
_db: &dyn ExpandDatabase,
|
||||||
_id: MacroCallId,
|
_id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
|
_span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
ExpandResult::ok(tt.clone())
|
ExpandResult::ok(tt.clone())
|
||||||
}
|
}
|
||||||
@ -100,6 +102,7 @@ fn derive_expand(
|
|||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
|
span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let loc = db.lookup_intern_macro_call(id);
|
let loc = db.lookup_intern_macro_call(id);
|
||||||
let derives = match &loc.kind {
|
let derives = match &loc.kind {
|
||||||
@ -107,13 +110,10 @@ fn derive_expand(
|
|||||||
attr_args
|
attr_args
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan {
|
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan { open: span, close: span }))
|
||||||
open: loc.call_site,
|
|
||||||
close: loc.call_site,
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
pseudo_derive_attr_expansion(tt, derives, loc.call_site)
|
pseudo_derive_attr_expansion(tt, derives, span)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pseudo_derive_attr_expansion(
|
pub fn pseudo_derive_attr_expansion(
|
||||||
|
@ -50,8 +50,8 @@ impl BuiltinDeriveExpander {
|
|||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
|
span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let span = db.lookup_intern_macro_call(id).call_site;
|
|
||||||
let span = span_with_def_site_ctxt(db, span, id);
|
let span = span_with_def_site_ctxt(db, span, id);
|
||||||
self.expander()(span, tt)
|
self.expander()(span, tt)
|
||||||
}
|
}
|
||||||
|
@ -62,8 +62,8 @@ impl BuiltinFnLikeExpander {
|
|||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
|
span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let span = db.lookup_intern_macro_call(id).call_site;
|
|
||||||
let span = span_with_def_site_ctxt(db, span, id);
|
let span = span_with_def_site_ctxt(db, span, id);
|
||||||
self.expander()(db, id, tt, span)
|
self.expander()(db, id, tt, span)
|
||||||
}
|
}
|
||||||
@ -75,8 +75,8 @@ impl EagerExpander {
|
|||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
|
span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let span = db.lookup_intern_macro_call(id).call_site;
|
|
||||||
let span = span_with_def_site_ctxt(db, span, id);
|
let span = span_with_def_site_ctxt(db, span, id);
|
||||||
self.expander()(db, id, tt, span)
|
self.expander()(db, id, tt, span)
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,7 @@ pub trait ExpandDatabase: SourceDatabase {
|
|||||||
/// Lowers syntactic macro call to a token tree representation. That's a firewall
|
/// Lowers syntactic macro call to a token tree representation. That's a firewall
|
||||||
/// query, only typing in the macro call itself changes the returned
|
/// query, only typing in the macro call itself changes the returned
|
||||||
/// subtree.
|
/// subtree.
|
||||||
fn macro_arg(&self, id: MacroCallId) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo);
|
fn macro_arg(&self, id: MacroCallId) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo, Span);
|
||||||
/// Fetches the expander for this macro.
|
/// Fetches the expander for this macro.
|
||||||
#[salsa::transparent]
|
#[salsa::transparent]
|
||||||
#[salsa::invoke(TokenExpander::macro_expander)]
|
#[salsa::invoke(TokenExpander::macro_expander)]
|
||||||
@ -144,14 +144,16 @@ pub fn expand_speculative(
|
|||||||
let span_map = RealSpanMap::absolute(FileId::BOGUS);
|
let span_map = RealSpanMap::absolute(FileId::BOGUS);
|
||||||
let span_map = SpanMapRef::RealSpanMap(&span_map);
|
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
|
// Build the subtree and token mapping for the speculative args
|
||||||
let (mut tt, undo_info) = match loc.kind {
|
let (mut tt, undo_info) = match loc.kind {
|
||||||
MacroCallKind::FnLike { .. } => (
|
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,
|
SyntaxFixupUndoInfo::NONE,
|
||||||
),
|
),
|
||||||
MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => (
|
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,
|
SyntaxFixupUndoInfo::NONE,
|
||||||
),
|
),
|
||||||
MacroCallKind::Derive { derive_attr_index: index, .. }
|
MacroCallKind::Derive { derive_attr_index: index, .. }
|
||||||
@ -159,12 +161,15 @@ pub fn expand_speculative(
|
|||||||
let censor = if let MacroCallKind::Derive { .. } = loc.kind {
|
let censor = if let MacroCallKind::Derive { .. } = loc.kind {
|
||||||
censor_derive_input(index, &ast::Adt::cast(speculative_args.clone())?)
|
censor_derive_input(index, &ast::Adt::cast(speculative_args.clone())?)
|
||||||
} else {
|
} 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 =
|
let censor_cfg =
|
||||||
cfg_process::process_cfg_attrs(speculative_args, &loc, db).unwrap_or_default();
|
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 {
|
fixups.append.retain(|it, _| match it {
|
||||||
syntax::NodeOrToken::Token(_) => true,
|
syntax::NodeOrToken::Token(_) => true,
|
||||||
it => !censor.contains(it) && !censor_cfg.contains(it),
|
it => !censor.contains(it) && !censor_cfg.contains(it),
|
||||||
@ -178,7 +183,7 @@ pub fn expand_speculative(
|
|||||||
span_map,
|
span_map,
|
||||||
fixups.append,
|
fixups.append,
|
||||||
fixups.remove,
|
fixups.remove,
|
||||||
loc.call_site,
|
span,
|
||||||
),
|
),
|
||||||
fixups.undo_info,
|
fixups.undo_info,
|
||||||
)
|
)
|
||||||
@ -200,9 +205,8 @@ pub fn expand_speculative(
|
|||||||
}?;
|
}?;
|
||||||
match attr.token_tree() {
|
match attr.token_tree() {
|
||||||
Some(token_tree) => {
|
Some(token_tree) => {
|
||||||
let mut tree =
|
let mut tree = syntax_node_to_token_tree(token_tree.syntax(), span_map, span);
|
||||||
syntax_node_to_token_tree(token_tree.syntax(), span_map, loc.call_site);
|
tree.delimiter = tt::Delimiter::invisible_spanned(span);
|
||||||
tree.delimiter = tt::Delimiter::invisible_spanned(loc.call_site);
|
|
||||||
|
|
||||||
Some(tree)
|
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.
|
// Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
|
||||||
let mut speculative_expansion = match loc.def.kind {
|
let mut speculative_expansion = match loc.def.kind {
|
||||||
MacroDefKind::ProcMacro(expander, _, ast) => {
|
MacroDefKind::ProcMacro(expander, _, ast) => {
|
||||||
tt.delimiter = tt::Delimiter::invisible_spanned(loc.call_site);
|
|
||||||
let span = db.proc_macro_span(ast);
|
let span = db.proc_macro_span(ast);
|
||||||
|
tt.delimiter = tt::Delimiter::invisible_spanned(span);
|
||||||
expander.expand(
|
expander.expand(
|
||||||
db,
|
db,
|
||||||
loc.def.krate,
|
loc.def.krate,
|
||||||
@ -230,22 +234,21 @@ pub fn expand_speculative(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
|
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, ..) => {
|
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, _) => {
|
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();
|
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
|
// FIXME: for derive attributes, this will return separate copies of the same structures! Though
|
||||||
// they may differ in spans due to differing call sites...
|
// they may differ in spans due to differing call sites...
|
||||||
fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo) {
|
fn macro_arg(
|
||||||
|
db: &dyn ExpandDatabase,
|
||||||
|
id: MacroCallId,
|
||||||
|
) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo, Span) {
|
||||||
let loc = db.lookup_intern_macro_call(id);
|
let loc = db.lookup_intern_macro_call(id);
|
||||||
|
|
||||||
if let MacroCallLoc {
|
if let MacroCallLoc {
|
||||||
@ -347,29 +353,31 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
|
|||||||
..
|
..
|
||||||
} = &loc
|
} = &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 (parse, map) = parse_with_map(db, loc.kind.file_id());
|
||||||
let root = parse.syntax_node();
|
let root = parse.syntax_node();
|
||||||
|
|
||||||
let (censor, item_node) = match loc.kind {
|
let (censor, item_node, span) = match loc.kind {
|
||||||
MacroCallKind::FnLike { ast_id, .. } => {
|
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| {
|
let dummy_tt = |kind| {
|
||||||
(
|
(
|
||||||
Arc::new(tt::Subtree {
|
Arc::new(tt::Subtree {
|
||||||
delimiter: tt::Delimiter {
|
delimiter: tt::Delimiter { open: span, close: span, kind },
|
||||||
open: loc.call_site,
|
|
||||||
close: loc.call_site,
|
|
||||||
kind,
|
|
||||||
},
|
|
||||||
token_trees: Box::default(),
|
token_trees: Box::default(),
|
||||||
}),
|
}),
|
||||||
SyntaxFixupUndoInfo::default(),
|
SyntaxFixupUndoInfo::default(),
|
||||||
|
span,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let node = &ast_id.to_ptr(db).to_node(&root);
|
|
||||||
let Some(tt) = node.token_tree() else {
|
let Some(tt) = node.token_tree() else {
|
||||||
return dummy_tt(tt::DelimiterKind::Invisible);
|
return dummy_tt(tt::DelimiterKind::Invisible);
|
||||||
};
|
};
|
||||||
@ -399,27 +407,43 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
|
|||||||
return dummy_tt(kind);
|
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() {
|
if loc.def.is_proc_macro() {
|
||||||
// proc macros expect their inputs without parentheses, MBEs expect it with them included
|
// proc macros expect their inputs without parentheses, MBEs expect it with them included
|
||||||
tt.delimiter.kind = tt::DelimiterKind::Invisible;
|
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, .. } => {
|
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
|
||||||
let node = ast_id.to_ptr(db).to_node(&root);
|
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, .. } => {
|
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
|
||||||
let node = ast_id.to_ptr(db).to_node(&root);
|
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 (mut tt, undo_info) = {
|
||||||
let syntax = item_node.syntax();
|
let syntax = item_node.syntax();
|
||||||
let censor_cfg = cfg_process::process_cfg_attrs(syntax, &loc, db).unwrap_or_default();
|
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 {
|
fixups.append.retain(|it, _| match it {
|
||||||
syntax::NodeOrToken::Token(_) => true,
|
syntax::NodeOrToken::Token(_) => true,
|
||||||
it => !censor.contains(it) && !censor_cfg.contains(it),
|
it => !censor.contains(it) && !censor_cfg.contains(it),
|
||||||
@ -433,7 +457,7 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
|
|||||||
map,
|
map,
|
||||||
fixups.append,
|
fixups.append,
|
||||||
fixups.remove,
|
fixups.remove,
|
||||||
loc.call_site,
|
span,
|
||||||
),
|
),
|
||||||
fixups.undo_info,
|
fixups.undo_info,
|
||||||
)
|
)
|
||||||
@ -444,11 +468,11 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> (Arc<tt::Subtree>, Syn
|
|||||||
tt.delimiter.kind = tt::DelimiterKind::Invisible;
|
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
|
// 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<SyntaxElement> {
|
fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet<SyntaxElement> {
|
||||||
// FIXME: handle `cfg_attr`
|
// FIXME: handle `cfg_attr`
|
||||||
cov_mark::hit!(derive_censoring);
|
cov_mark::hit!(derive_censoring);
|
||||||
@ -465,16 +489,11 @@ fn censor_derive_input(derive_attr_index: AttrId, node: &ast::Adt) -> FxHashSet<
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attributes expect the invoking attribute to be stripped\
|
/// Attributes expect the invoking attribute to be stripped
|
||||||
fn censor_attr_input(invoc_attr_index: AttrId, node: &ast::Item) -> FxHashSet<SyntaxElement> {
|
fn attr_source(invoc_attr_index: AttrId, node: &ast::Item) -> Option<ast::Attr> {
|
||||||
// FIXME: handle `cfg_attr`
|
// FIXME: handle `cfg_attr`
|
||||||
cov_mark::hit!(attribute_macro_attr_censoring);
|
cov_mark::hit!(attribute_macro_attr_censoring);
|
||||||
collect_attrs(node)
|
collect_attrs(node).nth(invoc_attr_index.ast_index()).and_then(|(_, attr)| Either::left(attr))
|
||||||
.nth(invoc_attr_index.ast_index())
|
|
||||||
.and_then(|(_, attr)| Either::left(attr))
|
|
||||||
.map(|attr| attr.syntax().clone().into())
|
|
||||||
.into_iter()
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenExpander {
|
impl TokenExpander {
|
||||||
@ -504,53 +523,54 @@ fn macro_expand(
|
|||||||
) -> ExpandResult<CowArc<tt::Subtree>> {
|
) -> ExpandResult<CowArc<tt::Subtree>> {
|
||||||
let _p = tracing::span!(tracing::Level::INFO, "macro_expand").entered();
|
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),
|
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 arg = &*macro_arg;
|
||||||
let res = match loc.def.kind {
|
let res =
|
||||||
MacroDefKind::Declarative(id) => {
|
match loc.def.kind {
|
||||||
db.decl_macro_expander(loc.def.krate, id).expand(db, arg.clone(), macro_call_id)
|
MacroDefKind::Declarative(id) => db
|
||||||
}
|
.decl_macro_expander(loc.def.krate, id)
|
||||||
MacroDefKind::BuiltIn(it, _) => {
|
.expand(db, arg.clone(), macro_call_id, span),
|
||||||
it.expand(db, macro_call_id, arg).map_err(Into::into)
|
MacroDefKind::BuiltIn(it, _) => {
|
||||||
}
|
it.expand(db, macro_call_id, arg, span).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);
|
|
||||||
}
|
}
|
||||||
res
|
MacroDefKind::BuiltInDerive(it, _) => {
|
||||||
}
|
it.expand(db, macro_call_id, arg, span).map_err(Into::into)
|
||||||
MacroDefKind::BuiltInAttr(it, _) => {
|
}
|
||||||
let mut res = it.expand(db, macro_call_id, arg);
|
MacroDefKind::BuiltInEager(it, _) => {
|
||||||
fixup::reverse_fixups(&mut res.value, &undo_info);
|
// This might look a bit odd, but we do not expand the inputs to eager macros here.
|
||||||
res
|
// 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
|
||||||
_ => unreachable!(),
|
// 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.
|
||||||
ExpandResult { value: res.value, err: res.err }
|
// 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) {
|
if let Err(value) = check_tt_count(&tt) {
|
||||||
return value.map(|()| {
|
return value.map(|()| {
|
||||||
CowArc::Owned(tt::Subtree {
|
CowArc::Owned(tt::Subtree {
|
||||||
delimiter: tt::Delimiter::invisible_spanned(loc.call_site),
|
delimiter: tt::Delimiter::invisible_spanned(span),
|
||||||
token_trees: Box::new([]),
|
token_trees: Box::new([]),
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@ -583,7 +603,7 @@ fn proc_macro_span(db: &dyn ExpandDatabase, ast: AstId<ast::Fn>) -> Span {
|
|||||||
|
|
||||||
fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<Arc<tt::Subtree>> {
|
fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<Arc<tt::Subtree>> {
|
||||||
let loc = db.lookup_intern_macro_call(id);
|
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 {
|
let (expander, ast) = match loc.def.kind {
|
||||||
MacroDefKind::ProcMacro(expander, _, ast) => (expander, ast),
|
MacroDefKind::ProcMacro(expander, _, ast) => (expander, ast),
|
||||||
@ -595,23 +615,25 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
|
|||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let span = db.proc_macro_span(ast);
|
let ExpandResult { value: mut tt, err } = {
|
||||||
let ExpandResult { value: mut tt, err } = expander.expand(
|
let span = db.proc_macro_span(ast);
|
||||||
db,
|
expander.expand(
|
||||||
loc.def.krate,
|
db,
|
||||||
loc.krate,
|
loc.def.krate,
|
||||||
¯o_arg,
|
loc.krate,
|
||||||
attr_arg,
|
¯o_arg,
|
||||||
span_with_def_site_ctxt(db, span, id),
|
attr_arg,
|
||||||
span_with_call_site_ctxt(db, span, id),
|
span_with_def_site_ctxt(db, span, id),
|
||||||
span_with_mixed_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
|
// Set a hard limit for the expanded tt
|
||||||
if let Err(value) = check_tt_count(&tt) {
|
if let Err(value) = check_tt_count(&tt) {
|
||||||
return value.map(|()| {
|
return value.map(|()| {
|
||||||
Arc::new(tt::Subtree {
|
Arc::new(tt::Subtree {
|
||||||
delimiter: tt::Delimiter::invisible_spanned(loc.call_site),
|
delimiter: tt::Delimiter::invisible_spanned(span),
|
||||||
token_trees: Box::new([]),
|
token_trees: Box::new([]),
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
@ -29,6 +29,7 @@ impl DeclarativeMacroExpander {
|
|||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
tt: tt::Subtree,
|
tt: tt::Subtree,
|
||||||
call_id: MacroCallId,
|
call_id: MacroCallId,
|
||||||
|
span: Span,
|
||||||
) -> ExpandResult<tt::Subtree> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let loc = db.lookup_intern_macro_call(call_id);
|
let loc = db.lookup_intern_macro_call(call_id);
|
||||||
let toolchain = db.toolchain(loc.def.krate);
|
let toolchain = db.toolchain(loc.def.krate);
|
||||||
@ -45,7 +46,7 @@ impl DeclarativeMacroExpander {
|
|||||||
});
|
});
|
||||||
match self.mac.err() {
|
match self.mac.err() {
|
||||||
Some(_) => ExpandResult::new(
|
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,
|
ExpandError::MacroDefinition,
|
||||||
),
|
),
|
||||||
None => self
|
None => self
|
||||||
@ -54,7 +55,7 @@ impl DeclarativeMacroExpander {
|
|||||||
&tt,
|
&tt,
|
||||||
|s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency),
|
|s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency),
|
||||||
new_meta_vars,
|
new_meta_vars,
|
||||||
loc.call_site,
|
span,
|
||||||
)
|
)
|
||||||
.map_err(Into::into),
|
.map_err(Into::into),
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
//!
|
//!
|
||||||
//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
|
//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
|
||||||
use base_db::CrateId;
|
use base_db::CrateId;
|
||||||
use span::Span;
|
use span::SyntaxContextId;
|
||||||
use syntax::{ted, Parse, SyntaxElement, SyntaxNode, TextSize, WalkEvent};
|
use syntax::{ted, Parse, SyntaxElement, SyntaxNode, TextSize, WalkEvent};
|
||||||
use triomphe::Arc;
|
use triomphe::Arc;
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ pub fn expand_eager_macro_input(
|
|||||||
macro_call: &ast::MacroCall,
|
macro_call: &ast::MacroCall,
|
||||||
ast_id: AstId<ast::MacroCall>,
|
ast_id: AstId<ast::MacroCall>,
|
||||||
def: MacroDefId,
|
def: MacroDefId,
|
||||||
call_site: Span,
|
call_site: SyntaxContextId,
|
||||||
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||||
) -> ExpandResult<Option<MacroCallId>> {
|
) -> ExpandResult<Option<MacroCallId>> {
|
||||||
let expand_to = ExpandTo::from_call_site(macro_call);
|
let expand_to = ExpandTo::from_call_site(macro_call);
|
||||||
@ -50,9 +50,10 @@ pub fn expand_eager_macro_input(
|
|||||||
def,
|
def,
|
||||||
krate,
|
krate,
|
||||||
kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None },
|
kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None },
|
||||||
call_site,
|
ctxt: call_site,
|
||||||
}
|
}
|
||||||
.intern(db);
|
.intern(db);
|
||||||
|
let (_, _, span) = db.macro_arg(arg_id);
|
||||||
let ExpandResult { value: (arg_exp, arg_exp_map), err: parse_err } =
|
let ExpandResult { value: (arg_exp, arg_exp_map), err: parse_err } =
|
||||||
db.parse_macro_expansion(arg_id.as_macro_file());
|
db.parse_macro_expansion(arg_id.as_macro_file());
|
||||||
|
|
||||||
@ -79,7 +80,7 @@ pub fn expand_eager_macro_input(
|
|||||||
return ExpandResult { value: None, err };
|
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;
|
subtree.delimiter.kind = crate::tt::DelimiterKind::Invisible;
|
||||||
|
|
||||||
@ -93,9 +94,10 @@ pub fn expand_eager_macro_input(
|
|||||||
arg: Arc::new(subtree),
|
arg: Arc::new(subtree),
|
||||||
arg_id,
|
arg_id,
|
||||||
error: err.clone(),
|
error: err.clone(),
|
||||||
|
span,
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
call_site,
|
ctxt: call_site,
|
||||||
};
|
};
|
||||||
|
|
||||||
ExpandResult { value: Some(loc.intern(db)), err }
|
ExpandResult { value: Some(loc.intern(db)), err }
|
||||||
@ -107,7 +109,7 @@ fn lazy_expand(
|
|||||||
macro_call: &ast::MacroCall,
|
macro_call: &ast::MacroCall,
|
||||||
ast_id: AstId<ast::MacroCall>,
|
ast_id: AstId<ast::MacroCall>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
call_site: Span,
|
call_site: SyntaxContextId,
|
||||||
) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> {
|
) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> {
|
||||||
let expand_to = ExpandTo::from_call_site(macro_call);
|
let expand_to = ExpandTo::from_call_site(macro_call);
|
||||||
let id = def.make_call(
|
let id = def.make_call(
|
||||||
@ -129,7 +131,7 @@ fn eager_macro_recur(
|
|||||||
mut offset: TextSize,
|
mut offset: TextSize,
|
||||||
curr: InFile<SyntaxNode>,
|
curr: InFile<SyntaxNode>,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
call_site: Span,
|
call_site: SyntaxContextId,
|
||||||
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
|
||||||
) -> ExpandResult<Option<(SyntaxNode, TextSize)>> {
|
) -> ExpandResult<Option<(SyntaxNode, TextSize)>> {
|
||||||
let original = curr.value.clone_for_update();
|
let original = curr.value.clone_for_update();
|
||||||
|
@ -65,7 +65,7 @@ pub(super) fn apply_mark(
|
|||||||
return apply_mark_internal(db, ctxt, call_id, transparency);
|
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 {
|
let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
|
||||||
call_site_ctxt.normalize_to_macros_2_0(db)
|
call_site_ctxt.normalize_to_macros_2_0(db)
|
||||||
} else {
|
} else {
|
||||||
@ -205,11 +205,10 @@ pub(crate) fn dump_syntax_contexts(db: &dyn ExpandDatabase) -> String {
|
|||||||
let id = e.key;
|
let id = e.key;
|
||||||
let expn_data = e.value.as_ref().unwrap();
|
let expn_data = e.value.as_ref().unwrap();
|
||||||
s.push_str(&format!(
|
s.push_str(&format!(
|
||||||
"\n{:?}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}",
|
"\n{:?}: parent: {:?}, call_site_ctxt: {:?}, kind: {:?}",
|
||||||
id,
|
id,
|
||||||
expn_data.kind.file_id(),
|
expn_data.kind.file_id(),
|
||||||
expn_data.call_site,
|
expn_data.ctxt,
|
||||||
SyntaxContextId::ROOT, // FIXME expn_data.def_site,
|
|
||||||
expn_data.kind.descr(),
|
expn_data.kind.descr(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -171,8 +171,7 @@ pub struct MacroCallLoc {
|
|||||||
pub def: MacroDefId,
|
pub def: MacroDefId,
|
||||||
pub krate: CrateId,
|
pub krate: CrateId,
|
||||||
pub kind: MacroCallKind,
|
pub kind: MacroCallKind,
|
||||||
// FIXME: Spans while relative to an anchor, are still rather unstable
|
pub ctxt: SyntaxContextId,
|
||||||
pub call_site: Span,
|
|
||||||
}
|
}
|
||||||
impl_intern_value_trivial!(MacroCallLoc);
|
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).
|
/// Call id of the eager macro's input file (this is the macro file for its fully expanded input).
|
||||||
arg_id: MacroCallId,
|
arg_id: MacroCallId,
|
||||||
error: Option<ExpandError>,
|
error: Option<ExpandError>,
|
||||||
|
/// TODO: Doc
|
||||||
|
span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
@ -429,9 +430,9 @@ impl MacroDefId {
|
|||||||
db: &dyn ExpandDatabase,
|
db: &dyn ExpandDatabase,
|
||||||
krate: CrateId,
|
krate: CrateId,
|
||||||
kind: MacroCallKind,
|
kind: MacroCallKind,
|
||||||
call_site: Span,
|
ctxt: SyntaxContextId,
|
||||||
) -> MacroCallId {
|
) -> 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<TextRange> {
|
pub fn definition_range(&self, db: &dyn ExpandDatabase) -> InFile<TextRange> {
|
||||||
@ -805,7 +806,7 @@ impl ExpansionInfo {
|
|||||||
let (parse, exp_map) = db.parse_macro_expansion(macro_file).value;
|
let (parse, exp_map) = db.parse_macro_expansion(macro_file).value;
|
||||||
let expanded = InMacroFile { file_id: macro_file, value: parse.syntax_node() };
|
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 = loc.def.ast_id().left().and_then(|id| {
|
||||||
let def_tt = match id.to_node(db) {
|
let def_tt = match id.to_node(db) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user