Skip to content

Commit 28d74a9

Browse files
committed
Shrink binding span.
1 parent fea7b59 commit 28d74a9

11 files changed

+302
-439
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -879,25 +879,33 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
879879
hir::PatKind::Binding(.., name, Some(sub)) => (*name, sub),
880880
_ => return,
881881
};
882-
let binding_span = pat.span.with_hi(name.span.hi());
882+
let mk_span = |pat_span, ident_span: Span| {
883+
if let Some(ident_span) = ident_span.find_ancestor_inside(pat_span) {
884+
pat_span.with_hi(ident_span.hi())
885+
} else {
886+
pat_span
887+
}
888+
};
889+
let binding_span = mk_span(pat.span, name.span);
883890

884891
let typeck_results = cx.typeck_results;
885892
let sess = cx.tcx.sess;
886893

887894
// Get the binding move, extract the mutability if by-ref.
888-
let mut_outer = match typeck_results.extract_binding_mode(sess, pat.hir_id, pat.span) {
895+
let mut_outer = match typeck_results.extract_binding_mode(sess, pat.hir_id, binding_span) {
889896
Some(ty::BindByValue(_)) if is_binding_by_move(cx, pat.hir_id) => {
890897
// We have `x @ pat` where `x` is by-move. Reject all borrows in `pat`.
891898
let mut conflicts_ref = Vec::new();
892-
sub.each_binding(|_, hir_id, span, _| {
899+
sub.each_binding(|_, hir_id, span, ident| {
900+
let span = mk_span(span, ident.span);
893901
match typeck_results.extract_binding_mode(sess, hir_id, span) {
894902
Some(ty::BindByValue(_)) | None => {}
895903
Some(ty::BindByReference(_)) => conflicts_ref.push(span),
896904
}
897905
});
898906
if !conflicts_ref.is_empty() {
899907
sess.emit_err(BorrowOfMovedValue {
900-
span: pat.span,
908+
span: binding_span,
901909
binding_span,
902910
conflicts_ref,
903911
name,
@@ -920,6 +928,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
920928
let mut conflicts_mut_mut = Vec::new();
921929
let mut conflicts_mut_ref = Vec::new();
922930
sub.each_binding(|_, hir_id, span, name| {
931+
let span = mk_span(span, name.span);
923932
match typeck_results.extract_binding_mode(sess, hir_id, span) {
924933
Some(ty::BindByReference(mut_inner)) => match (mut_outer, mut_inner) {
925934
// Both sides are `ref`.
@@ -957,20 +966,20 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa
957966
// Report errors if any.
958967
if report_mut_mut {
959968
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
960-
sess.emit_err(MultipleMutBorrows { span: pat.span, occurences });
969+
sess.emit_err(MultipleMutBorrows { span: binding_span, occurences });
961970
} else if report_mut_ref {
962971
// Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse.
963972
match mut_outer {
964973
Mutability::Mut => {
965-
sess.emit_err(AlreadyMutBorrowed { span: pat.span, occurences });
974+
sess.emit_err(AlreadyMutBorrowed { span: binding_span, occurences });
966975
}
967976
Mutability::Not => {
968-
sess.emit_err(AlreadyBorrowed { span: pat.span, occurences });
977+
sess.emit_err(AlreadyBorrowed { span: binding_span, occurences });
969978
}
970979
};
971980
} else if report_move_conflict {
972981
// Report by-ref and by-move conflicts, e.g. `ref x @ y`.
973-
sess.emit_err(MovedWhileBorrowed { span: pat.span, occurences });
982+
sess.emit_err(MovedWhileBorrowed { span: binding_span, occurences });
974983
}
975984
}
976985

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ error: cannot borrow value as mutable because it is also borrowed as immutable
22
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:36:9
33
|
44
LL | ref foo @ [.., ref mut bar] => (),
5-
| -------^^^^^^^^-----------^
6-
| | |
7-
| | value is mutably borrowed by `bar` here
5+
| ^^^^^^^ ----------- value is mutably borrowed by `bar` here
6+
| |
87
| value is borrowed by `foo` here
98

109
error: cannot borrow value as mutable because it is also borrowed as immutable
1110
--> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:120:9
1211
|
1312
LL | ref foo @ Some(box ref mut s) => (),
14-
| -------^^^^^^^^^^^^---------^
15-
| | |
16-
| | value is mutably borrowed by `s` here
13+
| ^^^^^^^ --------- value is mutably borrowed by `s` here
14+
| |
1715
| value is borrowed by `foo` here
1816

1917
error[E0382]: borrow of moved value: `x`

tests/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ error: cannot move out of value because it is borrowed
22
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:12:14
33
|
44
LL | Some(ref _y @ _z) => {}
5-
| ------^^^--
6-
| | |
7-
| | value is moved into `_z` here
5+
| ^^^^^^ -- value is moved into `_z` here
6+
| |
87
| value is borrowed by `_y` here
98

109
error: borrow of moved value
1110
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:19:14
1211
|
1312
LL | Some(_z @ ref _y) => {}
14-
| --^^^------
15-
| | |
16-
| | value borrowed here after move
13+
| ^^ ------ value borrowed here after move
14+
| |
1715
| value moved into `_z` here
1816
| move occurs because `_z` has type `X` which does not implement the `Copy` trait
1917
|
@@ -26,18 +24,16 @@ error: cannot move out of value because it is borrowed
2624
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:26:14
2725
|
2826
LL | Some(ref mut _y @ _z) => {}
29-
| ----------^^^--
30-
| | |
31-
| | value is moved into `_z` here
27+
| ^^^^^^^^^^ -- value is moved into `_z` here
28+
| |
3229
| value is mutably borrowed by `_y` here
3330

3431
error: borrow of moved value
3532
--> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:33:14
3633
|
3734
LL | Some(_z @ ref mut _y) => {}
38-
| --^^^----------
39-
| | |
40-
| | value borrowed here after move
35+
| ^^ ---------- value borrowed here after move
36+
| |
4137
| value moved into `_z` here
4238
| move occurs because `_z` has type `X` which does not implement the `Copy` trait
4339
|

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,64 @@ error: cannot move out of value because it is borrowed
22
--> $DIR/borrowck-pat-at-and-box.rs:31:9
33
|
44
LL | let ref a @ box b = Box::new(NC);
5-
| -----^^^^^^^-
6-
| | |
7-
| | value is moved into `b` here
5+
| ^^^^^ - value is moved into `b` here
6+
| |
87
| value is borrowed by `a` here
98

109
error: cannot borrow value as mutable because it is also borrowed as immutable
1110
--> $DIR/borrowck-pat-at-and-box.rs:34:9
1211
|
1312
LL | let ref a @ box ref mut b = Box::new(nc());
14-
| -----^^^^^^^---------
15-
| | |
16-
| | value is mutably borrowed by `b` here
13+
| ^^^^^ --------- value is mutably borrowed by `b` here
14+
| |
1715
| value is borrowed by `a` here
1816

1917
error: cannot borrow value as mutable because it is also borrowed as immutable
2018
--> $DIR/borrowck-pat-at-and-box.rs:36:9
2119
|
2220
LL | let ref a @ box ref mut b = Box::new(NC);
23-
| -----^^^^^^^---------
24-
| | |
25-
| | value is mutably borrowed by `b` here
21+
| ^^^^^ --------- value is mutably borrowed by `b` here
22+
| |
2623
| value is borrowed by `a` here
2724

2825
error: cannot borrow value as mutable because it is also borrowed as immutable
2926
--> $DIR/borrowck-pat-at-and-box.rs:38:9
3027
|
3128
LL | let ref a @ box ref mut b = Box::new(NC);
32-
| -----^^^^^^^---------
33-
| | |
34-
| | value is mutably borrowed by `b` here
29+
| ^^^^^ --------- value is mutably borrowed by `b` here
30+
| |
3531
| value is borrowed by `a` here
3632

3733
error: cannot borrow value as mutable because it is also borrowed as immutable
3834
--> $DIR/borrowck-pat-at-and-box.rs:42:9
3935
|
4036
LL | let ref a @ box ref mut b = Box::new(NC);
41-
| -----^^^^^^^---------
42-
| | |
43-
| | value is mutably borrowed by `b` here
37+
| ^^^^^ --------- value is mutably borrowed by `b` here
38+
| |
4439
| value is borrowed by `a` here
4540

4641
error: cannot borrow value as immutable because it is also borrowed as mutable
4742
--> $DIR/borrowck-pat-at-and-box.rs:48:9
4843
|
4944
LL | let ref mut a @ box ref b = Box::new(NC);
50-
| ---------^^^^^^^-----
51-
| | |
52-
| | value is borrowed by `b` here
45+
| ^^^^^^^^^ ----- value is borrowed by `b` here
46+
| |
5347
| value is mutably borrowed by `a` here
5448

5549
error: cannot borrow value as immutable because it is also borrowed as mutable
5650
--> $DIR/borrowck-pat-at-and-box.rs:62:9
5751
|
5852
LL | ref mut a @ box ref b => {
59-
| ---------^^^^^^^-----
60-
| | |
61-
| | value is borrowed by `b` here
53+
| ^^^^^^^^^ ----- value is borrowed by `b` here
54+
| |
6255
| value is mutably borrowed by `a` here
6356

6457
error: cannot borrow value as immutable because it is also borrowed as mutable
6558
--> $DIR/borrowck-pat-at-and-box.rs:54:11
6659
|
6760
LL | fn f5(ref mut a @ box ref b: Box<NC>) {
68-
| ---------^^^^^^^-----
69-
| | |
70-
| | value is borrowed by `b` here
61+
| ^^^^^^^^^ ----- value is borrowed by `b` here
62+
| |
7163
| value is mutably borrowed by `a` here
7264

7365
error[E0382]: borrow of moved value

tests/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref-inverse-promotion.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error: borrow of moved value
22
--> $DIR/borrowck-pat-by-move-and-ref-inverse-promotion.rs:6:9
33
|
44
LL | let a @ ref b = U;
5-
| -^^^-----
6-
| | |
7-
| | value borrowed here after move
5+
| ^ ----- value borrowed here after move
6+
| |
87
| value moved into `a` here
98
| move occurs because `a` has type `U` which does not implement the `Copy` trait
109
|

0 commit comments

Comments
 (0)