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

Better error message using byte string where only regular strings are allowed in attributes #54926

Closed
Havvy opened this issue Oct 9, 2018 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@Havvy
Copy link
Contributor

Havvy commented Oct 9, 2018

The error message shows the code that matters.

error: literal in `cfg` predicate value must be a string
 --> src/lib.rs:L:11
  |
L | #[cfg(X = b"a")]
  |    

It should probably suggest removing the b. Likewise if using single quotes, it should suggest using double quotes. Both should also be machine applicable. Raw strings are allowed, so nothing needs to be done with them.

@Havvy Havvy added A-diagnostics Area: Messages for errors, warnings, and lints C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Oct 9, 2018
@Havvy
Copy link
Contributor Author

Havvy commented Oct 9, 2018

It might also be valid to give this an error code and explain it all there.

@Havvy Havvy changed the title Better error message for byte string for value of key-value configuration option Better error message using byte string where only regular strings are allowed in attributes Oct 9, 2018
@Havvy
Copy link
Contributor Author

Havvy commented Oct 9, 2018

@petrochenkov mentioned in #54929 that the error message should be done for all places a byte string might be erroneously used instead of an actual string literal. I agree, so I'm generalizing this issue out to that.

@zackmdavis
Copy link
Member

Petrochenkov's technical-debt concern (which I agree with) also came up in #54683. I think the non-filling-the-compiler-with-garbage place to do this would have to be during parsing: perhaps parse_unsuffixed_lit could be generalized to take arguments specifying which literals are allowed (keeping parse_unsuffixed_lit as a less-general version that calls the generalized method)? (Struckthrough thought doesn't actually work because different attributes want to allow different kinds of literals and the parser doesn't and shouldn't know about the semantics of different attributes.)

@estebank
Copy link
Contributor

Self quoting from #54929:

I feel that we can move a lot of these ad-hoc diagnostics further up in the parser by having a more complex version of Parser::eat(&mut self, TokenKind) that also takes some textual info for diagnostics, and also checks for "confusables"—not only single tokens that look alike, like ; and :, but also cases like this one (b""/"").

bors added a commit that referenced this issue Oct 26, 2018
Suggest to remove prefix `b` in cfg attribute lint string

Closes #54926
r? @estebank
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-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

3 participants