Skip to content

Commit b329a7a

Browse files
committed
Auto merge of #40597 - jseyfried:improve_span_expn_info, r=<try>
macros: improve `Span`'s expansion information This PR improves `Span`'s expansion information. More specifically: - It refactors AST node span construction to preserve expansion information. - Today, we only use the underlying tokens' `BytePos`s, throwing away the `ExpnId`s. - This improves the accuracy of AST nodes' expansion information, fixing #30506. - It refactors `span.expn_id: ExpnId` to `span.ctxt: SyntaxContext` and removes `ExpnId`. - This gives all tokens as much hygiene information as `Ident`s. - This is groundwork for procedural macros 2.0 `TokenStream` API. - This is also groundwork for declarative macros 2.0, which will need this hygiene information for some non-`Ident` tokens. - It simplifies processing of spans' expansion information throughout the compiler. - It fixes #40649. r? @nrc
2 parents ccce2c6 + c9a2621 commit b329a7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+832
-1694
lines changed

src/librustc/hir/lowering.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ use std::mem;
5757
use syntax::attr;
5858
use syntax::ast::*;
5959
use syntax::errors;
60+
use syntax::ext::hygiene::{Mark, SyntaxContext};
6061
use syntax::ptr::P;
6162
use syntax::codemap::{self, respan, Spanned};
6263
use syntax::std_inject;
@@ -392,14 +393,16 @@ impl<'a> LoweringContext<'a> {
392393
}
393394

