Skip to content

Commit b59c8c7

Browse files
committed
Repalce Span with SyntaxContextId in MacroCallLoc
1 parent 08327e0 commit b59c8c7

19 files changed

+189
-164
lines changed

crates/hir-def/src/data.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl<'a> AssocItemCollector<'a> {
715715
}
716716
AssocItem::MacroCall(call) => {
717717
let file_id = self.expander.current_file_id();
718-
let MacroCall { ast_id, expand_to, call_site, ref path } = item_tree[call];
718+
let MacroCall { ast_id, expand_to, ctxt, ref path } = item_tree[call];
719719
let module = self.expander.module.local_id;
720720

721721
let resolver = |path| {
@@ -734,7 +734,7 @@ impl<'a> AssocItemCollector<'a> {
734734
match macro_call_as_call_id(
735735
self.db.upcast(),
736736
&AstIdWithPath::new(file_id, ast_id, Clone::clone(path)),
737-
call_site,
737+
ctxt,
738738
expand_to,
739739
self.expander.module.krate(),
740740
resolver,

crates/hir-def/src/item_tree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use intern::Interned;
4949
use la_arena::{Arena, Idx, IdxRange, RawIdx};
5050
use rustc_hash::FxHashMap;
5151
use smallvec::SmallVec;
52-
use span::{AstIdNode, FileAstId, Span};
52+
use span::{AstIdNode, FileAstId, SyntaxContextId};
5353
use stdx::never;
5454
use syntax::{ast, match_ast, SyntaxKind};
5555
use triomphe::Arc;
@@ -790,7 +790,7 @@ pub struct MacroCall {
790790
pub path: Interned<ModPath>,
791791
pub ast_id: FileAstId<ast::MacroCall>,
792792
pub expand_to: ExpandTo,
793-
pub call_site: Span,
793+
pub ctxt: SyntaxContextId,
794794
}
795795

796796
#[derive(Debug, Clone, Eq, PartialEq)]

crates/hir-def/src/item_tree/lower.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ impl<'a> Ctx<'a> {
567567
})?);
568568
let ast_id = self.source_ast_id_map.ast_id(m);
569569
let expand_to = hir_expand::ExpandTo::from_call_site(m);
570-
let res = MacroCall { path, ast_id, expand_to, call_site: span_map.span_for_range(range) };
570+
let res = MacroCall { path, ast_id, expand_to, ctxt: span_map.span_for_range(range).ctx };
571571
Some(id(self.data().macro_calls.alloc(res)))
572572
}
573573

crates/hir-def/src/item_tree/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,12 @@ impl Printer<'_> {
487487
}
488488
}
489489
ModItem::MacroCall(it) => {
490-
let MacroCall { path, ast_id, expand_to, call_site } = &self.tree[it];
490+
let MacroCall { path, ast_id, expand_to, ctxt } = &self.tree[it];
491491
let _ = writeln!(
492492
self,
493-
"// AstId: {:?}, Span: {}, ExpandTo: {:?}",
493+
"// AstId: {:?}, SyntaxContext: {}, ExpandTo: {:?}",
494494
ast_id.erase().into_raw(),
495-
call_site,
495+
ctxt,
496496
expand_to
497497
);
498498
wln!(self, "{}!(...);", path.display(self.db.upcast()));

crates/hir-def/src/item_tree/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ m!();
278278
// AstId: 2
279279
pub macro m2 { ... }
280280
281-
// AstId: 3, Span: 0:[email protected]#0, ExpandTo: Items
281+
// AstId: 3, SyntaxContext: 0, ExpandTo: Items
282282
m!(...);
283283
"#]],
284284
);

