Skip to content

Commit ec4a752

Browse files
committed
Auto merge of #53493 - matthewjasper:hair-spans, r=nikomatsakis
Use smaller span for adjustments on block expressions When returning a mutable reference don't use the entire body of the function as the span for the adjustments at the end. The error [in this case](https://github.com/rust-lang/rust/compare/master...matthewjasper:hair-spans?expand=1#diff-ecef8b1f15622fb48a803c9b61605c78) is worse, but neither error message is really what we want. I have some ideas on how to get a better error message that will have to wait for a future PR.
2 parents 83ddc33 + 7f7fada commit ec4a752

19 files changed

+143
-184
lines changed

src/librustc_mir/hair/cx/expr.rs

+27-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)) => {
@@ -180,6 +199,13 @@ fn apply_adjustment<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
180199
ExprKind::Use { source: cast_expr.to_ref() }
181200
}
182201
Adjust::Unsize => {
202+
// See the above comment for Adjust::Deref
203+
if let ExprKind::Block { body } = expr.kind {
204+
if let Some(ref last_expr) = body.expr {
205+
span = last_expr.span;
206+
expr.span = span;
207+
}
208+
}
183209
ExprKind::Unsize { source: expr.to_ref() }
184210
}
185211
};

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/object-lifetime/object-lifetime-default-from-box-error.nll.stderr

+6-11
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@ LL | ss.r = b; //~ ERROR 41:12: 41:13: explicit lifetime required in the typ
1111
| ^
1212

1313
error[E0621]: explicit lifetime required in the type of `ss`
14-
--> $DIR/object-lifetime-default-from-box-error.rs:24:48
14+
--> $DIR/object-lifetime-default-from-box-error.rs:28:5
1515
|
16-
LL | fn load(ss: &mut SomeStruct) -> Box<SomeTrait> {
17-
| _____________---------------____________________^
18-
| | |
19-
| | help: add explicit lifetime `'static` to the type of `ss`: `&mut SomeStruct<'static>`
20-
LL | | // `Box<SomeTrait>` defaults to a `'static` bound, so this return
21-
LL | | // is illegal.
22-
LL | |
23-
LL | | ss.r //~ ERROR explicit lifetime required in the type of `ss` [E0621]
24-
LL | | }
25-
| |_^ lifetime `'static` required
16+
LL | fn load(ss: &mut SomeStruct) -> Box<SomeTrait> {
17+
| --------------- help: add explicit lifetime `'static` to the type of `ss`: `&mut SomeStruct<'static>`
18+
...
19+
LL | ss.r //~ ERROR explicit lifetime required in the type of `ss` [E0621]
20+
| ^^^^ lifetime `'static` required
2621

2722
error[E0507]: cannot move out of borrowed content
2823
--> $DIR/object-lifetime-default-from-box-error.rs:28:5

src/test/ui/regions/region-object-lifetime-in-coercion.nll.stderr

+18-30
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,31 @@ LL | let x: Box<Foo + 'static> = Box::new(v);
3131
| ^^^^^^^^^^^ lifetime `'static` required
3232

3333
error[E0621]: explicit lifetime required in the type of `v`
34-
--> $DIR/region-object-lifetime-in-coercion.rs:23:38
34+
--> $DIR/region-object-lifetime-in-coercion.rs:24:5
3535
|
36-
LL | fn b(v: &[u8]) -> Box<Foo + 'static> {
37-
| _________-----________________________^
38-
| | |
39-
| | help: add explicit lifetime `'static` to the type of `v`: `&'static [u8]`
40-
LL | | Box::new(v)
41-
LL | | //~^ ERROR explicit lifetime required in the type of `v` [E0621]
42-
LL | | }
43-
| |_^ lifetime `'static` required
36+
LL | fn b(v: &[u8]) -> Box<Foo + 'static> {
37+
| ----- help: add explicit lifetime `'static` to the type of `v`: `&'static [u8]`
38+
LL | Box::new(v)
39+
| ^^^^^^^^^^^ lifetime `'static` required
4440

