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

Improve suggestion for field initializer on tuple struct #99885

Closed
ryanavella opened this issue Jul 29, 2022 · 3 comments
Closed

Improve suggestion for field initializer on tuple struct #99885

ryanavella opened this issue Jul 29, 2022 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ryanavella
Copy link

Given

struct Foo(());

fn bar(x: Foo) -> Foo {
    Foo(0: x.0 as _)
}

The output contains this suggestion, among others

error: casts cannot be followed by a field access
 --> src/lib.rs:4:9
  |
4 |     Foo(0: x.0 as _)
  |         ^^^^
  |
help: try surrounding the expression in parentheses
  |
4 |     Foo((0: x).0 as _)
  |         +    +
help: alternatively, remove the type ascription
  |
4 -     Foo(0: x.0 as _)
4 +     Foo(0.0 as _)
  |

Both the initial error message and the recommendation are confusing. Ideally we should recommend removing 0: since tuple structs do not use field initializers.

@ryanavella ryanavella added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 29, 2022
@ryanavella
Copy link
Author

Ah, TIL about type ascriptions. The error really confused me at first, but now I can see why rustc is getting tripped up.

Knowing that, this looks superficially related to #78907 because the user intended to move/bind a value but rustc just sees an invalid type ascription.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 30, 2022
…t-cast, r=davidtwco

don't call type ascription "cast"

Noticed in rust-lang#99885
@estebank
Copy link
Contributor

estebank commented Aug 3, 2022

@ryanavella don't feel bad about having gotten confused:

  • the error wasn't as clear as it should be
  • type ascription isn't stable, so it isn't publicized much
  • type ascription has a syntax that has very small syntactic distance to other valid things you might want to do
  • bad diagnostics due to type ascription have made it my most hated syntactic wart 😅

@chenyukang
Copy link
Member

we could close this issue now by removing type ascription.
current output is clear enough:

error: invalid `struct` delimiters or `fn` call arguments
 --> src/lib.rs:4:5
  |
4 |     Foo (0: x.0 as _)
  |     ^^^^^^^^^^^^^^^^^
  |
help: if `Foo` is a struct, use braces as delimiters
  |
4 |     Foo  { 0: x.0 as _ }
  |          ~             ~
help: if `Foo` is a function, use the arguments directly
  |
4 -     Foo (0: x.0 as _)
4 +     Foo (x.0 as _)
  |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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

3 participants