rust-lang / trait-system-refactor-initiative

The Rustc Trait System Refactor Initiative
21 stars 0 forks source link

Ambiguity in `assemble_candidates_after_normalizing_self_ty` causes coherence error #52

Closed compiler-errors closed 4 months ago

compiler-errors commented 1 year ago

This coherence failure...

pub(crate) trait Drive {}

trait DerefAndDrive {}

impl<T> Drive for T
where
    T: 'static,
    for<'a> &'a T: IntoIterator,
    for<'a> <&'a T as IntoIterator>::Item: DerefAndDrive,
{
}

impl Drive for () {}

fn main() {}

... is because the goal <&'a () as IntoIterator>::Item: DerefAndDrive is considered ambiguous when unifying the two impls above. That's because we try normalizing the self type of the goal, and &'a (): IntoIterator is treated as unknowable.

I think this is problematic, because even if we were to add an upstream implementation to be able to normalize <&'a () as IntoIterator>::Item, it couldn't implement DerefAndDrive since that's a trait local to the crate.

lcnr commented 1 year ago

I don't think we want to add any reasoning like this :thinking: at least if we can avoid it. It feels quite subtle to get right and complicates our rules.

The general pattern here is allowed because of an unsoundness in the old solver, see https://github.com/rust-lang/rust/issues/114061. If we care about not breaking the affected crate we should instead add a negative impl to core and rely on negative coherence to get for<'a> &'a T: IntoIterator to fail.

lcnr commented 4 months ago

opened https://github.com/nikis05/derive-visitor/issues/16, closing