Skip to content

Commit 9309e2e

Browse files
committed
Use smaller span for adjustments on block expressions
1 parent a9fe312 commit 9309e2e

12 files changed

+81
-95
lines changed

src/librustc_mir/hair/cx/expr.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
7878
mut expr: Expr<'tcx>,
7979
adjustment: &Adjustment<'tcx>)
8080
-> Expr<'tcx> {
81-
let Expr { temp_lifetime, span, .. } = expr;
81+
let Expr { temp_lifetime, mut span, .. } = expr;
8282
let kind = match adjustment.kind {
8383
Adjust::ReifyFnPointer => {
8484
ExprKind::ReifyFnPointer { source: expr.to_ref() }
@@ -96,6 +96,25 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
9696
ExprKind::Cast { source: expr.to_ref() }
9797
}
9898
Adjust::Deref(None) => {
99+
// Adjust the span from the block, to the last expression of the
100+
// block. This is a better span when returning a mutable reference
101+
// with too short a lifetime. The error message will use the span
102+
// from the assignment to the return place, which should only point
103+
// at the returned value, not the entire function body.
104+
//
105+
// fn return_short_lived<'a>(x: &'a mut i32) -> &'static mut i32 {
106+
// x
107+
// // ^ error message points at this expression.
108+
// }
109+
//
110+
// We don't need to do this adjustment in the next match arm since
111+
// deref coercions always start with a built-in deref.
112+
if let ExprKind::Block { body } = expr.kind {
113+
if let Some(ref last_expr) = body.expr {
114+
span = last_expr.span;
115+
expr.span = span;
116+
}
117+
}
99118
ExprKind::Deref { arg: expr.to_ref() }
100119
}
101120
Adjust::Deref(Some(deref)) => {

src/test/ui/issues/issue-17718-const-bad-values.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ static mut S: usize = 3;
1515
const C2: &'static mut usize = unsafe { &mut S };
1616
//~^ ERROR: constants cannot refer to statics
1717
//~| ERROR: references in constants may only refer to immutable values
18-
//~| ERROR: references in constants may only refer to immutable values
1918

2019
fn main() {}

src/test/ui/issues/issue-17718-const-bad-values.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ error[E0017]: references in constants may only refer to immutable values
1616
LL | const C2: &'static mut usize = unsafe { &mut S };
1717
| ^^^^^^ constants require immutable values
1818

19-
error[E0017]: references in constants may only refer to immutable values
20-
--> $DIR/issue-17718-const-bad-values.rs:15:32
21-
|
22-
LL | const C2: &'static mut usize = unsafe { &mut S };
23-
| ^^^^^^^^^^^^^^^^^ constants require immutable values
24-
25-
error: aborting due to 4 previous errors
19+
error: aborting due to 3 previous errors
2620

2721
Some errors occurred: E0013, E0017.
2822
For more information about an error, try `rustc --explain E0013`.

src/test/ui/issues/issue-46471-1.stderr

+7-10
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ LL | }
1212
error[E0597]: `z` does not live long enough (Mir)
1313
--> $DIR/issue-46471-1.rs:16:9
1414
|
15-
LL | let y = {
16-
| _____________-
17-
LL | | let mut z = 0;
18-
LL | | &mut z
19-
| | ^^^^^^ borrowed value does not live long enough
20-
LL | | };
21-
| | -
22-
| | |
23-
| |_____`z` dropped here while still borrowed
24-
| borrow later used here
15+
LL | &mut z
16+
| ^^^^^^
17+
| |
18+
| borrowed value does not live long enough
19+
| borrow later used here
20+
LL | };
21+
| - `z` dropped here while still borrowed
2522

2623
error: aborting due to 2 previous errors
2724

src/test/ui/match/match-ref-mut-invariance.nll.stderr

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ LL | match self.0 { ref mut x => x } //~ ERROR mismatched types
55
| ^
66

