Skip to content

Commit bb163c6

Browse files
committed
rustc: remove the name field from ParamTy.
1 parent 0d5122a commit bb163c6

File tree

13 files changed

+74
-52
lines changed

13 files changed

+74
-52
lines changed

src/librustc/hir/map/def_collector.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,38 @@ impl<'a> DefCollector<'a> {
7676
fn visit_async_fn(
7777
&mut self,
7878
id: NodeId,
79-
async_node_id: NodeId,
80-
return_impl_trait_id: NodeId,
8179
name: Name,
8280
span: Span,
83-
visit_fn: impl FnOnce(&mut DefCollector<'a>)
81+
header: &FnHeader,
82+
generics: &'a Generics,
83+
decl: &'a FnDecl,
84+
body: &'a Block,
8485
) {
86+
let (closure_id, return_impl_trait_id) = match header.asyncness {
87+
IsAsync::Async {
88+
closure_id,
89+
return_impl_trait_id,
90+
} => (closure_id, return_impl_trait_id),
91+
_ => unreachable!(),
92+
};
93+
8594
// For async functions, we need to create their inner defs inside of a
8695
// closure to match their desugared representation.
8796
let fn_def_data = DefPathData::ValueNs(name.as_interned_str());
8897
let fn_def = self.create_def(id, fn_def_data, ITEM_LIKE_SPACE, span);
8998
return self.with_parent(fn_def, |this| {
9099
this.create_def(return_impl_trait_id, DefPathData::ImplTrait, REGULAR_SPACE, span);
91-
let closure_def = this.create_def(async_node_id,
100+
101+
visit::walk_generics(this, generics);
102+
visit::walk_fn_decl(this, decl);
103+
104+
let closure_def = this.create_def(closure_id,
92105
DefPathData::ClosureExpr,
93106
REGULAR_SPACE,
94107
span);
95-
this.with_parent(closure_def, visit_fn)
108+
this.with_parent(closure_def, |this| {
109+
visit::walk_block(this, body);
110+
})
96111
})
97112
}
98113

@@ -122,17 +137,20 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
122137
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
123138
return visit::walk_item(self, i);
124139
}
125-
ItemKind::Fn(_, FnHeader { asyncness: IsAsync::Async {
126-
closure_id,
127-
return_impl_trait_id,
128-
}, .. }, ..) => {
140+
ItemKind::Fn(
141+
ref decl,
142+
ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
143+
ref generics,
144+
ref body,
145+
) => {
129146
return self.visit_async_fn(
130147
i.id,
131-
closure_id,
132-
return_impl_trait_id,
133148
i.ident.name,
134149
i.span,
135-
|this| visit::walk_item(this, i)
150+
header,
151+
generics,
152+
decl,
153+
body,
136154
)
137155
}
138156
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
@@ -233,18 +251,17 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
233251
fn visit_impl_item(&mut self, ii: &'a ImplItem) {
234252
let def_data = match ii.node {
235253
ImplItemKind::Method(MethodSig {
236-
header: FnHeader { asyncness: IsAsync::Async {
237-
closure_id,
238-
return_impl_trait_id,
239-
}, .. }, ..
240-
}, ..) => {
254+
header: ref header @ FnHeader { asyncness: IsAsync::Async { .. }, .. },
255+
ref decl,
256+
}, ref body) => {
241257
return self.visit_async_fn(
242258
ii.id,
243-
closure_id,
244-
return_impl_trait_id,
245259
ii.ident.name,
246260
ii.span,
247-
|this| visit::walk_impl_item(this, ii)
261+
header,
262+
&ii.generics,
263+
decl,
264+
body,
248265
)
249266
}
250267
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>

src/librustc/ich/impls_ty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,8 +940,7 @@ for ty::FloatVid
940940

941941
impl_stable_hash_for!(struct ty::ParamTy {
942942
idx,
943-
def_id,
944-
name
943+
def_id
945944
});
946945

947946
impl_stable_hash_for!(struct ty::TypeAndMut<'tcx> {

src/librustc/infer/error_reporting/need_type_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
7777
pub fn extract_type_name(&self, ty: &'a Ty<'tcx>) -> String {
7878
if let ty::Infer(ty::TyVar(ty_vid)) = (*ty).sty {
7979
let ty_vars = self.type_variables.borrow();
80-
if let TypeVariableOrigin::TypeParameterDefinition(_, name) =
80+
if let TypeVariableOrigin::TypeParameterDefinition(_, def_id) =
8181
*ty_vars.var_origin(ty_vid) {
82-
name.to_string()
82+
self.tcx.ty_param_name(def_id).to_string()
8383
} else {
8484
ty.to_string()
8585
}

src/librustc/infer/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
942942
// used in a path such as `Foo::<T, U>::new()` will
943943
// use an inference variable for `C` with `[T, U]`
944944
// as the substitutions for the default, `(T, U)`.
945-
let ty_var_id =
946-
self.type_variables
947-
.borrow_mut()
948-
.new_var(self.universe(),
949-
false,
950-
TypeVariableOrigin::TypeParameterDefinition(span, param.name));
945+
let ty_var_id = self.type_variables.borrow_mut().new_var(
946+
self.universe(),
947+
false,
948+
TypeVariableOrigin::TypeParameterDefinition(span, param.def_id),
949+
);
951950

952951
self.tcx.mk_var(ty_var_id).into()
953952
}

src/librustc/infer/type_variable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use syntax::symbol::InternedString;
1211
use syntax_pos::Span;
12+
use hir::def_id::DefId;
1313
use ty::{self, Ty};
1414

1515
use std::cmp;
@@ -53,7 +53,7 @@ pub enum TypeVariableOrigin {
5353
MiscVariable(Span),
5454
NormalizeProjectionType(Span),
5555
TypeInference(Span),
56-
TypeParameterDefinition(Span, InternedString),
56+
TypeParameterDefinition(Span, DefId),
5757

5858
/// one of the upvars or closure kind parameters in a `ClosureSubsts`
5959
/// (before it has been determined)

src/librustc/traits/error_reporting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,11 +1348,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13481348
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> { self.infcx.tcx }
13491349

13501350
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1351-
if let ty::Param(ty::ParamTy {name, ..}) = ty.sty {
1351+
if let ty::Param(ty::ParamTy {def_id, ..}) = ty.sty {
13521352
let infcx = self.infcx;
13531353
self.var_map.entry(ty).or_insert_with(||
13541354
infcx.next_ty_var(
1355-
TypeVariableOrigin::TypeParameterDefinition(DUMMY_SP, name)))
1355+
TypeVariableOrigin::TypeParameterDefinition(DUMMY_SP, def_id)))
13561356
} else {
13571357
ty.super_fold_with(self)
13581358
}

src/librustc/ty/context.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use syntax::attr;
7676
use syntax::source_map::MultiSpan;
7777
use syntax::edition::Edition;
7878
use syntax::feature_gate;
79-
use syntax::symbol::{keywords, Symbol};
79+
use syntax::symbol::Symbol;
8080
use syntax_pos::Span;
8181

8282
use hir;
@@ -2560,15 +2560,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
25602560
self.mk_ty(ty::Param(ty::ParamTy {
25612561
idx: def.index,
25622562
def_id: def.def_id,
2563-
name: def.name,
25642563
}))
25652564
}
25662565

25672566
pub fn mk_self_type(self, trait_def_id: DefId) -> Ty<'tcx> {
25682567
self.mk_ty(ty::Param(ty::ParamTy {
25692568
idx: 0,
25702569
def_id: trait_def_id,
2571-
name: keywords::SelfType.name().as_interned_str(),
25722570
}))
25732571
}
25742572

src/librustc/ty/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2637,25 +2637,39 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
26372637
}
26382638
}
26392639

2640-
pub fn ty_param_name(&self, id: DefId) -> InternedString {
2640+
pub fn ty_param_name(self, id: DefId) -> InternedString {
26412641
let def_key = self.def_key(id);
26422642
match def_key.disambiguated_data.data {
2643+
DefPathData::TypeParam(name) => name,
2644+
DefPathData::ImplTrait => {
2645+
let param_owner_def_id = DefId {
2646+
krate: id.krate,
2647+
index: def_key.parent.unwrap()
2648+
};
2649+
let generics = self.generics_of(param_owner_def_id);
2650+
let index = generics.param_def_id_to_index[&id];
2651+
generics.param_at(index, self).name
2652+
}
26432653
DefPathData::Trait(_) => {
26442654
keywords::SelfType.name().as_interned_str()
26452655
}
2646-
DefPathData::TypeParam(name) => name,
2656+
DefPathData::ClosureExpr => {
2657+
Symbol::intern("<synthetic closure param>").as_interned_str()
2658+
}
26472659
_ => bug!("ty_param_name: {:?} not a type parameter", id),
26482660
}
26492661
}
26502662

2651-
pub fn ty_param_owner(&self, id: DefId) -> DefId {
2663+
pub fn ty_param_owner(self, id: DefId) -> DefId {
26522664
let def_key = self.def_key(id);
26532665
match def_key.disambiguated_data.data {
2654-
DefPathData::Trait(_) => id,
2666+
DefPathData::ImplTrait |
26552667
DefPathData::TypeParam(_) => DefId {
26562668
krate: id.krate,
26572669
index: def_key.parent.unwrap()
26582670
},
2671+
DefPathData::Trait(_) |
2672+
DefPathData::ClosureExpr => id,
26592673
_ => bug!("ty_param_owner: {:?} not a type parameter", id),
26602674
}
26612675
}

src/librustc/ty/sty.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,6 @@ impl<'tcx> PolyFnSig<'tcx> {
964964
pub struct ParamTy {
965965
pub idx: u32,
966966
pub def_id: DefId,
967-
pub name: InternedString,
968967
}
969968

970969
/// A [De Bruijn index][dbi] is a standard means of representing

src/librustc/util/ppaux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,10 +1267,10 @@ define_print! {
12671267
define_print! {
12681268
() ty::ParamTy, (self, f, cx) {
12691269
display {
1270-
write!(f, "{}", self.name)
1270+
write!(f, "{}", ty::tls::with(|tcx| tcx.ty_param_name(self.def_id)))
12711271
}
12721272
debug {
1273-
write!(f, "{}/#{}", self.name, self.idx)
1273+
write!(f, "{}/#{}", ty::tls::with(|tcx| tcx.ty_param_name(self.def_id)), self.idx)
12741274
}
12751275
}
12761276
}

src/librustc_driver/test.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,9 @@ impl<'a, 'gcx, 'tcx> Env<'a, 'gcx, 'tcx> {
313313

314314
pub fn t_param(&self, index: u32) -> Ty<'tcx> {
315315
let def_id = self.infcx.tcx.hir.local_def_id(ast::CRATE_NODE_ID);
316-
let name = format!("T{}", index);
317316
self.infcx.tcx.mk_ty(ty::TyParam(ty::ParamTy {
318317
idx: index,
319318
def_id,
320-
name: Symbol::intern(&name).as_interned_str()
321319
}))
322320
}
323321

src/librustc_typeck/astconv.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,11 +1406,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
14061406
assert_eq!(opt_self_ty, None);
14071407
self.prohibit_generics(&path.segments);
14081408

1409-
let node_id = tcx.hir.as_local_node_id(did).unwrap();
1410-
let item_id = tcx.hir.get_parent_node(node_id);
1411-
let item_def_id = tcx.hir.local_def_id(item_id);
1412-
let generics = tcx.generics_of(item_def_id);
1413-
let index = generics.param_def_id_to_index[&tcx.hir.local_def_id(node_id)];
1409+
let param_owner_def_id = tcx.ty_param_owner(did);
1410+
let generics = tcx.generics_of(param_owner_def_id);
1411+
let index = generics.param_def_id_to_index[&did];
14141412
tcx.mk_ty_param(generics.param_at(index, tcx))
14151413
}
14161414
Def::SelfTy(_, Some(def_id)) => {

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2668,7 +2668,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
26682668

26692669
ty::Projection(ref data) => data.clean(cx),
26702670

2671-
ty::Param(ref p) => Generic(p.name.to_string()),
2671+
ty::Param(ref p) => Generic(p.to_string()),
26722672

26732673
ty::Anon(def_id, substs) => {
26742674
// Grab the "TraitA + TraitB" from `impl TraitA + TraitB`,

0 commit comments

Comments
 (0)