-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ICE with higher order associated types #129372
Comments
Can be somewhat reproduced on stable with pub fn into_vec<'inner>(dummy: impl FromRow<'inner>) {
let _f = dummy.prepare();
}
pub fn test() {
into_vec(Assume(Some("test")));
}
pub trait FromRow<'t> {
type Out<'a>;
fn prepare(self) -> impl for<'a> FnMut(&'a ()) -> Self::Out<'a>;
}
impl<'t, T: Value<'t>> FromRow<'t> for T
where
<T as Value<'t>>::Typ: MyTyp,
{
type Out<'a> = <T::Typ as MyTyp>::Out<'a>;
fn prepare(self) -> impl for<'a> FnMut(&'a ()) -> Self::Out<'a> {
move |_| loop {}
}
}
pub struct Assume<A>(pub(crate) A);
impl<'t, T, A: Value<'t, Typ = Option<T>>> Value<'t> for Assume<A> {
type Typ = T;
}
pub trait Value<'t> {
type Typ;
}
impl<'t> Value<'t> for &str {
type Typ = String;
}
impl<'t, T: Value<'t, Typ = X>, X: MyTyp> Value<'t> for Option<T> {
type Typ = Option<T::Typ>;
}
pub trait MyTyp: 'static {
type Out<'t>;
}
impl MyTyp for String {
type Out<'t> = Self;
} giving
(but no ICE) warning links to #114858 and seems related although the setup is very different It seems like this is a bug in normalization somewhere, but this specific ICE happens as part of getting debug info for types. So, building in release (or rather, without debug info) actually prevents the ICE but then still hits the same warning. |
Slightly reduced: fn into_vec(dummy: impl FromRow) {
let _f = dummy.prepare();
}
pub fn test() {
into_vec(Assume(()));
}
trait FromRow {
type Out<'a>;
fn prepare(self) -> impl for<'a> FnMut(&'a ()) -> Self::Out<'a>;
}
impl<T: Value<Typ: MyTyp>> FromRow for T {
type Out<'a> = <T::Typ as MyTyp>::Out<'a>;
fn prepare(self) -> impl for<'a> FnMut(&'a ()) -> Self::Out<'a> {
|_| loop {}
}
}
struct Assume<A>(A);
impl<T, A: Value<Typ = T>> Value for Assume<A> {
type Typ = T;
}
trait Value {
type Typ;
}
impl Value for () {
type Typ = ();
}
trait MyTyp {
type Out<'t>;
}
impl MyTyp for () {
type Out<'t> = ();
} |
So I had my own smaller reproducer, but still bigger than @cyrgani. However, after seeing theirs, I minimized it further similar to @cyrgani's version, except some of my comments might be useful. They're based on tracing the right parts of the compiler: pub struct Wrapper<T>(T);
struct Struct;
pub trait TraitA {
// NEEDS TO BE GAT
type AssocA<'t>;
}
pub trait TraitB {
type AssocB;
}
pub fn helper(v: impl MethodTrait) {
// monomorphization instantiates something it then normalizes to:
//
// Closure(
// DefId(0:27 ~ unnamed_1[00e7]::{impl#0}::method::{closure#0}),
// [
// Wrapper1<StructX>,
// i16,
// Binder {
// value: extern "RustCall" fn((&'^0 (),)) -> Alias(Projection, AliasTy { args: [StructX, '^0], def_id: DefId(0:10 ~ unnamed_1[00e7]::TraitA::AssocA), .. }),
// bound_vars: [Region(BrAnon)]
// },
// ()
// ]
// ),
//
// This should be completely normalized but isn't.
// so, normalizing again gives (StructX is inserted) for
// Alias(Projection, AliasTy { args: [StructX, '^0], def_id: DefId(0:10 ~ unnamed_1[00e7]::TraitA::AssocA), .. })
//
// Closure(
// DefId(0:27 ~ unnamed_1[00e7]::{impl#0}::method::{closure#0}),
// [
// Wrapper1<StructX>,
// i16,
// Binder {
// value: extern "RustCall" fn((&'^0 (),)) -> StructX, bound_vars: [Region(BrAnon)]
// },
// ()
// ]
// ).
let _local_that_causes_ice = v.method();
}
pub fn main() {
helper(Wrapper(Struct));
}
pub trait MethodTrait {
type Assoc<'a>;
fn method(self) -> impl for<'a> FnMut(&'a ()) -> Self::Assoc<'a>;
}
impl<T: TraitB> MethodTrait for T
where
<T as TraitB>::AssocB: TraitA,
{
type Assoc<'a> = <T::AssocB as TraitA>::AssocA<'a>;
// must be a method (through Self), the example below doesn't work (as a standalone function)
// fn helper2<M: MethodTrait>(_v: M) -> impl for<'a> FnMut(&'a ()) -> M::Assoc<'a> {
// move |_| loop {}
// }
fn method(self) -> impl for<'a> FnMut(&'a ()) -> Self::Assoc<'a> {
move |_| loop {}
}
}
impl<T, B> TraitB for Wrapper<B>
where
B: TraitB<AssocB = T>,
{
type AssocB = T;
}
impl TraitB for Struct {
type AssocB = Struct;
}
impl TraitA for Struct {
type AssocA<'t> = Self;
} So the specific ICE, like I said before, is essentially unrelated. It's because some debug assertion got changed into an actual assertion here. The problem is actually easier to see when you don't look at this assertion, but wait until the same root cause causes a warning later in the compiler when a sanity check is done here. What's going on is that the warning triggers when the initialization of a MIR local doesn't match the type of its declaration. In this case we're talking about a ZST function item, and in that codepath, to get the type of the function item at the declaration site, the declaration type is monomorphized, and then However, to check for the warning the type is only monomorphized, causing only one normalization: here As I show in my comment in the code example, the monomorphized type normalized once gives:
and the second normalization gives:
which, notably, is different! For some reason the first normalization round, rust is happy to normalize to something still containing a Projection which is normalized away in round two. In the assertion that causes the ICE, essentially the same happens. The compiler sees if normalizing is idempotent and doesn't change the type anymore, but it still does. |
Well, I thought I tried this at the start, but apparently not. Dumb me, whatever. The next solver (#107374) fixes all this so this will soon be a non-issue. Try it with |
add a few more crashtests Added them for rust-lang#123629, rust-lang#127033 and rust-lang#129372.
Rollup merge of rust-lang#129869 - cyrgani:master, r=Mark-Simulacrum add a few more crashtests Added them for rust-lang#123629, rust-lang#127033 and rust-lang#129372.
@rustbot label +S-bug-has-test |
Code
Meta
rustc --version --verbose
:Error output
Backtrace
rustc-ice-2024-08-21T20_21_27-40569.txt
The text was updated successfully, but these errors were encountered: