Skip to content

Commit

Permalink
Use Span instead of SyntaxContext in Ident
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Apr 6, 2018
1 parent 48fa6f9 commit baae274
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/libproc_macro/lib.rs
Expand Up @@ -1097,7 +1097,7 @@ 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) }
Expand Down
7 changes: 2 additions & 5 deletions src/librustc/hir/lowering.rs
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 Down Expand Up @@ -2089,10 +2089,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 Down
2 changes: 1 addition & 1 deletion src/librustc/ich/impls_hir.rs
Expand Up @@ -655,7 +655,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Ident {
hasher: &mut StableHasher<W>) {
let ast::Ident {
ref name,
ctxt: _ // Ignore this
span: _ // Ignore this
} = *self;

name.hash_stable(hcx, hasher);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/mod.rs
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
4 changes: 2 additions & 2 deletions src/librustc_driver/pretty.rs
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
13 changes: 6 additions & 7 deletions src/librustc_privacy/lib.rs
Expand Up @@ -35,7 +35,6 @@ use rustc::util::nodemap::NodeSet;
use syntax::ast::{self, CRATE_NODE_ID, Ident};
use syntax::symbol::keywords;
use syntax_pos::Span;
use syntax_pos::hygiene::SyntaxContext;

use std::cmp;
use std::mem::replace;
Expand Down Expand Up @@ -495,11 +494,11 @@ struct NamePrivacyVisitor<'a, 'tcx: 'a> {
impl<'a, 'tcx> NamePrivacyVisitor<'a, 'tcx> {
// Checks that a field in a struct constructor (expression or pattern) is accessible.
fn check_field(&mut self,
use_ctxt: SyntaxContext, // Syntax context of the field name at the use site
use_ctxt: Span, // Syntax context of the field name at the use site
span: Span, // Span of the field pattern, e.g. `x: 0`
def: &'tcx ty::AdtDef, // Definition of the struct or enum
field: &'tcx ty::FieldDef) { // Definition of the field
let ident = Ident { ctxt: use_ctxt.modern(), ..keywords::Invalid.ident() };
let ident = Ident::new(keywords::Invalid.name(), use_ctxt.modern());
let def_id = self.tcx.adjust_ident(ident, def.did, self.current_item).1;
if !def.is_enum() && !field.vis.is_accessible_from(def_id, self.tcx) {
struct_span_err!(self.tcx.sess, span, E0451, "field `{}` of {} `{}` is private",
Expand Down Expand Up @@ -573,14 +572,14 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
for variant_field in &variant.fields {
let field = fields.iter().find(|f| f.name.node == variant_field.name);
let (use_ctxt, span) = match field {
Some(field) => (field.name.node.to_ident().ctxt, field.span),
None => (base.span.ctxt(), base.span),
Some(field) => (field.name.node.to_ident().span, field.span),
None => (base.span, base.span),
};
self.check_field(use_ctxt, span, adt, variant_field);
}
} else {
for field in fields {
let use_ctxt = field.name.node.to_ident().ctxt;
let use_ctxt = field.name.node.to_ident().span;
let field_def = variant.field_named(field.name.node);
self.check_field(use_ctxt, field.span, adt, field_def);
}
Expand All @@ -599,7 +598,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NamePrivacyVisitor<'a, 'tcx> {
let adt = self.tables.pat_ty(pat).ty_adt_def().unwrap();
let variant = adt.variant_of_def(def);
for field in fields {
let use_ctxt = field.node.name.to_ident().ctxt;
let use_ctxt = field.node.name.to_ident().span;
let field_def = variant.field_named(field.node.name);
self.check_field(use_ctxt, field.span, adt, field_def);
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -157,7 +157,7 @@ impl<'a> Resolver<'a> {

// Disallow `use $crate;`
if source.name == keywords::DollarCrate.name() && path.segments.len() == 1 {
let crate_root = self.resolve_crate_root(source.ctxt, true);
let crate_root = self.resolve_crate_root(source.span.ctxt(), true);
let crate_name = match crate_root.kind {
ModuleKind::Def(_, name) => name,
ModuleKind::Block(..) => unreachable!(),
Expand Down
53 changes: 27 additions & 26 deletions src/librustc_resolve/lib.rs
Expand Up @@ -1912,10 +1912,11 @@ impl<'a> Resolver<'a> {
path_span: Span)
-> Option<LexicalScopeBinding<'a>> {
if ns == TypeNS {
ident.ctxt = if ident.name == keywords::SelfType.name() {
SyntaxContext::empty() // FIXME(jseyfried) improve `Self` hygiene
ident.span = if ident.name == keywords::SelfType.name() {
// FIXME(jseyfried) improve `Self` hygiene
ident.span.with_ctxt(SyntaxContext::empty())
} else {
ident.ctxt.modern()
ident.span.modern()
}
}

Expand All @@ -1931,10 +1932,10 @@ impl<'a> Resolver<'a> {

module = match self.ribs[ns][i].kind {
ModuleRibKind(module) => module,
MacroDefinition(def) if def == self.macro_def(ident.ctxt) => {
MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
// If an invocation of this macro created `ident`, give up on `ident`
// and switch to `ident`'s source from the macro definition.
ident.ctxt.remove_mark();
ident.span.remove_mark();
continue
}
_ => continue,
Expand All @@ -1954,9 +1955,9 @@ impl<'a> Resolver<'a> {
}
}

ident.ctxt = ident.ctxt.modern();
ident.span = ident.span.modern();
loop {
module = unwrap_or!(self.hygienic_lexical_parent(module, &mut ident.ctxt), break);
module = unwrap_or!(self.hygienic_lexical_parent(module, &mut ident.span), break);
let orig_current_module = self.current_module;
self.current_module = module; // Lexical resolutions can never be a privacy error.
let result = self.resolve_ident_in_module_unadjusted(
Expand All @@ -1980,10 +1981,10 @@ impl<'a> Resolver<'a> {
}
}

fn hygienic_lexical_parent(&mut self, mut module: Module<'a>, ctxt: &mut SyntaxContext)
fn hygienic_lexical_parent(&mut self, mut module: Module<'a>, span: &mut Span)
-> Option<Module<'a>> {
if !module.expansion.is_descendant_of(ctxt.outer()) {
return Some(self.macro_def_scope(ctxt.remove_mark()));
if !module.expansion.is_descendant_of(span.ctxt().outer()) {
return Some(self.macro_def_scope(span.remove_mark()));
}

if let ModuleKind::Block(..) = module.kind {
Expand All @@ -1995,7 +1996,7 @@ impl<'a> Resolver<'a> {
let parent_expansion = parent.expansion.modern();
if module_expansion.is_descendant_of(parent_expansion) &&
parent_expansion != module_expansion {
return if parent_expansion.is_descendant_of(ctxt.outer()) {
return if parent_expansion.is_descendant_of(span.ctxt().outer()) {
Some(parent)
} else {
None
Expand All @@ -2016,9 +2017,9 @@ impl<'a> Resolver<'a> {
record_used: bool,
span: Span)
-> Result<&'a NameBinding<'a>, Determinacy> {
ident.ctxt = ident.ctxt.modern();
ident.span = ident.span.modern();
let orig_current_module = self.current_module;
if let Some(def) = ident.ctxt.adjust(module.expansion) {
if let Some(def) = ident.span.adjust(module.expansion) {
self.current_module = self.macro_def_scope(def);
}
let result = self.resolve_ident_in_module_unadjusted(
Expand Down Expand Up @@ -2108,8 +2109,8 @@ impl<'a> Resolver<'a> {
// If an invocation of this macro created `ident`, give up on `ident`
// and switch to `ident`'s source from the macro definition.
MacroDefinition(def) => {
if def == self.macro_def(ident.ctxt) {
ident.ctxt.remove_mark();
if def == self.macro_def(ident.span.ctxt()) {
ident.span.remove_mark();
}
}
_ => {
Expand Down Expand Up @@ -2873,7 +2874,7 @@ impl<'a> Resolver<'a> {
}
if path.len() == 1 && this.self_type_is_available(span) {
if let Some(candidate) = this.lookup_assoc_candidate(ident.node, ns, is_expected) {
let self_is_available = this.self_value_is_available(path[0].node.ctxt, span);
let self_is_available = this.self_value_is_available(path[0].node.span, span);
match candidate {
AssocSuggestion::Field => {
err.span_suggestion(span, "try",
Expand Down Expand Up @@ -3084,9 +3085,9 @@ impl<'a> Resolver<'a> {
if let Some(LexicalScopeBinding::Def(def)) = binding { def != Def::Err } else { false }
}

fn self_value_is_available(&mut self, ctxt: SyntaxContext, span: Span) -> bool {
let ident = Ident { name: keywords::SelfValue.name(), ctxt: ctxt };
let binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, false, span);
fn self_value_is_available(&mut self, self_span: Span, path_span: Span) -> bool {
let ident = Ident::new(keywords::SelfValue.name(), self_span);
let binding = self.resolve_ident_in_lexical_scope(ident, ValueNS, false, path_span);
if let Some(LexicalScopeBinding::Def(def)) = binding { def != Def::Err } else { false }
}

Expand Down Expand Up @@ -3219,11 +3220,11 @@ impl<'a> Resolver<'a> {
let name = ident.node.name;

if i == 0 && ns == TypeNS && name == keywords::SelfValue.name() {
let mut ctxt = ident.node.ctxt.modern();
let mut ctxt = ident.node.span.ctxt().modern();
module = Some(self.resolve_self(&mut ctxt, self.current_module));
continue
} else if allow_super && ns == TypeNS && name == keywords::Super.name() {
let mut ctxt = ident.node.ctxt.modern();
let mut ctxt = ident.node.span.ctxt().modern();
let self_module = match i {
0 => self.resolve_self(&mut ctxt, self.current_module),
_ => module.unwrap(),
Expand All @@ -3245,11 +3246,11 @@ impl<'a> Resolver<'a> {
(i == 1 && name == keywords::Crate.name() &&
path[0].node.name == keywords::CrateRoot.name()) {
// `::a::b` or `::crate::a::b`
module = Some(self.resolve_crate_root(ident.node.ctxt, false));
module = Some(self.resolve_crate_root(ident.node.span.ctxt(), false));
continue
} else if i == 0 && name == keywords::DollarCrate.name() {
// `$crate::a::b`
module = Some(self.resolve_crate_root(ident.node.ctxt, true));
module = Some(self.resolve_crate_root(ident.node.span.ctxt(), true));
continue
} else if i == 1 && !token::is_path_segment_keyword(ident.node) {
let prev_name = path[0].node.name;
Expand Down Expand Up @@ -3771,12 +3772,12 @@ impl<'a> Resolver<'a> {
}
}

ident.ctxt = ident.ctxt.modern();
ident.span = ident.span.modern();
let mut search_module = self.current_module;
loop {
self.get_traits_in_module_containing_item(ident, ns, search_module, &mut found_traits);
search_module =
unwrap_or!(self.hygienic_lexical_parent(search_module, &mut ident.ctxt), break);
unwrap_or!(self.hygienic_lexical_parent(search_module, &mut ident.span), break);
}

if let Some(prelude) = self.prelude {
Expand Down Expand Up @@ -3808,7 +3809,7 @@ impl<'a> Resolver<'a> {
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
let module = binding.module().unwrap();
let mut ident = ident;
if ident.ctxt.glob_adjust(module.expansion, binding.span.ctxt().modern()).is_none() {
if ident.span.glob_adjust(module.expansion, binding.span.ctxt().modern()).is_none() {
continue
}
if self.resolve_ident_in_module_unadjusted(module, ident, ns, false, false, module.span)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/macros.rs
Expand Up @@ -140,7 +140,7 @@ impl<'a> base::Resolver for Resolver<'a> {
let ident = path.segments[0].identifier;
if ident.name == keywords::DollarCrate.name() {
path.segments[0].identifier.name = keywords::CrateRoot.name();
let module = self.0.resolve_crate_root(ident.ctxt, true);
let module = self.0.resolve_crate_root(ident.span.ctxt(), true);
if !module.is_local() {
let span = path.segments[0].span;
path.segments.insert(1, match module.kind {
Expand Down Expand Up @@ -534,7 +534,7 @@ impl<'a> Resolver<'a> {
}

module = match module {
Some(module) => self.hygienic_lexical_parent(module, &mut ident.ctxt),
Some(module) => self.hygienic_lexical_parent(module, &mut ident.span),
None => return potential_illegal_shadower,
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -238,7 +238,7 @@ impl<'a> Resolver<'a> {
}
let module = unwrap_or!(directive.imported_module.get(), return Err(Undetermined));
let (orig_current_module, mut ident) = (self.current_module, ident.modern());
match ident.ctxt.glob_adjust(module.expansion, directive.span.ctxt().modern()) {
match ident.span.glob_adjust(module.expansion, directive.span.ctxt().modern()) {
Some(Some(def)) => self.current_module = self.macro_def_scope(def),
Some(None) => {}
None => continue,
Expand Down Expand Up @@ -398,7 +398,7 @@ impl<'a> Resolver<'a> {
// Define `binding` in `module`s glob importers.
for directive in module.glob_importers.borrow_mut().iter() {
let mut ident = ident.modern();
let scope = match ident.ctxt.reverse_glob_adjust(module.expansion,
let scope = match ident.span.reverse_glob_adjust(module.expansion,
directive.span.ctxt().modern()) {
Some(Some(def)) => self.macro_def_scope(def),
Some(None) => directive.parent,
Expand Down Expand Up @@ -623,7 +623,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
"crate root imports need to be explicitly named: \
`use crate as name;`".to_string()));
} else {
Some(self.resolve_crate_root(source.ctxt.modern(), false))
Some(self.resolve_crate_root(source.span.ctxt().modern(), false))
}
} else if is_extern && !token::is_path_segment_keyword(source) {
let crate_id =
Expand Down Expand Up @@ -860,7 +860,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
resolution.borrow().binding().map(|binding| (ident, binding))
}).collect::<Vec<_>>();
for ((mut ident, ns), binding) in bindings {
let scope = match ident.ctxt.reverse_glob_adjust(module.expansion,
let scope = match ident.span.reverse_glob_adjust(module.expansion,
directive.span.ctxt().modern()) {
Some(Some(def)) => self.macro_def_scope(def),
Some(None) => self.current_module,
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -3213,10 +3213,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
if !tuple_like { continue }

debug!("tuple struct named {:?}", base_t);
let ident = ast::Ident {
name: Symbol::intern(&idx.node.to_string()),
ctxt: idx.span.ctxt().modern(),
};
let ident =
ast::Ident::new(Symbol::intern(&idx.node.to_string()), idx.span.modern());
let (ident, def_scope) =
self.tcx.adjust_ident(ident, base_def.did, self.body_id);
let fields = &base_def.non_enum_variant().fields;
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ast.rs
Expand Up @@ -150,7 +150,7 @@ impl PathSegment {
}
pub fn crate_root(span: Span) -> Self {
PathSegment {
identifier: Ident { ctxt: span.ctxt(), ..keywords::CrateRoot.ident() },
identifier: Ident::new(keywords::CrateRoot.name(), span),
span,
parameters: None,
}
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/base.rs
Expand Up @@ -876,8 +876,8 @@ impl<'a> ExtCtxt<'a> {
ast::Ident::from_str(st)
}
pub fn std_path(&self, components: &[&str]) -> Vec<ast::Ident> {
let def_site = SyntaxContext::empty().apply_mark(self.current_expansion.mark);
iter::once(Ident { ctxt: def_site, ..keywords::DollarCrate.ident() })
let def_site = DUMMY_SP.apply_mark(self.current_expansion.mark);
iter::once(Ident::new(keywords::DollarCrate.name(), def_site))
.chain(components.iter().map(|s| self.ident_of(s)))
.collect()
}
Expand Down

0 comments on commit baae274

Please sign in to comment.