Skip to content

Commit 71637c2

Browse files
committed
Use better spans for dummy accesses used in matches
1 parent 45b48b9 commit 71637c2

10 files changed

+85
-134
lines changed

src/librustc_mir/build/matches/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
6363
// injects a borrow of the matched input, which should have the same effect
6464
// as eddyb's hack. Once NLL is the default, we can remove the hack.
6565

66-
let dummy_source_info = self.source_info(span);
66+
let dummy_source_info = self.source_info(discriminant_span);
6767
let dummy_access = Rvalue::Discriminant(discriminant_place.clone());
6868
let dummy_ty = dummy_access.ty(&self.local_decls, tcx);
6969
let dummy_temp = self.temp(dummy_ty, dummy_source_info.span);
7070
self.cfg.push_assign(block, dummy_source_info, &dummy_temp, dummy_access);
7171

72-
let source_info = self.source_info(span);
72+
let source_info = self.source_info(discriminant_span);
7373
let borrowed_input_temp = if tcx.generate_borrow_of_any_match_input() {
7474
// The region is unknown at this point; we rely on NLL
7575
// inference to find an appropriate one. Therefore you can
@@ -134,9 +134,9 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
134134
// This should ensure that you cannot change
135135
// the variant for an enum while you are in
136136
// the midst of matching on it.
137-
137+
let pattern_source_info = self.source_info(pattern.span);
138138
self.cfg.push(*pre_binding_block, Statement {
139-
source_info,
139+
source_info: pattern_source_info,
140140
kind: StatementKind::ReadForMatch(borrow_temp.clone()),
141141
});
142142
}

src/test/ui/borrowck/issue-41962.stderr

+10-14
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,22 @@ LL | if let Some(thing) = maybe {
1717
= note: move occurs because the value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
1818

1919
error[E0382]: use of moved value: `maybe` (Mir)
20-
--> $DIR/issue-41962.rs:17:9
20+
--> $DIR/issue-41962.rs:17:30
2121
|
22-
LL | if let Some(thing) = maybe {
23-
| ^ ----- value moved here
24-
| _________|
25-
| |
26-
LL | | }
27-
| |_________^ value used here after move
22+
LL | if let Some(thing) = maybe {
23+
| ----- ^^^^^ value used here after move
24+
| |
25+
| value moved here
2826
|
2927
= note: move occurs because value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
3028

3129
error[E0382]: borrow of moved value: `maybe` (Mir)
32-
--> $DIR/issue-41962.rs:17:9
30+
--> $DIR/issue-41962.rs:17:30
3331
|
34-
LL | if let Some(thing) = maybe {
35-
| ^ ----- value moved here
36-
| _________|
37-
| |
38-
LL | | }
39-
| |_________^ value borrowed here after move
32+
LL | if let Some(thing) = maybe {
33+
| ----- ^^^^^ value borrowed here after move
34+
| |
35+
| value moved here
4036
|
4137
= note: move occurs because value has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
4238

src/test/ui/issue-17385.nll.stderr

+20-32
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
11
error[E0382]: use of moved value: `foo`
2-
--> $DIR/issue-17385.rs:28:5
2+
--> $DIR/issue-17385.rs:28:11
33
|
4-
LL | drop(foo);
5-
| --- value moved here
6-
LL | / match foo { //~ ERROR use of moved value
7-
LL | | X(1) => (),
8-
LL | | _ => unreachable!()
9-
LL | | }
10-
| |_____^ value used here after move
4+
LL | drop(foo);
5+
| --- value moved here
6+
LL | match foo { //~ ERROR use of moved value
7+
| ^^^ value used here after move
118
|
129
= note: move occurs because `foo` has type `X`, which does not implement the `Copy` trait
1310

1411
error[E0382]: borrow of moved value: `foo`
15-
--> $DIR/issue-17385.rs:28:5
12+
--> $DIR/issue-17385.rs:28:11
1613
|
17-
LL | drop(foo);
18-
| --- value moved here
19-
LL | / match foo { //~ ERROR use of moved value
20-
LL | | X(1) => (),
21-
LL | | _ => unreachable!()
22-
LL | | }
23-
| |_____^ value borrowed here after move
14+
LL | drop(foo);
15+
| --- value moved here
16+
LL | match foo { //~ ERROR use of moved value
17+
| ^^^ value borrowed here after move
2418
|
2519
= note: move occurs because `foo` has type `X`, which does not implement the `Copy` trait
2620

@@ -36,28 +30,22 @@ LL | X(1) => (),
3630
= note: move occurs because `foo` has type `X`, which does not implement the `Copy` trait
3731

3832
error[E0382]: use of moved value: `e`
39-
--> $DIR/issue-17385.rs:35:5
33+
--> $DIR/issue-17385.rs:35:11
4034
|
41-
LL | drop(e);
42-
| - value moved here
43-
LL | / match e { //~ ERROR use of moved value
44-
LL | | Enum::Variant1 => unreachable!(),
45-
LL | | Enum::Variant2 => ()
46-
LL | | }
47-
| |_____^ value used here after move
35+
LL | drop(e);
36+
| - value moved here
37+
LL | match e { //~ ERROR use of moved value
38+
| ^ value used here after move
4839
|
4940
= note: move occurs because `e` has type `Enum`, which does not implement the `Copy` trait
5041

5142
error[E0382]: borrow of moved value: `e`
52-
--> $DIR/issue-17385.rs:35:5
43+
--> $DIR/issue-17385.rs:35:11
5344
|
54-
LL | drop(e);
55-
| - value moved here
56-
LL | / match e { //~ ERROR use of moved value
57-
LL | | Enum::Variant1 => unreachable!(),
58-
LL | | Enum::Variant2 => ()
59-
LL | | }
60-
| |_____^ value borrowed here after move
45+
LL | drop(e);
46+
| - value moved here
47+
LL | match e { //~ ERROR use of moved value
48+
| ^ value borrowed here after move
6149
|
6250
= note: move occurs because `e` has type `Enum`, which does not implement the `Copy` trait
6351

src/test/ui/issue-27282-move-match-input-into-guard.stderr

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
error[E0505]: cannot move out of `b` because it is borrowed
22
--> $DIR/issue-27282-move-match-input-into-guard.rs:26:16
33
|
4-
LL | match b {
5-
| _____-
6-
| |_____|
7-
| ||
8-
LL | || &mut false => {},
9-
LL | || _ if { (|| { let bar = b; *bar = false; })();
10-
| || ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move out of `b` occurs here
11-
LL | || //~^ ERROR cannot move out of `b` because it is borrowed [E0505]
12-
... ||
13-
LL | || _ => panic!("surely we could never get here, since rustc warns it is unreachable."),
14-
LL | || }
15-
| || -
16-
| ||_____|
17-
| |______borrow of `b` occurs here
18-
| borrow later used here
4+
LL | match b {
5+
| - borrow of `b` occurs here
6+
LL | &mut false => {},
7+
LL | _ if { (|| { let bar = b; *bar = false; })();
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move out of `b` occurs here
9+
...
10+
LL | &mut true => { println!("You might think we should get here"); },
11+
| --------- borrow later used here
1912

2013
error[E0382]: use of moved value: `*b`
2114
--> $DIR/issue-27282-move-match-input-into-guard.rs:29:14

src/test/ui/issue-27282-mutate-before-diverging-arm-1.stderr

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
error[E0500]: closure requires unique access to `x` but it is already borrowed
22
--> $DIR/issue-27282-mutate-before-diverging-arm-1.rs:33:14
33
|
4-
LL | match x {
5-
| _____-
6-
| |_____|
7-
| ||
8-
LL | || &mut None => panic!("unreachable"),
9-
LL | || &mut Some(&_) if {
10-
LL | || // ForceFnOnce needed to exploit #27282
11-
LL | || (|| { *x = None; drop(force_fn_once); })();
12-
| || ^^ - borrow occurs due to use of `x` in closure
13-
| || |
14-
| || closure construction occurs here
15-
... ||
16-
LL | || _ => panic!("unreachable"),
17-
LL | || }
18-
| || -
19-
| ||_____|
20-
| |______borrow occurs here
21-
| borrow later used here
4+
LL | match x {
5+
| - borrow occurs here
6+
...
7+
LL | (|| { *x = None; drop(force_fn_once); })();
8+
| ^^ - borrow occurs due to use of `x` in closure
9+
| |
10+
| closure construction occurs here
11+
...
12+
LL | &mut Some(&a) if { // this binds to garbage if we've corrupted discriminant
13+
| ------------- borrow later used here
2214

2315
error: aborting due to previous error
2416

src/test/ui/issue-27282-mutate-before-diverging-arm-2.stderr

+10-19
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
error[E0500]: closure requires unique access to `x` but it is already borrowed
22
--> $DIR/issue-27282-mutate-before-diverging-arm-2.rs:38:18
33
|
4-
LL | match x {
5-
| _____-
6-
| |_____|
7-
| ||
8-
LL | || &mut None => panic!("unreachable"),
9-
LL | || &mut Some(&_)
10-
LL | || if {
11-
LL | || // ForceFnOnce needed to exploit #27282
12-
LL | || (|| { *x = None; drop(force_fn_once); })();
13-
| || ^^ - borrow occurs due to use of `x` in closure
14-
| || |
15-
| || closure construction occurs here
16-
... ||
17-
LL | || _ => panic!("unreachable"),
18-
LL | || }
19-
| || -
20-
| ||_____|
21-
| |______borrow occurs here
22-
| borrow later used here
4+
LL | match x {
5+
| - borrow occurs here
6+
...
7+
LL | (|| { *x = None; drop(force_fn_once); })();
8+
| ^^ - borrow occurs due to use of `x` in closure
9+
| |
10+
| closure construction occurs here
11+
...
12+
LL | &mut Some(&2)
13+
| ------------- borrow later used here
2314

2415
error: aborting due to previous error
2516

src/test/ui/issue-47412.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error[E0133]: access to union field is unsafe and requires unsafe function or block
2-
--> $DIR/issue-47412.rs:21:5
2+
--> $DIR/issue-47412.rs:21:11
33
|
44
LL | match u.void {}
5-
| ^^^^^^^^^^^^^^^ access to union field
5+
| ^^^^^^ access to union field
66
|
77
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
88

99
error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block
10-
--> $DIR/issue-47412.rs:27:5
10+
--> $DIR/issue-47412.rs:27:11
1111
|
1212
LL | match *ptr {}
13-
| ^^^^^^^^^^^^^ dereference of raw pointer
13+
| ^^^^ dereference of raw pointer
1414
|
1515
= note: raw pointers may be NULL, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior
1616

src/test/ui/lifetime-errors/ex1-return-one-existing-name-early-bound-in-struct.nll.stderr

+5-11
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ LL | other //~ ERROR explicit lifetime
55
| ^^^^^
66

77
error[E0621]: explicit lifetime required in the type of `other`
8-
--> $DIR/ex1-return-one-existing-name-early-bound-in-struct.rs:18:9
8+
--> $DIR/ex1-return-one-existing-name-early-bound-in-struct.rs:18:15
99
|
10-
LL | fn bar(&self, other: Foo) -> Foo<'a> {
11-
| ----- consider changing the type of `other` to `Foo<'a>`
12-
LL | / match *self {
13-
LL | | Foo::Bar(s) => {
14-
LL | | if s == "test" {
15-
LL | | other //~ ERROR explicit lifetime
16-
... |
17-
LL | | }
18-
LL | | }
19-
| |_________^ lifetime `'a` required
10+
LL | fn bar(&self, other: Foo) -> Foo<'a> {
11+
| ----- consider changing the type of `other` to `Foo<'a>`
12+
LL | match *self {
13+
| ^^^^^ lifetime `'a` required
2014

2115
error: aborting due to previous error
2216

src/test/ui/nll/borrowed-match-issue-45045.stderr

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
error[E0503]: cannot use `e` because it was mutably borrowed
2-
--> $DIR/borrowed-match-issue-45045.rs:24:5
2+
--> $DIR/borrowed-match-issue-45045.rs:24:11
33
|
4-
LL | let f = &mut e;
5-
| ------ borrow of `e` occurs here
6-
LL | let g = f;
7-
LL | / match e { //~ cannot use `e` because it was mutably borrowed [E0503]
8-
LL | | Xyz::A => println!("a"),
9-
LL | | //~^ cannot use `e` because it was mutably borrowed [E0503]
10-
LL | | Xyz::B => println!("b"),
11-
LL | | };
12-
| |_____^ use of borrowed `e`
13-
LL | *g = Xyz::B;
14-
| ----------- borrow later used here
4+
LL | let f = &mut e;
5+
| ------ borrow of `e` occurs here
6+
LL | let g = f;
7+
LL | match e { //~ cannot use `e` because it was mutably borrowed [E0503]
8+
| ^ use of borrowed `e`
9+
...
10+
LL | *g = Xyz::B;
11+
| ----------- borrow later used here
1512

1613
error[E0503]: cannot use `e` because it was mutably borrowed
1714
--> $DIR/borrowed-match-issue-45045.rs:25:9

src/test/ui/span/issue-40157.nll.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ error[E0597]: `foo` does not live long enough
22
--> $DIR/issue-40157.rs:12:53
33
|
44
LL | {println!("{:?}", match { let foo = vec![1, 2]; foo.get(1) } { x => x });}
5-
| ------------------------------^^^--------------------
6-
| | | |
7-
| | | `foo` dropped here while still borrowed
8-
| | borrowed value does not live long enough
9-
| borrow later used here
5+
| ------------------------^^^---------
6+
| | | |
7+
| | | `foo` dropped here while still borrowed
8+
| | borrowed value does not live long enough
9+
| borrow later used here
1010

1111
error: aborting due to previous error
1212

0 commit comments

Comments
 (0)