Skip to content

Commit 3b743f0

Browse files
committed
Add check for possible CStr literals in pre-2021
1 parent 84a554c commit 3b743f0

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use crate::errors::{
1010
ConstGenericWithoutBracesSugg, DocCommentDoesNotDocumentAnything, DocCommentOnParamType,
1111
DoubleColonInBound, ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg,
1212
GenericParamsWithoutAngleBrackets, GenericParamsWithoutAngleBracketsSugg,
13-
HelpIdentifierStartsWithNumber, InInTypo, IncorrectAwait, IncorrectSemicolon,
14-
IncorrectUseOfAwait, PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg,
15-
SelfParamNotFirst, StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg,
16-
StructLiteralNeedingParens, StructLiteralNeedingParensSugg, SuggAddMissingLetStmt,
17-
SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator, UnexpectedConstInGenericParam,
18-
UnexpectedConstParamDeclaration, UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets,
19-
UseEqInstead, WrapType,
13+
HelpIdentifierStartsWithNumber, HelpUseLatestEdition, InInTypo, IncorrectAwait,
14+
IncorrectSemicolon, IncorrectUseOfAwait, PatternMethodParamWithoutBody, QuestionMarkInType,
15+
QuestionMarkInTypeSugg, SelfParamNotFirst, StructLiteralBodyWithoutPath,
16+
StructLiteralBodyWithoutPathSugg, StructLiteralNeedingParens, StructLiteralNeedingParensSugg,
17+
SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator,
18+
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
19+
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
2020
};
2121
use crate::fluent_generated as fluent;
2222
use crate::parser;
@@ -640,6 +640,17 @@ impl<'a> Parser<'a> {
640640
}
641641
}
642642

643+
// extra info for `c"str"` before 2021 edition or `c "str"` in all editions
644+
if self.prev_token.is_ident_named(Symbol::intern("c"))
645+
&& let TokenKind::Literal(token::Lit { kind: token::Str | token::StrRaw(..), .. }) =
646+
&self.token.kind
647+
{
648+
err.note("you may be trying to declare a CStr literal");
649+
err.note("`CStr` literals require edition 2021");
650+
HelpUseLatestEdition::new().add_to_diagnostic(&mut err);
651+
err.note("`c` modifier in CStr literal cannot be followed by whitespace");
652+
}
653+
643654
// `pub` may be used for an item or `pub(crate)`
644655
if self.prev_token.is_ident_named(sym::public)
645656
&& (self.token.can_begin_item()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
c"str";
3+
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
4+
//~| NOTE expected one of 8 possible tokens
5+
//~| NOTE you may be trying to declare a CStr literal
6+
//~| NOTE `CStr` literals require edition 2021
7+
//~| HELP pass `--edition 2021` to `rustc`
8+
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
9+
//~| NOTE `c` modifier in CStr literal cannot be followed by whitespace
10+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
2+
--> $DIR/edition-cstr-2015-2018.rs:2:6
3+
|
4+
LL | c"str";
5+
| ^^^^^ expected one of 8 possible tokens
6+
|
7+
= note: you may be trying to declare a CStr literal
8+
= note: `CStr` literals require edition 2021
9+
= help: pass `--edition 2021` to `rustc`
10+
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
11+
= note: `c` modifier in CStr literal cannot be followed by whitespace
12+
13+
error: aborting due to 1 previous error
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2021
2+
3+
fn main() {
4+
c"str"; // This should pass
5+
c "str"; // This should fail
6+
//~^ ERROR expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
7+
//~| NOTE expected one of 8 possible tokens
8+
//~| NOTE you may be trying to declare a CStr literal
9+
//~| NOTE `CStr` literals require edition 2021
10+
//~| HELP pass `--edition 2021` to `rustc`
11+
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
12+
//~| NOTE `c` modifier in CStr literal cannot be followed by whitespace
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `"str"`
2+
--> $DIR/edition-cstr-2021.rs:5:7
3+
|
4+
LL | c "str"; // This should fail
5+
| ^^^^^ expected one of 8 possible tokens
6+
|
7+
= note: you may be trying to declare a CStr literal
8+
= note: `CStr` literals require edition 2021
9+
= help: pass `--edition 2021` to `rustc`
10+
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
11+
= note: `c` modifier in CStr literal cannot be followed by whitespace
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)