Skip to content

Trivial bounds with associated types regression #134238

Open
@konnorandrews

Description

@konnorandrews

Code

I tried writing some macro code that in some cases generates code similar to:

pub trait A {
    type X;
}

pub trait B: A<X = Self::Y> {
    type Y: C;
}

impl<L> A for L {
    type X = L;
}

pub trait C {}

impl C for () {}

impl<T> B for T 
where
    T: A,
    T::X: C,
{
    type Y = T::X;
}

fn demo() where for<'a> (): B {}

I expected to see this happen: The code should compile without error.

Instead, this happened: The following compile error happens.

error[E0284]: type annotations needed
  --> <source>:25:1
   |
25 | fn demo() where for<'a> (): B {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
   |
   = note: cannot satisfy `<() as B>::Y == _`

error[E0284]: type annotations needed
  --> <source>:25:29
   |
25 | fn demo() where for<'a> (): B {}
   |                             ^ cannot infer type
   |
   = note: cannot satisfy `<() as B>::Y == _`

Discussing with @compiler-errors some it would appear #122791 is the cause of the change. The above code does compile if it isn't a trivial bound. For example when you do this instead:

fn demo<T>() where T: B {}

fn other() {
    demo::<()>();
}

For more context on 1.78.0 and before if you remove the for<'a> you get the following.

error[E0271]: type mismatch resolving `<() as A>::X == <() as B>::Y`
  --> <source>:25:17
   |
25 | fn demo() where (): B {}
   |                 ^^^^^ type mismatch resolving `<() as A>::X == <() as B>::Y`
   |
note: expected this to be `()`
  --> <source>:10:14
   |
10 |     type X = L;
   |              ^
   = note:    expected unit type `()`
           found associated type `<() as B>::Y`
   = help: consider constraining the associated type `<() as B>::Y` to `()`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
   = help: see issue #48214

Version it worked on

It most recently worked on: 1.78.0

Version with regression

All versions including 1.79.0 and after. Specifically starting with nightly-2024-04-04 (found with a bisection).

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.P-lowLow priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions