Skip to content

Commit 5d94d42

Browse files
committed
Shrink span for bindings with subpatterns.
1 parent 380addd commit 5d94d42

31 files changed

+190
-236
lines changed

compiler/rustc_mir_build/src/build/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10361036
VarBindingForm {
10371037
binding_mode,
10381038
opt_ty_info,
1039-
opt_match_place: Some((Some(place), span)),
1039+
opt_match_place: Some((None, span)),
10401040
pat_span: span,
10411041
},
10421042
)))))

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
202202

203203
fn lower_pattern_unadjusted(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tcx>> {
204204
let mut ty = self.typeck_results.node_type(pat.hir_id);
205+
let mut span = pat.span;
205206

206207
let kind = match pat.kind {
207208
hir::PatKind::Wild => PatKind::Wild,
@@ -262,6 +263,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
262263
}
263264

264265
hir::PatKind::Binding(_, id, ident, ref sub) => {
266+
if let Some(ident_span) = ident.span.find_ancestor_inside(span) {
267+
span = span.with_hi(ident_span.hi());
268+
}
269+
265270
let bm = *self
266271
.typeck_results
267272
.pat_binding_modes()
@@ -326,7 +331,7 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
326331
hir::PatKind::Or(ref pats) => PatKind::Or { pats: self.lower_patterns(pats) },
327332
};
328333

329-
Box::new(Pat { span: pat.span, ty, kind })
334+
Box::new(Pat { span, ty, kind })
330335
}
331336

332337
fn lower_tuple_subpats(

src/test/mir-opt/exponential_or.match_tuple.SimplifyCfg-initial.after.mir

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
88
let mut _4: bool; // in scope 0 at $DIR/exponential-or.rs:+2:70: +2:77
99
let mut _5: bool; // in scope 0 at $DIR/exponential-or.rs:+2:62: +2:67
1010
let mut _6: bool; // in scope 0 at $DIR/exponential-or.rs:+2:62: +2:67
11-
let _7: u32; // in scope 0 at $DIR/exponential-or.rs:+2:10: +2:21
12-
let _8: u32; // in scope 0 at $DIR/exponential-or.rs:+2:57: +2:78
11+
let _7: u32; // in scope 0 at $DIR/exponential-or.rs:+2:10: +2:11
12+
let _8: u32; // in scope 0 at $DIR/exponential-or.rs:+2:57: +2:58
1313
let mut _9: u32; // in scope 0 at $DIR/exponential-or.rs:+2:83: +2:84
1414
let mut _10: u32; // in scope 0 at $DIR/exponential-or.rs:+2:87: +2:88
1515
scope 1 {
16-
debug y => _7; // in scope 1 at $DIR/exponential-or.rs:+2:10: +2:21
17-
debug z => _8; // in scope 1 at $DIR/exponential-or.rs:+2:57: +2:78
16+
debug y => _7; // in scope 1 at $DIR/exponential-or.rs:+2:10: +2:11
17+
debug z => _8; // in scope 1 at $DIR/exponential-or.rs:+2:57: +2:58
1818
}
1919

2020
bb0: {
@@ -61,10 +61,10 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
6161
}
6262

6363
bb9: {
64-
StorageLive(_7); // scope 0 at $DIR/exponential-or.rs:+2:10: +2:21
65-
_7 = (_1.0: u32); // scope 0 at $DIR/exponential-or.rs:+2:10: +2:21
66-
StorageLive(_8); // scope 0 at $DIR/exponential-or.rs:+2:57: +2:78
67-
_8 = (_1.3: u32); // scope 0 at $DIR/exponential-or.rs:+2:57: +2:78
64+
StorageLive(_7); // scope 0 at $DIR/exponential-or.rs:+2:10: +2:11
65+
_7 = (_1.0: u32); // scope 0 at $DIR/exponential-or.rs:+2:10: +2:11
66+
StorageLive(_8); // scope 0 at $DIR/exponential-or.rs:+2:57: +2:58
67+
_8 = (_1.3: u32); // scope 0 at $DIR/exponential-or.rs:+2:57: +2:58
6868
StorageLive(_9); // scope 1 at $DIR/exponential-or.rs:+2:83: +2:84
6969
_9 = _7; // scope 1 at $DIR/exponential-or.rs:+2:83: +2:84
7070
StorageLive(_10); // scope 1 at $DIR/exponential-or.rs:+2:87: +2:88

src/test/mir-opt/uniform_array_move_out.move_out_by_subslice.mir_map.0.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ fn move_out_by_subslice() -> () {
1515
let mut _11: std::boxed::Box<i32>; // in scope 0 at $DIR/uniform_array_move_out.rs:+1:21: +1:26
1616
scope 1 {
1717
debug a => _1; // in scope 1 at $DIR/uniform_array_move_out.rs:+1:9: +1:10
18-
let _12: [std::boxed::Box<i32>; 2]; // in scope 1 at $DIR/uniform_array_move_out.rs:+2:10: +2:17
18+
let _12: [std::boxed::Box<i32>; 2]; // in scope 1 at $DIR/uniform_array_move_out.rs:+2:10: +2:12
1919
scope 4 {
20-
debug _y => _12; // in scope 4 at $DIR/uniform_array_move_out.rs:+2:10: +2:17
20+
debug _y => _12; // in scope 4 at $DIR/uniform_array_move_out.rs:+2:10: +2:12
2121
}
2222
}
2323
scope 2 {
@@ -77,8 +77,8 @@ fn move_out_by_subslice() -> () {
7777
bb6: {
7878
StorageDead(_2); // scope 0 at $DIR/uniform_array_move_out.rs:+1:26: +1:27
7979
FakeRead(ForLet(None), _1); // scope 0 at $DIR/uniform_array_move_out.rs:+1:9: +1:10
80-
StorageLive(_12); // scope 1 at $DIR/uniform_array_move_out.rs:+2:10: +2:17
81-
_12 = move _1[0..2]; // scope 1 at $DIR/uniform_array_move_out.rs:+2:10: +2:17
80+
StorageLive(_12); // scope 1 at $DIR/uniform_array_move_out.rs:+2:10: +2:12
81+
_12 = move _1[0..2]; // scope 1 at $DIR/uniform_array_move_out.rs:+2:10: +2:12
8282
_0 = const (); // scope 0 at $DIR/uniform_array_move_out.rs:+0:27: +3:2
8383
drop(_12) -> [return: bb7, unwind: bb9]; // scope 1 at $DIR/uniform_array_move_out.rs:+3:1: +3:2
8484
}

src/test/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL | fn bindings_after_at_slice_patterns_move_binding(x: [String; 4]) {
2323
| - move occurs because `x` has type `[String; 4]`, which does not implement the `Copy` trait
2424
LL | match x {
2525
LL | a @ [.., _] => (),
26-
| ----------- value moved here
26+
| - value moved here
2727
...
2828
LL | &x;
2929
| ^^ value borrowed here after move
@@ -32,7 +32,7 @@ error[E0502]: cannot borrow `x` as immutable because it is also borrowed as muta
3232
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:28:5
3333
|
3434
LL | ref mut foo @ [.., _] => Some(foo),
35-
| --------------------- mutable borrow occurs here
35+
| ----------- mutable borrow occurs here
3636
...
3737
LL | &x;
3838
| ^^ immutable borrow occurs here
@@ -44,7 +44,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
4444
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:50:5
4545
|
4646
LL | [ref foo @ .., ref bar] => Some(foo),
47-
| ------------ immutable borrow occurs here
47+
| ------- immutable borrow occurs here
4848
...
4949
LL | &mut x;
5050
| ^^^^^^ mutable borrow occurs here
@@ -56,7 +56,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
5656
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:62:5
5757
|
5858
LL | ref foo @ [.., ref bar] => Some(foo),
59-
| ----------------------- immutable borrow occurs here
59+
| ------- immutable borrow occurs here
6060
...
6161
LL | &mut x;
6262
| ^^^^^^ mutable borrow occurs here
@@ -71,7 +71,7 @@ LL | fn bindings_after_at_or_patterns_move(x: Option<Test>) {
7171
| - move occurs because `x` has type `Option<Test>`, which does not implement the `Copy` trait
7272
LL | match x {
7373
LL | foo @ Some(Test::Foo | Test::Bar) => (),
74-
| ---------------------------------
74+
| ---
7575
| |
7676
| value moved here
7777
| value moved here
@@ -83,7 +83,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
8383
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:86:5
8484
|
8585
LL | ref foo @ Some(Test::Foo | Test::Bar) => Some(foo),
86-
| ------------------------------------- immutable borrow occurs here
86+
| ------- immutable borrow occurs here
8787
...
8888
LL | &mut x;
8989
| ^^^^^^ mutable borrow occurs here
@@ -95,7 +95,7 @@ error[E0502]: cannot borrow `x` as immutable because it is also borrowed as muta
9595
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:98:5
9696
|
9797
LL | ref mut foo @ Some(Test::Foo | Test::Bar) => Some(foo),
98-
| ----------------------------------------- mutable borrow occurs here
98+
| ----------- mutable borrow occurs here
9999
...
100100
LL | &x;
101101
| ^^ immutable borrow occurs here
@@ -107,7 +107,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
107107
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:112:5
108108
|
109109
LL | ref foo @ Some(box ref s) => Some(foo),
110-
| ------------------------- immutable borrow occurs here
110+
| ------- immutable borrow occurs here
111111
...
112112
LL | &mut x;
113113
| ^^^^^^ mutable borrow occurs here
@@ -122,7 +122,7 @@ LL | fn bindings_after_at_slice_patterns_or_patterns_moves(x: [Option<Test>; 4])
122122
| - move occurs because `x` has type `[Option<Test>; 4]`, which does not implement the `Copy` trait
123123
LL | match x {
124124
LL | a @ [.., Some(Test::Foo | Test::Bar)] => (),
125-
| -------------------------------------
125+
| -
126126
| |
127127
| value moved here
128128
| value moved here
@@ -134,7 +134,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
134134
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:144:5
135135
|
136136
LL | ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(a),
137-
| ------------------------------------------------- immutable borrow occurs here
137+
| ----- immutable borrow occurs here
138138
...
139139
LL | &mut x;
140140
| ^^^^^^ mutable borrow occurs here
@@ -146,7 +146,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
146146
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:156:5
147147
|
148148
LL | ref a @ [ref b @ .., Some(Test::Foo | Test::Bar)] => Some(b),
149-
| ---------- immutable borrow occurs here
149+
| ----- immutable borrow occurs here
150150
...
151151
LL | &mut x;
152152
| ^^^^^^ mutable borrow occurs here
@@ -158,7 +158,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
158158
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:170:5
159159
|
160160
LL | [_, ref a @ Some(box ref b), ..] => Some(a),
161-
| ----------------------- immutable borrow occurs here
161+
| ----- immutable borrow occurs here
162162
...
163163
LL | &mut x;
164164
| ^^^^^^ mutable borrow occurs here
@@ -170,7 +170,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
170170
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:186:5
171171
|
172172
LL | [_, ref a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
173-
| ------------------------------------------- immutable borrow occurs here
173+
| ----- immutable borrow occurs here
174174
...
175175
LL | &mut x;
176176
| ^^^^^^ mutable borrow occurs here
@@ -182,7 +182,7 @@ error[E0502]: cannot borrow `x` as immutable because it is also borrowed as muta
182182
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:200:5
183183
|
184184
LL | [_, ref mut a @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
185-
| ----------------------------------------------- mutable borrow occurs here
185+
| --------- mutable borrow occurs here
186186
...
187187
LL | &x;
188188
| ^^ immutable borrow occurs here
@@ -194,7 +194,7 @@ error[E0502]: cannot borrow `x` as mutable because it is also borrowed as immuta
194194
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:214:5
195195
|
196196
LL | ref a @ [_, ref b @ Some(box Test::Foo | box Test::Bar), ..] => Some(a),
197-
| ------------------------------------------------------------ immutable borrow occurs here
197+
| ----- immutable borrow occurs here
198198
...
199199
LL | &mut x;
200200
| ^^^^^^ mutable borrow occurs here

src/test/ui/borrowck/borrowck-describe-lvalue.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ LL | let x = &mut v;
200200
| ------ borrow of `v` occurs here
201201
LL | match v {
202202
LL | &[x @ ..] => println!("{:?}", x),
203-
| ^^^^^^ use of borrowed `v`
203+
| ^ use of borrowed `v`
204204
...
205205
LL | drop(x);
206206
| - borrow later used here
@@ -212,7 +212,7 @@ LL | let x = &mut v;
212212
| ------ borrow of `v` occurs here
213213
...
214214
LL | &[_, x @ ..] => println!("{:?}", x),
215-
| ^^^^^^ use of borrowed `v`
215+
| ^ use of borrowed `v`
216216
...
217217
LL | drop(x);
218218
| - borrow later used here
@@ -224,7 +224,7 @@ LL | let x = &mut v;
224224
| ------ borrow of `v` occurs here
225225
...
226226
LL | &[x @ .., _] => println!("{:?}", x),
227-
| ^^^^^^ use of borrowed `v`
227+
| ^ use of borrowed `v`
228228
...
229229
LL | drop(x);
230230
| - borrow later used here
@@ -236,7 +236,7 @@ LL | let x = &mut v;
236236
| ------ borrow of `v` occurs here
237237
...
238238
LL | &[_, x @ .., _] => println!("{:?}", x),
239-
| ^^^^^^ use of borrowed `v`
239+
| ^ use of borrowed `v`
240240
...
241241
LL | drop(x);
242242
| - borrow later used here

src/test/ui/borrowck/borrowck-move-out-from-array-match.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ error[E0382]: use of moved value: `a[..].0`
7979
--> $DIR/borrowck-move-out-from-array-match.rs:89:11
8080
|
8181
LL | [_y @ .., _, _] => {}
82-
| ------- value moved here
82+
| -- value moved here
8383
...
8484
LL | [(_x, _), _, _] => {}
8585
| ^^ value used here after move
@@ -90,7 +90,7 @@ error[E0382]: use of moved value: `a[..].0`
9090
--> $DIR/borrowck-move-out-from-array-match.rs:99:15
9191
|
9292
LL | [_, _, _y @ ..] => {}
93-
| ------- value moved here
93+
| -- value moved here
9494
...
9595
LL | [.., (_x, _)] => {}
9696
| ^^ value used here after move
@@ -101,7 +101,7 @@ error[E0382]: use of partially moved value: `a`
101101
--> $DIR/borrowck-move-out-from-array-match.rs:110:11
102102
|
103103
LL | [x @ .., _] => {}
104-
| ------ value partially moved here
104+
| - value partially moved here
105105
LL | }
106106
LL | match a {
107107
| ^ value used here after partial move

src/test/ui/borrowck/borrowck-move-out-from-array-no-overlap-match.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ error[E0382]: use of partially moved value: `a`
6868
--> $DIR/borrowck-move-out-from-array-no-overlap-match.rs:85:11
6969
|
7070
LL | [_, _y @ ..] => {}
71-
| ------- value partially moved here
71+
| -- value partially moved here
7272
LL | }
7373
LL | match a {
7474
| ^ value used here after partial move
@@ -79,7 +79,7 @@ error[E0382]: use of partially moved value: `a`
7979
--> $DIR/borrowck-move-out-from-array-no-overlap-match.rs:96:11
8080
|
8181
LL | [_y @ .., _] => {}
82-
| ------- value partially moved here
82+
| -- value partially moved here
8383
LL | }
8484
LL | match a {
8585
| ^ value used here after partial move
@@ -90,7 +90,7 @@ error[E0382]: use of partially moved value: `a`
9090
--> $DIR/borrowck-move-out-from-array-no-overlap-match.rs:109:11
9191
|
9292
LL | [x @ .., _, _] => {}
93-
| ------ value partially moved here
93+
| - value partially moved here
9494
LL | }
9595
LL | match a {
9696
| ^ value used here after partial move

src/test/ui/borrowck/borrowck-move-out-from-array-use-match.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ error[E0382]: borrow of moved value: `a[..]`
7979
--> $DIR/borrowck-move-out-from-array-use-match.rs:89:11
8080
|
8181
LL | [_y @ .., _, _] => {}
82-
| ------- value moved here
82+
| -- value moved here
8383
...
8484
LL | [(ref _x, _), _, _] => {}
8585
| ^^^^^^ value borrowed here after move
@@ -90,7 +90,7 @@ error[E0382]: borrow of moved value: `a[..]`
9090
--> $DIR/borrowck-move-out-from-array-use-match.rs:99:15
9191
|
9292
LL | [_, _, _y @ ..] => {}
93-
| ------- value moved here
93+
| -- value moved here
9494
...
9595
LL | [.., (ref _x, _)] => {}
9696
| ^^^^^^ value borrowed here after move
@@ -101,7 +101,7 @@ error[E0382]: use of partially moved value: `a`
101101
--> $DIR/borrowck-move-out-from-array-use-match.rs:110:11
102102
|
103103
LL | [x @ .., _] => {}
104-
| ------ value partially moved here
104+
| - value partially moved here
105105
LL | }
106106
LL | match a {
107107
| ^ value used here after partial move
@@ -134,7 +134,7 @@ error[E0382]: use of partially moved value: `a`
134134
--> $DIR/borrowck-move-out-from-array-use-match.rs:139:5
135135
|
136136
LL | [_, _, _x @ ..] => {}
137-
| ------- value partially moved here
137+
| -- value partially moved here
138138
LL | }
139139
LL | a[0] = Default::default();
140140
| ^^^^ value used here after partial move
@@ -145,7 +145,7 @@ error[E0382]: use of partially moved value: `a`
145145
--> $DIR/borrowck-move-out-from-array-use-match.rs:147:5
146146
|
147147
LL | [_, _, _x @ ..] => {}
148-
| ------- value partially moved here
148+
| -- value partially moved here
149149
LL | }
150150
LL | a[0].1 = Default::default();
151151
| ^^^^ value used here after partial move

src/test/ui/borrowck/borrowck-move-out-from-array-use-no-overlap-match.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ error[E0382]: use of partially moved value: `a`
6868
--> $DIR/borrowck-move-out-from-array-use-no-overlap-match.rs:85:11
6969
|
7070
LL | [_, _y @ ..] => {}
71-
| ------- value partially moved here
71+
| -- value partially moved here
7272
LL | }
7373
LL | match a {
7474
| ^ value used here after partial move
@@ -79,7 +79,7 @@ error[E0382]: use of partially moved value: `a`
7979
--> $DIR/borrowck-move-out-from-array-use-no-overlap-match.rs:96:11
8080
|
8181
LL | [_y @ .., _] => {}
82-
| ------- value partially moved here
82+
| -- value partially moved here
8383
LL | }
8484
LL | match a {
8585
| ^ value used here after partial move
@@ -90,7 +90,7 @@ error[E0382]: use of partially moved value: `a`
9090
--> $DIR/borrowck-move-out-from-array-use-no-overlap-match.rs:109:11
9191
|
9292
LL | [x @ .., _, _] => {}
93-
| ------ value partially moved here
93+
| - value partially moved here
9494
LL | }
9595
LL | match a {
9696
| ^ value used here after partial move

0 commit comments

Comments
 (0)