Skip to content

Commit 76a3655

Browse files
Rollup merge of #154803 - chenyukang:yukang-fix-154801-cfg-attr-span, r=JonathanBrouwer
Fix ICE from cfg_attr_trace Fixes #154801 Fixes #143094 r? @JonathanBrouwer The root cause is we recovery from parsing attribute error here: https://github.com/rust-lang/rust/blob/ed6f9af7d47f5a5eda2a4a1925d1e250b51a37f2/compiler/rustc_attr_parsing/src/parser.rs#L550 while the later suggestion code from type checking try to inspect the attr span of the `expr` in the second error, keep the span seems reasonable.
2 parents dde4886 + 45b4e3c commit 76a3655

5 files changed

Lines changed: 64 additions & 25 deletions

File tree

compiler/rustc_hir/src/hir.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,15 @@ impl Attribute {
13041304
Attribute::Unparsed(_) => false,
13051305
}
13061306
}
1307+
1308+
pub fn is_prefix_attr_for_suggestions(&self) -> bool {
1309+
match self {
1310+
Attribute::Unparsed(attr) => attr.span.desugaring_kind().is_none(),
1311+
// Other parsed attributes that can appear on expressions originate from source and
1312+
// should make suggestions treat the expression like a prefixed form.
1313+
Attribute::Parsed(_) => true,
1314+
}
1315+
}
13071316
}
13081317

13091318
impl AttributeExt for Attribute {

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,7 @@ use crate::{
5757
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5858
pub(crate) fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
5959
let has_attr = |id: HirId| -> bool {
60-
for attr in self.tcx.hir_attrs(id) {
61-
// For the purpose of rendering suggestions, disregard attributes
62-
// that originate from desugaring of any kind. For example, `x?`
63-
// desugars to `#[allow(unreachable_code)] match ...`. Failing to
64-
// ignore the prefix attribute in the desugaring would cause this
65-
// suggestion:
66-
//
67-
// let y: u32 = x?.try_into().unwrap();
68-
// ++++++++++++++++++++
69-
//
70-
// to be rendered as:
71-
//
72-
// let y: u32 = (x?).try_into().unwrap();
73-
// + +++++++++++++++++++++
74-
if attr.span().desugaring_kind().is_none() {
75-
return true;
76-
}
77-
}
78-
false
60+
self.tcx.hir_attrs(id).iter().any(hir::Attribute::is_prefix_attr_for_suggestions)
7961
};
8062

8163
// Special case: range expressions are desugared to struct literals in HIR,

compiler/rustc_lint/src/context.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -845,12 +845,7 @@ impl<'tcx> LateContext<'tcx> {
845845
/// be used for pretty-printing HIR by rustc_hir_pretty.
846846
pub fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
847847
let has_attr = |id: hir::HirId| -> bool {
848-
for attr in self.tcx.hir_attrs(id) {
849-
if attr.span().desugaring_kind().is_none() {
850-
return true;
851-
}
852-
}
853-
false
848+
self.tcx.hir_attrs(id).iter().any(hir::Attribute::is_prefix_attr_for_suggestions)
854849
};
855850
expr.precedence(&has_attr)
856851
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
let _x = 30;
3+
#[cfg_attr(, (cc))] //~ ERROR expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `,`
4+
_x //~ ERROR mismatched types
5+
}
6+
7+
fn inline_case() {
8+
let _x = 30;
9+
#[inline] //~ ERROR `#[inline]` attribute cannot be used on expressions
10+
_x //~ ERROR mismatched types
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found `,`
2+
--> $DIR/cfg-attr-parsed-span-issue-154801.rs:3:16
3+
|
4+
LL | #[cfg_attr(, (cc))]
5+
| ^
6+
|
7+
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
8+
help: must be of the form
9+
|
10+
LL - #[cfg_attr(, (cc))]
11+
LL + #[cfg_attr(predicate, attr1, attr2, ...)]
12+
|
13+
14+
error: `#[inline]` attribute cannot be used on expressions
15+
--> $DIR/cfg-attr-parsed-span-issue-154801.rs:9:5
16+
|
17+
LL | #[inline]
18+
| ^^^^^^^^^
19+
|
20+
= help: `#[inline]` can only be applied to functions
21+
22+
error[E0308]: mismatched types
23+
--> $DIR/cfg-attr-parsed-span-issue-154801.rs:4:5
24+
|
25+
LL | fn main() {
26+
| - expected `()` because of default return type
27+
...
28+
LL | _x
29+
| ^^ expected `()`, found integer
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/cfg-attr-parsed-span-issue-154801.rs:10:5
33+
|
34+
LL | fn inline_case() {
35+
| - help: try adding a return type: `-> i32`
36+
...
37+
LL | _x
38+
| ^^ expected `()`, found integer
39+
40+
error: aborting due to 4 previous errors
41+
42+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)