From d7072b5bb42eb005ab3adac2605f175d8719e1f1 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 28 Jun 2018 00:12:17 +0300 Subject: [PATCH] Fix rebase --- src/librustc/hir/lowering.rs | 35 ++++++++++---------- src/librustc/hir/map/mod.rs | 6 ++-- src/librustc/hir/mod.rs | 28 ++++++++++------ src/librustc/hir/print.rs | 4 +-- src/librustc/ich/impls_hir.rs | 1 + src/librustc/middle/resolve_lifetime.rs | 24 +++++++------- src/librustc_mir/hair/pattern/check_match.rs | 2 +- src/librustc_resolve/lib.rs | 10 +++--- src/librustc_typeck/check/mod.rs | 2 +- src/librustdoc/clean/auto_trait.rs | 2 +- src/librustdoc/clean/mod.rs | 10 +++--- src/libsyntax_pos/symbol.rs | 5 +++ 12 files changed, 71 insertions(+), 58 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 96f247f90d1..260ad003283 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -676,7 +676,7 @@ impl<'a> LoweringContext<'a> { // that collisions are ok here and this shouldn't // really show up for end-user. let str_name = match hir_name { - ParamName::Plain(ident) => name.as_interned_str(), + ParamName::Plain(ident) => ident.as_interned_str(), ParamName::Fresh(_) => keywords::UnderscoreLifetime.name().as_interned_str(), }; @@ -684,7 +684,7 @@ impl<'a> LoweringContext<'a> { self.resolver.definitions().create_def_with_parent( parent_id.index, def_node_id, - DefPathData::LifetimeDef(str_name), + DefPathData::LifetimeParam(str_name), DefIndexAddressSpace::High, Mark::root(), span, @@ -719,10 +719,10 @@ impl<'a> LoweringContext<'a> { return; } - let hir_name = ParamName::Plain(name); + let hir_name = ParamName::Plain(ident); if self.lifetimes_to_define.iter() - .any(|(_, lt_name)| *lt_name.modern() == hir_name.modern()) { + .any(|(_, lt_name)| lt_name.modern() == hir_name.modern()) { return; } @@ -1185,10 +1185,11 @@ impl<'a> LoweringContext<'a> { let ident = Ident::from_str(&pprust::ty_to_string(t)).with_span_pos(span); self.in_band_ty_params.push(hir::GenericParam { id: def_node_id, - ident: ParamName::Plain(ident), + name: ParamName::Plain(ident), pure_wrt_drop: false, attrs: hir_vec![], bounds: hir_bounds, + span, kind: hir::GenericParamKind::Type { default: None, synthetic: Some(hir::SyntheticTyParamKind::ImplTrait), @@ -1438,7 +1439,7 @@ impl<'a> LoweringContext<'a> { let name = match name { hir::LifetimeName::Underscore => { - hir::ParamName::Plain(keywords::UnderscoreLifetime.name()) + hir::ParamName::Plain(keywords::UnderscoreLifetime.ident()) } hir::LifetimeName::Param(param_name) => param_name, _ => bug!("expected LifetimeName::Param or ParamName::Plain"), @@ -2101,7 +2102,7 @@ impl<'a> LoweringContext<'a> { let future_params = P(hir::GenericArgs { args: hir_vec![], bindings: hir_vec![hir::TypeBinding { - name: Symbol::intern(FN_OUTPUT_NAME), + ident: Ident::from_str(FN_OUTPUT_NAME), ty: output_ty, id: this.next_id().node_id, span, @@ -2163,7 +2164,7 @@ impl<'a> LoweringContext<'a> { fn lower_lifetime(&mut self, l: &Lifetime) -> hir::Lifetime { let span = l.ident.span; - match self.lower_ident(l.ident) { + match l.ident { ident if ident.name == keywords::StaticLifetime.name() => self.new_named_lifetime(l.id, span, hir::LifetimeName::Static), ident if ident.name == keywords::UnderscoreLifetime.name() => @@ -2221,7 +2222,7 @@ impl<'a> LoweringContext<'a> { let lt = self.lower_lifetime(&Lifetime { id: param.id, ident: param.ident }); let param_name = match lt.name { hir::LifetimeName::Param(param_name) => param_name, - _ => hir::ParamName::Plain(lt.name.name()), + _ => hir::ParamName::Plain(lt.name.ident()), }; let param = hir::GenericParam { id: lt.id, @@ -2238,14 +2239,14 @@ impl<'a> LoweringContext<'a> { param } GenericParamKind::Type { ref default, .. } => { - let mut name = self.lower_ident(param.ident); - // Don't expose `Self` (recovered "keyword used as ident" parse error). // `rustc::ty` expects `Self` to be only used for a trait's `Self`. // Instead, use gensym("Self") to create a distinct name that looks the same. - if name == keywords::SelfType.name() { - name = Symbol::gensym("Self"); - } + let ident = if param.ident.name == keywords::SelfType.name() { + param.ident.gensym() + } else { + param.ident + }; let add_bounds = add_bounds.get(¶m.id).map_or(&[][..], |x| &x); if !add_bounds.is_empty() { @@ -2256,11 +2257,11 @@ impl<'a> LoweringContext<'a> { hir::GenericParam { id: self.lower_node_id(param.id).node_id, - name: hir::ParamName::Plain(name), - span: param.ident.span, + name: hir::ParamName::Plain(ident), pure_wrt_drop: attr::contains_name(¶m.attrs, "may_dangle"), attrs: self.lower_attrs(¶m.attrs), bounds, + span: ident.span, kind: hir::GenericParamKind::Type { default: default.as_ref().map(|x| { self.lower_ty(x, ImplTraitContext::Disallowed) @@ -3656,7 +3657,7 @@ impl<'a> LoweringContext<'a> { let e2 = self.lower_expr(e2); let ty_path = P(self.std_path(span, &["ops", "RangeInclusive"], None, false)); let ty = P(self.ty_path(id, span, hir::QPath::Resolved(None, ty_path))); - let new_seg = P(hir::PathSegment::from_name(Ident::from_str("new"))); + let new_seg = P(hir::PathSegment::from_ident(Ident::from_str("new"))); let new_path = hir::QPath::TypeRelative(ty, new_seg); let new = P(self.expr(span, hir::ExprPath(new_path), ThinVec::new())); hir::ExprCall(new, hir_vec![e1, e2]) diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 5f390f20aca..b7071970a04 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -616,7 +616,7 @@ impl<'hir> Map<'hir> { NodeItem(&Item { node: ItemTrait(..), .. }) => { keywords::SelfType.name() } - NodeGenericParam(param) => param.name.ident(), + NodeGenericParam(param) => param.name.ident().name, _ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)), } } @@ -955,7 +955,7 @@ impl<'hir> Map<'hir> { NodeField(f) => f.ident.name, NodeLifetime(lt) => lt.name.ident().name, NodeGenericParam(param) => param.name.ident().name, - NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.node, + NodeBinding(&Pat { node: PatKind::Binding(_,_,l,_), .. }) => l.name, NodeStructCtor(_) => self.name(self.get_parent(id)), _ => bug!("no name for {}", self.node_to_string(id)) } @@ -1021,7 +1021,7 @@ impl<'hir> Map<'hir> { Some(EntryBlock(_, _, block)) => block.span, Some(EntryStructCtor(_, _, _)) => self.expect_item(self.get_parent(id)).span, Some(EntryLifetime(_, _, lifetime)) => lifetime.span, - Some(EntryGenericParam(_, _, param)) => param.ident.span, + Some(EntryGenericParam(_, _, param)) => param.span, Some(EntryVisibility(_, _, &Visibility::Restricted { ref path, .. })) => path.span, Some(EntryVisibility(_, _, v)) => bug!("unexpected Visibility {:?}", v), Some(EntryLocal(_, _, local)) => local.span, diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index e46c0ba6f11..54164747210 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -201,7 +201,7 @@ pub struct Lifetime { #[derive(Debug, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub enum ParamName { /// Some user-given name like `T` or `'x`. - Plain(Name), + Plain(Ident), /// Synthetic name generated when user elided a lifetime in an impl header, /// e.g. the lifetimes in cases like these: @@ -220,10 +220,17 @@ pub enum ParamName { } impl ParamName { - pub fn name(&self) -> Name { + pub fn ident(&self) -> Ident { match *self { - ParamName::Plain(name) => name, - ParamName::Fresh(_) => keywords::UnderscoreLifetime.name(), + ParamName::Plain(ident) => ident, + ParamName::Fresh(_) => keywords::UnderscoreLifetime.ident(), + } + } + + pub fn modern(&self) -> ParamName { + match *self { + ParamName::Plain(ident) => ParamName::Plain(ident.modern()), + param_name => param_name, } } } @@ -247,24 +254,22 @@ impl LifetimeName { pub fn ident(&self) -> Ident { match *self { LifetimeName::Implicit => keywords::Invalid.ident(), - LifetimeName::Fresh(_) | LifetimeName::Underscore => - keywords::UnderscoreLifetime.ident(), + LifetimeName::Underscore => keywords::UnderscoreLifetime.ident(), LifetimeName::Static => keywords::StaticLifetime.ident(), - LifetimeName::Ident(ident) => ident, + LifetimeName::Param(param_name) => param_name.ident(), } } pub fn is_elided(&self) -> bool { - use self::LifetimeName::*; match self { - Implicit | Underscore => true, + LifetimeName::Implicit | LifetimeName::Underscore => true, // It might seem surprising that `Fresh(_)` counts as // *not* elided -- but this is because, as far as the code // in the compiler is concerned -- `Fresh(_)` variants act // equivalently to "some fresh name". They correspond to // early-bound regions on an impl, in other words. - Param(_) | Static => false, + LifetimeName::Param(_) | LifetimeName::Static => false, } } @@ -274,7 +279,7 @@ impl LifetimeName { pub fn modern(&self) -> LifetimeName { match *self { - LifetimeName::Ident(ident) => LifetimeName::Ident(ident.modern()), + LifetimeName::Param(param_name) => LifetimeName::Param(param_name.modern()), lifetime_name => lifetime_name, } } @@ -492,6 +497,7 @@ pub struct GenericParam { pub name: ParamName, pub attrs: HirVec, pub bounds: GenericBounds, + pub span: Span, pub pure_wrt_drop: bool, pub kind: GenericParamKind, diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 40dc9d04f21..c6f69a84d03 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -2113,7 +2113,7 @@ impl<'a> State<'a> { } pub fn print_generic_param(&mut self, param: &GenericParam) -> io::Result<()> { - self.print_name(param.name.name())?; + self.print_ident(param.name.ident())?; match param.kind { GenericParamKind::Lifetime { .. } => { let mut sep = ":"; @@ -2144,7 +2144,7 @@ impl<'a> State<'a> { } pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) -> io::Result<()> { - self.print_name(lifetime.name.name()) + self.print_ident(lifetime.name.ident()) } pub fn print_where_clause(&mut self, where_clause: &hir::WhereClause) -> io::Result<()> { diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index a9897186a10..d59a20c6522 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -203,6 +203,7 @@ impl_stable_hash_for!(struct hir::GenericParam { pure_wrt_drop, attrs, bounds, + span, kind }); diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 5d5ada7b91e..ed2b9c50689 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -689,7 +689,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> { GenericParamKind::Lifetime { .. } => { let (name, reg) = Region::early(&self.tcx.hir, &mut index, ¶m); if let hir::ParamName::Plain(param_name) = name { - if param_name == keywords::UnderscoreLifetime.name() { + if param_name.name == keywords::UnderscoreLifetime.name() { // Pick the elided lifetime "definition" if one exists // and use it to make an elision scope. elision = Some(reg); @@ -1175,7 +1175,7 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body) { ref lifetimes, s, .. } => { // FIXME (#24278): non-hygienic comparison - if let Some(def) = lifetimes.get(&hir::LifetimeName::Ident(label.modern())) { + if let Some(def) = lifetimes.get(&hir::ParamName::Plain(label.modern())) { let node_id = tcx.hir.as_local_node_id(def.id().unwrap()).unwrap(); signal_shadowing_problem( @@ -1397,10 +1397,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { debug!("node id first={:?}", node_id); if let Some((id, span, name)) = match self.tcx.hir.get(node_id) { hir::map::NodeLifetime(hir_lifetime) => { - Some((hir_lifetime.id, hir_lifetime.span, hir_lifetime.name.name())) + Some((hir_lifetime.id, hir_lifetime.span, hir_lifetime.name.ident())) } hir::map::NodeGenericParam(param) => { - Some((param.id, param.span, param.name.name())) + Some((param.id, param.span, param.name.ident())) } _ => None, } { @@ -1423,10 +1423,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { let node_id = self.tcx.hir.as_local_node_id(def_id).unwrap(); if let Some((id, span, name)) = match self.tcx.hir.get(node_id) { hir::map::NodeLifetime(hir_lifetime) => { - Some((hir_lifetime.id, hir_lifetime.span, hir_lifetime.name.name())) + Some((hir_lifetime.id, hir_lifetime.span, hir_lifetime.name.ident())) } hir::map::NodeGenericParam(param) => { - Some((param.id, param.span, param.name.name())) + Some((param.id, param.span, param.name.ident())) } _ => None, } { @@ -2243,7 +2243,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { }).collect(); for (i, (lifetime_i, lifetime_i_name)) in lifetimes.iter().enumerate() { if let hir::ParamName::Plain(_) = lifetime_i_name { - let name = lifetime_i_name.name(); + let name = lifetime_i_name.ident().name; if name == keywords::UnderscoreLifetime.name() || name == keywords::StaticLifetime.name() { let mut err = struct_span_err!( @@ -2251,7 +2251,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { lifetime_i.span, E0262, "invalid lifetime parameter name: `{}`", - lifetime + lifetime_i.name.ident(), ); err.span_label( lifetime_i.span, @@ -2269,7 +2269,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { lifetime_j.span, E0263, "lifetime name `{}` declared twice in the same scope", - lifetime_j.name.name() + lifetime_j.name.ident() ).span_label(lifetime_j.span, "declared twice") .span_label(lifetime_i.span, "previous declaration here") .emit(); @@ -2298,12 +2298,12 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { lifetime_i.span.to(lt.span), &format!( "unnecessary lifetime parameter `{}`", - lifetime_i.name.name(), + lifetime_i.name.ident(), ), ).help(&format!( "you can use the `'static` lifetime directly, in place \ of `{}`", - lifetime_i.name.name(), + lifetime_i.name.ident(), )).emit(); } hir::LifetimeName::Param(_) @@ -2549,7 +2549,7 @@ fn insert_late_bound_lifetimes( } debug!("insert_late_bound_lifetimes: lifetime {:?} with id {:?} is late-bound", - param.name.name(), + param.name.ident(), param.id); let inserted = map.late_bound.insert(param.id); diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index 0f4052b6fc8..19b3654d2c7 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -331,7 +331,7 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchVisitor, pat: &Pat) { err.span_suggestion_with_applicability( p.span, "to match on the variant, qualify the path", - format!("{}::{}", ty_path, name.node), + format!("{}::{}", ty_path, ident), Applicability::MachineApplicable ); err.emit(); diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index d3825ff4919..464356eb5d2 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1497,17 +1497,17 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> { args: Option>, is_value: bool ) -> hir::Path { - let mut segments = iter::once(keywords::CrateRoot.name()) + let mut segments = iter::once(keywords::CrateRoot.ident()) .chain( crate_root.into_iter() .chain(components.iter().cloned()) - .map(Symbol::intern) - ).map(hir::PathSegment::from_name).collect::>(); + .map(Ident::from_str) + ).map(hir::PathSegment::from_ident).collect::>(); if let Some(args) = args { - let name = segments.last().unwrap().name; + let ident = segments.last().unwrap().ident; *segments.last_mut().unwrap() = hir::PathSegment { - name, + ident, args: Some(args), infer_types: true, }; diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index f42366e5dbe..f8c4bfc9ad7 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3836,7 +3836,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // ... except when we try to 'break rust;'. // ICE this expression in particular (see #43162). if let hir::ExprPath(hir::QPath::Resolved(_, ref path)) = e.node { - if path.segments.len() == 1 && path.segments[0].name == "rust" { + if path.segments.len() == 1 && path.segments[0].ident.name == "rust" { fatally_break_rust(self.tcx.sess); } } diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 35483e33c47..527aef80a8d 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -251,7 +251,7 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> { match param.kind { ty::GenericParamDefKind::Lifetime => { let name = if param.name == "" { - hir::ParamName::Plain(keywords::StaticLifetime.name()) + hir::ParamName::Plain(keywords::StaticLifetime.ident()) } else { hir::ParamName::Plain(ast::Ident::from_interned_str(param.name)) }; diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index c4729ce67d8..e9f8aeb9ec8 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1896,19 +1896,19 @@ impl Clean for hir::GenericParam { hir::GenericBound::Outlives(lt) => lt, _ => panic!(), }); - let name = bounds.next().unwrap().name.name(); - let mut s = format!("{}: {}", self.name.name(), name); + let name = bounds.next().unwrap().name.ident(); + let mut s = format!("{}: {}", self.name.ident(), name); for bound in bounds { - s.push_str(&format!(" + {}", bound.name.name())); + s.push_str(&format!(" + {}", bound.name.ident())); } s } else { - self.name.name().to_string() + self.name.ident().to_string() }; (name, GenericParamDefKind::Lifetime) } hir::GenericParamKind::Type { ref default, synthetic, .. } => { - (self.name.name().clean(cx), GenericParamDefKind::Type { + (self.name.ident().name.clean(cx), GenericParamDefKind::Type { did: cx.tcx.hir.local_def_id(self.id), bounds: self.bounds.clean(cx), default: default.clean(cx), diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index b92553842bc..bb64dad1208 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -59,6 +59,11 @@ impl Ident { Ident::new(Symbol::intern(self.as_str().trim_left_matches('\'')), self.span) } + /// "Normalize" ident for use in comparisons using "item hygiene". + /// Identifiers with same string value become same if they came from the same "modern" macro + /// (e.g. `macro` item, but not `macro_rules` item) and stay different if they came from + /// different "modern" macros. + /// Technically, this operation strips all non-opaque marks from ident's syntactic context. pub fn modern(self) -> Ident { Ident::new(self.name, self.span.modern()) }