77
error: unsatisfied lifetime constraints
8-
--> $DIR/match-ref-mut-invariance.rs:19:49
8+
--> $DIR/match-ref-mut-invariance.rs:20:9
99
|
10-
LL | impl<'b> S<'b> {
11-
| -- lifetime `'b` defined here
12-
LL | fn bar<'a>(&'a mut self) -> &'a mut &'a i32 {
13-
| ____________--___________________________________^
14-
| | |
15-
| | lifetime `'a` defined here
16-
LL | | match self.0 { ref mut x => x } //~ ERROR mismatched types
17-
LL | | }
18-
| |_____^ returning this value requires that `'a` must outlive `'b`
10+
LL | impl<'b> S<'b> {
11+
| -- lifetime `'b` defined here
12+
LL | fn bar<'a>(&'a mut self) -> &'a mut &'a i32 {
13+
| -- lifetime `'a` defined here
14+
LL | match self.0 { ref mut x => x } //~ ERROR mismatched types
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'b`
1916

2017
error: aborting due to previous error
2118

src/test/ui/match/match-ref-mut-let-invariance.nll.stderr

+8-11
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ LL | x //~ ERROR mismatched types
55
| ^
66

77
error: unsatisfied lifetime constraints
8-
--> $DIR/match-ref-mut-let-invariance.rs:19:49
8+
--> $DIR/match-ref-mut-let-invariance.rs:21:9
99
|
10-
LL | impl<'b> S<'b> {
11-
| -- lifetime `'b` defined here
12-
LL | fn bar<'a>(&'a mut self) -> &'a mut &'a i32 {
13-
| ____________--___________________________________^
14-
| | |
15-
| | lifetime `'a` defined here
16-
LL | | let ref mut x = self.0;
17-
LL | | x //~ ERROR mismatched types
18-
LL | | }
19-
| |_____^ returning this value requires that `'a` must outlive `'b`
10+
LL | impl<'b> S<'b> {
11+
| -- lifetime `'b` defined here
12+
LL | fn bar<'a>(&'a mut self) -> &'a mut &'a i32 {
13+
| -- lifetime `'a` defined here
14+
LL | let ref mut x = self.0;
15+
LL | x //~ ERROR mismatched types
16+
| ^ returning this value requires that `'a` must outlive `'b`
2017

2118
error: aborting due to previous error
2219

src/test/ui/nll/mir_check_cast_unsize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
use std::fmt::Debug;
1616

1717
fn bar<'a>(x: &'a u32) -> &'static dyn Debug {
18-
//~^ ERROR unsatisfied lifetime constraints
1918
x
20-
//~^ WARNING not reporting region error due to nll
19+
//~^ ERROR unsatisfied lifetime constraints
20+
//~| WARNING not reporting region error due to nll
2121
}
2222

2323
fn main() {}
+6-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
warning: not reporting region error due to nll
2-
--> $DIR/mir_check_cast_unsize.rs:19:5
2+
--> $DIR/mir_check_cast_unsize.rs:18:5
33
|
44
LL | x
55
| ^
66

77
error: unsatisfied lifetime constraints
8-
--> $DIR/mir_check_cast_unsize.rs:17:46
8+
--> $DIR/mir_check_cast_unsize.rs:18:5
99
|
10-
LL | fn bar<'a>(x: &'a u32) -> &'static dyn Debug {
11-
| ________--____________________________________^
12-
| | |
13-
| | lifetime `'a` defined here
14-
LL | | //~^ ERROR unsatisfied lifetime constraints
15-
LL | | x
16-
LL | | //~^ WARNING not reporting region error due to nll
17-
LL | | }
18-
| |_^ returning this value requires that `'a` must outlive `'static`
10+
LL | fn bar<'a>(x: &'a u32) -> &'static dyn Debug {
11+
| -- lifetime `'a` defined here
12+
LL | x
13+
| ^ returning this value requires that `'a` must outlive `'static`
1914

2015
error: aborting due to previous error
2116

src/test/ui/object-lifetime/object-lifetime-default-elision.nll.stderr

+8-13
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@ LL | ss
55
| ^^
66

