Skip to content

Commit ee6a649

Browse files
Rollup merge of #126101 - lqd:revert-124099, r=wesleywiser
Revert "Disallow ambiguous attributes on expressions" on nightly As discussed in [today's t-compiler meeting](https://rust-lang.zulipchat.com/#narrow/stream/238009-t-compiler.2Fmeetings/topic/.5Bweekly.5D.202024-06-06/near/443079505), this reverts PR #124099 to fix P-critical beta regressions #125199. r? `@wesleywiser` Opening as draft so that `@wesleywiser` and `@apiraino,` you can tell me whether you wanted: 1. a `beta-accepted` revert of #124099 on nightly (this PR)? That will need to be backported to beta (even though #126093 may be the last of those) 2. a revert of #124099 on beta? 3. all of the above? I also opened #126102, another draft PR to revert #124099 on beta, should you choose options 2 or 3.
2 parents 37ff3dc + 216424d commit ee6a649

15 files changed

+49
-141
lines changed

compiler/rustc_parse/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,6 @@ parse_or_pattern_not_allowed_in_let_binding = top-level or-patterns are not allo
622622
parse_out_of_range_hex_escape = out of range hex escape
623623
.label = must be a character in the range [\x00-\x7f]
624624
625-
parse_outer_attr_ambiguous = ambiguous outer attributes
626-
627625
parse_outer_attr_explanation = outer attributes, like `#[test]`, annotate the item following them
628626
629627
parse_outer_attribute_not_allowed_on_if_else = outer attributes are not allowed on `if` and `else` branches

compiler/rustc_parse/src/errors.rs

-9
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,6 @@ pub(crate) struct OuterAttributeNotAllowedOnIfElse {
495495
pub attributes: Span,
496496
}
497497

498-
#[derive(Diagnostic)]
499-
#[diag(parse_outer_attr_ambiguous)]
500-
pub(crate) struct AmbiguousOuterAttributes {
501-
#[primary_span]
502-
pub span: Span,
503-
#[subdiagnostic]
504-
pub sugg: WrapInParentheses,
505-
}
506-
507498
#[derive(Diagnostic)]
508499
#[diag(parse_missing_in_in_for_loop)]
509500
pub(crate) struct MissingInInForLoop {

compiler/rustc_parse/src/parser/expr.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,7 @@ impl<'a> Parser<'a> {
328328
this.parse_expr_assoc_with(prec + prec_adjustment, LhsExpr::NotYetParsed)
329329
})?;
330330

331-
self.error_ambiguous_outer_attrs(&lhs, lhs_span, rhs.span);
332-
let span = lhs_span.to(rhs.span);
333-
331+
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
334332
lhs = match op {
335333
AssocOp::Add
336334
| AssocOp::Subtract
@@ -429,18 +427,6 @@ impl<'a> Parser<'a> {
429427
});
430428
}
431429

432-
fn error_ambiguous_outer_attrs(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) {
433-
if let Some(attr) = lhs.attrs.iter().find(|a| a.style == AttrStyle::Outer) {
434-
self.dcx().emit_err(errors::AmbiguousOuterAttributes {
435-
span: attr.span.to(rhs_span),
436-
sugg: errors::WrapInParentheses::Expression {
437-
left: attr.span.shrink_to_lo(),
438-
right: lhs_span.shrink_to_hi(),
439-
},
440-
});
441-
}
442-
}
443-
444430
/// Possibly translate the current token to an associative operator.
445431
/// The method does not advance the current token.
446432
///
@@ -520,8 +506,7 @@ impl<'a> Parser<'a> {
520506
None
521507
};
522508
let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span);
523-
self.error_ambiguous_outer_attrs(&lhs, lhs.span, rhs_span);
524-
let span = lhs.span.to(rhs_span);
509+
let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
525510
let limits =
526511
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
527512
let range = self.mk_range(Some(lhs), rhs, limits);
@@ -739,8 +724,7 @@ impl<'a> Parser<'a> {
739724
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
740725
) -> PResult<'a, P<Expr>> {
741726
let mk_expr = |this: &mut Self, lhs: P<Expr>, rhs: P<Ty>| {
742-
this.error_ambiguous_outer_attrs(&lhs, lhs_span, rhs.span);
743-
this.mk_expr(lhs_span.to(rhs.span), expr_kind(lhs, rhs))
727+
this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, rhs.span), expr_kind(lhs, rhs))
744728
};
745729

746730
// Save the state of the parser before parsing type normally, in case there is a
@@ -3858,6 +3842,16 @@ impl<'a> Parser<'a> {
38583842
self.mk_expr(span, ExprKind::Err(guar))
38593843
}
38603844

