Skip to content

Commit c6ca1e4

Browse files
committed
Use Idents in a number of structures in HIR
Namely: labels, type parameters, bindings in patterns, parameter names in functions without body. All of these do not need hygiene after lowering to HIR, only span locations.
1 parent e8215a4 commit c6ca1e4

File tree

24 files changed

+125
-156
lines changed

24 files changed

+125
-156
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ pub fn walk_ident<'v, V: Visitor<'v>>(visitor: &mut V, ident: Ident) {
426426
}
427427

428428
pub fn walk_label<'v, V: Visitor<'v>>(visitor: &mut V, label: &'v Label) {
429-
visitor.visit_name(label.span, label.name);
429+
visitor.visit_ident(label.ident);
430430
}
431431

432432
pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime) {
@@ -689,9 +689,9 @@ pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {
689689
PatKind::Ref(ref subpattern, _) => {
690690
visitor.visit_pat(subpattern)
691691
}
692-
PatKind::Binding(_, canonical_id, ref pth1, ref optional_subpattern) => {
692+
PatKind::Binding(_, canonical_id, ident, ref optional_subpattern) => {
693693
visitor.visit_def_mention(Def::Local(canonical_id));
694-
visitor.visit_name(pth1.span, pth1.node);
694+
visitor.visit_ident(ident);
695695
walk_list!(visitor, visit_pat, optional_subpattern);
696696
}
697697
PatKind::Lit(ref expression) => visitor.visit_expr(expression),
@@ -714,11 +714,11 @@ pub fn walk_foreign_item<'v, V: Visitor<'v>>(visitor: &mut V, foreign_item: &'v
714714
visitor.visit_name(foreign_item.span, foreign_item.name);
715715

716716
match foreign_item.node {
717-
ForeignItemFn(ref function_declaration, ref names, ref generics) => {
717+
ForeignItemFn(ref function_declaration, ref param_names, ref generics) => {
718718
visitor.visit_generics(generics);
719719
visitor.visit_fn_decl(function_declaration);
720-
for name in names {
721-
visitor.visit_name(name.span, name.node);
720+
for &param_name in param_names {
721+
visitor.visit_ident(param_name);
722722
}
723723
}
724724
ForeignItemStatic(ref typ, _) => visitor.visit_ty(typ),
@@ -832,11 +832,11 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
832832
visitor.visit_ty(ty);
833833
walk_list!(visitor, visit_nested_body, default);
834834
}
835-
TraitItemKind::Method(ref sig, TraitMethod::Required(ref names)) => {
835+
TraitItemKind::Method(ref sig, TraitMethod::Required(ref param_names)) => {
836836
visitor.visit_id(trait_item.id);
837837
visitor.visit_fn_decl(&sig.decl);
838-
for name in names {
839-
visitor.visit_name(name.span, name.node);
838+
for &param_name in param_names {
839+
visitor.visit_ident(param_name);
840840
}
841841
}
842842
TraitItemKind::Method(ref sig, TraitMethod::Provided(body_id)) => {

src/librustc/hir/lowering.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,7 @@ impl<'a> LoweringContext<'a> {
969969

970970
fn lower_label(&mut self, label: Option<Label>) -> Option<hir::Label> {
971971
label.map(|label| hir::Label {
972-
name: label.ident.name,
973-
span: label.ident.span,
972+
ident: label.ident,
974973
})
975974
}
976975

@@ -1195,11 +1194,10 @@ impl<'a> LoweringContext<'a> {
11951194

11961195
let hir_bounds = self.lower_param_bounds(bounds, itctx);
11971196
// Set the name to `impl Bound1 + Bound2`
1198-
let ident = Ident::from_str(&pprust::ty_to_string(t));
1197+
let ident = Ident::from_str(&pprust::ty_to_string(t)).with_span_pos(span);
11991198
self.in_band_ty_params.push(hir::GenericParam {
12001199
id: def_node_id,
1201-
name: ParamName::Plain(name),
1202-
span,
1200+
ident: ParamName::Plain(ident),
12031201
pure_wrt_drop: false,
12041202
attrs: hir_vec![],
12051203
bounds: hir_bounds,
@@ -1868,12 +1866,12 @@ impl<'a> LoweringContext<'a> {
18681866
}
18691867
}
18701868

1871-
fn lower_fn_args_to_names(&mut self, decl: &FnDecl) -> hir::HirVec<Spanned<Name>> {
1869+
fn lower_fn_args_to_names(&mut self, decl: &FnDecl) -> hir::HirVec<Ident> {
18721870
decl.inputs
18731871
.iter()
18741872
.map(|arg| match arg.pat.node {
1875-
PatKind::Ident(_, ident, None) => respan(ident.span, ident.name),
1876-
_ => respan(arg.pat.span, keywords::Invalid.name()),
1873+
PatKind::Ident(_, ident, _) => ident,
1874+
_ => Ident::new(keywords::Invalid.name(), arg.pat.span),
18771875
})
18781876
.collect()
18791877
}
@@ -3298,7 +3296,7 @@ impl<'a> LoweringContext<'a> {
32983296
hir::PatKind::Binding(
32993297
self.lower_binding_mode(binding_mode),
33003298
canonical_id,
3301-
respan(ident.span, ident.name),
3299+
ident,
33023300
sub.as_ref().map(|x| self.lower_pat(x)),
33033301
)
33043302
}
@@ -4524,7 +4522,7 @@ impl<'a> LoweringContext<'a> {
45244522
P(hir::Pat {
45254523
id: node_id,
45264524
hir_id,
4527-
node: hir::PatKind::Binding(bm, node_id, Spanned { span, node: ident.name }, None),
4525+
node: hir::PatKind::Binding(bm, node_id, ident.with_span_pos(span), None),
45284526
span,
45294527
})
45304528
}

src/librustc/hir/map/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl<'hir> Map<'hir> {
616616
NodeItem(&Item { node: ItemTrait(..), .. }) => {
617617
keywords::SelfType.name()
618618
}
619-
NodeGenericParam(param) => param.name.name(),
619+
NodeGenericParam(param) => param.name.ident(),
620620
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
621621
}
622622
}
@@ -1021,7 +1021,7 @@ impl<'hir> Map<'hir> {
10211021
Some(EntryBlock(_, _, block)) => block.span,
10221022
Some(EntryStructCtor(_, _, _)) => self.expect_item(self.get_parent(id)).span,
10231023
Some(EntryLifetime(_, _, lifetime)) => lifetime.span,
1024-
Some(EntryGenericParam(_, _, param)) => param.span,
1024+
Some(EntryGenericParam(_, _, param)) => param.ident.span,
10251025
Some(EntryVisibility(_, _, &Visibility::Restricted { ref path, .. })) => path.span,
10261026
Some(EntryVisibility(_, _, v)) => bug!("unexpected Visibility {:?}", v),
10271027
Some(EntryLocal(_, _, local)) => local.span,

src/librustc/hir/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,12 @@ pub const DUMMY_ITEM_LOCAL_ID: ItemLocalId = ItemLocalId(!0);
175175

176176
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)]
177177
pub struct Label {
178-
pub name: Name,
179-
pub span: Span,
178+
pub ident: Ident,
180179
}
181180

182181
impl fmt::Debug for Label {
183182
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
184-
write!(f, "label({:?})", self.name)
183+
write!(f, "label({:?})", self.ident)
185184
}
186185
}
187186

@@ -493,7 +492,6 @@ pub struct GenericParam {
493492
pub name: ParamName,
494493
pub attrs: HirVec<Attribute>,
495494
pub bounds: GenericBounds,
496-
pub span: Span,
497495
pub pure_wrt_drop: bool,
498496

499497
pub kind: GenericParamKind,
@@ -872,7 +870,7 @@ pub enum PatKind {
872870
/// The `NodeId` is the canonical ID for the variable being bound,
873871
/// e.g. in `Ok(x) | Err(x)`, both `x` use the same canonical ID,
874872
/// which is the pattern ID of the first `x`.
875-
Binding(BindingAnnotation, NodeId, Spanned<Name>, Option<P<Pat>>),
873+
Binding(BindingAnnotation, NodeId, Ident, Option<P<Pat>>),
876874

877875
/// A struct or struct variant pattern, e.g. `Variant {x, y, ..}`.
878876
/// The `bool` is `true` in the presence of a `..`.
@@ -1550,7 +1548,7 @@ pub struct TraitItem {
15501548
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
15511549
pub enum TraitMethod {
15521550
/// No default body in the trait, just a signature.
1553-
Required(HirVec<Spanned<Name>>),
1551+
Required(HirVec<Ident>),
15541552

15551553
/// Both signature and body are provided in the trait.
15561554
Provided(BodyId),
@@ -1645,7 +1643,7 @@ pub struct BareFnTy {
16451643
pub abi: Abi,
16461644
pub generic_params: HirVec<GenericParam>,
16471645
pub decl: P<FnDecl>,
1648-
pub arg_names: HirVec<Spanned<Name>>,
1646+
pub arg_names: HirVec<Ident>,
16491647
}
16501648

16511649
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
@@ -2185,7 +2183,7 @@ pub struct ForeignItem {
21852183
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
21862184
pub enum ForeignItem_ {
21872185
/// A foreign function
2188-
ForeignItemFn(P<FnDecl>, HirVec<Spanned<Name>>, Generics),
2186+
ForeignItemFn(P<FnDecl>, HirVec<Ident>, Generics),
21892187
/// A foreign static item (`static ext: u8`), with optional mutability
21902188
/// (the boolean is true when mutable)
21912189
ForeignItemStatic(P<Ty>, bool),

src/librustc/hir/pat_util.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use hir::def::Def;
1212
use hir::def_id::DefId;
1313
use hir::{self, HirId, PatKind};
1414
use syntax::ast;
15-
use syntax::codemap::Spanned;
1615
use syntax_pos::Span;
1716

1817
use std::iter::{Enumerate, ExactSizeIterator};
@@ -91,11 +90,11 @@ impl hir::Pat {
9190
/// Call `f` on every "binding" in a pattern, e.g., on `a` in
9291
/// `match foo() { Some(a) => (), None => () }`
9392
pub fn each_binding<F>(&self, mut f: F)
94-
where F: FnMut(hir::BindingAnnotation, HirId, Span, &Spanned<ast::Name>),
93+
where F: FnMut(hir::BindingAnnotation, HirId, Span, ast::Ident),
9594
{
9695
self.walk(|p| {
97-
if let PatKind::Binding(binding_mode, _, ref pth, _) = p.node {
98-
f(binding_mode, p.hir_id, p.span, pth);
96+
if let PatKind::Binding(binding_mode, _, ident, _) = p.node {
97+
f(binding_mode, p.hir_id, p.span, ident);
9998
}
10099
true
101100
});
@@ -132,20 +131,10 @@ impl hir::Pat {
132131
contains_bindings
133132
}
134133

135-
pub fn simple_name(&self) -> Option<ast::Name> {
134+
pub fn simple_ident(&self) -> Option<ast::Ident> {
136135
match self.node {
137-
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ref path1, None) |
138-
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ref path1, None) =>
139-
Some(path1.node),
140-
_ => None,
141-
}
142-
}
143-
144-
pub fn simple_span(&self) -> Option<Span> {
145-
match self.node {
146-
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ref path1, None) |
147-
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ref path1, None) =>
148-
Some(path1.span),
136+
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ident, None) |
137+
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ident, None) => Some(ident),
149138
_ => None,
150139
}
151140
}

src/librustc/hir/print.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use self::AnnNode::*;
1212

1313
use rustc_target::spec::abi::Abi;
1414
use syntax::ast;
15-
use syntax::codemap::{CodeMap, Spanned};
15+
use syntax::codemap::CodeMap;
1616
use syntax::parse::ParseSess;
1717
use syntax::parse::lexer::comments;
1818
use syntax::print::pp::{self, Breaks};
@@ -933,7 +933,7 @@ impl<'a> State<'a> {
933933
m: &hir::MethodSig,
934934
generics: &hir::Generics,
935935
vis: &hir::Visibility,
936-
arg_names: &[Spanned<ast::Name>],
936+
arg_names: &[ast::Ident],
937937
body_id: Option<hir::BodyId>)
938938
-> io::Result<()> {
939939
self.print_fn(&m.decl,
@@ -1380,7 +1380,7 @@ impl<'a> State<'a> {
13801380
}
13811381
hir::ExprWhile(ref test, ref blk, opt_label) => {
13821382
if let Some(label) = opt_label {
1383-
self.print_name(label.name)?;
1383+
self.print_ident(label.ident)?;
13841384
self.word_space(":")?;
13851385
}
13861386
self.head("while")?;
@@ -1390,7 +1390,7 @@ impl<'a> State<'a> {
13901390
}
13911391
hir::ExprLoop(ref blk, opt_label, _) => {
13921392
if let Some(label) = opt_label {
1393-
self.print_name(label.name)?;
1393+
self.print_ident(label.ident)?;
13941394
self.word_space(":")?;
13951395
}
13961396
self.head("loop")?;
@@ -1426,7 +1426,7 @@ impl<'a> State<'a> {
14261426
}
14271427
hir::ExprBlock(ref blk, opt_label) => {
14281428
if let Some(label) = opt_label {
1429-
self.print_name(label.name)?;
1429+
self.print_ident(label.ident)?;
14301430
self.word_space(":")?;
14311431
}
14321432
// containing cbox, will be closed by print-block at }
@@ -1468,7 +1468,7 @@ impl<'a> State<'a> {
14681468
self.s.word("break")?;
14691469
self.s.space()?;
14701470
if let Some(label) = destination.label {
1471-
self.print_name(label.name)?;
1471+
self.print_ident(label.ident)?;
14721472
self.s.space()?;
14731473
}
14741474
if let Some(ref expr) = *opt_expr {
@@ -1480,7 +1480,7 @@ impl<'a> State<'a> {
14801480
self.s.word("continue")?;
14811481
self.s.space()?;
14821482
if let Some(label) = destination.label {
1483-
self.print_name(label.name)?;
1483+
self.print_ident(label.ident)?;
14841484
self.s.space()?
14851485
}
14861486
}
@@ -1784,7 +1784,7 @@ impl<'a> State<'a> {
17841784
// is that it doesn't matter
17851785
match pat.node {
17861786
PatKind::Wild => self.s.word("_")?,
1787-
PatKind::Binding(binding_mode, _, ref path1, ref sub) => {
1787+
PatKind::Binding(binding_mode, _, ident, ref sub) => {
17881788
match binding_mode {
17891789
hir::BindingAnnotation::Ref => {
17901790
self.word_nbsp("ref")?;
@@ -1799,7 +1799,7 @@ impl<'a> State<'a> {
17991799
self.word_nbsp("mut")?;
18001800
}
18011801
}
1802-
self.print_name(path1.node)?;
1802+
self.print_ident(ident)?;
18031803
if let Some(ref p) = *sub {
18041804
self.s.word("@")?;
18051805
self.print_pat(&p)?;
@@ -1964,7 +1964,7 @@ impl<'a> State<'a> {
19641964
match arm.body.node {
19651965
hir::ExprBlock(ref blk, opt_label) => {
19661966
if let Some(label) = opt_label {
1967-
self.print_name(label.name)?;
1967+
self.print_ident(label.ident)?;
19681968
self.word_space(":")?;
19691969
}
19701970
// the block will close the pattern's ibox
@@ -1990,7 +1990,7 @@ impl<'a> State<'a> {
19901990
name: Option<ast::Name>,
19911991
generics: &hir::Generics,
19921992
vis: &hir::Visibility,
1993-
arg_names: &[Spanned<ast::Name>],
1993+
arg_names: &[ast::Ident],
19941994
body_id: Option<hir::BodyId>)
19951995
-> io::Result<()> {
19961996
self.print_fn_header_info(header, vis)?;
@@ -2007,8 +2007,8 @@ impl<'a> State<'a> {
20072007
assert!(arg_names.is_empty() || body_id.is_none());
20082008
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
20092009
s.ibox(indent_unit)?;
2010-
if let Some(name) = arg_names.get(i) {
2011-
s.s.word(&name.node.as_str())?;
2010+
if let Some(arg_name) = arg_names.get(i) {
2011+
s.s.word(&arg_name.as_str())?;
20122012
s.s.word(":")?;
20132013
s.s.space()?;
20142014
} else if let Some(body_id) = body_id {
@@ -2242,7 +2242,7 @@ impl<'a> State<'a> {
22422242
decl: &hir::FnDecl,
22432243
name: Option<ast::Name>,
22442244
generic_params: &[hir::GenericParam],
2245-
arg_names: &[Spanned<ast::Name>])
2245+
arg_names: &[ast::Ident])
22462246
-> io::Result<()> {
22472247
self.ibox(indent_unit)?;
22482248
if !generic_params.is_empty() {

src/librustc/ich/impls_hir.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ impl_stable_hash_for!(enum hir::LifetimeName {
155155
});
156156

157157
impl_stable_hash_for!(struct hir::Label {
158-
span,
159-
name
158+
ident
160159
});
161160

162161
impl_stable_hash_for!(struct hir::Lifetime {
@@ -201,7 +200,6 @@ impl_stable_hash_for!(enum hir::TraitBoundModifier {
201200
impl_stable_hash_for!(struct hir::GenericParam {
202201
id,
203202
name,
204-
span,
205203
pure_wrt_drop,
206204
attrs,
207205
bounds,

src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
131131
labels.clear();
132132
labels.push((pattern.span, format!("consider giving this closure parameter a type")));
133133
} else if let Some(pattern) = local_visitor.found_local_pattern {
134-
if let Some(simple_name) = pattern.simple_name() {
135-
labels.push((pattern.span, format!("consider giving `{}` a type", simple_name)));
134+
if let Some(simple_ident) = pattern.simple_ident() {
135+
labels.push((pattern.span, format!("consider giving `{}` a type", simple_ident)));
136136
} else {
137137
labels.push((pattern.span, format!("consider giving the pattern a type")));
138138
}

0 commit comments

Comments
 (0)