Skip to content

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

Description

@Havvy

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.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Oct 9, 2018
  2. Havvy commented on Oct 9, 2018

    @Havvy
    ContributorAuthor

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

  3. 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[/+] on Oct 9, 2018
  4. Havvy commented on Oct 9, 2018

    @Havvy
    ContributorAuthor

    @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.

  5. zackmdavis commented on Oct 9, 2018

    @zackmdavis
    Contributor

    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.)

  6. estebank commented on Oct 12, 2018

    @estebank
    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""/"").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions