Skip to content

E0308 with complex types produces an unhelpful expected type vs found type note #159043

Description

@EsotericPyramid

Code

// this does actually affect the diagnostic message 
// even though it doesn't show up, separate issue ig
mod really_really_really_long_module {
    pub trait FooBar {
        type Assoc: FooBar;
        type Wrapper<T>: FooBar;
    
        fn assoc(&self) -> Self::Assoc;
        fn wrap<T>(&self, value: T) -> Self::Wrapper<T>;
    }
}

use really_really_really_long_module::*;

fn bad_diagnostic<'a, T: FooBar>(value: T) -> <<<T::Wrapper<T> as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<<T::Assoc as FooBar>::Assoc> {
    value.assoc().assoc().assoc().wrap(value.assoc().assoc())
}

Current output

error[E0308]: mismatched types
  --> src/lib.rs:16:5
   |
15 | fn bad_diagnostic<'a, T: FooBar>(value: T) -> <<<T::Wrapper<T> as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<<T::Assoc as FooBar>::Assoc> {
   |                                               ----------------------------------------------------------------------------------------------------- expected `<<<... as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<...>` because of return type
16 |     value.assoc().assoc().assoc().wrap(value.assoc().assoc())
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `really_really_really_long_module::FooBar::Wrapper`, found `really_really_really_long_module::FooBar::Assoc`
   |
   = note: expected associated type `<<<... as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<...>`
              found associated type `<<<... as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<...>`
   = note: an associated type was expected, but a different one was found
   = note: the full name for the type has been written to '/Users/[username]/Library/Mobile Documents/com~apple~CloudDocs/Programs/rust_programs/bug_tests/target/debug/deps/bug_tests-ffcf3721a6d5102a.long-type-10445563434775290395.txt'
   = note: consider using `--verbose` to print the full type name to the console

Desired output

error[E0308]: mismatched types
  --> src/lib.rs:16:5
   |
15 | fn bad_diagnostic<'a, T: FooBar>(value: T) -> <<<T::Wrapper<T> as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<<T::Assoc as FooBar>::Assoc> {
   |                                               ----------------------------------------------------------------------------------------------------- expected `<<<... as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<...>` because of return type
16 |     value.assoc().assoc().assoc().wrap(value.assoc().assoc())
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `really_really_really_long_module::FooBar::Wrapper`, found `really_really_really_long_module::FooBar::Assoc`
   |
   = note: expected associated type `<<<... as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<...>`
              found associated type `<<<... as FooBar>::Assoc as FooBar>::Assoc as FooBar>::Wrapper<...>`
   = note: an associated type was expected, but a different one was found // add the actual types to this line OR don't elid them from the lines above
   = note: the full name for the type has been written to '/Users/[username]/Library/Mobile Documents/com~apple~CloudDocs/Programs/rust_programs/bug_tests/target/debug/deps/bug_tests-ffcf3721a6d5102a.long-type-10445563434775290395.txt'
   = note: consider using `--verbose` to print the full type name to the console

Rationale and extra context

Admittedly the example code is extremely artificial but I have actually ran into this a few times in actual code. Needless to say, getting an error that says 2 types which look identical but aren't with the only way to inspect them being going out of my way to look at a file with a ridiculously long name is very inconvenient and annoying.

Other cases

likely a separate issue, but moving FooBar outside of the module causes no eliding to take place even though the trait is printed exactly the same in the offending part of the message.

Rust Version

rustc 1.97.0 (2d8144b78 2026-07-07)
binary: rustc
commit-hash: 2d8144b7880597b6e6d3dfd63a9a9efae3f533d3
commit-date: 2026-07-07
host: aarch64-apple-darwin
release: 1.97.0
LLVM version: 22.1.6

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions