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

Tweak suggestions for bare trait used as a type #119148

Merged
merged 6 commits into from
Jan 5, 2024

Commits on Jan 3, 2024

  1. Provide better suggestions when encountering a bare trait as a type

    Add the following suggestions:
    
    ```
    error[E0782]: trait objects must include the `dyn` keyword
      --> $DIR/not-on-bare-trait-2021.rs:11:11
       |
    LL | fn bar(x: Foo) -> Foo {
       |           ^^^
       |
    help: use a generic type parameter, constrained by the trait `Foo`
       |
    LL | fn bar<T: Foo>(x: T) -> Foo {
       |       ++++++++    ~
    help: you can also use `impl Foo`, but users won't be able to specify the type paramer when calling the `fn`, having to rely exclusively on type inference
       |
    LL | fn bar(x: impl Foo) -> Foo {
       |           ++++
    help: alternatively, use a trait object to accept any type that implements `Foo`, accessing its methods at runtime using dynamic dispatch
       |
    LL | fn bar(x: &dyn Foo) -> Foo {
       |           ++++
    
    error[E0782]: trait objects must include the `dyn` keyword
      --> $DIR/not-on-bare-trait-2021.rs:11:19
       |
    LL | fn bar(x: Foo) -> Foo {
       |                   ^^^
       |
    help: use `impl Foo` to return an opaque type, as long as you return a single underlying type
       |
    LL | fn bar(x: Foo) -> impl Foo {
       |                   ++++
    help: alternatively, you can return an owned trait object
       |
    LL | fn bar(x: Foo) -> Box<dyn Foo> {
       |                   +++++++    +
    ```
    estebank committed Jan 3, 2024
    Configuration menu
    Copy the full SHA
    0487500 View commit details
    Browse the repository at this point in the history
  2. Account for multiple trait bounds in bare trait object suggestion

    Note the parentheses in the last suggestion:
    
    ```
    error[E0277]: the size for values of type `(dyn Foo + Send + 'static)` cannot be known at compilation time
      --> $DIR/not-on-bare-trait.rs:7:8
       |
    LL | fn foo(_x: Foo + Send) {
       |        ^^ doesn't have a size known at compile-time
       |
       = help: the trait `Sized` is not implemented for `(dyn Foo + Send + 'static)`
       = help: unsized fn params are gated as an unstable feature
    help: you can use `impl Trait` as the argument type
       |
    LL | fn foo(_x: impl Foo + Send) {
       |            ++++
    help: function arguments must have a statically known size, borrowed types always have a known size
       |
    LL | fn foo(_x: &(Foo + Send)) {
       |            ++          +
    ```
    estebank committed Jan 3, 2024
    Configuration menu
    Copy the full SHA
    8551cab View commit details
    Browse the repository at this point in the history
  3. Track HirId instead of Span in `ObligationCauseCode::SizedArgumen…

    …tType`
    
    This gets us more accurate suggestions.
    estebank committed Jan 3, 2024
    Configuration menu
    Copy the full SHA
    79bef72 View commit details
    Browse the repository at this point in the history
  4. review comments

    estebank committed Jan 3, 2024
    Configuration menu
    Copy the full SHA
    771966b View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    78ef946 View commit details
    Browse the repository at this point in the history

Commits on Jan 4, 2024

  1. Configuration menu
    Copy the full SHA
    698dfc3 View commit details
    Browse the repository at this point in the history