Skip to content

Commit e572a19

Browse files
committed
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.
1 parent e78913b commit e572a19

7 files changed

+80
-15
lines changed

compiler/rustc_parse/src/lexer/mod.rs

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

700700
if expn_data.edition >= Edition::Edition2021 {
701-
let mut silence = false;
702701
// In Rust 2021, this is a hard error.
703702
let sugg = if prefix == "rb" {
704703
Some(errors::UnknownPrefixSugg::UseBr(prefix_span))
705704
} else if expn_data.is_root() {
706705
if self.cursor.first() == '\''
707706
&& let Some(start) = self.last_lifetime
708707
&& self.cursor.third() != '\''
708+
&& let end = self.mk_sp(self.pos, self.pos + BytePos(1))
709+
&& !self.psess.source_map().is_multiline(start.until(end))
709710
{
710-
// An "unclosed `char`" error will be emitted already, silence redundant error.
711-
silence = true;
712-
Some(errors::UnknownPrefixSugg::MeantStr {
713-
start,
714-
end: self.mk_sp(self.pos, self.pos + BytePos(1)),
715-
})
711+
// FIXME: An "unclosed `char`" error will be emitted already in some cases,
712+
// but it's hard to silence this error while not also silencing important cases
713+
// too. We should use the error stashing machinery instead.
714+
Some(errors::UnknownPrefixSugg::MeantStr { start, end })
716715
} else {
717716
Some(errors::UnknownPrefixSugg::Whitespace(prefix_span.shrink_to_hi()))
718717
}
719718
} else {
720719
None
721720
};
722-
let err = errors::UnknownPrefix { span: prefix_span, prefix, sugg };
723-
if silence {
724-
self.dcx().create_err(err).delay_as_bug();
725-
} else {
726-
self.dcx().emit_err(err);
727-
}
721+
self.dcx().emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
728722
} else {
729723
// Before Rust 2021, only emit a lint for migration.
730724
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)