crates/hir-def/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ use hir_expand::{
9090
use item_tree::ExternBlock;
9191
use la_arena::Idx;
9292
use nameres::DefMap;
93-
use span::{AstIdNode, FileAstId, FileId, Span};
93+
use span::{AstIdNode, FileAstId, FileId, SyntaxContextId};
9494
use stdx::impl_from;
9595
use syntax::{ast, AstNode};
9696

@@ -1357,7 +1357,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
13571357
macro_call_as_call_id_with_eager(
13581358
db,
13591359
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
1360-
call_site,
1360+
call_site.ctx,
13611361
expands_to,
13621362
krate,
13631363
resolver,
@@ -1382,7 +1382,7 @@ impl<T: AstIdNode> AstIdWithPath<T> {
13821382
fn macro_call_as_call_id(
13831383
db: &dyn ExpandDatabase,
13841384
call: &AstIdWithPath<ast::MacroCall>,
1385-
call_site: Span,
1385+
call_site: SyntaxContextId,
13861386
expand_to: ExpandTo,
13871387
krate: CrateId,
13881388
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
@@ -1394,7 +1394,7 @@ fn macro_call_as_call_id(
13941394
fn macro_call_as_call_id_with_eager(
13951395
db: &dyn ExpandDatabase,
13961396
call: &AstIdWithPath<ast::MacroCall>,
1397-
call_site: Span,
1397+
call_site: SyntaxContextId,
13981398
expand_to: ExpandTo,
13991399
krate: CrateId,
14001400
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,

crates/hir-def/src/macro_expansion_tests/mbe/matching.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ m!(&k");
3333
"#,
3434
expect![[r#"
3535
macro_rules! m { ($i:literal) => {}; }
36-
/* error: mismatched delimiters */"#]],
36+
/* error: expected literal */"#]],
3737
);
3838
}
3939

crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ macro_rules! m1 { ($x:ident) => { ($x } }
9898
macro_rules! m2 { ($x:ident) => {} }
9999
100100
/* error: macro definition has parse errors */
101-
/* error: mismatched delimiters */
101+
/* error: expected ident */
102102
"#]],
103103
)
104104
}

crates/hir-def/src/nameres/attr_resolution.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir_expand::{
55
attrs::{Attr, AttrId, AttrInput},
66
MacroCallId, MacroCallKind, MacroDefId,
77
};
8-
use span::Span;
8+
use span::SyntaxContextId;
99
use syntax::{ast, SmolStr};
1010
use triomphe::Arc;
1111

@@ -109,7 +109,7 @@ pub(super) fn attr_macro_as_call_id(
109109
let arg = match macro_attr.input.as_deref() {
110110
Some(AttrInput::TokenTree(tt)) => {
111111
let mut tt = tt.as_ref().clone();
112-
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
112+
tt.delimiter.kind = tt::DelimiterKind::Invisible;
113113
Some(tt)
114114
}
115115

@@ -124,7 +124,7 @@ pub(super) fn attr_macro_as_call_id(
124124
attr_args: arg.map(Arc::new),
125125
invoc_attr_index: macro_attr.id,
126126
},
127-
macro_attr.span,
127+
macro_attr.ctxt,
128128
)
129129
}
130130

@@ -133,7 +133,7 @@ pub(super) fn derive_macro_as_call_id(
133133
item_attr: &AstIdWithPath<ast::Adt>,
134134
derive_attr_index: AttrId,
135135
derive_pos: u32,
136-
call_site: Span,
136+
call_site: SyntaxContextId,
137137
krate: CrateId,
138138
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
139139
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {

crates/hir-def/src/nameres/collector.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,13 @@ enum MacroDirectiveKind {
230230
FnLike {
231231
ast_id: AstIdWithPath<ast::MacroCall>,
232232
expand_to: ExpandTo,
233-
call_site: Span,
233+
ctxt: SyntaxContextId,
234234
},
235235
Derive {
236236
ast_id: AstIdWithPath<ast::Adt>,
237237
derive_attr: AttrId,
238238
derive_pos: usize,
239-
call_site: Span,
239+
ctxt: SyntaxContextId,
240240
},
241241
Attr {
242242
ast_id: AstIdWithPath<ast::Item>,
@@ -1126,7 +1126,7 @@ impl DefCollector<'_> {
11261126
let resolver_def_id = |path| resolver(path).map(|(_, it)| it);
11271127

11281128
match &directive.kind {
1129-
MacroDirectiveKind::FnLike { ast_id, expand_to, call_site } => {
1129+
MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => {
11301130
let call_id = macro_call_as_call_id(
11311131
self.db.upcast(),
11321132
ast_id,
@@ -1146,7 +1146,7 @@ impl DefCollector<'_> {
11461146
return Resolved::Yes;
11471147
}
11481148
}
1149-
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, call_site } => {
1149+
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, ctxt: call_site } => {
11501150
let id = derive_macro_as_call_id(
11511151
self.db,
11521152
ast_id,
@@ -1266,7 +1266,7 @@ impl DefCollector<'_> {
12661266
ast_id,
12671267
derive_attr: attr.id,
12681268
derive_pos: idx,
1269-
call_site,
1269+
ctxt: call_site.ctx,
12701270
},
12711271
container: directive.container,
12721272
});
@@ -1428,7 +1428,7 @@ impl DefCollector<'_> {
14281428

14291429
for directive in &self.unresolved_macros {
14301430
match &directive.kind {
1431-
MacroDirectiveKind::FnLike { ast_id, expand_to, call_site } => {
1431+
MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => {
14321432
// FIXME: we shouldn't need to re-resolve the macro here just to get the unresolved error!
14331433
let macro_call_as_call_id = macro_call_as_call_id(
14341434
self.db.upcast(),
@@ -1460,7 +1460,7 @@ impl DefCollector<'_> {
14601460
));
14611461
}
14621462
}
1463-
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, call_site: _ } => {
1463+
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, ctxt: _ } => {
14641464
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
14651465
directive.module_id,
14661466
MacroCallKind::Derive {
@@ -2289,7 +2289,7 @@ impl ModCollector<'_, '_> {
22892289

22902290
fn collect_macro_call(
22912291
&mut self,
2292-
&MacroCall { ref path, ast_id, expand_to, call_site }: &MacroCall,
2292+
&MacroCall { ref path, ast_id, expand_to, ctxt }: &MacroCall,
22932293
container: ItemContainerId,
22942294
) {
22952295
let ast_id = AstIdWithPath::new(self.file_id(), ast_id, ModPath::clone(path));
@@ -2303,7 +2303,7 @@ impl ModCollector<'_, '_> {
23032303
if let Ok(res) = macro_call_as_call_id_with_eager(
23042304
db.upcast(),
23052305
&ast_id,
2306-
call_site,
2306+
ctxt,
23072307
expand_to,
23082308
self.def_collector.def_map.krate,
23092309
|path| {
@@ -2361,7 +2361,7 @@ impl ModCollector<'_, '_> {
23612361
self.def_collector.unresolved_macros.push(MacroDirective {
23622362
module_id: self.module_id,
23632363
depth: self.macro_depth + 1,
2364-
kind: MacroDirectiveKind::FnLike { ast_id, expand_to, call_site },
2364+
kind: MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt },
23652365
container,
23662366
});
23672367
}

crates/hir-expand/src/attrs.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use either::Either;
77
use intern::Interned;
88
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
99
use smallvec::{smallvec, SmallVec};
10-
use span::Span;
10+
use span::{Span, SyntaxContextId};
1111
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
1212
use triomphe::Arc;
1313

@@ -53,7 +53,7 @@ impl RawAttrs {
5353
id,
5454
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
5555
path: Interned::new(ModPath::from(crate::name!(doc))),
56-
span: span_map.span_for_range(comment.syntax().text_range()),
56+
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
5757
}),
5858
});
5959
let entries: Arc<[Attr]> = Arc::from_iter(entries);
@@ -173,7 +173,7 @@ pub struct Attr {
173173
pub id: AttrId,
174174
pub path: Interned<ModPath>,
175175
pub input: Option<Interned<AttrInput>>,
176-
pub span: Span,
176+
pub ctxt: SyntaxContextId,
177177
}
178178

179179
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@@ -219,11 +219,11 @@ impl Attr {
219219
} else {
220220
None
221221
};
222-
Some(Attr { id, path, input, span })
222+
Some(Attr { id, path, input, ctxt: span.ctx })
223223
}
224224

225225
fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> {
226-
let span = tt.first()?.first_span();
226+
let ctxt = tt.first()?.first_span().ctx;
227227
let path_end = tt
228228
.iter()
229229
.position(|tt| {
@@ -255,7 +255,7 @@ impl Attr {
255255
}
256256
_ => None,
257257
};
258-
Some(Attr { id, path, input, span })
258+
Some(Attr { id, path, input, ctxt })
259259
}
260260

261261
pub fn path(&self) -> &ModPath {

crates/hir-expand/src/builtin_attr_macro.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ macro_rules! register_builtin {
1111
}
1212

1313
impl BuiltinAttrExpander {
14-
pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree) -> ExpandResult<tt::Subtree> {
14+
pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree, Span) -> ExpandResult<tt::Subtree> {
1515
match *self {
1616
$( BuiltinAttrExpander::$variant => $expand, )*
1717
}
@@ -34,8 +34,9 @@ impl BuiltinAttrExpander {
3434
db: &dyn ExpandDatabase,
3535
id: MacroCallId,
3636
tt: &tt::Subtree,
37+
span: Span,
3738
) -> ExpandResult<tt::Subtree> {
38-
self.expander()(db, id, tt)
39+
self.expander()(db, id, tt, span)
3940
}
4041

4142
pub fn is_derive(self) -> bool {
@@ -71,6 +72,7 @@ fn dummy_attr_expand(
7172
_db: &dyn ExpandDatabase,
7273
_id: MacroCallId,
7374
tt: &tt::Subtree,
75+
_span: Span,
7476
) -> ExpandResult<tt::Subtree> {
7577
ExpandResult::ok(tt.clone())
7678
}
@@ -100,20 +102,18 @@ fn derive_expand(
100102
db: &dyn ExpandDatabase,
101103
id: MacroCallId,
102104
tt: &tt::Subtree,
105+
span: Span,
103106
) -> ExpandResult<tt::Subtree> {
104107
let loc = db.lookup_intern_macro_call(id);
105108
let derives = match &loc.kind {
106109
MacroCallKind::Attr { attr_args: Some(attr_args), .. } if loc.def.is_attribute_derive() => {
107110
attr_args
108111
}
109112
_ => {
110-
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan {
111-
open: loc.call_site,
112-
close: loc.call_site,
113-
}))
113+
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan { open: span, close: span }))
114114
}
115115
};
116-
pseudo_derive_attr_expansion(tt, derives, loc.call_site)
116+
pseudo_derive_attr_expansion(tt, derives, span)
117117
}
118118

119119
pub fn pseudo_derive_attr_expansion(

crates/hir-expand/src/builtin_derive_macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ impl BuiltinDeriveExpander {
5050
db: &dyn ExpandDatabase,
5151
id: MacroCallId,
5252
tt: &tt::Subtree,
53+
span: Span,
5354
) -> ExpandResult<tt::Subtree> {
54-
let span = db.lookup_intern_macro_call(id).call_site;
5555
let span = span_with_def_site_ctxt(db, span, id);
5656
self.expander()(span, tt)
5757
}

crates/hir-expand/src/builtin_fn_macro.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl BuiltinFnLikeExpander {
6262
db: &dyn ExpandDatabase,
6363
id: MacroCallId,
6464
tt: &tt::Subtree,
65+
span: Span,
6566
) -> ExpandResult<tt::Subtree> {
66-
let span = db.lookup_intern_macro_call(id).call_site;
6767
let span = span_with_def_site_ctxt(db, span, id);
6868
self.expander()(db, id, tt, span)
6969
}
@@ -75,8 +75,8 @@ impl EagerExpander {
7575
db: &dyn ExpandDatabase,
7676
id: MacroCallId,
7777
tt: &tt::Subtree,
78+
span: Span,
7879
) -> ExpandResult<tt::Subtree> {
79-
let span = db.lookup_intern_macro_call(id).call_site;
8080
let span = span_with_def_site_ctxt(db, span, id);
8181
self.expander()(db, id, tt, span)
8282
}

0 commit comments

Comments
 (0)