Skip to content

Commit c8cf9f5

Browse files
petrochenkovmatthewjasper
authored andcommitted
Add with_{def_site,call_site,legacy}_ctxt, methods to Span
Use these to create call-site spans for AST passes when needed.
1 parent 0b86782 commit c8cf9f5

File tree

10 files changed

+138
-117
lines changed

10 files changed

+138
-117
lines changed

src/librustc_resolve/macros.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use syntax::edition::Edition;
1717
use syntax::ext::base::{self, Indeterminate, SpecialDerives};
1818
use syntax::ext::base::{MacroKind, SyntaxExtension};
1919
use syntax::ext::expand::{AstFragment, Invocation, InvocationKind};
20-
use syntax::ext::hygiene::{self, ExpnId, ExpnData, ExpnKind, Transparency};
20+
use syntax::ext::hygiene::{self, ExpnId, ExpnData, ExpnKind};
2121
use syntax::ext::tt::macro_rules;
2222
use syntax::feature_gate::{emit_feature_err, is_builtin_attr_name};
2323
use syntax::feature_gate::GateIssue;
@@ -131,23 +131,20 @@ impl<'a> base::Resolver for Resolver<'a> {
131131
// Create a Span with modern hygiene with a definition site of the provided
132132
// module, or a fake empty `#[no_implicit_prelude]` module if no module is
133133
// provided.
134-
fn span_for_ast_pass(
134+
fn expansion_for_ast_pass(
135135
&mut self,
136-
base_span: Span,
136+
call_site: Span,
137137
pass: AstPass,
138138
features: &[Symbol],
139139
parent_module_id: Option<NodeId>,
140-
) -> Span {
141-
let span = base_span.fresh_expansion_with_transparency(
142-
ExpnData::allow_unstable(
143-
ExpnKind::AstPass(pass),
144-
base_span,
145-
self.session.edition(),
146-
features.into(),
147-
),
148-
Transparency::Opaque,
149-
);
150-
let expn_id = span.ctxt().outer_expn();
140+
) -> ExpnId {
141+
let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
142+
ExpnKind::AstPass(pass),
143+
call_site,
144+
self.session.edition(),
145+
features.into(),
146+
)));
147+
151148
let parent_scope = if let Some(module_id) = parent_module_id {
152149
let parent_def_id = self.definitions.local_def_id(module_id);
153150
self.definitions.add_parent_module_of_macro_def(expn_id, parent_def_id);
@@ -160,7 +157,7 @@ impl<'a> base::Resolver for Resolver<'a> {
160157
self.empty_module
161158
};
162159
self.ast_transform_scopes.insert(expn_id, parent_scope);
163-
span
160+
expn_id
164161
}
165162

