Skip to content

Commit 0176a9e

Browse files
committed
Auto merge of #69129 - Centril:macro-legacy-errors, r=petrochenkov
Transition macro_legacy_warnings into a hard error Fixes #67098. r? @petrochenkov
2 parents 75b98fb + cec2a9f commit 0176a9e

File tree

7 files changed

+35
-97
lines changed

7 files changed

+35
-97
lines changed

src/librustc_expand/expand.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
832832
span: Span,
833833
) -> AstFragment {
834834
let mut parser = self.cx.new_parser_from_tts(toks);
835-
match parse_ast_fragment(&mut parser, kind, false) {
835+
match parse_ast_fragment(&mut parser, kind) {
836836
Ok(fragment) => {
837837
ensure_complete_parse(&mut parser, path, kind.name(), span);
838838
fragment
@@ -851,7 +851,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
851851
pub fn parse_ast_fragment<'a>(
852852
this: &mut Parser<'a>,
853853
kind: AstFragmentKind,
854-
macro_legacy_warnings: bool,
855854
) -> PResult<'a, AstFragment> {
856855
Ok(match kind {
857856
AstFragmentKind::Items => {
@@ -884,11 +883,9 @@ pub fn parse_ast_fragment<'a>(
884883
}
885884
AstFragmentKind::Stmts => {
886885
let mut stmts = SmallVec::new();
887-
while this.token != token::Eof &&
888-
// won't make progress on a `}`
889-
this.token != token::CloseDelim(token::Brace)
890-
{
891-
if let Some(stmt) = this.parse_full_stmt(macro_legacy_warnings)? {
886+
// Won't make progress on a `}`.
887+
while this.token != token::Eof && this.token != token::CloseDelim(token::Brace) {
888+
if let Some(stmt) = this.parse_full_stmt()? {
892889
stmts.push(stmt);
893890
}
894891
}

src/librustc_expand/mbe/macro_rules.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Pa
8787
impl<'a> ParserAnyMacro<'a> {
8888
crate fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFragment {
8989
let ParserAnyMacro { site_span, macro_ident, ref mut parser, arm_span } = *self;
90-
let fragment = panictry!(parse_ast_fragment(parser, kind, true).map_err(|mut e| {
90+
let fragment = panictry!(parse_ast_fragment(parser, kind).map_err(|mut e| {
9191
if parser.token == token::Eof && e.message().ends_with(", found `<eof>`") {
9292
if !e.span.is_dummy() {
9393
// early end of macro arm (#52866)

src/librustc_parse/parser/stmt.rs

+9-53
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,14 @@ impl<'a> Parser<'a> {
2222
/// Parses a statement. This stops just before trailing semicolons on everything but items.
2323
/// e.g., a `StmtKind::Semi` parses to a `StmtKind::Expr`, leaving the trailing `;` unconsumed.
2424
pub fn parse_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
25-
Ok(self.parse_stmt_without_recovery(true).unwrap_or_else(|mut e| {
25+
Ok(self.parse_stmt_without_recovery().unwrap_or_else(|mut e| {
2626
e.emit();
2727
self.recover_stmt_(SemiColonMode::Break, BlockMode::Ignore);
2828
None
2929
}))
3030
}
3131

32-
fn parse_stmt_without_recovery(
33-
&mut self,
34-
macro_legacy_warnings: bool,
35-
) -> PResult<'a, Option<Stmt>> {
32+
fn parse_stmt_without_recovery(&mut self) -> PResult<'a, Option<Stmt>> {
3633
maybe_whole!(self, NtStmt, |x| Some(x));
3734

3835
let attrs = self.parse_outer_attributes()?;
@@ -64,7 +61,7 @@ impl<'a> Parser<'a> {
6461
let path = self.parse_path(PathStyle::Expr)?;
6562

6663
if self.eat(&token::Not) {
67-
return self.parse_stmt_mac(lo, attrs.into(), path, macro_legacy_warnings);
64+
return self.parse_stmt_mac(lo, attrs.into(), path);
6865
}
6966

7067
let expr = if self.check(&token::OpenDelim(token::Brace)) {
@@ -127,7 +124,6 @@ impl<'a> Parser<'a> {
127124
lo: Span,
128125
attrs: AttrVec,
129126
path: ast::Path,
130-
legacy_warnings: bool,
131127
) -> PResult<'a, Option<Stmt>> {
132128
let args = self.parse_mac_args()?;
133129
let delim = args.delim();
@@ -140,30 +136,6 @@ impl<'a> Parser<'a> {
140136

141137
let kind = if delim == token::Brace || self.token == token::Semi || self.token == token::Eof
142138
{
143-
StmtKind::Mac(P((mac, style, attrs.into())))
144-
}
145-
// We used to incorrectly stop parsing macro-expanded statements here.
146-
// If the next token will be an error anyway but could have parsed with the
147-
// earlier behavior, stop parsing here and emit a warning to avoid breakage.
148-
else if legacy_warnings
149-
&& self.token.can_begin_expr()
150-
&& match self.token.kind {
151-
// These can continue an expression, so we can't stop parsing and warn.
152-
token::OpenDelim(token::Paren)
153-
| token::OpenDelim(token::Bracket)
154-
| token::BinOp(token::Minus)
155-
| token::BinOp(token::Star)
156-
| token::BinOp(token::And)
157-
| token::BinOp(token::Or)
158-
| token::AndAnd
159-
| token::OrOr
160-
| token::DotDot
161-
| token::DotDotDot
162-
| token::DotDotEq => false,
163-
_ => true,
164-
}
165-
{
166-
self.warn_missing_semicolon();
167139
StmtKind::Mac(P((mac, style, attrs)))
168140
} else {
169141
// Since none of the above applied, this is an expression statement macro.
@@ -310,7 +282,7 @@ impl<'a> Parser<'a> {
310282
// bar;
311283
//
312284
// which is valid in other languages, but not Rust.
313-
match self.parse_stmt_without_recovery(false) {
285+
match self.parse_stmt_without_recovery() {
314286
Ok(Some(stmt)) => {
315287
if self.look_ahead(1, |t| t == &token::OpenDelim(token::Brace))
316288
|| do_not_suggest_help
@@ -369,7 +341,7 @@ impl<'a> Parser<'a> {
369341
if self.token == token::Eof {
370342
break;
371343
}
372-
let stmt = match self.parse_full_stmt(false) {
344+
let stmt = match self.parse_full_stmt() {
373345
Err(mut err) => {
374346
self.maybe_annotate_with_ascription(&mut err, false);
375347
err.emit();
@@ -389,11 +361,11 @@ impl<'a> Parser<'a> {
389361
}
390362

391363
/// Parses a statement, including the trailing semicolon.
392-
pub fn parse_full_stmt(&mut self, macro_legacy_warnings: bool) -> PResult<'a, Option<Stmt>> {
364+
pub fn parse_full_stmt(&mut self) -> PResult<'a, Option<Stmt>> {
393365
// Skip looking for a trailing semicolon when we have an interpolated statement.
394366
maybe_whole!(self, NtStmt, |x| Some(x));
395367

396-
let mut stmt = match self.parse_stmt_without_recovery(macro_legacy_warnings)? {
368+
let mut stmt = match self.parse_stmt_without_recovery()? {
397369
Some(stmt) => stmt,
398370
None => return Ok(None),
399371
};
@@ -433,13 +405,8 @@ impl<'a> Parser<'a> {
433405
}
434406
}
435407
StmtKind::Local(..) => {
436-
// We used to incorrectly allow a macro-expanded let statement to lack a semicolon.
437-
if macro_legacy_warnings && self.token != token::Semi {
438-
self.warn_missing_semicolon();
439-
} else {
440-
self.expect_semi()?;
441-
eat_semi = false;
442-
}
408+
self.expect_semi()?;
409+
eat_semi = false;
443410
}
444411
_ => {}
445412
}
@@ -451,17 +418,6 @@ impl<'a> Parser<'a> {
451418
Ok(Some(stmt))
452419
}
453420

454-
fn warn_missing_semicolon(&self) {
455-
self.diagnostic()
456-
.struct_span_warn(self.token.span, {
457-
&format!("expected `;`, found {}", super::token_descr(&self.token))
458-
})
459-
.note({
460-
"this was erroneously allowed and will become a hard error in a future release"
461-
})
462-
.emit();
463-
}
464-
465421
pub(super) fn mk_block(&self, stmts: Vec<Stmt>, rules: BlockCheckMode, span: Span) -> P<Block> {
466422
P(Block { stmts, id: DUMMY_NODE_ID, rules, span })
467423
}

src/test/ui/missing/missing-semicolon-warning.rs

-12
This file was deleted.

src/test/ui/missing/missing-semicolon-warning.stderr

-24
This file was deleted.
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
macro_rules! m {
2+
($($e1:expr),*; $($e2:expr),*) => {
3+
$( let x = $e1 )*; //~ ERROR expected one of `.`, `;`, `?`, or
4+
$( println!("{}", $e2) )*;
5+
}
6+
}
7+
8+
fn main() { m!(0, 0; 0, 0); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: expected one of `.`, `;`, `?`, or an operator, found keyword `let`
2+
--> $DIR/missing-semicolon.rs:3:12
3+
|
4+
LL | $( let x = $e1 )*;
5+
| ^^^ expected one of `.`, `;`, `?`, or an operator
6+
...
7+
LL | fn main() { m!(0, 0; 0, 0); }
8+
| --------------- in this macro invocation
9+
|
10+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)