Skip to content

Commit d9c336a

Browse files
committed
default WF: Substitute defaults individually in the clauses.
1 parent 87c2ad0 commit d9c336a

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

src/librustc_typeck/check/wfcheck.rs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -375,58 +375,56 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
375375
use ty::subst::Subst;
376376
use rustc::ty::TypeFoldable;
377377

378+
let mut predicates = fcx.tcx.predicates_of(def_id);
379+
let mut substituted_predicates = Vec::new();
380+
378381
let generics = self.tcx.generics_of(def_id);
379382
let defaulted_params = generics.types.iter()
380383
.filter(|def| def.has_default &&
381384
def.index >= generics.parent_count() as u32);
382-
// Defaults must be well-formed.
383-
for d in defaulted_params.map(|p| p.def_id) {
385+
for param_def in defaulted_params {
386+
// Defaults must be well-formed.
387+
let d = param_def.def_id;
384388
fcx.register_wf_obligation(fcx.tcx.type_of(d), fcx.tcx.def_span(d), self.code.clone());
385-
}
386-
// Check that each default fulfills the bounds on it's parameter.
387-
// We go over each predicate and duplicate it, substituting defaults in the self type.
388-
let mut predicates = fcx.tcx.predicates_of(def_id);
389-
let mut default_predicates = Vec::new();
390-
// In `trait Trait : Super` predicates as `Self: Trait` and `Self: Super` are a problem.
391-
// Therefore we skip such predicates. This means we check less than we could.
392-
for pred in predicates.predicates.iter().filter(|p| !(is_trait && p.has_self_ty())) {
393-
let mut skip = false;
394-
let mut no_default = true;
395-
let substs = ty::subst::Substs::for_item(fcx.tcx, def_id, |def, _| {
396-
// All regions are identity.
397-
fcx.tcx.mk_region(ty::ReEarlyBound(def.to_early_bound_region_data()))
398-
}, |def, _| {
399-
// No default or generic comes from parent, identity substitution.
400-
if !def.has_default || (def.index as usize) < generics.parent_count() {
401-
fcx.tcx.mk_param_from_def(def)
402-
} else {
403-
no_default = false;
404-
// Has a default, use it in the substitution.
405-
let default_ty = fcx.tcx.type_of(def.def_id);
406-
407-
match default_ty.sty {
408-
// Skip `Self: Sized` when `Self` is the default. Needed in traits.
409-
ty::TyParam(ref p) if is_trait && p.is_self() => {
410-
if let ty::Predicate::Trait(p) = pred {
411-
if Some(p.def_id()) == fcx.tcx.lang_items().sized_trait() {
412-
skip = true;
413-
}
414-
}
389+
// Check the clauses are well-formed when the param is substituted by it's default.
390+
// In trait definitions, predicates as `Self: Trait` and `Self: Super` are problematic.
391+
// Therefore we skip such predicates. This means we check less than we could.
392+
for pred in predicates.predicates.iter().filter(|p| !(is_trait && p.has_self_ty())) {
393+
let mut skip = true;
394+
let substs = ty::subst::Substs::for_item(fcx.tcx, def_id, |def, _| {
395+
// All regions are identity.
396+
fcx.tcx.mk_region(ty::ReEarlyBound(def.to_early_bound_region_data()))
397+
}, |def, _| {
398+
let identity_substs = fcx.tcx.mk_param_from_def(def);
399+
if def.index != param_def.index {
400+
identity_substs
401+
} else {
402+
let sized = fcx.tcx.lang_items().sized_trait();
403+
let pred_is_sized = match pred {
404+
ty::Predicate::Trait(p) => Some(p.def_id()) == sized,
405+
_ => false,
406+
};
407+
let default_ty = fcx.tcx.type_of(def.def_id);
408+
let default_is_self = match default_ty.sty {
409+
ty::TyParam(ref p) => p.is_self(),
410+
_ => false
411+
};
412+
// In trait defs, skip `Self: Sized` when `Self` is the default.
413+
if is_trait && pred_is_sized && default_is_self {
414+
identity_substs
415+
} else {
416+
skip = false;
417+
default_ty
415418
}
416-
_ => ()
417419
}
418-
default_ty
420+
});
421+
if !skip {
422+
substituted_predicates.push(pred.subst(fcx.tcx, substs));
419423
}
420-
});
421-
422-
if skip || no_default {
423-
continue;
424424
}
425-
426-
default_predicates.push(pred.subst(fcx.tcx, substs));
427425
}
428426

429-
predicates.predicates.extend(default_predicates);
427+
predicates.predicates.extend(substituted_predicates);
430428
let predicates = predicates.instantiate_identity(fcx.tcx);
431429
let predicates = fcx.normalize_associated_types_in(span, &predicates);
432430

src/test/ui/type-check-defaults.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ trait SelfBound<T:Copy=Self> {}
2929

3030
trait FooTrait<T:Iterator = IntoIter<i32>> where T::Item : Add<u8> {}
3131

32+
trait Trait {}
33+
struct TwoParams<T, U>(T, U);
34+
impl Trait for TwoParams<i32, i32> {}
35+
// Check that each default is substituted individually in the clauses.
36+
struct Bogus<T = i32, U = i32>(TwoParams<T, U>) where TwoParams<T, U>: Trait;
37+
3238
fn main() { }

src/test/ui/type-check-defaults.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,23 @@ error[E0277]: the trait bound `i32: std::ops::Add<u8>` is not satisfied
6767
= help: the trait `std::ops::Add<u8>` is not implemented for `i32`
6868
= note: required by `std::ops::Add`
6969

70-
error: aborting due to 8 previous errors
70+
error[E0277]: the trait bound `TwoParams<i32, U>: Trait` is not satisfied
71+
--> $DIR/type-check-defaults.rs:36:1
72+
|
73+
36 | struct Bogus<T = i32, U = i32>(TwoParams<T, U>) where TwoParams<T, U>: Trait;
74+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `TwoParams<i32, U>`
75+
|
76+
= help: consider adding a `where TwoParams<i32, U>: Trait` bound
77+
= note: required by `Trait`
78+
79+
error[E0277]: the trait bound `TwoParams<T, i32>: Trait` is not satisfied
80+
--> $DIR/type-check-defaults.rs:36:1
81+
|
82+
36 | struct Bogus<T = i32, U = i32>(TwoParams<T, U>) where TwoParams<T, U>: Trait;
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `TwoParams<T, i32>`
84+
|
85+
= help: consider adding a `where TwoParams<T, i32>: Trait` bound
86+
= note: required by `Trait`
87+
88+
error: aborting due to 10 previous errors
7189

0 commit comments

Comments
 (0)