From 578bc438b0cd17cab59df335063d1b273ef22ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 13 Nov 2019 16:06:28 -0800 Subject: [PATCH 1/3] Do not ICE on trait aliases with missing obligations --- src/librustc_typeck/astconv.rs | 11 ++++++++--- src/test/ui/issues/issue-65673.rs | 12 ++++++++++++ src/test/ui/issues/issue-65673.stderr | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/issues/issue-65673.rs create mode 100644 src/test/ui/issues/issue-65673.stderr diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index c3bd916b9cead..46559422b03ae 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1226,10 +1226,15 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { /// Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by /// removing the dummy `Self` type (`trait_object_dummy_self`). - fn trait_ref_to_existential(&self, trait_ref: ty::TraitRef<'tcx>) - -> ty::ExistentialTraitRef<'tcx> { + fn trait_ref_to_existential( + &self, + trait_ref: ty::TraitRef<'tcx>, + ) -> ty::ExistentialTraitRef<'tcx> { if trait_ref.self_ty() != self.tcx().types.trait_object_dummy_self { - bug!("trait_ref_to_existential called on {:?} with non-dummy Self", trait_ref); + self.tcx().sess.delay_span_bug(DUMMY_SP, &format!( + "trait_ref_to_existential called on {:?} with non-dummy Self", + trait_ref, + )); } ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref) } diff --git a/src/test/ui/issues/issue-65673.rs b/src/test/ui/issues/issue-65673.rs new file mode 100644 index 0000000000000..ea1d70194b191 --- /dev/null +++ b/src/test/ui/issues/issue-65673.rs @@ -0,0 +1,12 @@ +#![feature(trait_alias)] +trait Trait {} +trait WithType { + type Ctx; +} +trait Alias = where T: Trait; + +impl WithType for T { + type Ctx = dyn Alias; +//~^ ERROR the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time +} +fn main() {} diff --git a/src/test/ui/issues/issue-65673.stderr b/src/test/ui/issues/issue-65673.stderr new file mode 100644 index 0000000000000..a556e35b6a944 --- /dev/null +++ b/src/test/ui/issues/issue-65673.stderr @@ -0,0 +1,17 @@ +error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time + --> $DIR/issue-65673.rs:9:5 + | +LL | type Ctx; + | --- associated type defined here +... +LL | impl WithType for T { + | ---------------------- in this `impl` item +LL | type Ctx = dyn Alias; + | ^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `std::marker::Sized` is not implemented for `(dyn Trait + 'static)` + = note: to learn more, visit + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. From 8d1c2612f818f6db6c626347138d5a20cababcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 14 Nov 2019 11:29:01 -0800 Subject: [PATCH 2/3] review comments --- src/librustc_typeck/astconv.rs | 4 ++++ src/test/ui/issues/issue-65673.rs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 46559422b03ae..205e3efc7f312 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1231,6 +1231,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { trait_ref: ty::TraitRef<'tcx>, ) -> ty::ExistentialTraitRef<'tcx> { if trait_ref.self_ty() != self.tcx().types.trait_object_dummy_self { + // FIXME: There appears to be a missing filter on top of `expand_trait_aliases`, which + // picks up non-supertraits where clauses - but also, the object safety completely + // ignores trait aliases, which could be object safety hazards. We `delay_span_bug` + // here to avoid an ICE in stable even when the feature is disabled. (#66420) self.tcx().sess.delay_span_bug(DUMMY_SP, &format!( "trait_ref_to_existential called on {:?} with non-dummy Self", trait_ref, diff --git a/src/test/ui/issues/issue-65673.rs b/src/test/ui/issues/issue-65673.rs index ea1d70194b191..4b47bd493a568 100644 --- a/src/test/ui/issues/issue-65673.rs +++ b/src/test/ui/issues/issue-65673.rs @@ -1,4 +1,4 @@ -#![feature(trait_alias)] +#![feature(trait_alias)] // Enabled to reduce stderr output, but can be triggered even if disabled. trait Trait {} trait WithType { type Ctx; From 0ff73535ed0044c517697fa3527da3f52069dc3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 17 Nov 2019 14:57:06 -0800 Subject: [PATCH 3/3] Move `trait_ref_to_existential` to a closure review comment --- src/librustc_typeck/astconv.rs | 40 ++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 205e3efc7f312..a33b2e32c8658 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -1224,25 +1224,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { ) } - /// Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by - /// removing the dummy `Self` type (`trait_object_dummy_self`). - fn trait_ref_to_existential( - &self, - trait_ref: ty::TraitRef<'tcx>, - ) -> ty::ExistentialTraitRef<'tcx> { - if trait_ref.self_ty() != self.tcx().types.trait_object_dummy_self { - // FIXME: There appears to be a missing filter on top of `expand_trait_aliases`, which - // picks up non-supertraits where clauses - but also, the object safety completely - // ignores trait aliases, which could be object safety hazards. We `delay_span_bug` - // here to avoid an ICE in stable even when the feature is disabled. (#66420) - self.tcx().sess.delay_span_bug(DUMMY_SP, &format!( - "trait_ref_to_existential called on {:?} with non-dummy Self", - trait_ref, - )); - } - ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref) - } - fn conv_object_ty_poly_trait_ref(&self, span: Span, trait_bounds: &[hir::PolyTraitRef], @@ -1424,13 +1405,30 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { debug!("regular_traits: {:?}", regular_traits); debug!("auto_traits: {:?}", auto_traits); + // Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by + // removing the dummy `Self` type (`trait_object_dummy_self`). + let trait_ref_to_existential = |trait_ref: ty::TraitRef<'tcx>| { + if trait_ref.self_ty() != dummy_self { + // FIXME: There appears to be a missing filter on top of `expand_trait_aliases`, + // which picks up non-supertraits where clauses - but also, the object safety + // completely ignores trait aliases, which could be object safety hazards. We + // `delay_span_bug` here to avoid an ICE in stable even when the feature is + // disabled. (#66420) + tcx.sess.delay_span_bug(DUMMY_SP, &format!( + "trait_ref_to_existential called on {:?} with non-dummy Self", + trait_ref, + )); + } + ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref) + }; + // Erase the `dummy_self` (`trait_object_dummy_self`) used above. let existential_trait_refs = regular_traits.iter().map(|i| { - i.trait_ref().map_bound(|trait_ref| self.trait_ref_to_existential(trait_ref)) + i.trait_ref().map_bound(|trait_ref| trait_ref_to_existential(trait_ref)) }); let existential_projections = bounds.projection_bounds.iter().map(|(bound, _)| { bound.map_bound(|b| { - let trait_ref = self.trait_ref_to_existential(b.projection_ty.trait_ref(tcx)); + let trait_ref = trait_ref_to_existential(b.projection_ty.trait_ref(tcx)); ty::ExistentialProjection { ty: b.ty, item_def_id: b.projection_ty.item_def_id,