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

Suggest setting associated types on type errors #63711

Closed
estebank opened this issue Aug 19, 2019 · 3 comments
Closed

Suggest setting associated types on type errors #63711

estebank opened this issue Aug 19, 2019 · 3 comments
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

The following code is missing a constraint for TryInto<Error=TryIntoError>:

use std::convert::TryInto;

struct TryIntoError;
struct X;

impl TryInto<usize> for X {
    type Error = TryIntoError;

    fn try_into(self) -> Result<usize, TryIntoError> {
        Ok(0)
    }
}

trait T: TryInto<usize> {
    fn foo(self) -> Result<usize, TryIntoError> {
        TryInto::try_into(self)
    }
}

which causes the following error:

error[E0308]: mismatched types
  --> src/main.rs:16:9
   |
15 |     fn foo(self) -> Result<usize, TryIntoError> {
   |                     --------------------------- expected `std::result::Result<usize, TryIntoError>` because of return type
16 |         TryInto::try_into(self)
   |         ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TryIntoError`, found associated type
   |
   = note: expected type `std::result::Result<_, TryIntoError>`
              found type `std::result::Result<_, <Self as std::convert::TryInto<usize>>::Error>`

This should be closer to

error[E0308]: mismatched types
  --> src/main.rs:16:9
   |
14 |   trait T: TryInto<usize> {
   |            -------------- help: explicitely set associated type `Error`: `TryInto<usize, TryIntoError>`
15 |     fn foo(self) -> Result<usize, TryIntoError> {
   |                     --------------------------- expected `std::result::Result<usize, TryIntoError>` because of return type
16 |         TryInto::try_into(self)
   |         ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TryIntoError`, found associated type
   |
   = note: expected type `std::result::Result<_, TryIntoError>`
              found type `std::result::Result<_, <Self as std::convert::TryInto<usize>>::Error>`

or

error[E0308]: mismatched types
  --> src/main.rs:16:9
   |
15 |     fn foo(self) -> Result<usize, TryIntoError> {
   |                     --------------------------- expected `std::result::Result<usize, TryIntoError>` because of return type
16 |         TryInto::try_into(self)
   |         ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TryIntoError`, found associated type
   |
   = note: expected type `std::result::Result<_, TryIntoError>`
              found type `std::result::Result<_, <Self as std::convert::TryInto<usize>>::Error>`
help: explicitely set associated type `Error`
   |
14 |   trait T: TryInto<usize, TryIntoError> {
   |                         ^^^^^^^^^^^^^^
help: alternatively, use `TryInto::Error`
   |
15 |     fn foo(self) -> Result<usize, Self::Error> {
   |                                   ^^^^^^^^^^^

CC reporter @minggfeng

@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints P-medium Medium priority A-associated-items Area: Associated items (types, constants & functions) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Aug 19, 2019
@estebank
Copy link
Contributor Author

Related case from https://twitter.com/ShekoHex/status/1165248106767814657:

use std::net::ToSocketAddrs;
pub struct Config<A>
where
    A: ToSocketAddrs,
{
    addr: A,
}

impl<A> Default for Config<A>
where
    A: ToSocketAddrs,
{
    fn default() -> Self {
        Self {
            addr: "127.0.0.1:1883",
        }
    }
}

fn start<A: ToSocketAddrs>(addr: A) {}
error[E0308]: mismatched types
  --> src/lib.rs:15:19
   |
15 |             addr: "127.0.0.1:1883",
   |                   ^^^^^^^^^^^^^^^^ expected type parameter, found reference
   |
   = note: expected type `A`
              found type `&'static str`

Centril added a commit to Centril/rust that referenced this issue Sep 21, 2019
…-obk

Add explanation to type mismatch involving type params and assoc types

CC rust-lang#63711
@estebank
Copy link
Contributor Author

Case from #56183:

pub struct MyType<T> {
    val: T,
}

impl<T: ToString> MyType<T> {
    pub fn new(val: T) -> Self {
        Self {
            val
        }
    }
    
    pub fn new_from_self(&self, val: u8) -> MyType<u8> {
        MyType {
            val
        }
    }

    pub fn new_from_self_generic(&self, val: u8) -> Self {
        Self {
            val
        }
    }
}

@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Oct 5, 2019
@estebank
Copy link
Contributor Author

Triage, current output for these cases:

error[E0308]: mismatched types
  --> file12.rs:16:9
   |
15 |     fn foo(self) -> Result<usize, TryIntoError> {
   |                     --------------------------- expected `std::result::Result<usize, TryIntoError>` because of return type
16 |         TryInto::try_into(self)
   |         ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `TryIntoError`, found associated type
   |
   = note: expected enum `std::result::Result<_, TryIntoError>`
              found enum `std::result::Result<_, <Self as std::convert::TryInto<usize>>::Error>`
   = note: consider constraining the associated type `<Self as std::convert::TryInto<usize>>::Error` to `TryIntoError`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
error[E0308]: mismatched types
  --> file12.rs:15:19
   |
9  | impl<A> Default for Config<A>
   |      - this type parameter
...
15 |             addr: "127.0.0.1:1883",
   |                   ^^^^^^^^^^^^^^^^ expected type parameter `A`, found `&str`
   |
   = note: expected type parameter `A`
                   found reference `&'static str`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
error[E0308]: mismatched types
  --> file12.rs:20:13
   |
5  | impl<T: ToString> MyType<T> {
   |      - this type parameter
...
20 |             val
   |             ^^^ expected type parameter `T`, found `u8`
   |
   = note: expected type parameter `T`
                        found type `u8`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

Centril added a commit to Centril/rust that referenced this issue Dec 20, 2019
Tweak errors for missing associated types and type parameters

* On `dyn Trait` missing associated types, provide a structured suggestion for them
* On missing type parameters, provide structured suggestion for them
* Point at trait definition when missing required type parameter
* Tweak output of E0658
* Tweak wording of E0719
* Account for `Trait1 + Trait2` case

Fix rust-lang#66380, fix rust-lang#60595. CC rust-lang#63711.
bors added a commit that referenced this issue Dec 26, 2019
Tweak errors for missing associated types and type parameters

* On `dyn Trait` missing associated types, provide a structured suggestion for them
* On missing type parameters, provide structured suggestion for them
* Point at trait definition when missing required type parameter
* Tweak output of E0658
* Tweak wording of E0719
* Account for `Trait1 + Trait2` case

Fix #66380, fix #60595. CC #63711.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

1 participant