3845+
/// Create expression span ensuring the span of the parent node
3846+
/// is larger than the span of lhs and rhs, including the attributes.
3847+
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) -> Span {
3848+
lhs.attrs
3849+
.iter()
3850+
.find(|a| a.style == AttrStyle::Outer)
3851+
.map_or(lhs_span, |a| a.span)
3852+
.to(rhs_span)
3853+
}
3854+
38613855
fn collect_tokens_for_expr(
38623856
&mut self,
38633857
attrs: AttrWrapper,

src/tools/clippy/tests/ui/cfg_attr_rustfmt.fixed

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn foo(
1616

1717
fn skip_on_statements() {
1818
#[rustfmt::skip]
19-
{ 5+3; }
19+
5+3;
2020
}
2121

2222
#[rustfmt::skip]
@@ -33,11 +33,11 @@ mod foo {
3333
#[clippy::msrv = "1.29"]
3434
fn msrv_1_29() {
3535
#[cfg_attr(rustfmt, rustfmt::skip)]
36-
{ 1+29; }
36+
1+29;
3737
}
3838

3939
#[clippy::msrv = "1.30"]
4040
fn msrv_1_30() {
4141
#[rustfmt::skip]
42-
{ 1+30; }
42+
1+30;
4343
}

src/tools/clippy/tests/ui/cfg_attr_rustfmt.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn foo(
1616

1717
fn skip_on_statements() {
1818
#[cfg_attr(rustfmt, rustfmt::skip)]
19-
{ 5+3; }
19+
5+3;
2020
}
2121

2222
#[cfg_attr(rustfmt, rustfmt_skip)]
@@ -33,11 +33,11 @@ mod foo {
3333
#[clippy::msrv = "1.29"]
3434
fn msrv_1_29() {
3535
#[cfg_attr(rustfmt, rustfmt::skip)]
36-
{ 1+29; }
36+
1+29;
3737
}
3838

3939
#[clippy::msrv = "1.30"]
4040
fn msrv_1_30() {
4141
#[cfg_attr(rustfmt, rustfmt::skip)]
42-
{ 1+30; }
42+
1+30;
4343
}

src/tools/rustfmt/tests/source/attrib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ type Os = NoSource;
214214
// #3313
215215
fn stmt_expr_attributes() {
216216
let foo ;
217-
(#[must_use]
218-
foo) = false ;
217+
#[must_use]
218+
foo = false ;
219219
}
220220

221221
// #3509

src/tools/rustfmt/tests/target/attrib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ type Os = NoSource;
248248
// #3313
249249
fn stmt_expr_attributes() {
250250
let foo;
251-
(#[must_use]
252-
foo) = false;
251+
#[must_use]
252+
foo = false;
253253
}
254254

255255
// #3509

tests/pretty/ast-stmt-expr-attr.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ fn syntax() {
1313
let _ = #[attr] ();
1414
let _ = #[attr] (#[attr] 0,);
1515
let _ = #[attr] (#[attr] 0, 0);
16-
let _ = (#[attr] 0) + #[attr] 0;
17-
let _ = (#[attr] 0) / #[attr] 0;
18-
let _ = (#[attr] 0) & #[attr] 0;
19-
let _ = (#[attr] 0) % #[attr] 0;
16+
let _ = #[attr] 0 + #[attr] 0;
17+
let _ = #[attr] 0 / #[attr] 0;
18+
let _ = #[attr] 0 & #[attr] 0;
19+
let _ = #[attr] 0 % #[attr] 0;
2020
let _ = #[attr] (0 + 0);
2121
let _ = #[attr] !0;
2222
let _ = #[attr] -0;
2323
let _ = #[attr] false;
2424
let _ = #[attr] 0;
2525
let _ = #[attr] 'c';
26-
let _ = (#[attr] x) as Y;
26+
let _ = #[attr] x as Y;
2727
let _ = #[attr] (x as Y);
2828
let _ =
2929
#[attr] while true {
@@ -88,18 +88,18 @@ fn syntax() {
8888
let _ = ();
8989
foo
9090
};
91-
let _ = (#[attr] x) = y;
91+
let _ = #[attr] x = y;
9292
let _ = #[attr] (x = y);
93-
let _ = (#[attr] x) += y;
93+
let _ = #[attr] x += y;
9494
let _ = #[attr] (x += y);
9595
let _ = #[attr] foo.bar;
9696
let _ = (#[attr] foo).bar;
9797
let _ = #[attr] foo.0;
9898
let _ = (#[attr] foo).0;
9999
let _ = #[attr] foo[bar];
100100
let _ = (#[attr] foo)[bar];
101-
let _ = (#[attr] 0)..#[attr] 0;
102-
let _ = (#[attr] 0)..;
101+
let _ = #[attr] 0..#[attr] 0;
102+
let _ = #[attr] 0..;
103103
let _ = #[attr] (0..0);
104104
let _ = #[attr] (0..);
105105
let _ = #[attr] (..0);

tests/pretty/stmt_expr_attributes.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ fn _11() {
147147
let _ = #[rustc_dummy] (0);
148148
let _ = #[rustc_dummy] (0,);
149149
let _ = #[rustc_dummy] (0, 0);
150-
let _ = (#[rustc_dummy] 0) + #[rustc_dummy] 0;
150+
let _ = #[rustc_dummy] 0 + #[rustc_dummy] 0;
151151
let _ = #[rustc_dummy] !0;
152152
let _ = #[rustc_dummy] -0i32;
153153
let _ = #[rustc_dummy] false;
154154
let _ = #[rustc_dummy] 'c';
155155
let _ = #[rustc_dummy] 0;
156-
let _ = (#[rustc_dummy] 0) as usize;
156+
let _ = #[rustc_dummy] 0 as usize;
157157
let _ =
158158
#[rustc_dummy] while false {
159159
#![rustc_dummy]
@@ -208,8 +208,8 @@ fn _11() {
208208
};
209209
let const {} = #[rustc_dummy] const {};
210210
let mut x = 0;
211-
let _ = (#[rustc_dummy] x) = 15;
212-
let _ = (#[rustc_dummy] x) += 15;
211+
let _ = #[rustc_dummy] x = 15;
212+
let _ = #[rustc_dummy] x += 15;
213213
let s = Foo { data: () };
214214
let _ = #[rustc_dummy] s.data;
215215
let _ = (#[rustc_dummy] s).data;
@@ -219,8 +219,8 @@ fn _11() {
219219
let v = vec!(0);
220220
let _ = #[rustc_dummy] v[0];
221221
let _ = (#[rustc_dummy] v)[0];
222-
let _ = (#[rustc_dummy] 0)..#[rustc_dummy] 0;
223-
let _ = (#[rustc_dummy] 0)..;
222+
let _ = #[rustc_dummy] 0..#[rustc_dummy] 0;
223+
let _ = #[rustc_dummy] 0..;
224224
let _ = #[rustc_dummy] (0..0);
225225
let _ = #[rustc_dummy] (0..);
226226
let _ = #[rustc_dummy] (..0);

tests/ui/lint/unused/unused-doc-comments-edge-cases.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ fn doc_comment_between_if_else(num: u8) -> bool {
2020
}
2121

2222
fn doc_comment_on_expr(num: u8) -> bool {
23-
(/// useless doc comment
23+
/// useless doc comment
2424
//~^ ERROR: attributes on expressions are experimental
2525
//~| ERROR: unused doc comment
26-
num) == 3
26+
num == 3
2727
}
2828

2929
fn doc_comment_on_expr_field() -> bool {

tests/ui/lint/unused/unused-doc-comments-edge-cases.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | else {
55
| ^^^^ expected expression
66

77
error[E0658]: attributes on expressions are experimental
8-
--> $DIR/unused-doc-comments-edge-cases.rs:23:6
8+
--> $DIR/unused-doc-comments-edge-cases.rs:23:5
99
|
10-
LL | (/// useless doc comment
11-
| ^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | /// useless doc comment
11+
| ^^^^^^^^^^^^^^^^^^^^^^^
1212
|
1313
= note: see issue #15701 <https://github.com./rust-lang/rust/issues/15701> for more information
1414
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
@@ -32,12 +32,12 @@ LL | #![deny(unused_doc_comments)]
3232
| ^^^^^^^^^^^^^^^^^^^
3333

3434
error: unused doc comment
35-
--> $DIR/unused-doc-comments-edge-cases.rs:23:6
35+
--> $DIR/unused-doc-comments-edge-cases.rs:23:5
3636
|
37-
LL | (/// useless doc comment
38-
| ^^^^^^^^^^^^^^^^^^^^^^^
37+
LL | /// useless doc comment
38+
| ^^^^^^^^^^^^^^^^^^^^^^^
3939
...
40-
LL | num) == 3
40+
LL | num == 3
4141
| --- rustdoc does not generate documentation for expressions
4242
|
4343
= help: use `//` for a plain comment

tests/ui/parser/attribute/attr-binary-expr-ambigous.fixed

-15
This file was deleted.

tests/ui/parser/attribute/attr-binary-expr-ambigous.rs

-15
This file was deleted.

tests/ui/parser/attribute/attr-binary-expr-ambigous.stderr

-46
This file was deleted.

tests/ui/proc-macro/issue-81555.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ use test_macros::identity_attr;
1010
fn main() {
1111
let _x;
1212
let y = ();
13-
(#[identity_attr] _x) = y;
13+
#[identity_attr]
14+
_x = y;
1415
}

0 commit comments

Comments
 (0)