166163
fn resolve_imports(&mut self) {

src/libsyntax/ext/base.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::attr::{HasAttrs, Stability, Deprecation};
33
use crate::source_map::SourceMap;
44
use crate::edition::Edition;
55
use crate::ext::expand::{self, AstFragment, Invocation};
6-
use crate::ext::hygiene::{ExpnId, Transparency};
6+
use crate::ext::hygiene::ExpnId;
77
use crate::mut_visit::{self, MutVisitor};
88
use crate::parse::{self, parser, DirectoryOwnership};
99
use crate::parse::token;
@@ -658,13 +658,13 @@ pub trait Resolver {
658658
extra_placeholders: &[NodeId]);
659659
fn register_builtin_macro(&mut self, ident: ast::Ident, ext: SyntaxExtension);
660660

661-
fn span_for_ast_pass(
661+
fn expansion_for_ast_pass(
662662
&mut self,
663-
span: Span,
663+
call_site: Span,
664664
pass: AstPass,
665665
features: &[Symbol],
666666
parent_module_id: Option<NodeId>,
667-
) -> Span;
667+
) -> ExpnId;
668668

669669
fn resolve_imports(&mut self);
670670

@@ -750,20 +750,20 @@ impl<'a> ExtCtxt<'a> {
750750
/// Equivalent of `Span::def_site` from the proc macro API,
751751
/// except that the location is taken from the span passed as an argument.
752752
pub fn with_def_site_ctxt(&self, span: Span) -> Span {
753-
span.with_ctxt_from_mark(self.current_expansion.id, Transparency::Opaque)
753+
span.with_def_site_ctxt(self.current_expansion.id)
754754
}
755755

756756
/// Equivalent of `Span::call_site` from the proc macro API,
757757
/// except that the location is taken from the span passed as an argument.
758758
pub fn with_call_site_ctxt(&self, span: Span) -> Span {
759-
span.with_ctxt_from_mark(self.current_expansion.id, Transparency::Transparent)
759+
span.with_call_site_ctxt(self.current_expansion.id)
760760
}
761761

762762
/// Span with a context reproducing `macro_rules` hygiene (hygienic locals, unhygienic items).
763763
/// FIXME: This should be eventually replaced either with `with_def_site_ctxt` (preferably),
764764
/// or with `with_call_site_ctxt` (where necessary).
765765
pub fn with_legacy_ctxt(&self, span: Span) -> Span {
766-
span.with_ctxt_from_mark(self.current_expansion.id, Transparency::SemiTransparent)
766+
span.with_legacy_ctxt(self.current_expansion.id)
767767
}
768768

769769
/// Returns span for the macro which originally caused the current expansion to happen.

src/libsyntax_ext/proc_macro_harness.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,13 @@ fn mk_decls(
326326
custom_attrs: &[ProcMacroDef],
327327
custom_macros: &[ProcMacroDef],
328328
) -> P<ast::Item> {
329-
let span = cx.resolver.span_for_ast_pass(
329+
let expn_id = cx.resolver.expansion_for_ast_pass(
330330
DUMMY_SP,
331331
AstPass::ProcMacroHarness,
332332
&[sym::rustc_attrs, sym::proc_macro_internals],
333333
None,
334334
);
335+
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
335336

336337
let proc_macro = Ident::new(sym::proc_macro, span);
337338
let krate = cx.item(span,

src/libsyntax_ext/standard_library_imports.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ pub fn inject(
2828
&[sym::std]
2929
};
3030

31-
let span = resolver.span_for_ast_pass(
31+
let expn_id = resolver.expansion_for_ast_pass(
3232
DUMMY_SP,
3333
AstPass::StdImports,
3434
&[sym::prelude_import],
3535
None,
3636
);
37+
let span = DUMMY_SP.with_def_site_ctxt(expn_id);
38+
let call_site = DUMMY_SP.with_call_site_ctxt(expn_id);
3739

3840
// .rev() to preserve ordering above in combination with insert(0, ...)
3941
for &orig_name_sym in names.iter().rev() {
4042
let (rename, orig_name) = if rust_2018 {
4143
(Ident::new(kw::Underscore, span), Some(orig_name_sym))
4244
} else {
43-
(Ident::with_dummy_span(orig_name_sym), None)
45+
(Ident::new(orig_name_sym, call_site), None)
4446
};
4547
krate.module.items.insert(0, P(ast::Item {
4648
attrs: vec![attr::mk_attr_outer(
@@ -65,7 +67,7 @@ pub fn inject(
6567
.collect()
6668
} else {
6769
[kw::PathRoot, name, sym::prelude, sym::v1].iter()
68-
.map(|symbol| ast::PathSegment::from_ident(ast::Ident::with_dummy_span(*symbol)))
70+
.map(|symbol| ast::PathSegment::from_ident(ast::Ident::new(*symbol, call_site)))
6971
.collect()
7072
};
7173

src/libsyntax_ext/test_harness.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,16 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
9797
};
9898
// Create an identifier that will hygienically resolve the test
9999
// case name, even in another module.
100-
let sp = self.cx.ext_cx.resolver.span_for_ast_pass(
100+
let expn_id = self.cx.ext_cx.resolver.expansion_for_ast_pass(
101101
module.inner,
102102
AstPass::TestHarness,
103103
&[],
104104
Some(parent),
105105
);
106-
let expn = sp.ctxt().outer_expn();
107106
for test in &mut tests {
108-
test.ident.span = test.ident.span.apply_mark(expn, Transparency::Opaque);
107+
// See the comment on `mk_main` for why we're using
108+
// `apply_mark` directly.
109+
test.ident.span = test.ident.span.apply_mark(expn_id, Transparency::Opaque);
109110
}
110111
self.cx.test_cases.extend(tests);
111112
}
@@ -207,12 +208,13 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
207208
// #![main]
208209
// test::test_main_static(&[..tests]);
209210
// }
210-
let sp = cx.ext_cx.resolver.span_for_ast_pass(
211+
let expn_id = cx.ext_cx.resolver.expansion_for_ast_pass(
211212
DUMMY_SP,
212213
AstPass::TestHarness,
213214
&[sym::main, sym::test, sym::rustc_attrs],
214215
None,
215216
);
217+
let sp = DUMMY_SP.with_def_site_ctxt(expn_id);
216218
let ecx = &cx.ext_cx;
217219
let test_id = Ident::new(sym::test, sp);
218220

src/libsyntax_pos/hygiene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl SyntaxContext {
360360
}
361361

362362
/// Extend a syntax context with a given expansion and transparency.
363-
pub fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
363+
crate fn apply_mark(self, expn_id: ExpnId, transparency: Transparency) -> SyntaxContext {
364364
HygieneData::with(|data| data.apply_mark(self, expn_id, transparency))
365365
}
366366

src/libsyntax_pos/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,25 @@ impl Span {
514514
span.ctxt)
515515
}
516516

517+
/// Equivalent of `Span::def_site` from the proc macro API,
518+
/// except that the location is taken from the `self` span.
519+
pub fn with_def_site_ctxt(self, expn_id: ExpnId) -> Span {
520+
self.with_ctxt_from_mark(expn_id, Transparency::Opaque)
521+
}
522+
523+
/// Equivalent of `Span::call_site` from the proc macro API,
524+
/// except that the location is taken from the `self` span.
525+
pub fn with_call_site_ctxt(&self, expn_id: ExpnId) -> Span {
526+
self.with_ctxt_from_mark(expn_id, Transparency::Transparent)
527+
}
528+
529+
/// Span with a context reproducing `macro_rules` hygiene (hygienic locals, unhygienic items).
530+
/// FIXME: This should be eventually replaced either with `with_def_site_ctxt` (preferably),
531+
/// or with `with_call_site_ctxt` (where necessary).
532+
pub fn with_legacy_ctxt(&self, expn_id: ExpnId) -> Span {
533+
self.with_ctxt_from_mark(expn_id, Transparency::SemiTransparent)
534+
}
535+
517536
/// Produces a span with the same location as `self` and context produced by a macro with the
518537
/// given ID and transparency, assuming that macro was defined directly and not produced by
519538
/// some other macro (which is the case for built-in and procedural macros).

src/test/ui/proc-macro/dollar-crate-issue-57089.stdout

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,80 @@ PRINT-BANG INPUT (DISPLAY): struct M ($crate :: S) ;
22
PRINT-BANG INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
5-
span: #2 bytes(LO..HI),
5+
span: #3 bytes(LO..HI),
66
},
77
Ident {
88
ident: "M",
9-
span: #2 bytes(LO..HI),
9+
span: #3 bytes(LO..HI),
1010
},
1111
Group {
1212
delimiter: Parenthesis,
1313
stream: TokenStream [
1414
Ident {
1515
ident: "$crate",
16-
span: #2 bytes(LO..HI),
16+
span: #3 bytes(LO..HI),
1717
},
1818
Punct {
1919
ch: ':',
2020
spacing: Joint,
21-
span: #2 bytes(LO..HI),
21+
span: #3 bytes(LO..HI),
2222
},
2323
Punct {
2424
ch: ':',
2525
spacing: Alone,
26-
span: #2 bytes(LO..HI),
26+
span: #3 bytes(LO..HI),
2727
},
2828
Ident {
2929
ident: "S",
30-
span: #2 bytes(LO..HI),
30+
span: #3 bytes(LO..HI),
3131
},
3232
],
33-
span: #2 bytes(LO..HI),
33+
span: #3 bytes(LO..HI),
3434
},
3535
Punct {
3636
ch: ';',
3737
spacing: Alone,
38-
span: #2 bytes(LO..HI),
38+
span: #3 bytes(LO..HI),
3939
},
4040
]
4141
PRINT-ATTR INPUT (DISPLAY): struct A(crate::S);
4242
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A ($crate :: S) ;
4343
PRINT-ATTR INPUT (DEBUG): TokenStream [
4444
Ident {
4545
ident: "struct",
46-
span: #2 bytes(LO..HI),
46+
span: #3 bytes(LO..HI),
4747
},
4848
Ident {
4949
ident: "A",
50-
span: #2 bytes(LO..HI),
50+
span: #3 bytes(LO..HI),
5151
},
5252
Group {
5353
delimiter: Parenthesis,
5454
stream: TokenStream [
5555
Ident {
5656
ident: "$crate",
57-
span: #2 bytes(LO..HI),
57+
span: #3 bytes(LO..HI),
5858
},
5959
Punct {
6060
ch: ':',
6161
spacing: Joint,
62-
span: #2 bytes(LO..HI),
62+
span: #3 bytes(LO..HI),
6363
},
6464
Punct {
6565
ch: ':',
6666
spacing: Alone,
67-
span: #2 bytes(LO..HI),
67+
span: #3 bytes(LO..HI),
6868
},
6969
Ident {
7070
ident: "S",
71-
span: #2 bytes(LO..HI),
71+
span: #3 bytes(LO..HI),
7272
},
7373
],
74-
span: #2 bytes(LO..HI),
74+
span: #3 bytes(LO..HI),
7575
},
7676
Punct {
7777
ch: ';',
7878
spacing: Alone,
79-
span: #2 bytes(LO..HI),
79+
span: #3 bytes(LO..HI),
8080
},
8181
]

0 commit comments

Comments
 (0)