Skip to content

Commit 3ed227d

Browse files
committed
Correctly subst defaults with the in-scope substs
1 parent 0ad02ab commit 3ed227d

File tree

6 files changed

+98
-61
lines changed

6 files changed

+98
-61
lines changed

src/librustc/middle/infer/mod.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,12 +1048,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10481048
.collect()
10491049
}
10501050

1051+
// We have to take `&mut Substs` in order to provide the correct substitutions for defaults
1052+
// along the way, for this reason we don't return them.
10511053
pub fn type_vars_for_defs(&self,
10521054
span: Span,
1053-
// substs: Substs,
1054-
defs: &[ty::TypeParameterDef<'tcx>])
1055-
-> Vec<ty::Ty<'tcx>> {
1055+
space: subst::ParamSpace,
1056+
substs: &mut Substs<'tcx>,
1057+
defs: &[ty::TypeParameterDef<'tcx>]) {
10561058

1059+
// This doesn't work ...
10571060
fn definition_span<'tcx>(tcx: &ty::ctxt<'tcx>, def_id: ast::DefId) -> Span {
10581061
let parent = tcx.map.get_parent(def_id.node);
10591062
debug!("definition_span def_id={:?} parent={:?} node={:?} parent_node={:?}",
@@ -1070,24 +1073,21 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10701073
}
10711074
}
10721075

1073-
let mut substs = Substs::empty();
10741076
let mut vars = Vec::with_capacity(defs.len());
10751077

10761078
for def in defs.iter() {
1077-
let default = def.default.map(|default| {
1079+
let default = def.default.subst_spanned(self.tcx, substs, Some(span)).map(|default| {
10781080
type_variable::Default {
10791081
ty: default,
10801082
origin_span: span,
10811083
definition_span: definition_span(self.tcx, def.def_id)
10821084
}
10831085
});
1084-
//.subst(self.tcx, &substs)
1086+
10851087
let ty_var = self.next_ty_var_with_default(default);
1086-
substs.types.push(subst::ParamSpace::SelfSpace, ty_var);
1088+
substs.types.push(space, ty_var);
10871089
vars.push(ty_var)
10881090
}
1089-
1090-
vars
10911091
}
10921092

10931093
/// Given a set of generics defined on a type or impl, returns a substitution mapping each
@@ -1097,17 +1097,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10971097
generics: &ty::Generics<'tcx>)
10981098
-> subst::Substs<'tcx>
10991099
{
1100-
let mut type_params = subst::VecPerParamSpace::empty();
1101-
1102-
for space in subst::ParamSpace::all().iter() {
1103-
type_params.replace(*space,
1104-
self.type_vars_for_defs(span, generics.types.get_slice(*space)))
1105-
}
1100+
let type_params = subst::VecPerParamSpace::empty();
11061101

11071102
let region_params =
11081103
generics.regions.map(
11091104
|d| self.next_region_var(EarlyBoundRegion(span, d.name)));
1110-
subst::Substs::new(type_params, region_params)
1105+
1106+
let mut substs = subst::Substs::new(type_params, region_params);
1107+
1108+
for space in subst::ParamSpace::all().iter() {
1109+
self.type_vars_for_defs(
1110+
span,
1111+
*space,
1112+
&mut substs,
1113+
generics.types.get_slice(*space));
1114+
}
1115+
1116+
return substs;
11111117
}
11121118

11131119
/// Given a set of generics defined on a trait, returns a substitution mapping each output
@@ -1125,13 +1131,17 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11251131
assert!(generics.regions.len(subst::SelfSpace) == 0);
11261132
assert!(generics.regions.len(subst::FnSpace) == 0);
11271133

1128-
let type_parameter_defs = generics.types.get_slice(subst::TypeSpace);
1129-
let type_parameters = self.type_vars_for_defs(span, type_parameter_defs);
1134+
let type_params = Vec::new();
11301135

11311136
let region_param_defs = generics.regions.get_slice(subst::TypeSpace);
11321137
let regions = self.region_vars_for_defs(span, region_param_defs);
11331138

1134-
subst::Substs::new_trait(type_parameters, regions, self_ty)
1139+
let mut substs = subst::Substs::new_trait(type_params, regions, self_ty);
1140+
1141+
let type_parameter_defs = generics.types.get_slice(subst::TypeSpace);
1142+
self.type_vars_for_defs(span, subst::TypeSpace, &mut substs, type_parameter_defs);
1143+
1144+
return substs;
11351145
}
11361146

11371147
pub fn fresh_bound_region(&self, debruijn: ty::DebruijnIndex) -> ty::Region {

src/librustc/middle/subst.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'tcx> Substs<'tcx> {
154154
}
155155

156156
impl RegionSubsts {
157-
fn map<F>(self, op: F) -> RegionSubsts where
157+
pub fn map<F>(self, op: F) -> RegionSubsts where
158158
F: FnOnce(VecPerParamSpace<ty::Region>) -> VecPerParamSpace<ty::Region>,
159159
{
160160
match self {

src/librustc_typeck/check/method/confirm.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,12 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
8484

8585
// Create substitutions for the method's type parameters.
8686
let rcvr_substs = self.fresh_receiver_substs(self_ty, &pick);
87-
let (method_types, method_regions) =
88-
self.instantiate_method_substs(&pick, supplied_method_types);
89-
let all_substs = rcvr_substs.with_method(method_types, method_regions);
87+
let all_substs =
88+
self.instantiate_method_substs(
89+
&pick,
90+
supplied_method_types,
91+
rcvr_substs);
92+
9093
debug!("all_substs={:?}", all_substs);
9194

9295
// Create the final signature for the method, replacing late-bound regions.
@@ -302,8 +305,9 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
302305

303306
fn instantiate_method_substs(&mut self,
304307
pick: &probe::Pick<'tcx>,
305-
supplied_method_types: Vec<Ty<'tcx>>)
306-
-> (Vec<Ty<'tcx>>, Vec<ty::Region>)
308+
supplied_method_types: Vec<Ty<'tcx>>,
309+
substs: subst::Substs<'tcx>)
310+
-> subst::Substs<'tcx>
307311
{
308312
// Determine the values for the generic parameters of the method.
309313
// If they were not explicitly supplied, just construct fresh
@@ -313,21 +317,6 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
313317
let method_types = method.generics.types.get_slice(subst::FnSpace);
314318
let num_method_types = method_types.len();
315319

316-
let method_types = {
317-
if num_supplied_types == 0 {
318-
self.fcx.infcx().type_vars_for_defs(self.span, method_types)
319-
} else if num_method_types == 0 {
320-
span_err!(self.tcx().sess, self.span, E0035,
321-
"does not take type parameters");
322-
self.fcx.infcx().type_vars_for_defs(self.span, method_types)
323-
} else if num_supplied_types != num_method_types {
324-
span_err!(self.tcx().sess, self.span, E0036,
325-
"incorrect number of type parameters given for this method");
326-
vec![self.tcx().types.err; num_method_types]
327-
} else {
328-
supplied_method_types
329-
}
330-
};
331320

332321
// Create subst for early-bound lifetime parameters, combining
333322
// parameters from the type and those from the method.
@@ -339,7 +328,33 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
339328
pick.item.as_opt_method().unwrap()
340329
.generics.regions.get_slice(subst::FnSpace));
341330

342-
(method_types, method_regions)
331+
let subst::Substs { types, regions } = substs;
332+
let regions = regions.map(|r| r.with_vec(subst::FnSpace, method_regions));
333+
let mut final_substs = subst::Substs { types: types, regions: regions };
334+
335+
if num_supplied_types == 0 {
336+
self.fcx.infcx().type_vars_for_defs(
337+
self.span,
338+
subst::FnSpace,
339+
&mut final_substs,
340+
method_types);
341+
} else if num_method_types == 0 {
342+
span_err!(self.tcx().sess, self.span, E0035,
343+
"does not take type parameters");
344+
self.fcx.infcx().type_vars_for_defs(
345+
self.span,
346+
subst::FnSpace,
347+
&mut final_substs,
348+
method_types);
349+
} else if num_supplied_types != num_method_types {
350+
span_err!(self.tcx().sess, self.span, E0036,
351+
"incorrect number of type parameters given for this method");
352+
final_substs.types.replace(subst::FnSpace, vec![self.tcx().types.err; num_method_types]);
353+
} else {
354+
final_substs.types.replace(subst::FnSpace, supplied_method_types);
355+
}
356+
357+
return final_substs;
343358
}
344359

345360
fn unify_receivers(&mut self,

src/librustc_typeck/check/method/mod.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,28 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
169169

170170
let type_parameter_defs = trait_def.generics.types.get_slice(subst::TypeSpace);
171171
let expected_number_of_input_types = type_parameter_defs.len();
172-
let input_types = match opt_input_types {
172+
173+
assert_eq!(trait_def.generics.types.len(subst::FnSpace), 0);
174+
assert!(trait_def.generics.regions.is_empty());
175+
176+
// Construct a trait-reference `self_ty : Trait<input_tys>`
177+
let mut substs = subst::Substs::new_trait(Vec::new(), Vec::new(), self_ty);
178+
179+
match opt_input_types {
173180
Some(input_types) => {
174181
assert_eq!(expected_number_of_input_types, input_types.len());
175-
input_types
182+
substs.types.replace(subst::ParamSpace::TypeSpace, input_types);
176183
}
177184

178185
None => {
179-
fcx.inh.infcx.type_vars_for_defs(span, type_parameter_defs)
186+
fcx.inh.infcx.type_vars_for_defs(
187+
span,
188+
subst::ParamSpace::TypeSpace,
189+
&mut substs,
190+
type_parameter_defs);
180191
}
181-
};
182-
183-
assert_eq!(trait_def.generics.types.len(subst::FnSpace), 0);
184-
assert!(trait_def.generics.regions.is_empty());
185-
186-
// Construct a trait-reference `self_ty : Trait<input_tys>`
187-
let substs = subst::Substs::new_trait(input_types, Vec::new(), self_ty);
192+
}
193+
188194
let trait_ref = ty::TraitRef::new(trait_def_id, fcx.tcx().mk_substs(substs));
189195

190196
// Construct an obligation

src/librustc_typeck/check/method/probe.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,16 +1200,12 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
12001200
return impl_ty;
12011201
}
12021202

1203-
let placeholder;
1203+
let mut placeholder;
12041204
let mut substs = substs;
12051205
if
12061206
!method.generics.types.is_empty_in(subst::FnSpace) ||
12071207
!method.generics.regions.is_empty_in(subst::FnSpace)
12081208
{
1209-
let method_types =
1210-
self.infcx().type_vars_for_defs(self.span,
1211-
method.generics.types.get_slice(subst::FnSpace));
1212-
12131209
// In general, during probe we erase regions. See
12141210
// `impl_self_ty()` for an explanation.
12151211
let method_regions =
@@ -1218,7 +1214,14 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
12181214
.map(|_| ty::ReStatic)
12191215
.collect();
12201216

1221-
placeholder = (*substs).clone().with_method(method_types, method_regions);
1217+
placeholder = (*substs).clone().with_method(Vec::new(), method_regions);
1218+
1219+
self.infcx().type_vars_for_defs(
1220+
self.span,
1221+
subst::FnSpace,
1222+
&mut placeholder,
1223+
method.generics.types.get_slice(subst::FnSpace));
1224+
12221225
substs = &placeholder;
12231226
}
12241227

src/librustc_typeck/check/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,8 +2610,10 @@ pub fn impl_self_ty<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
26102610
debug!("impl_self_ty: tps={:?} rps={:?} raw_ty={:?}", tps, rps, raw_ty);
26112611

26122612
let rps = fcx.inh.infcx.region_vars_for_defs(span, rps);
2613-
let tps = fcx.inh.infcx.type_vars_for_defs(span, tps);
2614-
let substs = subst::Substs::new_type(tps, rps);
2613+
let mut substs = subst::Substs::new(
2614+
VecPerParamSpace::empty(),
2615+
VecPerParamSpace::new(rps, Vec::new(), Vec::new()));
2616+
fcx.inh.infcx.type_vars_for_defs(span, ParamSpace::TypeSpace, &mut substs, tps);
26152617
let substd_ty = fcx.instantiate_type_scheme(span, &substs, &raw_ty);
26162618

26172619
TypeAndSubsts { substs: substs, ty: substd_ty }
@@ -4628,6 +4630,7 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
46284630
}
46294631
}
46304632
}
4633+
46314634
if let Some(self_ty) = opt_self_ty {
46324635
if type_defs.len(subst::SelfSpace) == 1 {
46334636
substs.types.push(subst::SelfSpace, self_ty);
@@ -4640,7 +4643,7 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
46404643
// variables. If the user provided some types, we may still need
46414644
// to add defaults. If the user provided *too many* types, that's
46424645
// a problem.
4643-
for &space in &ParamSpace::all() {
4646+
for &space in &[subst::SelfSpace, subst::TypeSpace, subst::FnSpace] {
46444647
adjust_type_parameters(fcx, span, space, type_defs,
46454648
require_type_space, &mut substs);
46464649
assert_eq!(substs.types.len(space), type_defs.len(space));
@@ -4853,7 +4856,7 @@ pub fn instantiate_path<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
48534856
// Nothing specified at all: supply inference variables for
48544857
// everything.
48554858
if provided_len == 0 && !(require_type_space && space == subst::TypeSpace) {
4856-
substs.types.replace(space, fcx.infcx().type_vars_for_defs(span, &desired[..]));
4859+
fcx.infcx().type_vars_for_defs(span, space, substs, &desired[..]);
48574860
return;
48584861
}
48594862

0 commit comments

Comments
 (0)