394395
fn allow_internal_unstable(&self, reason: &'static str, mut span: Span) -> Span {
395-
span.expn_id = self.sess.codemap().record_expansion(codemap::ExpnInfo {
396+
let mark = Mark::fresh();
397+
mark.set_expn_info(codemap::ExpnInfo {
396398
call_site: span,
397399
callee: codemap::NameAndSpan {
398400
format: codemap::CompilerDesugaring(Symbol::intern(reason)),
399401
span: Some(span),
400402
allow_internal_unstable: true,
401403
},
402404
});
405+
span.ctxt = SyntaxContext::empty().apply_mark(mark);
403406
span
404407
}
405408

@@ -1986,7 +1989,7 @@ impl<'a> LoweringContext<'a> {
19861989
volatile: asm.volatile,
19871990
alignstack: asm.alignstack,
19881991
dialect: asm.dialect,
1989-
expn_id: asm.expn_id,
1992+
ctxt: asm.ctxt,
19901993
};
19911994
let outputs =
19921995
asm.outputs.iter().map(|out| self.lower_expr(&out.expr)).collect();

src/librustc/hir/map/def_collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'a> DefCollector<'a> {
9292
fn visit_macro_invoc(&mut self, id: NodeId, const_expr: bool) {
9393
if let Some(ref mut visit) = self.visit_macro_invoc {
9494
visit(MacroInvocationData {
95-
mark: Mark::from_placeholder_id(id),
95+
mark: id.placeholder_to_mark(),
9696
const_expr: const_expr,
9797
def_index: self.parent_def.unwrap(),
9898
})

src/librustc/hir/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ use hir::def::Def;
3333
use hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
3434
use util::nodemap::{NodeMap, FxHashSet};
3535

36-
use syntax_pos::{Span, ExpnId, DUMMY_SP};
36+
use syntax_pos::{Span, DUMMY_SP};
3737
use syntax::codemap::{self, Spanned};
3838
use syntax::abi::Abi;
3939
use syntax::ast::{Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
4040
use syntax::ast::{Attribute, Lit, StrStyle, FloatTy, IntTy, UintTy, MetaItem};
41+
use syntax::ext::hygiene::SyntaxContext;
4142
use syntax::ptr::P;
4243
use syntax::symbol::{Symbol, keywords};
4344
use syntax::tokenstream::TokenStream;
@@ -1367,7 +1368,7 @@ pub struct InlineAsm {
13671368
pub volatile: bool,
13681369
pub alignstack: bool,
13691370
pub dialect: AsmDialect,
1370-
pub expn_id: ExpnId,
1371+
pub ctxt: SyntaxContext,
13711372
}
13721373

13731374
/// represents an argument in a function header

src/librustc/ich/caching_codemap_view.rs

-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ impl<'tcx> CachingCodemapView<'tcx> {
4747
}
4848
}
4949

50-
pub fn codemap(&self) -> &'tcx CodeMap {
51-
self.codemap
52-
}
53-
5450
pub fn byte_pos_to_line_and_col(&mut self,
5551
pos: BytePos)
5652
-> Option<(Rc<FileMap>, usize, BytePos)> {

src/librustc/middle/region.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl CodeExtent {
236236
// (This is the special case aluded to in the
237237
// doc-comment for this method)
238238
let stmt_span = blk.stmts[r.first_statement_index as usize].span;
239-
Some(Span { lo: stmt_span.hi, hi: blk.span.hi, expn_id: stmt_span.expn_id })
239+
Some(Span { lo: stmt_span.hi, hi: blk.span.hi, ctxt: stmt_span.ctxt })
240240
}
241241
}
242242
}

src/librustc/middle/resolve_lifetime.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use syntax::ast;
2929
use syntax::attr;
3030
use syntax::ptr::P;
3131
use syntax::symbol::keywords;
32-
use syntax_pos::{mk_sp, Span};
32+
use syntax_pos::Span;
3333
use errors::DiagnosticBuilder;
3434
use util::nodemap::{NodeMap, NodeSet, FxHashSet, FxHashMap, DefIdMap};
3535
use rustc_back::slice;
@@ -1468,8 +1468,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
14681468
self.resolve_lifetime_ref(bound);
14691469
} else {
14701470
self.insert_lifetime(bound, Region::Static);
1471-
let full_span = mk_sp(lifetime_i.lifetime.span.lo, bound.span.hi);
1472-
self.sess.struct_span_warn(full_span,
1471+
self.sess.struct_span_warn(lifetime_i.lifetime.span.to(bound.span),
14731472
&format!("unnecessary lifetime parameter `{}`", lifetime_i.lifetime.name))
14741473
.help(&format!("you can use the `'static` lifetime directly, in place \
14751474
of `{}`", lifetime_i.lifetime.name))

src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
467467
}
468468

469469
pub fn check_stability(self, def_id: DefId, id: NodeId, span: Span) {
470-
if self.sess.codemap().span_allows_unstable(span) {
470+
if span.allows_unstable() {
471471
debug!("stability: \
472472
skipping span={:?} since it is internal", span);
473473
return;

src/librustc_driver/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
583583

584584
krate = time(time_passes, "crate injection", || {
585585
let alt_std_name = sess.opts.alt_std_name.clone();
586-
syntax::std_inject::maybe_inject_crates_ref(&sess.parse_sess, krate, alt_std_name)
586+
syntax::std_inject::maybe_inject_crates_ref(krate, alt_std_name)
587587
});
588588

589589
let mut addl_plugins = Some(addl_plugins);
@@ -801,7 +801,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
801801

802802
// Discard hygiene data, which isn't required after lowering to HIR.
803803
if !keep_hygiene_data(sess) {
804-
syntax::ext::hygiene::reset_hygiene_data();
804+
syntax::ext::hygiene::clear_markings();
805805
}
806806

807807
Ok(ExpansionResult {

src/librustc_errors/emitter.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use self::Destination::*;
1212

13-
use syntax_pos::{COMMAND_LINE_SP, DUMMY_SP, FileMap, Span, MultiSpan, CharPos};
13+
use syntax_pos::{DUMMY_SP, FileMap, Span, MultiSpan, CharPos};
1414

1515
use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapper};
1616
use RenderSpan::*;
@@ -151,7 +151,7 @@ impl EmitterWriter {
151151

152152
if let Some(ref cm) = self.cm {
153153
for span_label in msp.span_labels() {
154-
if span_label.span == DUMMY_SP || span_label.span == COMMAND_LINE_SP {
154+
if span_label.span == DUMMY_SP {
155155
continue;
156156
}
157157
let lo = cm.lookup_char_pos(span_label.span.lo);
@@ -615,15 +615,15 @@ impl EmitterWriter {
615615
let mut max = 0;
616616
if let Some(ref cm) = self.cm {
617617
for primary_span in msp.primary_spans() {
618-
if primary_span != &DUMMY_SP && primary_span != &COMMAND_LINE_SP {
618+
if primary_span != &DUMMY_SP {
619619
let hi = cm.lookup_char_pos(primary_span.hi);
620620
if hi.line > max {
621621
max = hi.line;
622622
}
623623
}
624624
}
625625
for span_label in msp.span_labels() {
626-
if span_label.span != DUMMY_SP && span_label.span != COMMAND_LINE_SP {
626+
if span_label.span != DUMMY_SP {
627627
let hi = cm.lookup_char_pos(span_label.span.hi);
628628
if hi.line > max {
629629
max = hi.line;
@@ -659,20 +659,20 @@ impl EmitterWriter {
659659

660660
// First, find all the spans in <*macros> and point instead at their use site
661661
for sp in span.primary_spans() {
662-
if (*sp == COMMAND_LINE_SP) || (*sp == DUMMY_SP) {
662+
if *sp == DUMMY_SP {
663663
continue;
664664
}
665665
if cm.span_to_filename(sp.clone()).contains("macros>") {
666-
let v = cm.macro_backtrace(sp.clone());
666+
let v = sp.macro_backtrace();
667667
if let Some(use_site) = v.last() {
668668
before_after.push((sp.clone(), use_site.call_site.clone()));
669669
}
670670
}
671-
for trace in cm.macro_backtrace(sp.clone()).iter().rev() {
671+
for trace in sp.macro_backtrace().iter().rev() {
672672
// Only show macro locations that are local
673673
// and display them like a span_note
674674
if let Some(def_site) = trace.def_site_span {
675-
if (def_site == COMMAND_LINE_SP) || (def_site == DUMMY_SP) {
675+
if def_site == DUMMY_SP {
676676
continue;
677677
}
678678
// Check to make sure we're not in any <*macros>
@@ -689,11 +689,11 @@ impl EmitterWriter {
689689
span.push_span_label(label_span, label_text);
690690
}
691691
for sp_label in span.span_labels() {
692-
if (sp_label.span == COMMAND_LINE_SP) || (sp_label.span == DUMMY_SP) {
692+
if sp_label.span == DUMMY_SP {
693693
continue;
694694
}
695695
if cm.span_to_filename(sp_label.span.clone()).contains("macros>") {
696-
let v = cm.macro_backtrace(sp_label.span.clone());
696+
let v = sp_label.span.macro_backtrace();
697697
if let Some(use_site) = v.last() {
698698
before_after.push((sp_label.span.clone(), use_site.call_site.clone()));
699699
}
@@ -848,7 +848,7 @@ impl EmitterWriter {
848848
// Make sure our primary file comes first
849849
let primary_lo = if let (Some(ref cm), Some(ref primary_span)) =
850850
(self.cm.as_ref(), msp.primary_span().as_ref()) {
851-
if primary_span != &&DUMMY_SP && primary_span != &&COMMAND_LINE_SP {
851+
if primary_span != &&DUMMY_SP {
852852
cm.lookup_char_pos(primary_span.lo)
853853
} else {
854854
emit_to_destination(&buffer.render(), level, &mut self.dst)?;

src/librustc_errors/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ pub mod styled_buffer;
4848
mod lock;
4949

5050
use syntax_pos::{BytePos, Loc, FileLinesResult, FileName, MultiSpan, Span, NO_EXPANSION};
51-
use syntax_pos::MacroBacktrace;
5251

5352
#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
5453
pub enum RenderSpan {
@@ -75,7 +74,6 @@ pub trait CodeMapper {
7574
fn span_to_lines(&self, sp: Span) -> FileLinesResult;
7675
fn span_to_string(&self, sp: Span) -> String;
7776
fn span_to_filename(&self, sp: Span) -> FileName;
78-
fn macro_backtrace(&self, span: Span) -> Vec<MacroBacktrace>;
7977
fn merge_spans(&self, sp_lhs: Span, sp_rhs: Span) -> Option<Span>;
8078
}
8179

@@ -120,7 +118,7 @@ impl CodeSuggestion {
120118
let bounding_span = Span {
121119
lo: lo,
122120
hi: hi,
123-
expn_id: NO_EXPANSION,
121+
ctxt: NO_EXPANSION,
124122
};
125123
let lines = cm.span_to_lines(bounding_span).unwrap();
126124
assert!(!lines.lines.is_empty());

src/librustc_incremental/calculate_svh/svh_visitor.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ use self::SawTraitOrImplItemComponent::*;
1717
use syntax::abi::Abi;
1818
use syntax::ast::{self, Name, NodeId};
1919
use syntax::attr;
20+
use syntax::ext::hygiene::SyntaxContext;
2021
use syntax::parse::token;
2122
use syntax::symbol::InternedString;
22-
use syntax_pos::{Span, NO_EXPANSION, COMMAND_LINE_EXPN, BytePos};
23+
use syntax_pos::{Span, BytePos};
2324
use syntax::tokenstream;
2425
use rustc::hir;
2526
use rustc::hir::*;
@@ -92,10 +93,10 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
9293
span.hi
9394
};
9495

95-
let expn_kind = match span.expn_id {
96-
NO_EXPANSION => SawSpanExpnKind::NoExpansion,
97-
COMMAND_LINE_EXPN => SawSpanExpnKind::CommandLine,
98-
_ => SawSpanExpnKind::SomeExpansion,
96+
let expn_kind = if span.ctxt == SyntaxContext::empty() {
97+
SawSpanExpnKind::NoExpansion
98+
} else {
99+
SawSpanExpnKind::SomeExpansion
99100
};
100101

101102
let loc1 = self.codemap.byte_pos_to_line_and_col(span.lo);
@@ -121,8 +122,7 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
121122
saw.hash(self.st);
122123

123124
if expn_kind == SawSpanExpnKind::SomeExpansion {
124-
let call_site = self.codemap.codemap().source_callsite(span);
125-
self.hash_span(call_site);
125+
self.hash_span(span.source_callsite());
126126
}
127127
}
128128

@@ -483,7 +483,6 @@ fn saw_impl_item(ii: &ImplItemKind) -> SawTraitOrImplItemComponent {
483483
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
484484
enum SawSpanExpnKind {
485485
NoExpansion,
486-
CommandLine,
487486
SomeExpansion,
488487
}
489488

@@ -501,7 +500,7 @@ impl<'a> Hash for StableInlineAsm<'a> {
501500
volatile,
502501
alignstack,
503502
dialect,
504-
expn_id: _, // This is used for error reporting
503+
ctxt: _, // This is used for error reporting
505504
} = *self.0;
506505

507506
asm.as_str().hash(state);

src/librustc_metadata/cstore_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use syntax::ast;
3636
use syntax::attr;
3737
use syntax::parse::filemap_to_stream;
3838
use syntax::symbol::Symbol;
39-
use syntax_pos::{mk_sp, Span};
39+
use syntax_pos::{Span, NO_EXPANSION};
4040
use rustc::hir::svh::Svh;
4141
use rustc_back::target::Target;
4242
use rustc::hir;
@@ -400,7 +400,7 @@ impl CrateStore for cstore::CStore {
400400
let source_name = format!("<{} macros>", name);
401401

402402
let filemap = sess.parse_sess.codemap().new_filemap(source_name, None, def.body);
403-
let local_span = mk_sp(filemap.start_pos, filemap.end_pos);
403+
let local_span = Span { lo: filemap.start_pos, hi: filemap.end_pos, ctxt: NO_EXPANSION };
404404
let body = filemap_to_stream(&sess.parse_sess, filemap);
405405

406406
// Mark the attrs as used

src/librustc_metadata/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use syntax::attr;
3939
use syntax::ast;
4040
use syntax::codemap;
4141
use syntax::ext::base::MacroKind;
42-
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP};
42+
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP, NO_EXPANSION};
4343

4444
pub struct DecodeContext<'a, 'tcx: 'a> {
4545
opaque: opaque::Decoder<'a>,
@@ -243,7 +243,7 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
243243
let sess = if let Some(sess) = self.sess {
244244
sess
245245
} else {
246-
return Ok(syntax_pos::mk_sp(lo, hi));
246+
return Ok(Span { lo: lo, hi: hi, ctxt: NO_EXPANSION });
247247
};
248248

249249
let (lo, hi) = if lo > hi {
@@ -290,7 +290,7 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
290290
let lo = (lo - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
291291
let hi = (hi - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
292292

293-
Ok(syntax_pos::mk_sp(lo, hi))
293+
Ok(Span { lo: lo, hi: hi, ctxt: NO_EXPANSION })
294294
}
295295
}
296296

src/librustc_mir/transform/qualify_consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
223223
}
224224

225225
// This comes from a macro that has #[allow_internal_unstable].
226-
if self.tcx.sess.codemap().span_allows_unstable(self.span) {
226+
if self.span.allows_unstable() {
227227
return;
228228
}
229229

@@ -805,7 +805,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
805805
self.def_id.is_local() &&
806806

807807
// this doesn't come from a macro that has #[allow_internal_unstable]
808-
!self.tcx.sess.codemap().span_allows_unstable(self.span)
808+
!self.span.allows_unstable()
809809
{
810810
let mut err = self.tcx.sess.struct_span_err(self.span,
811811
"const fns are an unstable feature");

src/librustc_plugin/load.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::env;
2020
use std::mem;
2121
use std::path::PathBuf;
2222
use syntax::ast;
23-
use syntax_pos::{Span, COMMAND_LINE_SP};
23+
use syntax_pos::{Span, DUMMY_SP};
2424

2525
/// Pointer to a registrar function.
2626
pub type PluginRegistrarFun =
@@ -81,7 +81,7 @@ pub fn load_plugins(sess: &Session,
8181

8282
if let Some(plugins) = addl_plugins {
8383
for plugin in plugins {
84-
loader.load_plugin(COMMAND_LINE_SP, &plugin, vec![]);
84+
loader.load_plugin(DUMMY_SP, &plugin, vec![]);
8585
}
8686
}
8787

src/librustc_resolve/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ pub struct BuildReducedGraphVisitor<'a, 'b: 'a> {
680680

681681
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
682682
fn visit_invoc(&mut self, id: ast::NodeId) -> &'b InvocationData<'b> {
683-
let mark = Mark::from_placeholder_id(id);
683+
let mark = id.placeholder_to_mark();
684684
self.resolver.current_module.unresolved_invocations.borrow_mut().insert(mark);
685685
let invocation = self.resolver.invocations[&mark];
686686
invocation.module.set(self.resolver.current_module);

0 commit comments

Comments
 (0)