Skip to content

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

@glandium

Description

@glandium
Contributor

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>.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
on Jul 6, 2018
glandium

glandium commented on Jul 11, 2018

@glandium
ContributorAuthor

This all stems from the fact that in some cases, types are just printed out by formatting the Ty<'ctx> instances, while in other cases, there is manual machinery to highlight parts of the type that handle things, and that doesn't have the default-param elimination that PrintContext::parameterized has.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @glandium@varkor

        Issue actions

          Error messages don't "simplify" types with default generic params in some cases · Issue #52097 · rust-lang/rust