Change:
ICE whilst experimenting with opaque/existential types. I tried this code: I expected to see this happen: in stdout Instead, this happened: ICE! : Backtrace: (The above backtrace but with )
I can still reproduce the issue after a , new output of :
Not completely sure, but I think this might be another example of this ICE ():
I've looked into this a little bit - I don't know what the correct behavior is, but hopefully this saves someone some time when they go to fix this (or someone can tell me what the correct behavior is and I can fix it). The following assumes the test case from . This error is emitted during MIR type-check here: It happens when the isn't in in . is to during typeck writeback, here: In particular, this case is when we're considering the for , where has two values - and . For , we end up infering the concrete type to be and fall into this branch: This code exists to avoid considering a defining use in this example: gets those values here from , which is returned from : That ends up at here: This looks over the type (in this case, the return type, , of , whose definition is ) and looks for the opaque types, and creates inference variables for their concrete types. As a result of this, is effectively considered a defining use for both and . is inferred to be - trivially, because of the return value of . Because is used in the type of , whatever type that ends up being will be considered a defining use of too - and the impl of for (what is) says that should be - so 's concrete type is considered to be . That triggers the logic in writeback where we skip adding the concrete type to - and hence the ICE in MIR type check. As I see it, there are two possible options here: 't consider a defining use in this case (when the use in the return type is in a bound), so the concrete type will come from the other defining uses. that the inference variable that ends up being doesn't come from a defining use (but from the trait implementation) and so should be ignored or an error emitted.
Triage: This is no longer ICE with the latest nightly, marking as E-needs-test.

Discussion:
<ins> #![feature(associated_type_defaults)] #![feature(generic_associated_types)] #![allow(incomplete_features)] trait Trait1 { fn foo(); } trait Trait2 { type Associated: Trait1 = Self; //~^ ERROR: the trait bound `Self: Trait1` is not satisfied //~| the size for values of type `Self` cannot be known } impl Trait2 for () {} fn call_foo<T: Trait2>() { T::Associated::foo() } fn main() { call_foo::<()>() } </ins>

<ins> error[E0277]: the trait bound `Self: Trait1` is not satisfied --> $DIR/issue-74816.rs:10:5 | LL |     type Associated: Trait1 = Self; |     ^^^^^^^^^^^^^^^^^------^^^^^^^^ |     |                | |     |                required by this bound in `Trait2::Associated` |     the trait `Trait1` is not implemented for `Self` | help: consider further restricting `Self` | LL | trait Trait2: Trait1 { |             ^^^^^^^^ error[E0277]: the size for values of type `Self` cannot be known at compilation time --> $DIR/issue-74816.rs:10:5 | LL |     type Associated: Trait1 = Self; |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |     | |     doesn't have a size known at compile-time |     required by this bound in `Trait2::Associated` | help: consider further restricting `Self` | LL | trait Trait2: Sized { |             ^^^^^^^ error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0277`. </ins>

<ins> #![feature(type_alias_impl_trait)] #![feature(impl_trait_in_bindings)] #![allow(incomplete_features)] type FooArg<'a> = &'a dyn ToString; type FooRet = impl std::fmt::Debug; type FooItem = Box<dyn Fn(FooArg) -> FooRet>; type Foo = impl Iterator<Item = FooItem>; //~ ERROR: type mismatch #[repr(C)] struct Bar(u8); impl Iterator for Bar { type Item = FooItem; fn next(&mut self) -> Option<Self::Item> { Some(Box::new(quux)) } } fn quux(st: FooArg) -> FooRet { Some(st.to_string()) } fn ham() -> Foo { Bar(1) } fn oof() -> impl std::fmt::Debug { let mut bar = ham(); let func = bar.next().unwrap(); return func(&"oof"); } fn main() { let _ = oof(); } </ins>

<ins> error[E0271]: type mismatch resolving `<Bar as Iterator>::Item == Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>` --> $DIR/issue-70877.rs:9:12 | LL | type FooRet = impl std::fmt::Debug; |               -------------------- the expected opaque type ... LL | type Foo = impl Iterator<Item = FooItem>; |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found enum `Option` | = note: expected struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> impl Debug + 'static)>` found struct `Box<(dyn for<'r> Fn(&'r (dyn ToString + 'r)) -> Option<String> + 'static)>` error: aborting due to previous error For more information about this error, try `rustc --explain E0271`. </ins>

<ins> // check-pass // Regression test of #70944, should compile fine. use std::ops::Index; pub struct KeyA; pub struct KeyB; pub struct KeyC; pub trait Foo: Index<KeyA> + Index<KeyB> + Index<KeyC> {} pub trait FooBuilder { type Inner: Foo; fn inner(&self) -> &Self::Inner; } pub fn do_stuff(foo: &impl FooBuilder) { let inner = foo.inner(); &inner[KeyA]; &inner[KeyB]; &inner[KeyC]; } fn main() {} </ins>

<ins> // check-pass #![feature(trait_alias)] struct Bar; trait Foo {} impl Foo for Bar {} trait Baz = Foo where Bar: Foo; fn new() -> impl Baz { Bar } fn main() { let _ = new(); } </ins>

<ins> #![feature(unsize)] use std::marker::Unsize; pub trait CastTo<T: ?Sized>: Unsize<T> { fn cast_to(&self) -> &T; } impl<T: ?Sized, U: ?Sized + Unsize<T>> CastTo<T> for U { fn cast_to(&self) -> &T { self } } impl<T: ?Sized> Cast for T {} pub trait Cast { fn cast<T: ?Sized>(&self) -> &T where Self: CastTo<T>, { self } } pub trait Foo: CastTo<[i32]> {} impl Foo for [i32; 0] {} fn main() { let x: &dyn Foo = &[]; let x = x.cast::<[i32]>(); //~^ ERROR: the trait bound `dyn Foo: CastTo<[i32]>` is not satisfied } </ins>

<ins> error[E0277]: the trait bound `dyn Foo: CastTo<[i32]>` is not satisfied --> $DIR/issue-71659.rs:30:15 | LL |     let x = x.cast::<[i32]>(); |               ^^^^ the trait `CastTo<[i32]>` is not implemented for `dyn Foo` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. </ins>

<ins> pub trait Callback { fn cb(); } pub trait Processing { type Call: Callback; } fn f<P: Processing + ?Sized>() { P::Call::cb(); } fn main() { struct MyCall; f::<dyn Processing<Call = MyCall>>(); //~^ ERROR: the trait bound `MyCall: Callback` is not satisfied } </ins>

<ins> error[E0277]: the trait bound `MyCall: Callback` is not satisfied --> $DIR/issue-75707.rs:15:5 | LL | fn f<P: Processing + ?Sized>() { |         ---------- required by this bound in `f` ... LL |     f::<dyn Processing<Call = MyCall>>(); |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Callback` is not implemented for `MyCall` error: aborting due to previous error For more information about this error, try `rustc --explain E0277`. </ins>