Skip to content

Commit 6bf6c29

Browse files
authoredFeb 23, 2025
Unrolled build for rust-lang#137435
Rollup merge of rust-lang#137435 - estebank:match-arm-2, r=compiler-errors Fix "missing match arm body" suggestion involving `!` Include the match arm guard in the gated span, so that the suggestion to add a body is correct instead of inserting the body before the guard. Make the suggestion verbose. ``` error: `match` arm with no body --> $DIR/feature-gate-never_patterns.rs:43:9 | LL | Some(_) if false, | ^^^^^^^^^^^^^^^^ | help: add a body after the pattern | LL | Some(_) if false => { todo!() }, | ++++++++++++++ ``` r? `@compiler-errors`
2 parents bb2cc59 + a8f8b8d commit 6bf6c29

File tree

5 files changed

+94
-16
lines changed

5 files changed

+94
-16
lines changed
 

‎compiler/rustc_ast_passes/src/errors.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,14 @@ pub(crate) struct NegativeBoundWithParentheticalNotation {
804804
pub(crate) struct MatchArmWithNoBody {
805805
#[primary_span]
806806
pub span: Span,
807-
#[suggestion(code = " => todo!(),", applicability = "has-placeholders")]
807+
// We include the braces around `todo!()` so that a comma is optional, and we don't have to have
808+
// any logic looking at the arm being replaced if there was a comma already or not for the
809+
// resulting code to be correct.
810+
#[suggestion(
811+
code = " => {{ todo!() }}",
812+
applicability = "has-placeholders",
813+
style = "verbose"
814+
)]
808815
pub suggestion: Span,
809816
}
810817

‎compiler/rustc_parse/src/parser/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3125,10 +3125,11 @@ impl<'a> Parser<'a> {
31253125
let mut result = if armless {
31263126
// A pattern without a body, allowed for never patterns.
31273127
arm_body = None;
3128+
let span = lo.to(this.prev_token.span);
31283129
this.expect_one_of(&[exp!(Comma)], &[exp!(CloseBrace)]).map(|x| {
31293130
// Don't gate twice
31303131
if !pat.contains_never_pattern() {
3131-
this.psess.gated_spans.gate(sym::never_patterns, pat.span);
3132+
this.psess.gated_spans.gate(sym::never_patterns, span);
31323133
}
31333134
x
31343135
})

‎tests/ui/feature-gates/feature-gate-never_patterns.stderr

+30-5
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,34 @@ error: `match` arm with no body
5858
--> $DIR/feature-gate-never_patterns.rs:38:9
5959
|
6060
LL | Some(_)
61-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
61+
| ^^^^^^^
62+
|
63+
help: add a body after the pattern
64+
|
65+
LL | Some(_) => { todo!() }
66+
| ++++++++++++++
6267

6368
error: `match` arm with no body
6469
--> $DIR/feature-gate-never_patterns.rs:43:9
6570
|
6671
LL | Some(_) if false,
67-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
72+
| ^^^^^^^^^^^^^^^^
73+
|
74+
help: add a body after the pattern
75+
|
76+
LL | Some(_) if false => { todo!() },
77+
| ++++++++++++++
6878

6979
error: `match` arm with no body
7080
--> $DIR/feature-gate-never_patterns.rs:45:9
7181
|
7282
LL | Some(_) if false
73-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
83+
| ^^^^^^^^^^^^^^^^
84+
|
85+
help: add a body after the pattern
86+
|
87+
LL | Some(_) if false => { todo!() }
88+
| ++++++++++++++
7489

7590
error[E0658]: `!` patterns are experimental
7691
--> $DIR/feature-gate-never_patterns.rs:50:13
@@ -96,13 +111,23 @@ error: `match` arm with no body
96111
--> $DIR/feature-gate-never_patterns.rs:64:9
97112
|
98113
LL | Some(_)
99-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
114+
| ^^^^^^^
115+
|
116+
help: add a body after the pattern
117+
|
118+
LL | Some(_) => { todo!() }
119+
| ++++++++++++++
100120

101121
error: `match` arm with no body
102122
--> $DIR/feature-gate-never_patterns.rs:70:9
103123
|
104124
LL | Some(_) if false
105-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
125+
| ^^^^^^^^^^^^^^^^
126+
|
127+
help: add a body after the pattern
128+
|
129+
LL | Some(_) if false => { todo!() }
130+
| ++++++++++++++
106131

107132
error: a guard on a never pattern will never be run
108133
--> $DIR/feature-gate-never_patterns.rs:54:19

‎tests/ui/parser/macro/macro-expand-to-match-arm.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ error: `match` arm with no body
1414
--> $DIR/macro-expand-to-match-arm.rs:14:9
1515
|
1616
LL | arm!(None => {}),
17-
| ^^^^^^^^^^^^^^^^- help: add a body after the pattern: `=> todo!(),`
17+
| ^^^^^^^^^^^^^^^^
18+
|
19+
help: add a body after the pattern
20+
|
21+
LL | arm!(None => {}) => { todo!() },
22+
| ++++++++++++++
1823

1924
error: aborting due to 2 previous errors
2025

‎tests/ui/parser/match-arm-without-body.stderr

+48-8
Original file line numberDiff line numberDiff line change
@@ -68,49 +68,89 @@ error: `match` arm with no body
6868
--> $DIR/match-arm-without-body.rs:7:9
6969
|
7070
LL | Some(_)
71-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
71+
| ^^^^^^^
72+
|
73+
help: add a body after the pattern
74+
|
75+
LL | Some(_) => { todo!() }
76+
| ++++++++++++++
7277

7378
error: `match` arm with no body
7479
--> $DIR/match-arm-without-body.rs:30:9
7580
|
7681
LL | Some(_) if true
77-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
82+
| ^^^^^^^^^^^^^^^
83+
|
84+
help: add a body after the pattern
85+
|
86+
LL | Some(_) if true => { todo!() }
87+
| ++++++++++++++
7888

7989
error: `match` arm with no body
8090
--> $DIR/match-arm-without-body.rs:40:9
8191
|
8292
LL | Some(_) if true,
83-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
93+
| ^^^^^^^^^^^^^^^
94+
|
95+
help: add a body after the pattern
96+
|
97+
LL | Some(_) if true => { todo!() },
98+
| ++++++++++++++
8499

85100
error: `match` arm with no body
86101
--> $DIR/match-arm-without-body.rs:45:9
87102
|
88103
LL | Some(_) if true,
89-
| ^^^^^^^- help: add a body after the pattern: `=> todo!(),`
104+
| ^^^^^^^^^^^^^^^
105+
|
106+
help: add a body after the pattern
107+
|
108+
LL | Some(_) if true => { todo!() },
109+
| ++++++++++++++
90110

91111
error: `match` arm with no body
92112
--> $DIR/match-arm-without-body.rs:51:9
93113
|
94114
LL | pat!()
95-
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
115+
| ^^^^^^
116+
|
117+
help: add a body after the pattern
118+
|
119+
LL | pat!() => { todo!() }
120+
| ++++++++++++++
96121

97122
error: `match` arm with no body
98123
--> $DIR/match-arm-without-body.rs:56:9
99124
|
100125
LL | pat!(),
101-
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
126+
| ^^^^^^
127+
|
128+
help: add a body after the pattern
129+
|
130+
LL | pat!() => { todo!() },
131+
| ++++++++++++++
102132

103133
error: `match` arm with no body
104134
--> $DIR/match-arm-without-body.rs:61:9
105135
|
106136
LL | pat!() if true,
107-
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
137+
| ^^^^^^^^^^^^^^
138+
|
139+
help: add a body after the pattern
140+
|
141+
LL | pat!() if true => { todo!() },
142+
| ++++++++++++++
108143

109144
error: `match` arm with no body
110145
--> $DIR/match-arm-without-body.rs:72:9
111146
|
112147
LL | pat!(),
113-
| ^^^^^^- help: add a body after the pattern: `=> todo!(),`
148+
| ^^^^^^
149+
|
150+
help: add a body after the pattern
151+
|
152+
LL | pat!() => { todo!() },
153+
| ++++++++++++++
114154

115155
error: aborting due to 13 previous errors
116156

0 commit comments

Comments
 (0)
Please sign in to comment.