Skip to content

Commit

Permalink
Auto merge of #49154 - petrochenkov:spident, r=eddyb
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
bors committed Apr 6, 2018
2 parents db4235c + 1458684 commit a143462
Show file tree
Hide file tree
Showing 76 changed files with 791 additions and 858 deletions.
18 changes: 10 additions & 8 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,15 +1097,17 @@ impl TokenTree {
}).into();
},
self::TokenTree::Term(tt) => {
let ident = ast::Ident { name: tt.sym, ctxt: tt.span.0.ctxt() };
let ident = ast::Ident::new(tt.sym, tt.span.0);
let sym_str = tt.sym.as_str();
let token =
if sym_str.starts_with("'") { Lifetime(ident) }
else if sym_str.starts_with("r#") {
let name = Symbol::intern(&sym_str[2..]);
let ident = ast::Ident { name, ctxt: tt.span.0.ctxt() };
Ident(ident, true)
} else { Ident(ident, false) };
let token = if sym_str.starts_with("'") {
Lifetime(ident)
} else if sym_str.starts_with("r#") {
let name = Symbol::intern(&sym_str[2..]);
let ident = ast::Ident::new(name, ident.span);
Ident(ident, true)
} else {
Ident(ident, false)
};
return TokenTree::Token(tt.span.0, token).into();
}
self::TokenTree::Literal(self::Literal {
Expand Down
58 changes: 27 additions & 31 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ impl<'a> LoweringContext<'a> {

fn lower_ident(&mut self, ident: Ident) -> Name {
let ident = ident.modern();
if ident.ctxt == SyntaxContext::empty() {
if ident.span.ctxt() == SyntaxContext::empty() {
return ident.name;
}
*self.name_map
Expand All @@ -920,7 +920,7 @@ impl<'a> LoweringContext<'a> {
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
label.map(|label| hir::Label {
name: label.ident.name,
span: label.span,
span: label.ident.span,
})
}

Expand Down Expand Up @@ -1358,7 +1358,7 @@ impl<'a> LoweringContext<'a> {
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
Spanned {
node: hir::Variant_ {
name: v.node.name.name,
name: v.node.ident.name,
attrs: self.lower_attrs(&v.node.attrs),
data: self.lower_variant_data(&v.node.data),
disr_expr: v.node
Expand Down Expand Up @@ -1607,7 +1607,7 @@ impl<'a> LoweringContext<'a> {
}

hir::PathSegment::new(
self.lower_ident(segment.identifier),
self.lower_ident(segment.ident),
parameters,
infer_types,
)
Expand Down Expand Up @@ -1720,7 +1720,7 @@ impl<'a> LoweringContext<'a> {
decl.inputs
.iter()
.map(|arg| match arg.pat.node {
PatKind::Ident(_, ident, None) => respan(ident.span, ident.node.name),
PatKind::Ident(_, ident, None) => respan(ident.span, ident.name),
_ => respan(arg.pat.span, keywords::Invalid.name()),
})
.collect()
Expand Down Expand Up @@ -1810,7 +1810,7 @@ impl<'a> LoweringContext<'a> {
default: tp.default
.as_ref()
.map(|x| self.lower_ty(x, ImplTraitContext::Disallowed)),
span: tp.span,
span: tp.ident.span,
pure_wrt_drop: attr::contains_name(&tp.attrs, "may_dangle"),
synthetic: tp.attrs
.iter()
Expand All @@ -1822,21 +1822,22 @@ impl<'a> LoweringContext<'a> {
}

fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime {
let span = l.ident.span;
match self.lower_ident(l.ident) {
x if x == "'static" => self.new_named_lifetime(l.id, l.span, hir::LifetimeName::Static),
x if x == "'static" => self.new_named_lifetime(l.id, span, hir::LifetimeName::Static),
x if x == "'_" => match self.anonymous_lifetime_mode {
AnonymousLifetimeMode::CreateParameter => {
let fresh_name = self.collect_fresh_in_band_lifetime(l.span);
self.new_named_lifetime(l.id, l.span, fresh_name)
let fresh_name = self.collect_fresh_in_band_lifetime(span);
self.new_named_lifetime(l.id, span, fresh_name)
}

AnonymousLifetimeMode::PassThrough => {
self.new_named_lifetime(l.id, l.span, hir::LifetimeName::Underscore)
self.new_named_lifetime(l.id, span, hir::LifetimeName::Underscore)
}
},
name => {
self.maybe_collect_in_band_lifetime(l.span, name);
self.new_named_lifetime(l.id, l.span, hir::LifetimeName::Name(name))
self.maybe_collect_in_band_lifetime(span, name);
self.new_named_lifetime(l.id, span, hir::LifetimeName::Name(name))
}
}
}
Expand Down Expand Up @@ -2089,10 +2090,7 @@ impl<'a> LoweringContext<'a> {
name: self.lower_ident(match f.ident {
Some(ident) => ident,
// FIXME(jseyfried) positional field hygiene
None => Ident {
name: Symbol::intern(&index.to_string()),
ctxt: f.span.ctxt(),
},
None => Ident::new(Symbol::intern(&index.to_string()), f.span),
}),
vis: self.lower_visibility(&f.vis, None),
ty: self.lower_ty(&f.ty, ImplTraitContext::Disallowed),
Expand All @@ -2102,7 +2100,7 @@ impl<'a> LoweringContext<'a> {

fn lower_field(&mut self, f: &Field) -> hir::Field {
hir::Field {
name: respan(f.ident.span, self.lower_ident(f.ident.node)),
name: respan(f.ident.span, self.lower_ident(f.ident)),
expr: P(self.lower_expr(&f.expr)),
span: f.span,
is_shorthand: f.is_shorthand,
Expand Down Expand Up @@ -2359,11 +2357,11 @@ impl<'a> LoweringContext<'a> {

// Correctly resolve `self` imports
if path.segments.len() > 1
&& path.segments.last().unwrap().identifier.name == keywords::SelfValue.name()
&& path.segments.last().unwrap().ident.name == keywords::SelfValue.name()
{
let _ = path.segments.pop();
if rename.is_none() {
*name = path.segments.last().unwrap().identifier.name;
*name = path.segments.last().unwrap().ident.name;
}
}

Expand Down Expand Up @@ -2804,7 +2802,7 @@ impl<'a> LoweringContext<'a> {
fn lower_pat(&mut self, p: &Pat) -> P<hir::Pat> {
let node = match p.node {
PatKind::Wild => hir::PatKind::Wild,
PatKind::Ident(ref binding_mode, pth1, ref sub) => {
PatKind::Ident(ref binding_mode, ident, ref sub) => {
match self.resolver.get_resolution(p.id).map(|d| d.base_def()) {
// `None` can occur in body-less function signatures
def @ None | def @ Some(Def::Local(_)) => {
Expand All @@ -2815,16 +2813,16 @@ impl<'a> LoweringContext<'a> {
hir::PatKind::Binding(
self.lower_binding_mode(binding_mode),
canonical_id,
respan(pth1.span, pth1.node.name),
respan(ident.span, ident.name),
sub.as_ref().map(|x| self.lower_pat(x)),
)
}
Some(def) => hir::PatKind::Path(hir::QPath::Resolved(
None,
P(hir::Path {
span: pth1.span,
span: ident.span,
def,
segments: hir_vec![hir::PathSegment::from_name(pth1.node.name)],
segments: hir_vec![hir::PathSegment::from_name(ident.name)],
}),
)),
}
Expand Down Expand Up @@ -2939,7 +2937,7 @@ impl<'a> LoweringContext<'a> {
ImplTraitContext::Disallowed,
);
let args = args.iter().map(|x| self.lower_expr(x)).collect();
hir::ExprMethodCall(hir_seg, seg.span, args)
hir::ExprMethodCall(hir_seg, seg.ident.span, args)
}
ExprKind::Binary(binop, ref lhs, ref rhs) => {
let binop = self.lower_binop(binop);
Expand Down Expand Up @@ -3074,7 +3072,7 @@ impl<'a> LoweringContext<'a> {
),
ExprKind::Field(ref el, ident) => hir::ExprField(
P(self.lower_expr(el)),
respan(ident.span, self.lower_ident(ident.node)),
respan(ident.span, self.lower_ident(ident)),
),
ExprKind::TupField(ref el, ident) => hir::ExprTupField(P(self.lower_expr(el)), ident),
ExprKind::Index(ref el, ref er) => {
Expand Down Expand Up @@ -3505,12 +3503,10 @@ impl<'a> LoweringContext<'a> {
let attr = {
// allow(unreachable_code)
let allow = {
let allow_ident = self.str_to_ident("allow");
let uc_ident = self.str_to_ident("unreachable_code");
let uc_meta_item = attr::mk_spanned_word_item(e.span, uc_ident);
let uc_nested = NestedMetaItemKind::MetaItem(uc_meta_item);
let uc_spanned = respan(e.span, uc_nested);
attr::mk_spanned_list_item(e.span, allow_ident, vec![uc_spanned])
let allow_ident = Ident::from_str("allow").with_span_pos(e.span);
let uc_ident = Ident::from_str("unreachable_code").with_span_pos(e.span);
let uc_nested = attr::mk_nested_word_item(uc_ident);
attr::mk_list_item(e.span, allow_ident, vec![uc_nested])
};
attr::mk_spanned_attr_outer(e.span, attr::mk_attr_id(), allow)
};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
for v in &enum_definition.variants {
let variant_def_index =
this.create_def(v.node.data.id(),
DefPathData::EnumVariant(v.node.name.name.as_str()),
DefPathData::EnumVariant(v.node.ident.name.as_str()),
REGULAR_SPACE,
v.span);
this.with_parent(variant_def_index, |this| {
Expand Down Expand Up @@ -202,15 +202,15 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
lifetime_def.lifetime.id,
DefPathData::LifetimeDef(lifetime_def.lifetime.ident.name.as_str()),
REGULAR_SPACE,
lifetime_def.lifetime.span
lifetime_def.lifetime.ident.span
);
}
GenericParam::Type(ref ty_param) => {
self.create_def(
ty_param.id,
DefPathData::TypeParam(ty_param.ident.name.as_str()),
REGULAR_SPACE,
ty_param.span
ty_param.ident.span
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,12 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Ident {
hcx: &mut StableHashingContext<'a>,
hasher: &mut StableHasher<W>) {
let ast::Ident {
ref name,
ctxt: _ // Ignore this
name,
span,
} = *self;

name.hash_stable(hcx, hasher);
span.hash_stable(hcx, hasher);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl_stable_hash_for!(enum ::syntax::ast::FloatTy { F32, F64 });
impl_stable_hash_for!(enum ::syntax::ast::Unsafety { Unsafe, Normal });
impl_stable_hash_for!(enum ::syntax::ast::Constness { Const, NotConst });
impl_stable_hash_for!(enum ::syntax::ast::Defaultness { Default, Final });
impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, span, ident });
impl_stable_hash_for!(struct ::syntax::ast::Lifetime { id, ident });
impl_stable_hash_for!(enum ::syntax::ast::StrStyle { Cooked, Raw(pounds) });
impl_stable_hash_for!(enum ::syntax::ast::AttrStyle { Outer, Inner });

Expand Down Expand Up @@ -211,7 +211,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Attribute {
style.hash_stable(hcx, hasher);
path.segments.len().hash_stable(hcx, hasher);
for segment in &path.segments {
segment.identifier.name.hash_stable(hcx, hasher);
segment.ident.name.hash_stable(hcx, hasher);
}
for tt in tokens.trees() {
tt.hash_stable(hcx, hasher);
Expand Down Expand Up @@ -341,7 +341,7 @@ impl_stable_hash_for!(enum ::syntax::ast::NestedMetaItemKind {
});

impl_stable_hash_for!(struct ::syntax::ast::MetaItem {
name,
ident,
node,
span
});
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,8 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> {
ast_visit::walk_ty(self, t);
}

fn visit_ident(&mut self, sp: Span, id: ast::Ident) {
run_lints!(self, check_ident, early_passes, sp, id);
fn visit_ident(&mut self, ident: ast::Ident) {
run_lints!(self, check_ident, early_passes, ident);
}

fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, _a: &[ast::Attribute], n: ast::NodeId) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'a> LintLevelsBuilder<'a> {
continue
}
};
let name = word.name();
let name = word.ident.name;
match store.check_lint_name(&name.as_str()) {
CheckLintNameResult::Ok(ids) => {
let src = LintSource::Node(name, li.span);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub trait LateLintPass<'a, 'tcx>: LintPass {
}

pub trait EarlyLintPass: LintPass {
fn check_ident(&mut self, _: &EarlyContext, _: Span, _: ast::Ident) { }
fn check_ident(&mut self, _: &EarlyContext, _: ast::Ident) { }
fn check_crate(&mut self, _: &EarlyContext, _: &ast::Crate) { }
fn check_crate_post(&mut self, _: &EarlyContext, _: &ast::Crate) { }
fn check_mod(&mut self, _: &EarlyContext, _: &ast::Mod, _: Span, _: ast::NodeId) { }
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1683,12 +1683,12 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
} else if meta_item.is_meta_item_list() {
let msg = format!(
"invalid predicate in --cfg command line argument: `{}`",
meta_item.name()
meta_item.ident
);
early_error(ErrorOutputType::default(), &msg)
}

(meta_item.name(), meta_item.value_str())
(meta_item.ident.name, meta_item.value_str())
})
.collect::<ast::CrateConfig>()
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/on_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedDirective {
for command in self.subcommands.iter().chain(Some(self)).rev() {
if let Some(ref condition) = command.condition {
if !attr::eval_condition(condition, &tcx.sess.parse_sess, &mut |c| {
options.contains(&(c.name().as_str().to_string(),
options.contains(&(c.ident.name.as_str().to_string(),
match c.value_str().map(|s| s.as_str().to_string()) {
Some(s) => Some(s),
None => None
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2088,8 +2088,8 @@ impl<'a, 'gcx, 'tcx> VariantDef {
return Some(index);
}
let mut ident = name.to_ident();
while ident.ctxt != SyntaxContext::empty() {
ident.ctxt.remove_mark();
while ident.span.ctxt() != SyntaxContext::empty() {
ident.span.remove_mark();
if let Some(field) = self.fields.iter().position(|f| f.name.to_ident() == ident) {
return Some(field);
}
Expand Down Expand Up @@ -2558,7 +2558,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
LOCAL_CRATE => self.hir.definitions().expansion(scope.index),
_ => Mark::root(),
};
let scope = match ident.ctxt.adjust(expansion) {
let scope = match ident.span.adjust(expansion) {
Some(macro_def) => self.hir.definitions().macro_def_scope(macro_def),
None if block == DUMMY_NODE_ID => DefId::local(CRATE_DEF_INDEX), // Dummy DefId
None => self.hir.get_module_parent(block),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ impl RustcDefaultCalls {
let mut cfgs = Vec::new();
for &(name, ref value) in sess.parse_sess.config.iter() {
let gated_cfg = GatedCfg::gate(&ast::MetaItem {
name,
ident: ast::Ident::with_empty_ctxt(name),
node: ast::MetaItemKind::Word,
span: DUMMY_SP,
});
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,11 @@ impl<'a> PrinterSupport for HygieneAnnotation<'a> {
impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
fn post(&self, s: &mut pprust::State, node: pprust::AnnNode) -> io::Result<()> {
match node {
pprust::NodeIdent(&ast::Ident { name, ctxt }) => {
pprust::NodeIdent(&ast::Ident { name, span }) => {
s.s.space()?;
// FIXME #16420: this doesn't display the connections
// between syntax contexts
s.synth_comment(format!("{}{:?}", name.as_u32(), ctxt))
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
}
pprust::NodeName(&name) => {
s.s.space()?;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/assert_dep_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a, 'tcx> IfThisChanged<'a, 'tcx> {
for list_item in attr.meta_item_list().unwrap_or_default() {
match list_item.word() {
Some(word) if value.is_none() =>
value = Some(word.name().clone()),
value = Some(word.ident.name),
_ =>
// FIXME better-encapsulate meta_item (don't directly access `node`)
span_bug!(list_item.span(), "unexpected meta-item {:?}", list_item.node),
Expand Down
Loading

0 comments on commit a143462

Please sign in to comment.