Skip to content

Commit 68359e2

Browse files
authored
Rollup merge of #123223 - estebank:issue-123079, r=pnkfelix
Fix invalid silencing of parsing error Given ```rust macro_rules! a { ( ) => { impl<'b> c for d { e::<f'g> } }; } ``` ensure an error is emitted. Fix #123079.
2 parents ffea7e2 + e572a19 commit 68359e2

7 files changed

+80
-15
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -697,33 +697,27 @@ impl<'psess, 'src> StringReader<'psess, 'src> {
697697
let expn_data = prefix_span.ctxt().outer_expn_data();
698698

699699
if expn_data.edition >= Edition::Edition2021 {
700-
let mut silence = false;
701700
// In Rust 2021, this is a hard error.
702701
let sugg = if prefix == "rb" {
703702
Some(errors::UnknownPrefixSugg::UseBr(prefix_span))
704703
} else if expn_data.is_root() {
705704
if self.cursor.first() == '\''
706705
&& let Some(start) = self.last_lifetime
707706
&& self.cursor.third() != '\''
707+
&& let end = self.mk_sp(self.pos, self.pos + BytePos(1))
708+
&& !self.psess.source_map().is_multiline(start.until(end))
708709
{
709-
// An "unclosed `char`" error will be emitted already, silence redundant error.
710-
silence = true;
711-
Some(errors::UnknownPrefixSugg::MeantStr {
712-
start,
713-
end: self.mk_sp(self.pos, self.pos + BytePos(1)),
714-
})
710+
// FIXME: An "unclosed `char`" error will be emitted already in some cases,
711+
// but it's hard to silence this error while not also silencing important cases
712+
// too. We should use the error stashing machinery instead.
713+
Some(errors::UnknownPrefixSugg::MeantStr { start, end })
715714
} else {
716715
Some(errors::UnknownPrefixSugg::Whitespace(prefix_span.shrink_to_hi()))
717716
}
718717
} else {
719718
None
720719
};
721-
let err = errors::UnknownPrefix { span: prefix_span, prefix, sugg };
722-
if silence {
723-
self.dcx().create_err(err).delay_as_bug();
724-
} else {
725-
self.dcx().emit_err(err);
726-
}
720+
self.dcx().emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
727721
} else {
728722
// Before Rust 2021, only emit a lint for migration.
729723
self.psess.buffer_lint_with_diagnostic(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ edition:2021
2+
macro_rules! a {
3+
( ) => {
4+
impl<'b> c for d {
5+
e::<f'g> //~ ERROR prefix `f` is unknown
6+
}
7+
};
8+
}
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: prefix `f` is unknown
2+
--> $DIR/dont-ice-on-invalid-lifetime-in-macro-definition.rs:5:17
3+
|
4+
LL | e::<f'g>
5+
| ^ unknown prefix
6+
|
7+
= note: prefixed identifiers and literals are reserved since Rust 2021
8+
help: consider inserting whitespace here
9+
|
10+
LL | e::<f 'g>
11+
| +
12+
13+
error: aborting due to 1 previous error
14+

tests/ui/lexer/lex-bad-str-literal-as-char-3.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
//@[rust2021] edition:2021
44
fn main() {
55
println!('hello world');
6-
//[rust2015,rust2018,rust2021]~^ ERROR unterminated character literal
6+
//~^ ERROR unterminated character literal
7+
//[rust2021]~| ERROR prefix `world` is unknown
78
}

tests/ui/lexer/lex-bad-str-literal-as-char-3.rust2021.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
error: prefix `world` is unknown
2+
--> $DIR/lex-bad-str-literal-as-char-3.rs:5:21
3+
|
4+
LL | println!('hello world');
5+
| ^^^^^ unknown prefix
6+
|
7+
= note: prefixed identifiers and literals are reserved since Rust 2021
8+
help: if you meant to write a string literal, use double quotes
9+
|
10+
LL | println!("hello world");
11+
| ~ ~
12+
113
error[E0762]: unterminated character literal
214
--> $DIR/lex-bad-str-literal-as-char-3.rs:5:26
315
|
@@ -9,6 +21,6 @@ help: if you meant to write a string literal, use double quotes
921
LL | println!("hello world");
1022
| ~ ~
1123

12-
error: aborting due to 1 previous error
24+
error: aborting due to 2 previous errors
1325

1426
For more information about this error, try `rustc --explain E0762`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@edition:2021
2+
macro_rules! foo {
3+
() => {
4+
println!('hello world');
5+
//~^ ERROR unterminated character literal
6+
//~| ERROR prefix `world` is unknown
7+
}
8+
}
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: prefix `world` is unknown
2+
--> $DIR/lex-bad-str-literal-as-char-4.rs:4:25
3+
|
4+
LL | println!('hello world');
5+
| ^^^^^ unknown prefix
6+
|
7+
= note: prefixed identifiers and literals are reserved since Rust 2021
8+
help: if you meant to write a string literal, use double quotes
9+
|
10+
LL | println!("hello world");
11+
| ~ ~
12+
13+
error[E0762]: unterminated character literal
14+
--> $DIR/lex-bad-str-literal-as-char-4.rs:4:30
15+
|
16+
LL | println!('hello world');
17+
| ^^^
18+
|
19+
help: if you meant to write a string literal, use double quotes
20+
|
21+
LL | println!("hello world");
22+
| ~ ~
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0762`.

0 commit comments

Comments
 (0)