Skip to content

Unclear error when on mismatched type parameter with a default value #120785

Closed
@progval

Description

@progval

Code

pub struct LooooooongObscureType<A, B>(A, B);
pub struct WrapperWithMismatchedParameters<W = usize, B = Vec<W>>(W, B);

pub struct MainType<H, L> {
    g: H,
    l: L,
}

pub fn f(
    x: MainType<
        LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
        WrapperWithMismatchedParameters<usize, &'static [usize]>,
    >,
) {
    g(x);
}

pub fn g(
    x: MainType<
        LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
        WrapperWithMismatchedParameters,
    >,
) {
    unimplemented!();
}

Current output

error[E0308]: mismatched types
  --> src/lib.rs:15:7
   |
15 |     g(x);
   |     - ^ expected `Vec<usize>`, found `&'static [usize]`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected struct `MainType<_, WrapperWithMismatchedParameters<_>>` (`Vec<usize>`)
              found struct `MainType<_, WrapperWithMismatchedParameters<_>>` (`&'static [usize]`)
note: function defined here
  --> src/lib.rs:18:8
   |
18 |   pub fn g(
   |          ^
19 | /     x: MainType<
20 | |         LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
21 | |         WrapperWithMismatchedParameters,
22 | |     >,
   | |_____-

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

error[E0308]: mismatched types
  --> src/lib.rs:15:7
   |
15 |     g(x);
   |     - ^ expected `Vec<usize>`, found `&'static [usize]`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected struct `MainType<_, WrapperWithMismatchedParameters<_, Vec<usize>>>`
              found struct `MainType<_, WrapperWithMismatchedParameters<_, &'static [usize]>>`
note: function defined here
  --> src/lib.rs:18:8
   |
18 |   pub fn g(
   |          ^
19 | /     x: MainType<
20 | |         LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
21 | |         WrapperWithMismatchedParameters,
22 | |     >,
   | |_____-

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error

or

error[E0308]: mismatched types
  --> src/lib.rs:15:7
   |
15 |     g(x);
   |     - ^ expected `Vec<usize>`, found `&'static [usize]`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected struct `MainType<_, WrapperWithMismatchedParameters<_, Vec<usize>>>`
              found struct `MainType<_, WrapperWithMismatchedParameters<_, &'static [usize]>>`
note: function defined here
  --> src/lib.rs:18:8
   |
18 |   pub fn g(
   |          ^
19 | /     x: MainType<
20 | |         LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
21 | |         WrapperWithMismatchedParameters<usize, Vec<usize>>,
22 | |     >,
   | |_____-

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

The current error message does not tell me the mismatch is in WrapperWithMismatchedParameters rather than in LooooooongObscureType. It is also inconsistent with the error message I would get if WrapperWithMismatchedParameters has without the default types.

Other cases

When removing the default for the first parameter (which is not the mismatched one) like this:

pub struct LooooooongObscureType<A, B>(A, B);
pub struct WrapperWithMismatchedParameters<W, B = Vec<W>>(W, B);

pub struct MainType<H, L> {
    g: H,
    l: L,
}

pub fn f(
    x: MainType<
        LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
        WrapperWithMismatchedParameters<usize, &'static [usize]>,
    >,
) {
    g(x);
}

pub fn g(
    x: MainType<
        LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
        WrapperWithMismatchedParameters<usize>,
    >,
) {
    unimplemented!();
}

the error message is clearer:

error[E0308]: mismatched types
  --> src/lib.rs:15:7
   |
15 |     g(x);
   |     - ^ expected `Vec<usize>`, found `&'static [usize]`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected struct `MainType<_, WrapperWithMismatchedParameters<_, Vec<usize>>>`
              found struct `MainType<_, WrapperWithMismatchedParameters<_, &'static [usize]>>`
note: function defined here
  --> src/lib.rs:18:8
   |
18 |   pub fn g(
   |          ^
19 | /     x: MainType<
20 | |         LooooooongObscureType<Vec<&'static [usize]>, &'static [u64]>,
21 | |         WrapperWithMismatchedParameters<usize>,
22 | |     >,
   | |_____-

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to 1 previous error

Rust Version

rustc 1.75.0

Anything else?

playground

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions