Skip to content

Error messages don't "simplify" types with default generic params in some cases #52097

Closed
@glandium

Description

@glandium

In #50882, adding an Alloc parameter to the Box type, that defaults to Global induces changes to UI tests that are indicative of something that seemed to need improvement in the compiler, namely that when you have a type Foo<A, B = Bar>, errors should present Foo<A> instead of Foo<A, Bar> when they can.

It turns out, the compiler does. But not consistently. The following is derived from src/test/compile-fail/terr-sorts.rs:

#![crate_type = "lib"]
trait Alloc {}
struct Global;
impl Alloc for Global {}

struct Foo<T, A: Alloc = Global>(T, A);

struct foo {
    a: isize,
    b: isize,
}

type bar = Foo<foo>;
type baz = Foo<usize>;

fn want_foo(f: foo) {}
fn have_bar(b: bar) {
    want_foo(b);
}
fn want_usize(f: usize) {}
fn have_baz(b: baz) {
    want_usize(b);
}

(Playground version)

This yields the following errors:

error[E0308]: mismatched types
  --> src/lib.rs:18:14
   |
18 |     want_foo(b);
   |              ^ expected struct `foo`, found struct `Foo`
   |
   = note: expected type `foo`
              found type `Foo<foo, Global>`

error[E0308]: mismatched types
  --> src/lib.rs:22:16
   |
22 |     want_usize(b);
   |                ^ expected usize, found struct `Foo`
   |
   = note: expected type `usize`
              found type `Foo<usize>`

See how it prints Foo<foo, Global> instead of the (imho) desired Foo<foo>, but Foo<usize>, not Foo<usize, Global>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions