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 dereference of Box when inner type is expected #90627

Merged
merged 1 commit into from
Nov 7, 2021

Commits on Nov 6, 2021

  1. 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 `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 committed Nov 6, 2021
    Configuration menu
    Copy the full SHA
    d93f7f9 View commit details
    Browse the repository at this point in the history