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

Add check for possible CStr literals in pre-2021 #118691

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use crate::errors::{
ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType,
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
HelpIdentifierStartsWithNumber, InInTypo, IncorrectAwait, IncorrectSemicolon,
IncorrectUseOfAwait, PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg,
SelfParamNotFirst, StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg,
StructLiteralNeedingParens, StructLiteralNeedingParensSugg, SuggAddMissingLetStmt,
SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator, UnexpectedConstInGenericParam,
UnexpectedConstParamDeclaration, UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets,
UseEqInstead, WrapType,
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
IncorrectSemicolon, IncorrectUseOfAwait, PatternMethodParamWithoutBody, QuestionMarkInType,
QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens, StructLiteralNeedingParensSugg,
SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator,
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
};
use crate::fluent_generated as fluent;
use crate::parser;
Expand Down Expand Up @@ -640,6 +640,28 @@ impl<'a> Parser<'a> {
}
}

// Try to detect an intended c-string literal while using a pre-2021 edition. The heuristic
// here is to identify a cooked, uninterpolated `c` id immediately followed by a string, or
// a cooked, uninterpolated `cr` id immediately followed by a string or a `#`, in an edition
// where c-string literals are not allowed. There is the very slight possibility of a false
// positive for a `cr#` that wasn't intended to start a c-string literal, but identifying
// that in the parser requires unbounded lookahead, so we only add a hint to the existing
// error rather than replacing it entirely.
if ((self.prev_token.kind == TokenKind::Ident(sym::c, false)
&& matches!(&self.token.kind, TokenKind::Literal(token::Lit { kind: token::Str, .. })))
|| (self.prev_token.kind == TokenKind::Ident(sym::cr, false)
&& matches!(
&self.token.kind,
TokenKind::Literal(token::Lit { kind: token::Str, .. }) | token::Pound
)))
&& self.prev_token.span.hi() == self.token.span.lo()
&& !self.token.span.at_least_rust_2021()
{
err.note("you may be trying to write a c-string literal");
err.note("c-string literals require Rust 2021 or later");
HelpUseLatestEdition::new().add_to_diagnostic(&mut err);
}

// `pub` may be used for an item or `pub(crate)`
if self.prev_token.is_ident_named(sym::public)
&& (self.token.can_begin_item()
Expand Down
62 changes: 62 additions & 0 deletions tests/ui/editions/edition-cstr-2015-2018.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
macro_rules! construct { ($x:ident) => { $x"str" } }
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
//~| NOTE expected one of 8 possible tokens

macro_rules! contain { () => { c"str" } }
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
//~| NOTE expected one of 8 possible tokens
//~| NOTE you may be trying to write a c-string literal
//~| NOTE c-string literals require Rust 2021 or later
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide

fn check_macro_construct() {
construct!(c); //~ NOTE in this expansion of construct!
}

fn check_macro_contain() {
contain!();
//~^ NOTE in this expansion of contain!
//~| NOTE in this expansion of contain!
//~| NOTE in this expansion of contain!
//~| NOTE in this expansion of contain!
//~| NOTE in this expansion of contain!
}

fn check_basic() {
c"str";
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
//~| NOTE expected one of 8 possible tokens
//~| NOTE you may be trying to write a c-string literal
//~| NOTE c-string literals require Rust 2021 or later
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}

fn check_craw() {
cr"str";
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
//~| NOTE expected one of 8 possible tokens
//~| NOTE you may be trying to write a c-string literal
//~| NOTE c-string literals require Rust 2021 or later
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}

fn check_craw_hash() {
cr##"str"##;
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `#`
//~| NOTE expected one of 8 possible tokens
//~| NOTE you may be trying to write a c-string literal
//~| NOTE c-string literals require Rust 2021 or later
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}

fn check_cstr_space() {
c "str";
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
//~| NOTE expected one of 8 possible tokens
}

fn main() {}
67 changes: 67 additions & 0 deletions tests/ui/editions/edition-cstr-2015-2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
--> $DIR/edition-cstr-2015-2018.rs:27:6
|
LL | c"str";
| ^^^^^ expected one of 8 possible tokens
|
= note: you may be trying to write a c-string literal
= note: c-string literals require Rust 2021 or later
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
--> $DIR/edition-cstr-2015-2018.rs:37:7
|
LL | cr"str";
| ^^^^^ expected one of 8 possible tokens
|
= note: you may be trying to write a c-string literal
= note: c-string literals require Rust 2021 or later
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `#`
--> $DIR/edition-cstr-2015-2018.rs:47:7
|
LL | cr##"str"##;
| ^ expected one of 8 possible tokens
|
= note: you may be trying to write a c-string literal
= note: c-string literals require Rust 2021 or later
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
--> $DIR/edition-cstr-2015-2018.rs:57:7
|
LL | c "str";
| ^^^^^ expected one of 8 possible tokens

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
--> $DIR/edition-cstr-2015-2018.rs:1:44
|
LL | macro_rules! construct { ($x:ident) => { $x"str" } }
| ^^^^^ expected one of 8 possible tokens
...
LL | construct!(c);
| ------------- in this macro invocation
|
= note: this error originates in the macro `construct` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
--> $DIR/edition-cstr-2015-2018.rs:5:33
|
LL | macro_rules! contain { () => { c"str" } }
| ^^^^^ expected one of 8 possible tokens
...
LL | contain!();
| ---------- in this macro invocation
|
= note: you may be trying to write a c-string literal
= note: c-string literals require Rust 2021 or later
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
= note: this error originates in the macro `contain` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 6 previous errors

Loading