Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
progval opened this issue Feb 8, 2024 · 1 comment · Fixed by #121416
Closed

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

progval opened this issue Feb 8, 2024 · 1 comment · Fixed by #121416
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@progval
Copy link
Contributor

progval commented Feb 8, 2024

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

@progval progval added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 8, 2024
@veera-sivarajan
Copy link
Contributor

@rustbot claim

veera-sivarajan added a commit to veera-sivarajan/rust that referenced this issue Feb 21, 2024
veera-sivarajan added a commit to veera-sivarajan/rust that referenced this issue Feb 21, 2024
veera-sivarajan added a commit to veera-sivarajan/rust that referenced this issue Feb 21, 2024
veera-sivarajan added a commit to veera-sivarajan/rust that referenced this issue Feb 21, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 23, 2024
…thercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 28, 2024
…nethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Mar 1, 2024
…nethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Mar 1, 2024
…nethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 1, 2024
…nethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 1, 2024
…nethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 1, 2024
…nethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 1, 2024
…nethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
@bors bors closed this as completed in 4996194 Mar 1, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 1, 2024
Rollup merge of rust-lang#121416 - veera-sivarajan:bugfix-120785, r=nnethercote

Improve error messages for generics with default parameters

Fixes rust-lang#120785

Issue: Previously, all type parameters with default types were deliberately ignored to simplify error messages. For example, an error message for Box type would display `Box<T>` instead of `Box<T, _>`. But, this resulted in unclear error message when a concrete type was used instead of the default type.

Fix: This PR fixes it by checking if a concrete type is specified after a default type to display the entire type name or the simplified type name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants