diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 2c538d2bba56..3ab2cd274b90 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -1040,13 +1040,6 @@ impl<'a, 'gcx, 'tcx> Predicate<'tcx> { Predicate::ConstEvaluatable(def_id, const_substs.subst(tcx, substs)), } } - - pub fn as_poly_trait_predicate(&self) -> Option<&PolyTraitPredicate<'tcx>> { - match self { - Predicate::Trait(trait_pred) => Some(trait_pred), - _ => None - } - } } #[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 463cc2aa6b3e..16115fa9be46 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -392,14 +392,13 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> { // Check that trait predicates are WF when params are substituted by their defaults. // We don't want to overly constrain the predicates that may be written but we // want to catch obviously wrong cases such as `struct Foo` - // or cases that may cause backwards incompatibility such as a library going from - // `pub struct Foo` to `pub struct Foo` where U: Trait` - // which may break existing uses of Foo. - // Therefore the check we do is: If if all params appearing in the LHS of the predicate - // have defaults then we verify that it is WF with all defaults substituted simultaneously. + // or cases where defaults don't work together such as: + // `struct Foo where T: into` + // Therefore we check if a predicate in which all type params are defaulted + // is WF with those defaults simultaneously substituted. // For more examples see tests `defaults-well-formedness.rs` and `type-check-defaults.rs`. // - // First, we build the defaulted substitution. + // First we build the defaulted substitution. let mut defaulted_params = Vec::new(); let substs = ty::subst::Substs::for_item(fcx.tcx, def_id, |def, _| { // All regions are identity. @@ -414,33 +413,35 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> { fcx.tcx.type_of(def.def_id) } }); - // In `trait Trait: Super`, checking `Self: Trait` or `Self: Super` is problematic. - // We avoid those by skipping any predicates in trait declarations that contain `Self`, - // which is excessive so we end up checking less than we could. - for pred in predicates.predicates.iter() - .filter_map(ty::Predicate::as_poly_trait_predicate) - .filter(|p| !(is_trait && p.has_self_ty())) { - let is_defaulted_param = |ty: ty::Ty| match ty.sty { - ty::TyParam(p) => defaulted_params.contains(&p.idx), - _ => false - }; - // If there is a non-defaulted param in the LHS, don't check the substituted predicate. - // `skip_binder()` is ok, we're only inspecting the type params. - if !pred.skip_binder().self_ty().walk().all(is_defaulted_param) { + let defaulted_params = &defaulted_params; + // Now we build the substituted predicates. + for &pred in predicates.predicates.iter() { + struct HasNonDefaulted<'a> { defaulted_params: &'a Vec } + impl<'tcx, 'a> ty::fold::TypeVisitor<'tcx> for HasNonDefaulted<'a> { + fn visit_ty(&mut self, t: Ty<'tcx>) -> bool { + match t.sty { + ty::TyParam(p) => !self.defaulted_params.contains(&p.idx), + _ => t.super_visit_with(self) + } + } + } + // If there is a non-defaulted param in the predicate, don't check it. + if pred.visit_with(&mut HasNonDefaulted { defaulted_params }) { continue; } let substituted_pred = pred.subst(fcx.tcx, substs); - // `skip_binder()` is ok, we're only inspecting for `has_self_ty()`. - let substituted_lhs = substituted_pred.skip_binder().self_ty(); // In trait defs, don't check `Self: Sized` when `Self` is the default. - let pred_is_sized = Some(pred.def_id()) == fcx.tcx.lang_items().sized_trait(); - if is_trait && substituted_lhs.has_self_ty() && pred_is_sized { - continue; + if let ty::Predicate::Trait(trait_pred) = substituted_pred { + // `skip_binder()` is ok, we're only inspecting for `has_self_ty()`. + let lhs_is_self = trait_pred.skip_binder().self_ty().has_self_ty(); + let pred_sized = Some(trait_pred.def_id()) == fcx.tcx.lang_items().sized_trait(); + if is_trait && lhs_is_self && pred_sized { + continue; + } } - let pred = ty::Predicate::Trait(pred.subst(fcx.tcx, substs)); - // Avoid duplicates. - if !predicates.predicates.contains(&pred) { - substituted_predicates.push(pred); + // Avoid duplication of predicates that contain no parameters, for example. + if !predicates.predicates.contains(&substituted_pred) { + substituted_predicates.push(substituted_pred); } } diff --git a/src/test/run-pass/defaults-well-formedness.rs b/src/test/run-pass/defaults-well-formedness.rs index abbd35b3909a..6a7b1c51ccd2 100644 --- a/src/test/run-pass/defaults-well-formedness.rs +++ b/src/test/run-pass/defaults-well-formedness.rs @@ -11,11 +11,13 @@ trait Trait {} struct Foo(U, V) where U: Trait; -trait Trait2 {} +trait Marker {} struct TwoParams(T, U); -impl Trait2 for TwoParams {} +impl Marker for TwoParams {} // Check that defaults are substituted simultaneously. -struct IndividuallyBogus(TwoParams) where TwoParams: Trait2; +struct IndividuallyBogus(TwoParams) where TwoParams: Marker; // Clauses with non-defaulted params are not checked. -struct NonDefaultedInClause(TwoParams) where TwoParams: Trait2; +struct NonDefaultedInClause(TwoParams) where TwoParams: Marker; +struct DefaultedLhs(U, V) where V: Trait; + fn main() {} diff --git a/src/test/ui/type-check-defaults.rs b/src/test/ui/type-check-defaults.rs index cc20f81ae164..ff3fb44bb264 100644 --- a/src/test/ui/type-check-defaults.rs +++ b/src/test/ui/type-check-defaults.rs @@ -37,11 +37,14 @@ trait Super { } trait Base: Super { } //~^ error: the trait bound `T: std::marker::Copy` is not satisfied [E0277] -trait Trait {} -struct DefaultedLhs(U, V) where V: Trait; -//~^ error: the trait bound `i32: Trait` is not satisfied [E0277] +trait ProjectionPred> where T::Item : Add {} +//~^ error: the trait bound `i32: std::ops::Add` is not satisfied [E0277] + +// Defaults must work together. +struct TwoParams(T, U) where T: Bar; +//~^ the trait bound `u32: Bar` is not satisfied [E0277] +trait Bar {} +impl Bar for u32 { } +impl Bar for String { } -// FIXME: Deal with projection predicates -// trait ProjectionPred> where T::Item : Add {} -// ~^ error: the trait bound `i32: std::ops::Add` is not satisfied [E0277] fn main() { } diff --git a/src/test/ui/type-check-defaults.stderr b/src/test/ui/type-check-defaults.stderr index ede1b417c7c9..412a7966f0aa 100644 --- a/src/test/ui/type-check-defaults.stderr +++ b/src/test/ui/type-check-defaults.stderr @@ -79,17 +79,28 @@ note: required by `Super` 36 | trait Super { } | ^^^^^^^^^^^^^^^^^^^^ -error[E0277]: the trait bound `i32: Trait` is not satisfied - --> $DIR/type-check-defaults.rs:41:1 - | -41 | struct DefaultedLhs(U, V) where V: Trait; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `i32` - | -note: required by `Trait` +error[E0277]: the trait bound `i32: std::ops::Add` is not satisfied --> $DIR/type-check-defaults.rs:40:1 | -40 | trait Trait {} - | ^^^^^^^^^^^^^^ +40 | trait ProjectionPred> where T::Item : Add {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `i32 + u8` + | + = help: the trait `std::ops::Add` is not implemented for `i32` + = note: required by `std::ops::Add` -error: aborting due to 9 previous errors +error[E0277]: the trait bound `u32: Bar` is not satisfied + --> $DIR/type-check-defaults.rs:44:1 + | +44 | struct TwoParams(T, U) where T: Bar; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar` is not implemented for `u32` + | + = help: the following implementations were found: + > +note: required by `Bar` + --> $DIR/type-check-defaults.rs:46:1 + | +46 | trait Bar {} + | ^^^^^^^^^^^^ + +error: aborting due to 10 previous errors