Skip to content

Commit ea540b0

Browse files
committed
Auto merge of #66392 - estebank:trait-alias-ice, r=eddyb
Do not ICE on trait aliases with missing obligations Fix #65673.
2 parents f50d6ea + 0ff7353 commit ea540b0

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/librustc_typeck/astconv.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -1224,16 +1224,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12241224
)
12251225
}
12261226

1227-
/// Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by
1228-
/// removing the dummy `Self` type (`trait_object_dummy_self`).
1229-
fn trait_ref_to_existential(&self, trait_ref: ty::TraitRef<'tcx>)
1230-
-> ty::ExistentialTraitRef<'tcx> {
1231-
if trait_ref.self_ty() != self.tcx().types.trait_object_dummy_self {
1232-
bug!("trait_ref_to_existential called on {:?} with non-dummy Self", trait_ref);
1233-
}
1234-
ty::ExistentialTraitRef::erase_self_ty(self.tcx(), trait_ref)
1235-
}
1236-
12371227
fn conv_object_ty_poly_trait_ref(&self,
12381228
span: Span,
12391229
trait_bounds: &[hir::PolyTraitRef],
@@ -1415,13 +1405,30 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14151405
debug!("regular_traits: {:?}", regular_traits);
14161406
debug!("auto_traits: {:?}", auto_traits);
14171407

1408+
// Transform a `PolyTraitRef` into a `PolyExistentialTraitRef` by
1409+
// removing the dummy `Self` type (`trait_object_dummy_self`).
1410+
let trait_ref_to_existential = |trait_ref: ty::TraitRef<'tcx>| {
1411+
if trait_ref.self_ty() != dummy_self {
1412+
// FIXME: There appears to be a missing filter on top of `expand_trait_aliases`,
1413+
// which picks up non-supertraits where clauses - but also, the object safety
1414+
// completely ignores trait aliases, which could be object safety hazards. We
1415+
// `delay_span_bug` here to avoid an ICE in stable even when the feature is
1416+
// disabled. (#66420)
1417+
tcx.sess.delay_span_bug(DUMMY_SP, &format!(
1418+
"trait_ref_to_existential called on {:?} with non-dummy Self",
1419+
trait_ref,
1420+
));
1421+
}
1422+
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
1423+
};
1424+
14181425
// Erase the `dummy_self` (`trait_object_dummy_self`) used above.
14191426
let existential_trait_refs = regular_traits.iter().map(|i| {
1420-
i.trait_ref().map_bound(|trait_ref| self.trait_ref_to_existential(trait_ref))
1427+
i.trait_ref().map_bound(|trait_ref| trait_ref_to_existential(trait_ref))
14211428
});
14221429
let existential_projections = bounds.projection_bounds.iter().map(|(bound, _)| {
14231430
bound.map_bound(|b| {
1424-
let trait_ref = self.trait_ref_to_existential(b.projection_ty.trait_ref(tcx));
1431+
let trait_ref = trait_ref_to_existential(b.projection_ty.trait_ref(tcx));
14251432
ty::ExistentialProjection {
14261433
ty: b.ty,
14271434
item_def_id: b.projection_ty.item_def_id,

src/test/ui/issues/issue-65673.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(trait_alias)] // Enabled to reduce stderr output, but can be triggered even if disabled.
2+
trait Trait {}
3+
trait WithType {
4+
type Ctx;
5+
}
6+
trait Alias<T> = where T: Trait;
7+
8+
impl<T> WithType for T {
9+
type Ctx = dyn Alias<T>;
10+
//~^ ERROR the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
11+
}
12+
fn main() {}

src/test/ui/issues/issue-65673.stderr

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
2+
--> $DIR/issue-65673.rs:9:5
3+
|
4+
LL | type Ctx;
5+
| --- associated type defined here
6+
...
7+
LL | impl<T> WithType for T {
8+
| ---------------------- in this `impl` item
9+
LL | type Ctx = dyn Alias<T>;
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
11+
|
12+
= help: the trait `std::marker::Sized` is not implemented for `(dyn Trait + 'static)`
13+
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)