Skip to content

Commit a143462

Browse files
committed
Auto merge of #49154 - petrochenkov:spident, r=eddyb
AST: Give spans to all identifiers Change representation of `ast::Ident` from `{ name: Symbol, ctxt: SyntaxContext }` to `{ name: Symbol, span: Span }`. Syntax contexts still can be extracted from spans (`span.ctxt()`). Why this should not require more memory: - `Span` is `u32` just like `SyntaxContext`. - Despite keeping more spans in AST we don't actually *create* more spans, so the number of "outlined" spans kept in span interner shouldn't become larger. Why this may be slightly slower: - When we need to extract ctxt from an identifier instead of just field read we need to do bit field extraction possibly followed by and access by index into span interner's vector. Both operations should be fast (unless the span interner is under some synchronization) and we already do ctxt extraction from spans all the time during macro expansion, so the difference should be lost in noise. cc #48842 (comment)
2 parents db4235c + 1458684 commit a143462

Some content is hidden

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

76 files changed

+791
-858
lines changed

src/libproc_macro/lib.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1097,15 +1097,17 @@ impl TokenTree {
10971097
}).into();
10981098
},
10991099
self::TokenTree::Term(tt) => {
1100-
let ident = ast::Ident { name: tt.sym, ctxt: tt.span.0.ctxt() };
1100+
let ident = ast::Ident::new(tt.sym, tt.span.0);
11011101
let sym_str = tt.sym.as_str();
1102-
let token =
1103-
if sym_str.starts_with("'") { Lifetime(ident) }
1104-
else if sym_str.starts_with("r#") {
1105-
let name = Symbol::intern(&sym_str[2..]);
1106-
let ident = ast::Ident { name, ctxt: tt.span.0.ctxt() };
1107-
Ident(ident, true)
1108-
} else { Ident(ident, false) };
1102+
let token = if sym_str.starts_with("'") {
1103+
Lifetime(ident)
1104+
} else if sym_str.starts_with("r#") {
1105+
let name = Symbol::intern(&sym_str[2..]);
1106+
let ident = ast::Ident::new(name, ident.span);
1107+
Ident(ident, true)
1108+
} else {
1109+
Ident(ident, false)
1110+
};
11091111
return TokenTree::Token(tt.span.0, token).into();
11101112
}
11111113
self::TokenTree::Literal(self::Literal {

src/librustc/hir/lowering.rs

+27-31
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<'a> LoweringContext<'a> {
909909

910910
fn lower_ident(&mut self, ident: Ident) -> Name {
911911
let ident = ident.modern();
912-
if ident.ctxt == SyntaxContext::empty() {
912+
if ident.span.ctxt() == SyntaxContext::empty() {
913913
return ident.name;
914914
}
915915
*self.name_map
@@ -920,7 +920,7 @@ impl<'a> LoweringContext<'a> {
920920
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
921921
label.map(|label| hir::Label {
922922
name: label.ident.name,
923-
span: label.span,
923+
span: label.ident.span,
924924
})
925925
}
926926

@@ -1358,7 +1358,7 @@ impl<'a> LoweringContext<'a> {
13581358
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
13591359
Spanned {
13601360
node: hir::Variant_ {
1361-
name: v.node.name.name,
1361+
name: v.node.ident.name,
13621362
attrs: self.lower_attrs(&v.node.attrs),
13631363
data: self.lower_variant_data(&v.node.data),
13641364
disr_expr: v.node
@@ -1607,7 +1607,7 @@ impl<'a> LoweringContext<'a> {
16071607
}
16081608

16091609
hir::PathSegment::new(
1610-
self.lower_ident(segment.identifier),
1610+
self.lower_ident(segment.ident),
16111611
parameters,
16121612
infer_types,
16131613
)
@@ -1720,7 +1720,7 @@ impl<'a> LoweringContext<'a> {
17201720
decl.inputs
17211721
.iter()
17221722
.map(|arg| match arg.pat.node {
1723-
PatKind::Ident(_, ident, None) => respan(ident.span, ident.node.name),
1723+
PatKind::Ident(_, ident, None) => respan(ident.span, ident.name),
17241724
_ => respan(arg.pat.span, keywords::Invalid.name()),
17251725
})
17261726
.collect()
@@ -1810,7 +1810,7 @@ impl<'a> LoweringContext<'a> {
18101810
default: tp.default
18111811
.as_ref()
18121812
.map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
1813-
span: tp.span,
1813+
span: tp.ident.span,
18141814
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
18151815
synthetic: tp.attrs
18161816
.iter()
@@ -1822,21 +1822,22 @@ impl<'a> LoweringContext<'a> {
18221822
}
18231823

18241824
fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
1825+
let span = l.ident.span;
18251826
match self.lower_ident(l.ident) {
1826-
x if x == "'static" => self.new_named_lifetime(l.id, l.span, hir::LifetimeName::Static),
1827+
x if x == "'static" => self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
18271828
x if x == "'_" => match self.anonymous_lifetime_mode {
18281829
AnonymousLifetimeMode::CreateParameter => {
1829-
let fresh_name = self.collect_fresh_in_band_lifetime(l.span);
1830-
self.new_named_lifetime(l.id, l.span, fresh_name)
1830+
let fresh_name = self.collect_fresh_in_band_lifetime(span);
1831+
self.new_named_lifetime(l.id, span, fresh_name)
18311832
}
18321833

18331834
AnonymousLifetimeMode::PassThrough => {
1834-
self.new_named_lifetime(l.id, l.span, hir::LifetimeName::Underscore)
1835+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
18351836
}
18361837
},
18371838
name => {
1838-
self.maybe_collect_in_band_lifetime(l.span, name);
1839-
self.new_named_lifetime(l.id, l.span, hir::LifetimeName::Name(name))
1839+
self.maybe_collect_in_band_lifetime(span, name);
1840+
self.new_named_lifetime(l.id, span, hir::LifetimeName::Name(name))
18401841
}
18411842
}
18421843
}
@@ -2089,10 +2090,7 @@ impl<'a> LoweringContext<'a> {
20892090
name: self.lower_ident(match f.ident {
20902091
Some(ident) => ident,
20912092
// FIXME(jseyfried) positional field hygiene
2092-
None => Ident {
2093-
name: Symbol::intern(&index.to_string()),
2094-
ctxt: f.span.ctxt(),
2095-
},
2093+
None => Ident::new(Symbol::intern(&index.to_string()), f.span),
20962094
}),
20972095
vis: self.lower_visibility(&f.vis, None),
20982096
ty: self.lower_ty(&f.ty, ImplTraitContext::Disallowed),
@@ -2102,7 +2100,7 @@ impl<'a> LoweringContext<'a> {
21022100

21032101
fn lower_field(&mut self, f: &Field) -> hir::Field {
21042102
hir::Field {
2105-
name: respan(f.ident.span, self.lower_ident(f.ident.node)),
2103+
name: respan(f.ident.span, self.lower_ident(f.ident)),
21062104
expr: P(self.lower_expr(&f.expr)),
21072105
span: f.span,
21082106
is_shorthand: f.is_shorthand,
@@ -2359,11 +2357,11 @@ impl<'a> LoweringContext<'a> {
23592357

23602358
// Correctly resolve `self` imports
23612359
if path.segments.len() > 1
2362-
&& path.segments.last().unwrap().identifier.name == keywords::SelfValue.name()
2360+
&& path.segments.last().unwrap().ident.name == keywords::SelfValue.name()
23632361
{
23642362
let _ = path.segments.pop();
23652363
if rename.is_none() {
2366-
*name = path.segments.last().unwrap().identifier.name;
2364+
*name = path.segments.last().unwrap().ident.name;
23672365
}
23682366
}
23692367

@@ -2804,7 +2802,7 @@ impl<'a> LoweringContext<'a> {
28042802
fn lower_pat(&mut self, p: &Pat) -> P<hir::Pat> {
28052803
let node = match p.node {
28062804
PatKind::Wild => hir::PatKind::Wild,
2807-
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
2805+
PatKind::Ident(ref binding_mode, ident, ref sub) => {
28082806
match self.resolver.get_resolution(p.id).map(|d| d.base_def()) {
28092807
// `None` can occur in body-less function signatures
28102808
def @ None | def @ Some(Def::Local(_)) => {
@@ -2815,16 +2813,16 @@ impl<'a> LoweringContext<'a> {
28152813
hir::PatKind::Binding(
28162814
self.lower_binding_mode(binding_mode),
28172815
canonical_id,
2818-
respan(pth1.span, pth1.node.name),
2816+
respan(ident.span, ident.name),
28192817
sub.as_ref().map(|x| self.lower_pat(x)),
28202818
)
28212819
}
28222820
Some(def) => hir::PatKind::Path(hir::QPath::Resolved(
28232821
None,
28242822
P(hir::Path {
2825-
span: pth1.span,
2823+
span: ident.span,
28262824
def,
2827-
segments: hir_vec![hir::PathSegment::from_name(pth1.node.name)],
2825+
segments: hir_vec![hir::PathSegment::from_name(ident.name)],
28282826
}),
28292827
)),
28302828
}
@@ -2939,7 +2937,7 @@ impl<'a> LoweringContext<'a> {
29392937
ImplTraitContext::Disallowed,
29402938
);
29412939
let args = args.iter().map(|x| self.lower_expr(x)).collect();
2942-
hir::ExprMethodCall(hir_seg, seg.span, args)
2940+
hir::ExprMethodCall(hir_seg, seg.ident.span, args)
29432941
}
29442942
ExprKind::Binary(binop, ref lhs, ref rhs) => {
29452943
let binop = self.lower_binop(binop);
@@ -3074,7 +3072,7 @@ impl<'a> LoweringContext<'a> {
30743072
),
30753073
ExprKind::Field(ref el, ident) => hir::ExprField(
30763074
P(self.lower_expr(el)),
3077-
respan(ident.span, self.lower_ident(ident.node)),
3075+
respan(ident.span, self.lower_ident(ident)),
30783076
),
30793077
ExprKind::TupField(ref el, ident) => hir::ExprTupField(P(self.lower_expr(el)), ident),
30803078
ExprKind::Index(ref el, ref er) => {
@@ -3505,12 +3503,10 @@ impl<'a> LoweringContext<'a> {
35053503
let attr = {
35063504
// allow(unreachable_code)
35073505
let allow = {
3508-
let allow_ident = self.str_to_ident("allow");
3509-
let uc_ident = self.str_to_ident("unreachable_code");
3510-
let uc_meta_item = attr::mk_spanned_word_item(e.span, uc_ident);
3511-
let uc_nested = NestedMetaItemKind::MetaItem(uc_meta_item);
3512-
let uc_spanned = respan(e.span, uc_nested);
3513-
attr::mk_spanned_list_item(e.span, allow_ident, vec![uc_spanned])
3506+
let allow_ident = Ident::from_str("allow").with_span_pos(e.span);
3507+
let uc_ident = Ident::from_str("unreachable_code").with_span_pos(e.span);
3508+
let uc_nested = attr::mk_nested_word_item(uc_ident);
3509+
attr::mk_list_item(e.span, allow_ident, vec![uc_nested])
35143510
};
35153511
attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow)
35163512
};

src/librustc/hir/map/def_collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
132132
for v in &enum_definition.variants {
133133
let variant_def_index =
134134
this.create_def(v.node.data.id(),
135-
DefPathData::EnumVariant(v.node.name.name.as_str()),
135+
DefPathData::EnumVariant(v.node.ident.name.as_str()),
136136
REGULAR_SPACE,
137137
v.span);
138138
this.with_parent(variant_def_index, |this| {
@@ -202,15 +202,15 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
202202
lifetime_def.lifetime.id,
203203
DefPathData::LifetimeDef(lifetime_def.lifetime.ident.name.as_str()),
204204
REGULAR_SPACE,
205-
lifetime_def.lifetime.span
205+
lifetime_def.lifetime.ident.span
206206
);
207207
}
208208
GenericParam::Type(ref ty_param) => {
209209
self.create_def(
210210
ty_param.id,
211211
DefPathData::TypeParam(ty_param.ident.name.as_str()),
212212
REGULAR_SPACE,
213-
ty_param.span
213+
ty_param.ident.span
214214
);
215215
}
216216
}

src/librustc/ich/impls_hir.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,12 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Ident {
654654
hcx: &mut StableHashingContext<'a>,
655655
hasher: &mut StableHasher<W>) {
656656
let ast::Ident {
657-
ref name,
658-
ctxt: _ // Ignore this
657+
name,
658+
span,
659659
} = *self;
660660

661661
name.hash_stable(hcx, hasher);
662+
span.hash_stable(hcx, hasher);
662663
}
663664
}
664665

src/librustc/ich/impls_syntax.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
162162
impl_stable_hash_for!(enum ::syntax::ast::Unsafety { Unsafe, Normal });
163163
impl_stable_hash_for!(enum ::syntax::ast::Constness { Const, NotConst });
164164
impl_stable_hash_for!(enum ::syntax::ast::Defaultness { Default, Final });
165-
impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, span, ident });
165+
impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident });
166166
impl_stable_hash_for!(enum ::syntax::ast::StrStyle { Cooked, Raw(pounds) });
167167
impl_stable_hash_for!(enum ::syntax::ast::AttrStyle { Outer, Inner });
168168

@@ -211,7 +211,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
211211
style.hash_stable(hcx, hasher);
212212
path.segments.len().hash_stable(hcx, hasher);
213213
for segment in &path.segments {
214-
segment.identifier.name.hash_stable(hcx, hasher);
214+
segment.ident.name.hash_stable(hcx, hasher);
215215
}
216216
for tt in tokens.trees() {
217217
tt.hash_stable(hcx, hasher);
@@ -341,7 +341,7 @@ impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItemKind {
341341
});
342342

343343
impl_stable_hash_for!(struct ::syntax::ast::MetaItem {
344-
name,
344+
ident,
345345
node,
346346
span
347347
});

src/librustc/lint/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,8 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
952952
ast_visit::walk_ty(self, t);
953953
}
954954

955-
fn visit_ident(&mut self, sp: Span, id: ast::Ident) {
956-
run_lints!(self, check_ident, early_passes, sp, id);
955+
fn visit_ident(&mut self, ident: ast::Ident) {
956+
run_lints!(self, check_ident, early_passes, ident);
957957
}
958958

959959
fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, _a: &[ast::Attribute], n: ast::NodeId) {

src/librustc/lint/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl<'a> LintLevelsBuilder<'a> {
221221
continue
222222
}
223223
};
224-
let name = word.name();
224+
let name = word.ident.name;
225225
match store.check_lint_name(&name.as_str()) {
226226
CheckLintNameResult::Ok(ids) => {
227227
let src = LintSource::Node(name, li.span);

src/librustc/lint/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub trait LateLintPass<'a, 'tcx>: LintPass {
236236
}
237237

238238
pub trait EarlyLintPass: LintPass {
239-
fn check_ident(&mut self, _: &EarlyContext, _: Span, _: ast::Ident) { }
239+
fn check_ident(&mut self, _: &EarlyContext, _: ast::Ident) { }
240240
fn check_crate(&mut self, _: &EarlyContext, _: &ast::Crate) { }
241241
fn check_crate_post(&mut self, _: &EarlyContext, _: &ast::Crate) { }
242242
fn check_mod(&mut self, _: &EarlyContext, _: &ast::Mod, _: Span, _: ast::NodeId) { }

src/librustc/session/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1683,12 +1683,12 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
16831683
} else if meta_item.is_meta_item_list() {
16841684
let msg = format!(
16851685
"invalid predicate in --cfg command line argument: `{}`",
1686-
meta_item.name()
1686+
meta_item.ident
16871687
);
16881688
early_error(ErrorOutputType::default(), &msg)
16891689
}
16901690

1691-
(meta_item.name(), meta_item.value_str())
1691+
(meta_item.ident.name, meta_item.value_str())
16921692
})
16931693
.collect::<ast::CrateConfig>()
16941694
}

src/librustc/traits/on_unimplemented.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
190190
for command in self.subcommands.iter().chain(Some(self)).rev() {
191191
if let Some(ref condition) = command.condition {
192192
if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| {
193-
options.contains(&(c.name().as_str().to_string(),
193+
options.contains(&(c.ident.name.as_str().to_string(),
194194
match c.value_str().map(|s| s.as_str().to_string()) {
195195
Some(s) => Some(s),
196196
None => None

src/librustc/ty/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2088,8 +2088,8 @@ impl<'a, 'gcx, 'tcx> VariantDef {
20882088
return Some(index);
20892089
}
20902090
let mut ident = name.to_ident();
2091-
while ident.ctxt != SyntaxContext::empty() {
2092-
ident.ctxt.remove_mark();
2091+
while ident.span.ctxt() != SyntaxContext::empty() {
2092+
ident.span.remove_mark();
20932093
if let Some(field) = self.fields.iter().position(|f| f.name.to_ident() == ident) {
20942094
return Some(field);
20952095
}
@@ -2558,7 +2558,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25582558
LOCAL_CRATE => self.hir.definitions().expansion(scope.index),
25592559
_ => Mark::root(),
25602560
};
2561-
let scope = match ident.ctxt.adjust(expansion) {
2561+
let scope = match ident.span.adjust(expansion) {
25622562
Some(macro_def) => self.hir.definitions().macro_def_scope(macro_def),
25632563
None if block == DUMMY_NODE_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
25642564
None => self.hir.get_module_parent(block),

src/librustc_driver/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ impl RustcDefaultCalls {
10451045
let mut cfgs = Vec::new();
10461046
for &(name, ref value) in sess.parse_sess.config.iter() {
10471047
let gated_cfg = GatedCfg::gate(&ast::MetaItem {
1048-
name,
1048+
ident: ast::Ident::with_empty_ctxt(name),
10491049
node: ast::MetaItemKind::Word,
10501050
span: DUMMY_SP,
10511051
});

src/librustc_driver/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ impl<'a> PrinterSupport for HygieneAnnotation<'a> {
466466
impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
467467
fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
468468
match node {
469-
pprust::NodeIdent(&ast::Ident { name, ctxt }) => {
469+
pprust::NodeIdent(&ast::Ident { name, span }) => {
470470
s.s.space()?;
471471
// FIXME #16420: this doesn't display the connections
472472
// between syntax contexts
473-
s.synth_comment(format!("{}{:?}", name.as_u32(), ctxt))
473+
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
474474
}
475475
pprust::NodeName(&name) => {
476476
s.s.space()?;

src/librustc_incremental/assert_dep_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a, 'tcx> IfThisChanged<'a, 'tcx> {
110110
for list_item in attr.meta_item_list().unwrap_or_default() {
111111
match list_item.word() {
112112
Some(word) if value.is_none() =>
113-
value = Some(word.name().clone()),
113+
value = Some(word.ident.name),
114114
_ =>
115115
// FIXME better-encapsulate meta_item (don't directly access `node`)
116116
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item.node),

0 commit comments

Comments
 (0)