77
error: unsatisfied lifetime constraints
8-
--> $DIR/object-lifetime-default-elision.rs:64:53
8+
--> $DIR/object-lifetime-default-elision.rs:81:5
99
|
10-
LL | fn load3<'a,'b>(ss: &'a SomeTrait) -> &'b SomeTrait {
11-
| __________--_--______________________________________^
12-
| | | |
13-
| | | lifetime `'b` defined here
14-
| | lifetime `'a` defined here
15-
LL | | // Under old rules, the fully elaborated types of input/output were:
16-
LL | | //
17-
LL | | // for<'a,'b,'c>fn(&'a (SomeTrait+'c)) -> &'b (SomeTrait+'a)
18-
... |
19-
LL | | //~| ERROR cannot infer
20-
LL | | }
21-
| |_^ returning this value requires that `'a` must outlive `'b`
10+
LL | fn load3<'a,'b>(ss: &'a SomeTrait) -> &'b SomeTrait {
11+
| -- -- lifetime `'b` defined here
12+
| |
13+
| lifetime `'a` defined here
14+
...
15+
LL | ss
16+
| ^^ returning this value requires that `'a` must outlive `'b`
2217

2318
error: aborting due to previous error
2419

src/test/ui/regions/regions-reborrow-from-shorter-mut-ref-mut-ref.nll.stderr

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ LL | &mut ***p //~ ERROR 14:5: 14:14: lifetime mismatch [E0623]
55
| ^^^^^^^^^
66

77
error: unsatisfied lifetime constraints
8-
--> $DIR/regions-reborrow-from-shorter-mut-ref-mut-ref.rs:13:85
8+
--> $DIR/regions-reborrow-from-shorter-mut-ref-mut-ref.rs:14:5
99
|
10-
LL | fn copy_borrowed_ptr<'a, 'b, 'c>(p: &'a mut &'b mut &'c mut isize) -> &'b mut isize {
11-
| ______________________--__--_________________________________________________________^
12-
| | | |
13-
| | | lifetime `'b` defined here
14-
| | lifetime `'a` defined here
15-
LL | | &mut ***p //~ ERROR 14:5: 14:14: lifetime mismatch [E0623]
16-
LL | | }
17-
| |_^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
10+
LL | fn copy_borrowed_ptr<'a, 'b, 'c>(p: &'a mut &'b mut &'c mut isize) -> &'b mut isize {
11+
| -- -- lifetime `'b` defined here
12+
| |
13+
| lifetime `'a` defined here
14+
LL | &mut ***p //~ ERROR 14:5: 14:14: lifetime mismatch [E0623]
15+
| ^^^^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
1816

1917
error: aborting due to previous error
2018

src/test/ui/regions/regions-reborrow-from-shorter-mut-ref.nll.stderr

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ LL | &mut **p //~ ERROR 16:5: 16:13: lifetime mismatch [E0623]
55
| ^^^^^^^^
66

77
error: unsatisfied lifetime constraints
8-
--> $DIR/regions-reborrow-from-shorter-mut-ref.rs:15:73
8+
--> $DIR/regions-reborrow-from-shorter-mut-ref.rs:16:5
99
|
10-
LL | fn copy_borrowed_ptr<'a, 'b>(p: &'a mut &'b mut isize) -> &'b mut isize {
11-
| ______________________--__--_____________________________________________^
12-
| | | |
13-
| | | lifetime `'b` defined here
14-
| | lifetime `'a` defined here
15-
LL | | &mut **p //~ ERROR 16:5: 16:13: lifetime mismatch [E0623]
16-
LL | | }
17-
| |_^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
10+
LL | fn copy_borrowed_ptr<'a, 'b>(p: &'a mut &'b mut isize) -> &'b mut isize {
11+
| -- -- lifetime `'b` defined here
12+
| |
13+
| lifetime `'a` defined here
14+
LL | &mut **p //~ ERROR 16:5: 16:13: lifetime mismatch [E0623]
15+
| ^^^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
1816

1917
error: aborting due to previous error
2018

src/test/ui/regions/regions-trait-object-subtyping.nll.stderr

+8-11
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@ LL | x //~ ERROR mismatched types
1111
| ^
1212

1313
error: unsatisfied lifetime constraints
14-
--> $DIR/regions-trait-object-subtyping.rs:23:51
14+
--> $DIR/regions-trait-object-subtyping.rs:25:5
1515
|
16-
LL | fn foo3<'a,'b>(x: &'a mut Dummy) -> &'b mut Dummy {
17-
| _________--_--_____________________________________^
18-
| | | |
19-
| | | lifetime `'b` defined here
20-
| | lifetime `'a` defined here
21-
LL | | // Without knowing 'a:'b, we can't coerce
22-
LL | | x //~ ERROR lifetime bound not satisfied
23-
LL | | //~^ ERROR cannot infer an appropriate lifetime
24-
LL | | }
25-
| |_^ returning this value requires that `'a` must outlive `'b`
16+
LL | fn foo3<'a,'b>(x: &'a mut Dummy) -> &'b mut Dummy {
17+
| -- -- lifetime `'b` defined here
18+
| |
19+
| lifetime `'a` defined here
20+
LL | // Without knowing 'a:'b, we can't coerce
21+
LL | x //~ ERROR lifetime bound not satisfied
22+
| ^ returning this value requires that `'a` must outlive `'b`
2623

2724
error: unsatisfied lifetime constraints
2825
--> $DIR/regions-trait-object-subtyping.rs:32:5

0 commit comments

Comments
 (0)