Skip to content

Commit a13c7da

Browse files
authored
Rollup merge of rust-lang#102893 - TaKO8Ki:fix-102878, r=davidtwco
Fix ICE rust-lang#102878 Fixes rust-lang#102878
2 parents cb67283 + 6826028 commit a13c7da

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

compiler/rustc_error_messages/src/lib.rs

-13
Original file line numberDiff line numberDiff line change
@@ -337,19 +337,6 @@ impl DiagnosticMessage {
337337
}
338338
}
339339
}
340-
341-
/// Returns the `String` contained within the `DiagnosticMessage::Str` variant, assuming that
342-
/// this diagnostic message is of the legacy, non-translatable variety. Panics if this
343-
/// assumption does not hold.
344-
///
345-
/// Don't use this - it exists to support some places that do comparison with diagnostic
346-
/// strings.
347-
pub fn expect_str(&self) -> &str {
348-
match self {
349-
DiagnosticMessage::Str(s) => s,
350-
_ => panic!("expected non-translatable diagnostic message"),
351-
}
352-
}
353340
}
354341

355342
/// `From` impl that enables existing diagnostic calls to functions which now take

compiler/rustc_expand/src/mbe/macro_rules.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::{NodeId, DUMMY_NODE_ID};
1414
use rustc_ast_pretty::pprust;
1515
use rustc_attr::{self as attr, TransparencyError};
1616
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
17-
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder};
17+
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, DiagnosticMessage};
1818
use rustc_feature::Features;
1919
use rustc_lint_defs::builtin::{
2020
RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
@@ -68,19 +68,22 @@ fn emit_frag_parse_err(
6868
kind: AstFragmentKind,
6969
) {
7070
// FIXME(davidtwco): avoid depending on the error message text
71-
if parser.token == token::Eof && e.message[0].0.expect_str().ends_with(", found `<eof>`") {
72-
if !e.span.is_dummy() {
73-
// early end of macro arm (#52866)
74-
e.replace_span_with(parser.sess.source_map().next_point(parser.token.span));
75-
}
71+
if parser.token == token::Eof
72+
&& let DiagnosticMessage::Str(message) = &e.message[0].0
73+
&& message.ends_with(", found `<eof>`")
74+
{
7675
let msg = &e.message[0];
7776
e.message[0] = (
78-
rustc_errors::DiagnosticMessage::Str(format!(
77+
DiagnosticMessage::Str(format!(
7978
"macro expansion ends with an incomplete expression: {}",
80-
msg.0.expect_str().replace(", found `<eof>`", ""),
79+
message.replace(", found `<eof>`", ""),
8180
)),
8281
msg.1,
8382
);
83+
if !e.span.is_dummy() {
84+
// early end of macro arm (#52866)
85+
e.replace_span_with(parser.sess.source_map().next_point(parser.token.span));
86+
}
8487
}
8588
if e.span.is_dummy() {
8689
// Get around lack of span in error (#30128)

src/test/ui/macros/issue-102878.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
macro_rules!test{($l:expr,$_:r)=>({const:y y)}
2+
//~^ ERROR mismatched closing delimiter: `)`
3+
//~| ERROR invalid fragment specifier `r`
4+
//~| ERROR expected identifier, found keyword `const`
5+
//~| ERROR expected identifier, found keyword `const`
6+
//~| ERROR expected identifier, found `:`
7+
8+
fn s(){test!(1,i)}
9+
10+
fn main() {}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error: mismatched closing delimiter: `)`
2+
--> $DIR/issue-102878.rs:1:35
3+
|
4+
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
5+
| -^ ^ mismatched closing delimiter
6+
| ||
7+
| |unclosed delimiter
8+
| closing delimiter possibly meant for this
9+
10+
error: invalid fragment specifier `r`
11+
--> $DIR/issue-102878.rs:1:27
12+
|
13+
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
14+
| ^^^^
15+
|
16+
= help: valid fragment specifiers are `ident`, `block`, `stmt`, `expr`, `pat`, `ty`, `lifetime`, `literal`, `path`, `meta`, `tt`, `item` and `vis`
17+
18+
error: expected identifier, found keyword `const`
19+
--> $DIR/issue-102878.rs:1:36
20+
|
21+
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
22+
| ^^^^^ expected identifier, found keyword
23+
...
24+
LL | fn s(){test!(1,i)}
25+
| ---------- in this macro invocation
26+
|
27+
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
28+
help: escape `const` to use it as an identifier
29+
|
30+
LL | macro_rules!test{($l:expr,$_:r)=>({r#const:y y)}
31+
| ++
32+
33+
error: expected identifier, found keyword `const`
34+
--> $DIR/issue-102878.rs:1:36
35+
|
36+
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
37+
| ^^^^^ expected identifier, found keyword
38+
...
39+
LL | fn s(){test!(1,i)}
40+
| ---------- in this macro invocation
41+
|
42+
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
43+
help: escape `const` to use it as an identifier
44+
|
45+
LL | macro_rules!test{($l:expr,$_:r)=>({r#const:y y)}
46+
| ++
47+
48+
error: expected identifier, found `:`
49+
--> $DIR/issue-102878.rs:1:41
50+
|
51+
LL | macro_rules!test{($l:expr,$_:r)=>({const:y y)}
52+
| ^ expected identifier
53+
...
54+
LL | fn s(){test!(1,i)}
55+
| ---------- in this macro invocation
56+
|
57+
= note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
58+
59+
error: aborting due to 5 previous errors
60+

0 commit comments

Comments
 (0)