4541
error[E0621]: explicit lifetime required in the type of `v`
46-
--> $DIR/region-object-lifetime-in-coercion.rs:28:28
42+
--> $DIR/region-object-lifetime-in-coercion.rs:31:5
4743
|
48-
LL | fn c(v: &[u8]) -> Box<Foo> {
49-
| _________-----______________^
50-
| | |
51-
| | help: add explicit lifetime `'static` to the type of `v`: `&'static [u8]`
52-
LL | | // same as previous case due to RFC 599
53-
LL | |
54-
LL | | Box::new(v)
55-
LL | | //~^ ERROR explicit lifetime required in the type of `v` [E0621]
56-
LL | | }
57-
| |_^ lifetime `'static` required
44+
LL | fn c(v: &[u8]) -> Box<Foo> {
45+
| ----- help: add explicit lifetime `'static` to the type of `v`: `&'static [u8]`
46+
...
47+
LL | Box::new(v)
48+
| ^^^^^^^^^^^ lifetime `'static` required
5849

5950
error: unsatisfied lifetime constraints
60-
--> $DIR/region-object-lifetime-in-coercion.rs:35:41
51+
--> $DIR/region-object-lifetime-in-coercion.rs:36:5
6152
|
62-
LL | fn d<'a,'b>(v: &'a [u8]) -> Box<Foo+'b> {
63-
| ______--_--______________________________^
64-
| | | |
65-
| | | lifetime `'b` defined here
66-
| | lifetime `'a` defined here
67-
LL | | Box::new(v)
68-
LL | | //~^ ERROR cannot infer an appropriate lifetime due to conflicting
69-
LL | | }
70-
| |_^ returning this value requires that `'a` must outlive `'b`
53+
LL | fn d<'a,'b>(v: &'a [u8]) -> Box<Foo+'b> {
54+
| -- -- lifetime `'b` defined here
55+
| |
56+
| lifetime `'a` defined here
57+
LL | Box::new(v)
58+
| ^^^^^^^^^^^ returning this value requires that `'a` must outlive `'b`
7159

7260
error: aborting due to 4 previous errors
7361

src/test/ui/regions/regions-close-object-into-object-2.nll.stderr

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ LL | box B(&*v) as Box<X> //~ ERROR cannot infer
55
| ^^^
66

77
error: unsatisfied lifetime constraints
8-
--> $DIR/regions-close-object-into-object-2.rs:19:57
8+
--> $DIR/regions-close-object-into-object-2.rs:20:5
99
|
10-
LL | fn g<'a, T: 'static>(v: Box<A<T>+'a>) -> Box<X+'static> {
11-
| ______--_________________________________________________^
12-
| | |
13-
| | lifetime `'a` defined here
14-
LL | | box B(&*v) as Box<X> //~ ERROR cannot infer
15-
LL | | }
16-
| |_^ returning this value requires that `'a` must outlive `'static`
10+
LL | fn g<'a, T: 'static>(v: Box<A<T>+'a>) -> Box<X+'static> {
11+
| -- lifetime `'a` defined here
12+
LL | box B(&*v) as Box<X> //~ ERROR cannot infer
13+
| ^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
1714

1815
error[E0597]: `*v` does not live long enough
1916
--> $DIR/regions-close-object-into-object-2.rs:20:11

src/test/ui/regions/regions-close-object-into-object-4.nll.stderr

+8-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ warning: not reporting region error due to nll
2828
LL | box B(&*v) as Box<X> //~ ERROR cannot infer
2929
| ^^^^^^^^^^
3030

31-
error: unsatisfied lifetime constraints
32-
--> $DIR/regions-close-object-into-object-4.rs:19:51
33-
|
34-
LL | fn i<'a, T, U>(v: Box<A<U>+'a>) -> Box<X+'static> {
35-
| ______--___________________________________________^
36-
| | |
37-
| | lifetime `'a` defined here
38-
LL | | box B(&*v) as Box<X> //~ ERROR cannot infer
39-
LL | | }
40-
| |_^ returning this value requires that `'a` must outlive `'static`
41-
4231
error[E0310]: the parameter type `U` may not live long enough
4332
--> $DIR/regions-close-object-into-object-4.rs:20:5
4433
|
@@ -47,6 +36,14 @@ LL | box B(&*v) as Box<X> //~ ERROR cannot infer
4736
|
4837
= help: consider adding an explicit lifetime bound `U: 'static`...
4938

39+
error: unsatisfied lifetime constraints
40+
--> $DIR/regions-close-object-into-object-4.rs:20:5
41+
|
42+
LL | fn i<'a, T, U>(v: Box<A<U>+'a>) -> Box<X+'static> {
43+
| -- lifetime `'a` defined here
44+
LL | box B(&*v) as Box<X> //~ ERROR cannot infer
45+
| ^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
46+
5047
error[E0310]: the parameter type `U` may not live long enough
5148
--> $DIR/regions-close-object-into-object-4.rs:20:9
5249
|

src/test/ui/regions/regions-proc-bound-capture.nll.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ LL | Box::new(move|| { *x }) //~ ERROR explicit lifetime required in the typ
55
| ^^^^^^^^^^^^^
66

77
error[E0621]: explicit lifetime required in the type of `x`
8-
--> $DIR/regions-proc-bound-capture.rs:17:62
8+
--> $DIR/regions-proc-bound-capture.rs:19:5
99
|
10-
LL | fn static_proc(x: &isize) -> Box<FnMut()->(isize) + 'static> {
11-
| ___________________------_____________________________________^
12-
| | |
13-
| | help: add explicit lifetime `'static` to the type of `x`: `&'static isize`
14-
LL | | // This is illegal, because the region bound on `proc` is 'static.
15-
LL | | Box::new(move|| { *x }) //~ ERROR explicit lifetime required in the type of `x` [E0621]
16-
LL | | }
17-
| |_^ lifetime `'static` required
10+
LL | fn static_proc(x: &isize) -> Box<FnMut()->(isize) + 'static> {
11+
| ------ help: add explicit lifetime `'static` to the type of `x`: `&'static isize`
12+
LL | // This is illegal, because the region bound on `proc` is 'static.
13+
LL | Box::new(move|| { *x }) //~ ERROR explicit lifetime required in the type of `x` [E0621]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
1815

1916
error: aborting due to previous error
2017

0 commit comments

Comments
 (0)