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

"Try wrapping the expression" action wraps in all viable types #19282

Open
rcorre opened this issue Mar 4, 2025 · 7 comments
Open

"Try wrapping the expression" action wraps in all viable types #19282

rcorre opened this issue Mar 4, 2025 · 7 comments
Labels
A-diagnostics diagnostics / error reporting C-bug Category: bug

Comments

@rcorre
Copy link

rcorre commented Mar 4, 2025

rust-analyzer version: rust-analyzer 1.84.0 (9fc6b43 2025-01-07)

rustc version: rustc 1.84.0 (9fc6b4312 2025-01-07)

editor or extension: helix 25.01.1 (7275b7f8)

relevant settings: None?

code snippet to reproduce:

enum Thing {
    Foo(String),
    Bar(String),
}

fn thing() -> Thing {
    String::new()
}

rust-analyzer suggests:

mismatched types
expected `Thing`, found `String`
try wrapping the expression in a variant of `Thing`: `Thing::Foo(`, `)`, `Thing::Bar(`, `)`

Taking this action results in:

enum Thing {
    Foo(String),
    Bar(String),
}

fn thing() -> Thing {
    Thing::Foo(Thing::Bar(String::new()))
}

Should "Wrap in Thing::Foo" and "Wrap in Thing::Bar" be separate actions?

@rcorre rcorre added the C-bug Category: bug label Mar 4, 2025
@lnicola
Copy link
Member

lnicola commented Mar 4, 2025

Can't reproduce, even on 1.84. Those suggestions come from rustc and are marked with "suggestion_applicability": "MaybeIncorrect", so we shouldn't be showing it:

Image

@lnicola
Copy link
Member

lnicola commented Mar 4, 2025

Hmm, it's strange. In Helix I don't get the action at first, but it does show up after Space-d, Enter, Space-a.

@lnicola lnicola added the A-diagnostics diagnostics / error reporting label Mar 4, 2025
@lnicola
Copy link
Member

lnicola commented Mar 4, 2025

My bad, we do take MaybeIncorrect into account, it just doesn't seem to show up for me for some reason.

@rcorre
Copy link
Author

rcorre commented Mar 4, 2025

I'm not too familiar with how rust-analyzer is processing diagnostics, but FWIW, it looks like rustc prints each on a separate line:

❯ rustc src/main.rs 
error[E0308]: mismatched types
 --> src/main.rs:7:5
  |
6 | fn thing() -> Thing {
  |               ----- expected `Thing` because of return type
7 |     String::new()
  |     ^^^^^^^^^^^^^ expected `Thing`, found `String`
  |
help: try wrapping the expression in a variant of `Thing`
  |
7 |     Thing::Foo(String::new())
  |     +++++++++++             +
7 |     Thing::Bar(String::new())
  |     +++++++++++             +

@lnicola
Copy link
Member

lnicola commented Mar 4, 2025

It suggests both changes in the same place:

    "children": [
      {
        "children": [],
        "code": null,
        "level": "help",
        "message": "try wrapping the expression in a variant of `Thing`",
        "rendered": null,
        "spans": [
          {
            "byte_end": 76,
            "byte_start": 76,
            "column_end": 5,
            "column_start": 5,
            "expansion": null,
            "file_name": "src/main.rs",
            "is_primary": true,
            "label": null,
            "line_end": 7,
            "line_start": 7,
            "suggested_replacement": "Thing::Foo(",
            "suggestion_applicability": "MaybeIncorrect",
            "text": [
              {
                "highlight_end": 5,
                "highlight_start": 5,
                "text": "    String::new()"
              }
            ]
          },
          {
            "byte_end": 89,
            "byte_start": 89,
            "column_end": 18,
            "column_start": 18,
            "expansion": null,
            "file_name": "src/main.rs",
            "is_primary": true,
            "label": null,
            "line_end": 7,
            "line_start": 7,
            "suggested_replacement": ")",
            "suggestion_applicability": "MaybeIncorrect",
            "text": [
              {
                "highlight_end": 18,
                "highlight_start": 18,
                "text": "    String::new()"
              }
            ]
          },
          {
            "byte_end": 76,
            "byte_start": 76,
            "column_end": 5,
            "column_start": 5,
            "expansion": null,
            "file_name": "src/main.rs",
            "is_primary": true,
            "label": null,
            "line_end": 7,
            "line_start": 7,
            "suggested_replacement": "Thing::Bar(",
            "suggestion_applicability": "MaybeIncorrect",
            "text": [
              {
                "highlight_end": 5,
                "highlight_start": 5,
                "text": "    String::new()"
              }
            ]
          },
          {
            "byte_end": 89,
            "byte_start": 89,
            "column_end": 18,
            "column_start": 18,
            "expansion": null,
            "file_name": "src/main.rs",
            "is_primary": true,
            "label": null,
            "line_end": 7,
            "line_start": 7,
            "suggested_replacement": ")",
            "suggestion_applicability": "MaybeIncorrect",
            "text": [
              {
                "highlight_end": 18,
                "highlight_start": 18,
                "text": "    String::new()"
              }
            ]
          }
        ]
      }
    ],

@rcorre
Copy link
Author

rcorre commented Mar 5, 2025

It seems like it would be pretty hard for rust-analyzer to get this right without more information from rustc. Should I file a rustc issue?

@ChayimFriedman2
Copy link
Contributor

Yes it looks like a rustc issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics diagnostics / error reporting C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants