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

Suggestion for E0308 produces invalid syntax for traits with unconstrained associated types, when a type parameter is expected #117501

Closed
polina4096 opened this issue Nov 1, 2023 · 0 comments · Fixed by #117505
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@polina4096
Copy link

polina4096 commented Nov 1, 2023

Code

trait MyTrait {
    type T;
    
    fn bar(self) -> Self::T;
}

fn foo<A: MyTrait, B>(a: A) -> B {
    return a.bar();
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:8:12
  |
7 | fn foo<A: MyTrait, B>(a: A) -> B {
  |                    -           - expected `B` because of return type
  |                    |
  |                    this type parameter
8 |     return a.bar();
  |            ^^^^^^^ expected type parameter `B`, found associated type
  |
  = note: expected type parameter `B`
            found associated type `<A as MyTrait>::T`
help: consider further restricting this bound
  |
7 | fn foo<A: MyTrait + <T = B>, B>(a: A) -> B {
  |                   +++++++++

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:8:12
  |
7 | fn foo<A: MyTrait, B>(a: A) -> B {
  |                    -           - expected `B` because of return type
  |                    |
  |                    this type parameter
8 |     return a.bar();
  |            ^^^^^^^ expected type parameter `B`, found associated type
  |
  = note: expected type parameter `B`
            found associated type `<A as MyTrait>::T`
help: consider constraining the associated type `<A as MyTrait>::T` to `B`
  |
7 | fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
  |                  +++++++

Rationale and extra context

When a type parameter is expected, but an unconstrained associated type is found rustc suggests a syntactically incorrect fix with a misleading help message about trait bounds instead of associated type constraints.

Other cases

trait MyTrait {
    type T;
}

fn foo<A: MyTrait, B>(val: A::T) -> B {
    val
}
trait MyTrait {
    type T;
}

fn foo<A: MyTrait, B>(val: A::T) {
    let _: B = val;
}
trait MyTrait {
    type T;
    fn bar(self) -> Self::T;
}

fn foo<A: MyTrait, B>(a: A) {
    let _: B = a.bar();
}

Anything else?

No response

@polina4096 polina4096 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 Nov 1, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 1, 2023
@polina4096 polina4096 changed the title Help suggestion for E0308 gives invalid syntax for traits with associated types Help suggestion for E0308 gives invalid syntax for traits with associated types when no type T is specified, but type T is expected Nov 1, 2023
@polina4096 polina4096 changed the title Help suggestion for E0308 gives invalid syntax for traits with associated types when no type T is specified, but type T is expected Suggestion for E0308 produces invalid syntax for traits with associated types when no type T is specified, but type T is expected Nov 1, 2023
@polina4096 polina4096 changed the title Suggestion for E0308 produces invalid syntax for traits with associated types when no type T is specified, but type T is expected Suggestion for E0308 produces invalid syntax for traits with unconstrained associated types, when a type parameter is expected Nov 1, 2023
@WaffleLapkin WaffleLapkin added A-typesystem Area: The type system D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 1, 2023
@estebank estebank self-assigned this Nov 2, 2023
estebank added a commit to estebank/rust that referenced this issue Nov 2, 2023
Suggest

```
error[E0308]: mismatched types
  --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12
   |
LL | pub fn foo<A: MyTrait, B>(a: A) -> B {
   |                        -           - expected `B` because of return type
   |                        |
   |                        expected this type parameter
LL |     return a.bar();
   |            ^^^^^^^ expected type parameter `B`, found associated type
   |
   = note: expected type parameter `B`
             found associated type `<A as MyTrait>::T`
help: consider further restricting this bound
   |
LL | pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
   |                      +++++++
```

instead of

```
error[E0308]: mismatched types
  --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12
   |
LL | pub fn foo<A: MyTrait, B>(a: A) -> B {
   |                        -           - expected `B` because of return type
   |                        |
   |                        expected this type parameter
LL |     return a.bar();
   |            ^^^^^^^ expected type parameter `B`, found associated type
   |
   = note: expected type parameter `B`
             found associated type `<A as MyTrait>::T`
help: consider further restricting this bound
   |
LL | pub fn foo<A: MyTrait + <T = B>, B>(a: A) -> B {
   |                      +++++++++
```

Fix rust-lang#117501.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 3, 2023
Fix incorrect trait bound restriction suggestion

Suggest

```
error[E0308]: mismatched types
  --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12
   |
LL | pub fn foo<A: MyTrait, B>(a: A) -> B {
   |                        -           - expected `B` because of return type
   |                        |
   |                        expected this type parameter
LL |     return a.bar();
   |            ^^^^^^^ expected type parameter `B`, found associated type
   |
   = note: expected type parameter `B`
             found associated type `<A as MyTrait>::T`
help: consider further restricting this bound
   |
LL | pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
   |                      +++++++
```

instead of

```
error[E0308]: mismatched types
  --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12
   |
LL | pub fn foo<A: MyTrait, B>(a: A) -> B {
   |                        -           - expected `B` because of return type
   |                        |
   |                        expected this type parameter
LL |     return a.bar();
   |            ^^^^^^^ expected type parameter `B`, found associated type
   |
   = note: expected type parameter `B`
             found associated type `<A as MyTrait>::T`
help: consider further restricting this bound
   |
LL | pub fn foo<A: MyTrait + <T = B>, B>(a: A) -> B {
   |                      +++++++++
```

Fix rust-lang#117501.
@bors bors closed this as completed in 9e7345b Nov 3, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 3, 2023
Rollup merge of rust-lang#117505 - estebank:issue-117501, r=TaKO8Ki

Fix incorrect trait bound restriction suggestion

Suggest

```
error[E0308]: mismatched types
  --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12
   |
LL | pub fn foo<A: MyTrait, B>(a: A) -> B {
   |                        -           - expected `B` because of return type
   |                        |
   |                        expected this type parameter
LL |     return a.bar();
   |            ^^^^^^^ expected type parameter `B`, found associated type
   |
   = note: expected type parameter `B`
             found associated type `<A as MyTrait>::T`
help: consider further restricting this bound
   |
LL | pub fn foo<A: MyTrait<T = B>, B>(a: A) -> B {
   |                      +++++++
```

instead of

```
error[E0308]: mismatched types
  --> $DIR/restrict-assoc-type-of-generic-bound.rs:9:12
   |
LL | pub fn foo<A: MyTrait, B>(a: A) -> B {
   |                        -           - expected `B` because of return type
   |                        |
   |                        expected this type parameter
LL |     return a.bar();
   |            ^^^^^^^ expected type parameter `B`, found associated type
   |
   = note: expected type parameter `B`
             found associated type `<A as MyTrait>::T`
help: consider further restricting this bound
   |
LL | pub fn foo<A: MyTrait + <T = B>, B>(a: A) -> B {
   |                      +++++++++
```

Fix rust-lang#117501.
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 A-typesystem Area: The type system D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue 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.

4 participants