Skip to content

Commit 3ae2468

Browse files
committed
Clean up shared subst code
1 parent d9190da commit 3ae2468

File tree

7 files changed

+101
-144
lines changed

7 files changed

+101
-144
lines changed

src/librustc/infer/mod.rs

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -905,34 +905,34 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
905905
self.next_region_var(RegionVariableOrigin::NLL(origin))
906906
}
907907

908-
/// Create a region inference variable for the given
909-
/// region parameter definition.
910-
pub fn region_var_for_def(&self,
911-
span: Span,
912-
def: &ty::GenericParamDef)
913-
-> ty::Region<'tcx> {
914-
self.next_region_var(EarlyBoundRegion(span, def.name))
915-
}
916-
917-
/// Create a type inference variable for the given
918-
/// type parameter definition. The substitutions are
919-
/// for actual parameters that may be referred to by
920-
/// the default of this type parameter, if it exists.
921-
/// E.g. `struct Foo<A, B, C = (A, B)>(...);` when
922-
/// used in a path such as `Foo::<T, U>::new()` will
923-
/// use an inference variable for `C` with `[T, U]`
924-
/// as the substitutions for the default, `(T, U)`.
925-
pub fn type_var_for_def(&self,
926-
span: Span,
927-
def: &ty::GenericParamDef)
928-
-> Ty<'tcx> {
929-
let ty_var_id = self.type_variables
930-
.borrow_mut()
931-
.new_var(self.universe(),
932-
false,
933-
TypeVariableOrigin::TypeParameterDefinition(span, def.name));
934-
935-
self.tcx.mk_var(ty_var_id)
908+
pub fn var_for_def(&self,
909+
span: Span,
910+
param: &ty::GenericParamDef)
911+
-> UnpackedKind<'tcx> {
912+
match param.kind {
913+
GenericParamDefKind::Lifetime => {
914+
// Create a region inference variable for the given
915+
// region parameter definition.
916+
UnpackedKind::Lifetime(self.next_region_var(EarlyBoundRegion(span, param.name)))
917+
}
918+
GenericParamDefKind::Type(_) => {
919+
// Create a type inference variable for the given
920+
// type parameter definition. The substitutions are
921+
// for actual parameters that may be referred to by
922+
// the default of this type parameter, if it exists.
923+
// E.g. `struct Foo<A, B, C = (A, B)>(...);` when
924+
// used in a path such as `Foo::<T, U>::new()` will
925+
// use an inference variable for `C` with `[T, U]`
926+
// as the substitutions for the default, `(T, U)`.
927+
let ty_var_id = self.type_variables
928+
.borrow_mut()
929+
.new_var(self.universe(),
930+
false,
931+
TypeVariableOrigin::TypeParameterDefinition(span, param.name));
932+
933+
UnpackedKind::Type(self.tcx.mk_var(ty_var_id))
934+
}
935+
}
936936
}
937937

938938
/// Given a set of generics defined on a type or impl, returns a substitution mapping each
@@ -942,14 +942,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
942942
def_id: DefId)
943943
-> &'tcx Substs<'tcx> {
944944
Substs::for_item(self.tcx, def_id, |param, _| {
945-
match param.kind {
946-
GenericParamDefKind::Lifetime => {
947-
UnpackedKind::Lifetime(self.region_var_for_def(span, param))
948-
}
949-
GenericParamDefKind::Type(_) => {
950-
UnpackedKind::Type(self.type_var_for_def(span, param))
951-
}
952-
}
945+
self.var_for_def(span, param)
953946
})
954947
}
955948

src/librustc/ty/context.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,18 +2329,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23292329
let substs = Substs::for_item(self, def_id, |param, substs| {
23302330
match param.kind {
23312331
GenericParamDefKind::Lifetime => bug!(),
2332-
GenericParamDefKind::Type(_) => {
2332+
GenericParamDefKind::Type(ty_param) => {
23332333
if param.index == 0 {
23342334
UnpackedKind::Type(ty)
23352335
} else {
2336-
match param.kind {
2337-
ty::GenericParamDefKind::Type(ty_param) => {
2338-
assert!(ty_param.has_default);
2339-
UnpackedKind::Type(
2340-
self.type_of(param.def_id).subst(self, substs))
2341-
}
2342-
_ => unreachable!()
2343-
}
2336+
assert!(ty_param.has_default);
2337+
UnpackedKind::Type(self.type_of(param.def_id).subst(self, substs))
23442338
}
23452339
}
23462340
}

src/librustc_typeck/astconv.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,32 +276,26 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
276276
};
277277
UnpackedKind::Lifetime(lt)
278278
}
279-
GenericParamDefKind::Type(_) => {
279+
GenericParamDefKind::Type(ty) => {
280280
let i = param.index as usize;
281281

282282
// Handle Self first, so we can adjust the index to match the AST.
283283
if let (0, Some(ty)) = (i, self_ty) {
284284
return UnpackedKind::Type(ty);
285285
}
286286

287-
let has_default = match param.kind {
288-
GenericParamDefKind::Type(ty) => ty.has_default,
289-
_ => unreachable!()
290-
};
291-
292287
let i = i - (lt_accepted + own_self);
293288
let ty = if i < ty_provided {
294289
// A provided type parameter.
295290
self.ast_ty_to_ty(&parameters.types[i])
296291
} else if infer_types {
297292
// No type parameters were provided, we can infer all.
298-
let ty_var = if !default_needs_object_self(param) {
293+
if !default_needs_object_self(param) {
299294
self.ty_infer_for_def(param, span)
300295
} else {
301296
self.ty_infer(span)
302-
};
303-
ty_var
304-
} else if has_default {
297+
}
298+
} else if ty.has_default {
305299
// No type parameter provided, but a default exists.
306300

307301
// If we are converting an object type, then the

src/librustc_typeck/check/method/confirm.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -318,36 +318,28 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
318318
let provided = &segment.parameters;
319319
let own_counts = method_generics.own_counts();
320320
Substs::for_item(self.tcx, pick.item.def_id, |param, _| {
321-
match param.kind {
322-
GenericParamDefKind::Lifetime => {
323-
let i = param.index as usize;
324-
let lt = if i < parent_substs.len() {
325-
parent_substs.region_at(i)
326-
} else if let Some(lifetime)
327-
= provided.as_ref().and_then(|p| p.lifetimes.get(i - parent_substs.len())) {
328-
AstConv::ast_region_to_region(self.fcx, lifetime, Some(param))
329-
} else {
330-
self.region_var_for_def(self.span, param)
331-
};
332-
UnpackedKind::Lifetime(lt)
333-
}
334-
GenericParamDefKind::Type(_) => {
335-
let i = param.index as usize;
336-
let ty = if i < parent_substs.len() {
337-
parent_substs.type_at(i)
338-
} else if let Some(ast_ty)
339-
= provided.as_ref().and_then(|p| {
340-
let idx =
341-
i - parent_substs.len() - own_counts.lifetimes;
342-
p.types.get(idx)
343-
})
344-
{
345-
self.to_ty(ast_ty)
346-
} else {
347-
self.type_var_for_def(self.span, param)
348-
};
349-
UnpackedKind::Type(ty)
321+
let i = param.index as usize;
322+
if i < parent_substs.len() {
323+
parent_substs[i].unpack()
324+
} else {
325+
match param.kind {
326+
GenericParamDefKind::Lifetime => {
327+
if let Some(lifetime) = provided.as_ref().and_then(|p| {
328+
p.lifetimes.get(i - parent_substs.len())
329+
}) {
330+
return UnpackedKind::Lifetime(
331+
AstConv::ast_region_to_region(self.fcx, lifetime, Some(param)));
332+
}
333+
}
334+
GenericParamDefKind::Type(_) => {
335+
if let Some(ast_ty) = provided.as_ref().and_then(|p| {
336+
p.types.get(i - parent_substs.len() - own_counts.lifetimes)
337+
}) {
338+
return UnpackedKind::Type(self.to_ty(ast_ty));
339+
}
340+
}
350341
}
342+
self.var_for_def(self.span, param)
351343
}
352344
})
353345
}

src/librustc_typeck/check/method/mod.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
256256
// Construct a trait-reference `self_ty : Trait<input_tys>`
257257
let substs = Substs::for_item(self.tcx, trait_def_id, |param, _| {
258258
match param.kind {
259-
GenericParamDefKind::Lifetime => {
260-
UnpackedKind::Lifetime(self.region_var_for_def(span, param))
261-
}
259+
GenericParamDefKind::Lifetime => {}
262260
GenericParamDefKind::Type(_) => {
263-
let ty = if param.index == 0 {
264-
self_ty
261+
if param.index == 0 {
262+
return UnpackedKind::Type(self_ty);
265263
} else if let Some(ref input_types) = opt_input_types {
266-
input_types[param.index as usize - 1]
267-
} else {
268-
self.type_var_for_def(span, param)
269-
};
270-
UnpackedKind::Type(ty)
264+
return UnpackedKind::Type(input_types[param.index as usize - 1]);
265+
}
271266
}
272267
}
268+
self.var_for_def(span, param)
273269
});
274270

275271
let trait_ref = ty::TraitRef::new(trait_def_id, substs);

src/librustc_typeck/check/method/probe.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,26 +1389,17 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
13891389
xform_fn_sig.subst(self.tcx, substs)
13901390
} else {
13911391
let substs = Substs::for_item(self.tcx, method, |param, _| {
1392-
match param.kind {
1393-
GenericParamDefKind::Lifetime => {
1394-
let i = param.index as usize;
1395-
let lt = if i < substs.len() {
1396-
substs.region_at(i)
1397-
} else {
1392+
let i = param.index as usize;
1393+
if i < substs.len() {
1394+
substs[i].unpack()
1395+
} else {
1396+
match param.kind {
1397+
GenericParamDefKind::Lifetime => {
13981398
// In general, during probe we erase regions. See
13991399
// `impl_self_ty()` for an explanation.
1400-
self.tcx.types.re_erased
1401-
};
1402-
UnpackedKind::Lifetime(lt)
1403-
}
1404-
GenericParamDefKind::Type(_) => {
1405-
let i = param.index as usize;
1406-
let ty = if i < substs.len() {
1407-
substs.type_at(i)
1408-
} else {
1409-
self.type_var_for_def(self.span, param)
1410-
};
1411-
UnpackedKind::Type(ty)
1400+
UnpackedKind::Lifetime(self.tcx.types.re_erased)
1401+
}
1402+
GenericParamDefKind::Type(_) => self.var_for_def(self.span, param),
14121403
}
14131404
}
14141405
});

src/librustc_typeck/check/mod.rs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,11 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
17461746
fn ty_infer_for_def(&self,
17471747
ty_param_def: &ty::GenericParamDef,
17481748
span: Span) -> Ty<'tcx> {
1749-
self.type_var_for_def(span, ty_param_def)
1749+
if let UnpackedKind::Type(ty) = self.var_for_def(span, ty_param_def) {
1750+
ty
1751+
} else {
1752+
unreachable!()
1753+
}
17501754
}
17511755

17521756
fn projected_ty_from_poly_trait_ref(&self,
@@ -4759,17 +4763,26 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
47594763
(None, None) => (0, false)
47604764
};
47614765
let substs = Substs::for_item(self.tcx, def.def_id(), |param, substs| {
4766+
let mut i = param.index as usize;
4767+
4768+
let segment = if i < fn_start {
4769+
if let GenericParamDefKind::Type(_) = param.kind {
4770+
// Handle Self first, so we can adjust the index to match the AST.
4771+
if has_self && i == 0 {
4772+
return opt_self_ty.map(|ty| UnpackedKind::Type(ty)).unwrap_or_else(|| {
4773+
self.var_for_def(span, param)
4774+
});
4775+
}
4776+
}
4777+
i -= has_self as usize;
4778+
type_segment
4779+
} else {
4780+
i -= fn_start;
4781+
fn_segment
4782+
};
4783+
47624784
match param.kind {
47634785
GenericParamDefKind::Lifetime => {
4764-
let mut i = param.index as usize;
4765-
4766-
let segment = if i < fn_start {
4767-
i -= has_self as usize;
4768-
type_segment
4769-
} else {
4770-
i -= fn_start;
4771-
fn_segment
4772-
};
47734786
let lifetimes = segment.map_or(&[][..], |(s, _)| {
47744787
s.parameters.as_ref().map_or(&[][..], |p| &p.lifetimes[..])
47754788
});
@@ -4782,21 +4795,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
47824795
UnpackedKind::Lifetime(lt)
47834796
}
47844797
GenericParamDefKind::Type(_) => {
4785-
let mut i = param.index as usize;
4786-
4787-
let segment = if i < fn_start {
4788-
// Handle Self first, so we can adjust the index to match the AST.
4789-
if has_self && i == 0 {
4790-
return UnpackedKind::Type(opt_self_ty.unwrap_or_else(|| {
4791-
self.type_var_for_def(span, param)
4792-
}));
4793-
}
4794-
i -= has_self as usize;
4795-
type_segment
4796-
} else {
4797-
i -= fn_start;
4798-
fn_segment
4799-
};
48004798
let (types, infer_types) = segment.map_or((&[][..], true), |(s, _)| {
48014799
(s.parameters.as_ref().map_or(&[][..], |p| &p.types[..]), s.infer_types)
48024800
});
@@ -4811,24 +4809,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
48114809
_ => unreachable!()
48124810
};
48134811

4814-
let ty = if let Some(ast_ty) = types.get(i) {
4812+
if let Some(ast_ty) = types.get(i) {
48154813
// A provided type parameter.
4816-
self.to_ty(ast_ty)
4814+
UnpackedKind::Type(self.to_ty(ast_ty))
48174815
} else if !infer_types && has_default {
48184816
// No type parameter provided, but a default exists.
48194817
let default = self.tcx.type_of(param.def_id);
4820-
self.normalize_ty(
4818+
UnpackedKind::Type(self.normalize_ty(
48214819
span,
48224820
default.subst_spanned(self.tcx, substs, Some(span))
4823-
)
4821+
))
48244822
} else {
48254823
// No type parameters were provided, we can infer all.
48264824
// This can also be reached in some error cases:
48274825
// We prefer to use inference variables instead of
48284826
// TyError to let type inference recover somewhat.
4829-
self.type_var_for_def(span, param)
4830-
};
4831-
UnpackedKind::Type(ty)
4827+
self.var_for_def(span, param)
4828+
}
48324829
}
48334830
}
48344831
});

0 commit comments

Comments
 (0)