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

Incorrect help message for type declared on struct using raw identifier #69053

Open
olegnn opened this issue Feb 11, 2020 · 10 comments
Open

Incorrect help message for type declared on struct using raw identifier #69053

olegnn opened this issue Feb 11, 2020 · 10 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@olegnn
Copy link
Contributor

olegnn commented Feb 11, 2020

This code

struct r#struct<r#fn>;

fn main() {}

(Playground)

Produces incorrect help message:

   Compiling playground v0.0.1 (/playground)
warning: type `struct` should have an upper camel case name
 --> src/main.rs:1:8
  |
1 | struct r#struct<r#fn>;
  |        ^^^^^^^^ help: convert the identifier to upper camel case: `Struct`
  |
  = note: `#[warn(non_camel_case_types)]` on by default

warning: type parameter `fn` should have an upper camel case name
 --> src/main.rs:1:17
  |
1 | struct r#struct<r#fn>;
  |                 ^^^^ help: convert the identifier to upper camel case: `Fn`

error[E0392]: parameter `fn` is never used
 --> src/main.rs:1:17
  |
1 | struct r#struct<r#fn>;
  |                 ^^^^ unused parameter
  |
  = help: consider removing `fn`, referring to it in a field, or using a marker such as `std::marker::PhantomData`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0392`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@Centril
Copy link
Contributor

Centril commented Feb 11, 2020

Related: #69052 and #69054.

@petrochenkov
Copy link
Contributor

I wonder whether cases like this need to be "fixed" at all.
There going to be an endless stream of these issues, basically every diagnostic that reports an arbitrary identifier.

I'm not sure whether #68963 alone has the right cost/benefit ratio for merging, let alone changing this everywhere.

@petrochenkov
Copy link
Contributor

Suggestion №1: Forget about raw identifier after the original tokens are lost (~lexing/parsing/expansion) and never report them.

Suggestion №2: Use an approximate identifier printing routine that may print edition-specific keywords inaccurately, but at least doesn't require keeping precise spans.
Note that even #68963 doesn't do edition hygiene correctly, because the spans it uses may be stripped from "transparent" macro layers (e.g. ident = ident.modern()) which are not needed for name resolution, but must be preserved for edition hygiene.

@olegnn
Copy link
Contributor Author

olegnn commented Feb 12, 2020

@petrochenkov I didn't get the problem - why we can't format all identifiers according to the edition used by current project? It's the only thing which matters, we don't need to know from and how we got this identifier. Even if we import something from, let's say old reqwest with edition=2015, which had async module, if we're using edition=2018, we should format it r#async, and if we're using 2015, we can use it as a pure ident async.

@petrochenkov
Copy link
Contributor

why we can't format all identifiers according to the edition used by current project?

We can, that would be a very good approximation for the suggestion №2, since we are mostly reporting error for identifiers coming directly from the current crate.

That would be Ident::with_dummy_span(name) from the original version of #68963.
The only thing is that I'd like to see a method with some other name than Ident::with_dummy_span(name), because it's entirely unclear why you need to create an Ident, if you can just print the name, unless you know about this subtlety about raw identifiers.

@estebank
Copy link
Contributor

The PR looked trivial enough by using Idents instead of Symbols to be worth it as a best-effort approach to correct code.

The reason I feel these in particular are important is because normally we suggest using raw idents when the parser can identify them, but in these cases of final and fn the parser does not recover gracefully guiding the user in the right direction.

@olegnn
Copy link
Contributor Author

olegnn commented Feb 20, 2020

@petrochenkov what is the best place to define this function if we assume to use it not only for resolve diagnostics but also for any kind of help messages and notes, which include identifiers?

@petrochenkov
Copy link
Contributor

Hmm, rustc_span should be the common denominator.
It can be a method on Symbol.
It should include something like "guess" in its name since it's an approximation.

@petrochenkov
Copy link
Contributor

@olegnn
Ok, I perhaps went a bit overboard with the "guess" naming, given that ident.to_string() already does some guessing (#69387).

I added the method with a detailed comment in #69491.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 27, 2020
…acrum

rustc_span: Add `Symbol::to_ident_string` for use in diagnostic messages

Covers the same error reporting use case (rust-lang#69387 (comment)) as the `Display` impl for `Ident`.
cc rust-lang#69053

Follow-up to rust-lang#69387.
r? @Mark-Simulacrum
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 28, 2020
…acrum

rustc_span: Add `Symbol::to_ident_string` for use in diagnostic messages

Covers the same error reporting use case (rust-lang#69387 (comment)) as the `Display` impl for `Ident`.
cc rust-lang#69053

Follow-up to rust-lang#69387.
r? @Mark-Simulacrum
@JohnTitor JohnTitor 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 Apr 19, 2020
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
@zaneduffield
Copy link

@petrochenkov I was going to have a go at fixing this since #68963 became inactive, but I think you were right before when you said

There going to be an endless stream of these issues

and one can expect the user to understand that they need the raw identifiers when copying the compiler's suggestions (they just tried to build with one).

In any case, if the user copies the example from the diagnostic that omits the r# from the identifier then the compiler is going to suggest using it on the next build.

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

7 participants