Skip to content

discard_impls_shadowed_by_env forces unnecessary ambiguity #96

Closed
rust-lang/rust
#133643
@lcnr

Description

the following works with the old solver but breaks with new:

trait Outer {
    type Assoc;
}

trait Id<U: ?Sized> {
    type Id;
}
impl<T, U: ?Sized + Copy> Id<U> for T {
    type Id = T;
}

fn unconstrained<T>() -> T {
    todo!()
}

fn create<T: Outer, U: Copy>(
    u: U,
) -> <<T as Id<U>>::Id as Outer>::Assoc {
    todo!()
}

fn foo<T: Outer<Assoc = i32>>() {
    let u = unconstrained();
    let assoc = create::<T, _>(u);
    assoc.abs();
    let _: i32 = u;
}

The type of assoc is <<T as Id<?u>>::Id as Outer>::Assoc and we have a T: Outer<Assoc = i32> where bound. <T as Id<?u>>::Id uses the impl candidate and normalizes to T, doing so tries to prove U: Copy, resulting in Certainty::Ambiguity. We now use the ParamEnv candidate for <<T as Id<?u>>::Id as Outer>::Assoc (<T as Outer>::Assoc after normalizing the self type). In discard_impls_shadowed_by_env there exists an ambiguous ParamEnv trait candidate, causing us to discard all constraints.

We should somehow only limit "discard all constraints" to the case where there are multiple ParamEnv candidates and we're unable to choose, not if we have an applicable ParamEnv candidate which is ambiguous.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions