Skip to content

Commit 6a86737

Browse files
committed
Fix ICE from invalid delimeter in attrs parsing
1 parent 2f7620a commit 6a86737

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,20 @@ pub(crate) struct MetaBadDelimSugg {
822822
pub close: Span,
823823
}
824824

825+
/// Diagnostic for when open and close spans are the same (e.g., from DelimSpan::from_single).
826+
#[derive(Diagnostic)]
827+
#[diag(attr_parsing_meta_bad_delim)]
828+
pub(crate) struct MetaBadDelimSingle {
829+
#[primary_span]
830+
pub span: Span,
831+
#[suggestion(
832+
attr_parsing_meta_bad_delim_suggestion,
833+
code = "(...)",
834+
applicability = "machine-applicable"
835+
)]
836+
pub sugg_span: Span,
837+
}
838+
825839
#[derive(Diagnostic)]
826840
#[diag(attr_parsing_invalid_meta_item)]
827841
pub(crate) struct InvalidMetaItem {

compiler/rustc_attr_parsing/src/validate_attr.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,15 @@ fn check_meta_bad_delim(psess: &ParseSess, span: DelimSpan, delim: Delimiter) {
130130
if let Delimiter::Parenthesis = delim {
131131
return;
132132
}
133-
psess.dcx().emit_err(errors::MetaBadDelim {
134-
span: span.entire(),
135-
sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
136-
});
133+
134+
if span.open == span.close {
135+
psess.dcx().emit_err(errors::MetaBadDelimSingle { span: span.open, sugg_span: span.open });
136+
} else {
137+
psess.dcx().emit_err(errors::MetaBadDelim {
138+
span: span.entire(),
139+
sugg: errors::MetaBadDelimSugg { open: span.open, close: span.close },
140+
});
141+
}
137142
}
138143

139144
/// Checks that the given meta-item is compatible with this `AttributeTemplate`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![core::contracts::requires]
2+
//~^ ERROR use of unstable library feature `contracts`
3+
//~| ERROR inner macro attributes are unstable
4+
//~| ERROR wrong meta list delimiters
5+
//~| ERROR `#[prelude_import]` is for use by rustc only
6+
//~| ERROR mismatched types
7+
#[allow{}]
8+
fn main() {}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0658]: use of unstable library feature `contracts`
2+
--> $DIR/invalid-delimeter-ice-146808.rs:1:4
3+
|
4+
LL | #![core::contracts::requires]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
8+
= help: add `#![feature(contracts)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: inner macro attributes are unstable
12+
--> $DIR/invalid-delimeter-ice-146808.rs:1:4
13+
|
14+
LL | #![core::contracts::requires]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= note: see issue #54726 <https://github.com/rust-lang/rust/issues/54726> for more information
18+
= help: add `#![feature(custom_inner_attributes)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error: wrong meta list delimiters
22+
--> $DIR/invalid-delimeter-ice-146808.rs:1:1
23+
|
24+
LL | / #![core::contracts::requires]
25+
... |
26+
LL | | #[allow{}]
27+
LL | | fn main() {}
28+
| |____________^ help: the delimiters should be `(` and `)`: `(...)`
29+
30+
error[E0658]: `#[prelude_import]` is for use by rustc only
31+
--> $DIR/invalid-delimeter-ice-146808.rs:1:1
32+
|
33+
LL | / #![core::contracts::requires]
34+
... |
35+
LL | | #[allow{}]
36+
LL | | fn main() {}
37+
| |____________^
38+
|
39+
= help: add `#![feature(prelude_import)]` to the crate attributes to enable
40+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
41+
42+
error[E0308]: mismatched types
43+
--> $DIR/invalid-delimeter-ice-146808.rs:1:1
44+
|
45+
LL | #![core::contracts::requires]
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
47+
48+
error: aborting due to 5 previous errors
49+
50+
Some errors have detailed explanations: E0308, E0658.
51+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)