Skip to content

Conversation

camelid
Copy link
Member

@camelid camelid commented Nov 5, 2021

For example:

enum Ty {
    Unit,
    List(Box<Ty>),
}

fn foo(x: Ty) -> Ty {
    match x {
        Ty::Unit => Ty::Unit,
        Ty::List(elem) => foo(elem),
    }
}

Before, the only suggestion was to rewrap inner with Ty::Wrapper,
which is unhelpful and confusing:

error[E0308]: mismatched types
 --> src/test/ui/suggestions/boxed-variant-field.rs:9:31
  |
9 |         Ty::List(elem) => foo(elem),
  |                               ^^^^
  |                               |
  |                               expected enum `Ty`, found struct `Box`
  |                               help: try using a variant of the expected enum: `Ty::List(elem)`
  |
  = note: expected enum `Ty`
           found struct `Box<Ty>`

Now, rustc will first suggest dereferencing the Box, which is most
likely what the user intended:

error[E0308]: mismatched types
 --> src/test/ui/suggestions/boxed-variant-field.rs:9:31
  |
9 |         Ty::List(elem) => foo(elem),
  |                               ^^^^ expected enum `Ty`, found struct `Box`
  |
  = note: expected enum `Ty`
           found struct `Box<Ty>`
help: try dereferencing the `Box`
  |
9 |         Ty::List(elem) => foo(*elem),
  |                               +
help: try using a variant of the expected enum
  |
9 |         Ty::List(elem) => foo(Ty::List(elem)),
  |                               ~~~~~~~~~~~~~~

r? @davidtwco

@camelid camelid added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Nov 5, 2021
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 5, 2021
@camelid camelid added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 5, 2021
@camelid
Copy link
Member Author

camelid commented Nov 5, 2021

This suggestion is already emitted in other situations, but not the situation described in the PR description:

error[E0308]: mismatched types
 --> src/main.rs:2:18
  |
2 |     let x: i32 = Box::new(123);
  |            ---   ^^^^^^^^^^^^^ expected `i32`, found struct `Box`
  |            |
  |            expected due to this
  |
  = note: expected type `i32`
           found struct `Box<{integer}>`
help: consider dereferencing the type
  |
2 |     let x: i32 = *Box::new(123);
  |                  +

| ^
| |
| expected struct `Foo`, found struct `Box`
| help: try dereferencing the `Box`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I use span_suggestion_verbose instead to improve cases like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, r=me after that.

| ^
| |
| expected struct `Foo`, found struct `Box`
| help: try dereferencing the `Box`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, r=me after that.

@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 6, 2021
@camelid
Copy link
Member Author

camelid commented Nov 6, 2021

Thanks!

@bors r=davidtwco rollup

@bors
Copy link
Collaborator

bors commented Nov 6, 2021

📌 Commit ab95e04e121af4bb1ff1c30e30b1f3ab77c507ef has been approved by davidtwco

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 6, 2021
For example:

    enum Ty {
        Unit,
        List(Box<Ty>),
    }

    fn foo(x: Ty) -> Ty {
        match x {
            Ty::Unit => Ty::Unit,
            Ty::List(elem) => foo(elem),
        }
    }

Before, the only suggestion was to rewrap `elem` with `Ty::List`,
which is unhelpful and confusing:

    error[E0308]: mismatched types
     --> src/test/ui/suggestions/boxed-variant-field.rs:9:31
      |
    9 |         Ty::List(elem) => foo(elem),
      |                               ^^^^
      |                               |
      |                               expected enum `Ty`, found struct `Box`
      |                               help: try using a variant of the expected enum: `Ty::List(elem)`
      |
      = note: expected enum `Ty`
               found struct `Box<Ty>`

Now, rustc will first suggest dereferencing the `Box`, which is most
likely what the user intended:

    error[E0308]: mismatched types
     --> src/test/ui/suggestions/boxed-variant-field.rs:9:31
      |
    9 |         Ty::List(elem) => foo(elem),
      |                               ^^^^ expected enum `Ty`, found struct `Box`
      |
      = note: expected enum `Ty`
               found struct `Box<Ty>`
    help: try dereferencing the `Box`
      |
    9 |         Ty::List(elem) => foo(*elem),
      |                               +
    help: try using a variant of the expected enum
      |
    9 |         Ty::List(elem) => foo(Ty::List(elem)),
      |                               ~~~~~~~~~~~~~~
@camelid
Copy link
Member Author

camelid commented Nov 6, 2021

(Fixed something confusing in the commit message)

@bors r=davidtwco

@bors
Copy link
Collaborator

bors commented Nov 6, 2021

📌 Commit d93f7f9 has been approved by davidtwco

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 6, 2021
Suggest dereference of `Box` when inner type is expected

For example:

    enum Ty {
        Unit,
        List(Box<Ty>),
    }

    fn foo(x: Ty) -> Ty {
        match x {
            Ty::Unit => Ty::Unit,
            Ty::List(elem) => foo(elem),
        }
    }

Before, the only suggestion was to rewrap `inner` with `Ty::Wrapper`,
which is unhelpful and confusing:

    error[E0308]: mismatched types
     --> src/test/ui/suggestions/boxed-variant-field.rs:9:31
      |
    9 |         Ty::List(elem) => foo(elem),
      |                               ^^^^
      |                               |
      |                               expected enum `Ty`, found struct `Box`
      |                               help: try using a variant of the expected enum: `Ty::List(elem)`
      |
      = note: expected enum `Ty`
               found struct `Box<Ty>`

Now, rustc will first suggest dereferencing the `Box`, which is most
likely what the user intended:

    error[E0308]: mismatched types
     --> src/test/ui/suggestions/boxed-variant-field.rs:9:31
      |
    9 |         Ty::List(elem) => foo(elem),
      |                               ^^^^ expected enum `Ty`, found struct `Box`
      |
      = note: expected enum `Ty`
               found struct `Box<Ty>`
    help: try dereferencing the `Box`
      |
    9 |         Ty::List(elem) => foo(*elem),
      |                               +
    help: try using a variant of the expected enum
      |
    9 |         Ty::List(elem) => foo(Ty::List(elem)),
      |                               ~~~~~~~~~~~~~~

r? `@davidtwco`
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 6, 2021
…askrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#90487 (Add a chapter on reading Rustdoc output)
 - rust-lang#90508 (Apply adjustments for field expression even if inaccessible)
 - rust-lang#90627 (Suggest dereference of `Box` when inner type is expected)
 - rust-lang#90642 (use matches!() macro in more places)
 - rust-lang#90646 (type error go brrrrrrrr)
 - rust-lang#90649 (Run reveal_all on MIR when inlining is activated.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1d9fe9c into rust-lang:master Nov 7, 2021
@rustbot rustbot added this to the 1.58.0 milestone Nov 7, 2021
@camelid camelid deleted the suggest-box-deref branch November 7, 2021 02:08
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 14, 2022
…ef-suggestion, r=camelid

Deduplicate box deref and regular deref suggestions

Remove the suggestion code special-cased for Box deref.

r? `@camelid`
since you introduced the code in rust-lang#90627
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 14, 2022
…ef-suggestion, r=camelid

Deduplicate box deref and regular deref suggestions

Remove the suggestion code special-cased for Box deref.

r? ``@camelid``
since you introduced the code in rust-lang#90627
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 14, 2022
…ef-suggestion, r=camelid

Deduplicate box deref and regular deref suggestions

Remove the suggestion code special-cased for Box deref.

r? ```@camelid```
since you introduced the code in rust-lang#90627
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants