Skip to content

Commit 7c677f8

Browse files
committed
When lowering paren surrounded pattern, keep outer span
Parentheses surrounding patterns are not ketp in the HIR (and are warned against in the AST). In order to avoid having some suggestions break, we keep the outer span (including parentheses) when lowering the patterns. ``` error: a never pattern is always unreachable --> $DIR/ICE-133117-duplicate-never-arm.rs:9:26 | LL | (!|!) if true => {} | ^^ this will never be executed | help: remove the match arm expression | LL - (!|!) if true => {} LL + (!|!), | ``` ``` error[E0308]: mismatched types --> $DIR/well-typed-edition-2024.rs:135:15 | LL | let [&mut &(mut x)] = &mut [&0]; | ^^^^^^^^ --------- this expression has type `&mut [&{integer}; 1]` | | | expected integer, found `&_` | = note: expected type `{integer}` found reference `&_` help: consider removing `&` from the pattern | LL - let [&mut &(mut x)] = &mut [&0]; LL + let [&mut (mut x)] = &mut [&0]; | ```
1 parent d8e961e commit 7c677f8

16 files changed

+45
-31
lines changed

compiler/rustc_ast_lowering/src/expr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
689689
removal_span: if pat.span.eq_ctxt(arm.span) {
690690
// - ! => {}
691691
// + !,
692-
// FIXME: account for `(!|!)`, as pat.span ends *within* the parentheses.
693692
pat.span.shrink_to_hi().with_hi(arm.span.hi())
694693
} else {
695694
// Subtly incorrect, but close enough if macros are involved.

compiler/rustc_ast_lowering/src/pat.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2020
self.arena.alloc(self.lower_pat_mut(pattern))
2121
}
2222

23-
fn lower_pat_mut(&mut self, mut pattern: &Pat) -> hir::Pat<'hir> {
23+
fn lower_pat_mut(&mut self, outer_pattern: &Pat) -> hir::Pat<'hir> {
24+
let mut pattern = outer_pattern;
2425
ensure_sufficient_stack(|| {
2526
// loop here to avoid recursion
2627
let pat_hir_id = self.lower_node_id(pattern.id);
@@ -147,7 +148,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
147148
}
148149
};
149150

150-
self.pat_with_node_id_of(pattern, node, pat_hir_id)
151+
self.pat_with_node_id_of(outer_pattern, node, pat_hir_id)
151152
})
152153
}
153154

tests/ui/asm/naked-functions.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ LL | &b: &i32,
8989
| ^^
9090

9191
error: patterns not allowed in naked function parameters
92-
--> $DIR/naked-functions.rs:29:6
92+
--> $DIR/naked-functions.rs:29:5
9393
|
9494
LL | (None | Some(_)): Option<std::ptr::NonNull<u8>>,
95-
| ^^^^^^^^^^^^^^
95+
| ^^^^^^^^^^^^^^^^
9696

9797
error: patterns not allowed in naked function parameters
9898
--> $DIR/naked-functions.rs:31:5

tests/ui/closures/2229_closure_analysis/bad-pattern.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ LL | let _0 = v1;
1717
| +
1818

1919
error[E0005]: refutable pattern in local binding
20-
--> $DIR/bad-pattern.rs:14:14
20+
--> $DIR/bad-pattern.rs:14:13
2121
|
2222
LL | let (0 | 1) = v1;
23-
| ^^^^^ pattern `2_u32..=u32::MAX` not covered
23+
| ^^^^^^^ pattern `2_u32..=u32::MAX` not covered
2424
|
2525
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
2626
= note: for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html

tests/ui/or-patterns/issue-69875-should-have-been-expanded-earlier-non-exhaustive.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0005]: refutable pattern in local binding
2-
--> $DIR/issue-69875-should-have-been-expanded-earlier-non-exhaustive.rs:2:10
2+
--> $DIR/issue-69875-should-have-been-expanded-earlier-non-exhaustive.rs:2:9
33
|
44
LL | let (0 | (1 | 2)) = 0;
5-
| ^^^^^^^^^^^ patterns `i32::MIN..=-1_i32` and `3_i32..=i32::MAX` not covered
5+
| ^^^^^^^^^^^^^ patterns `i32::MIN..=-1_i32` and `3_i32..=i32::MAX` not covered
66
|
77
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
88
= note: for more information, visit https://doc.rust-lang.org/book/ch19-02-refutability.html

tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ error: cannot borrow value as mutable more than once at a time
1010
--> $DIR/borrowck-pat-ref-mut-and-ref.rs:33:9
1111
|
1212
LL | let ref mut a @ (ref b @ ref mut c) = u(); // sub-in-sub
13-
| ^^^^^^^^^ ----- --------- value is mutably borrowed by `c` here
14-
| | |
15-
| | value is borrowed by `b` here
13+
| ^^^^^^^^^ ------ --------- value is mutably borrowed by `c` here
14+
| | |
15+
| | value is borrowed by `b` here
1616
| value is mutably borrowed by `a` here
1717

1818
error: cannot borrow value as mutable because it is also borrowed as immutable
19-
--> $DIR/borrowck-pat-ref-mut-and-ref.rs:33:22
19+
--> $DIR/borrowck-pat-ref-mut-and-ref.rs:33:21
2020
|
2121
LL | let ref mut a @ (ref b @ ref mut c) = u(); // sub-in-sub
22-
| ^^^^^ --------- value is mutably borrowed by `c` here
23-
| |
24-
| value is borrowed by `b` here
22+
| ^^^^^^ --------- value is mutably borrowed by `c` here
23+
| |
24+
| value is borrowed by `b` here
2525

2626
error: cannot borrow value as mutable because it is also borrowed as immutable
2727
--> $DIR/borrowck-pat-ref-mut-and-ref.rs:37:9

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.classic2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ LL | let [&mut &(mut x)] = &[&mut 0];
211211
help: consider removing `&` from the pattern
212212
|
213213
LL - let [&mut &(mut x)] = &[&mut 0];
214-
LL + let [&mut mut x)] = &[&mut 0];
214+
LL + let [&mut (mut x)] = &[&mut 0];
215215
|
216216

217217
error: aborting due to 14 previous errors

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.stable2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ LL | let [&mut &(mut x)] = &[&mut 0];
276276
help: consider removing `&` from the pattern
277277
|
278278
LL - let [&mut &(mut x)] = &[&mut 0];
279-
LL + let [&mut mut x)] = &[&mut 0];
279+
LL + let [&mut (mut x)] = &[&mut 0];
280280
|
281281

282282
error: aborting due to 21 previous errors

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ LL | let [&mut &(mut x)] = &[&mut 0];
302302
help: consider removing `&` from the pattern
303303
|
304304
LL - let [&mut &(mut x)] = &[&mut 0];
305-
LL + let [&mut mut x)] = &[&mut 0];
305+
LL + let [&mut (mut x)] = &[&mut 0];
306306
|
307307

308308
error: aborting due to 21 previous errors

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/pattern-errors.structural2024.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,20 @@ LL + let &[&mut x] = &mut &mut [0];
142142
|
143143

144144
error[E0658]: binding cannot be both mutable and by-reference
145-
--> $DIR/pattern-errors.rs:102:12
145+
--> $DIR/pattern-errors.rs:102:11
146146
|
147147
LL | let [&(mut x)] = &[&0];
148-
| ^^^^
148+
| ^^^^^
149149
|
150150
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
151151
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
152152
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
153153

154154
error[E0658]: binding cannot be both mutable and by-reference
155-
--> $DIR/pattern-errors.rs:107:12
155+
--> $DIR/pattern-errors.rs:107:11
156156
|
157157
LL | let [&(mut x)] = &mut [&0];
158-
| ^^^^
158+
| ^^^^^
159159
|
160160
= note: see issue #123076 <https://github.com/rust-lang/rust/issues/123076> for more information
161161
= help: add `#![feature(mut_ref)]` to the crate attributes to enable

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.classic2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ LL | let [&mut &(mut x)] = &mut [&0];
107107
help: consider removing `&` from the pattern
108108
|
109109
LL - let [&mut &(mut x)] = &mut [&0];
110-
LL + let [&mut mut x)] = &mut [&0];
110+
LL + let [&mut (mut x)] = &mut [&0];
111111
|
112112

113113
error[E0596]: cannot borrow data in a `&` reference as mutable

tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/well-typed-edition-2024.structural2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ LL | let [&mut &(mut x)] = &mut [&0];
107107
help: consider removing `&` from the pattern
108108
|
109109
LL - let [&mut &(mut x)] = &mut [&0];
110-
LL + let [&mut mut x)] = &mut [&0];
110+
LL + let [&mut (mut x)] = &mut [&0];
111111
|
112112

113113
error[E0596]: cannot borrow data in a `&` reference as mutable

tests/ui/rfcs/rfc-0000-never_patterns/ICE-133063-never-arm-no-otherwise-block.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | (!|!) if false => {}
77
help: remove the match arm expression
88
|
99
LL - (!|!) if false => {}
10-
LL + (!|!,
10+
LL + (!|!),
1111
|
1212

1313
error: aborting due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ run-rustfix
2+
#![feature(never_type, never_patterns)]
3+
#![allow(incomplete_features, dead_code, unused_parens, unreachable_patterns)]
4+
5+
enum Void {}
6+
7+
fn foo(x: Void) {
8+
match x {
9+
(!|!), //~ ERROR a never pattern is always unreachable
10+
(!|!), //~ ERROR a never pattern is always unreachable
11+
}
12+
}
13+
14+
fn main() {}

tests/ui/rfcs/rfc-0000-never_patterns/ICE-133117-duplicate-never-arm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#![feature(never_type)]
2-
#![feature(never_patterns)]
3-
#![allow(incomplete_features)]
1+
//@ run-rustfix
2+
#![feature(never_type, never_patterns)]
3+
#![allow(incomplete_features, dead_code, unused_parens, unreachable_patterns)]
44

55
enum Void {}
66

tests/ui/rfcs/rfc-0000-never_patterns/ICE-133117-duplicate-never-arm.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | (!|!) if true => {}
77
help: remove the match arm expression
88
|
99
LL - (!|!) if true => {}
10-
LL + (!|!,
10+
LL + (!|!),
1111
|
1212

1313
error: a never pattern is always unreachable
@@ -19,7 +19,7 @@ LL | (!|!) if true => {}
1919
help: remove the match arm expression
2020
|
2121
LL - (!|!) if true => {}
22-
LL + (!|!,
22+
LL + (!|!),
2323
|
2424

2525
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)