Skip to content

Commit 7ac979d

Browse files
committed
Auto merge of #43838 - eddyb:stable-rvalue-promotion, r=arielb1
Stabilize rvalue promotion to 'static. Closes #38865. Documentation PR at rust-lang/reference#98.
2 parents 3f94b71 + 014333f commit 7ac979d

26 files changed

+88
-99
lines changed

src/doc/reference

src/doc/unstable-book/src/language-features/rvalue-static-promotion.md

-23
This file was deleted.

src/librustc/middle/mem_categorization.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -873,10 +873,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
873873
let promotable = self.tcx.rvalue_promotable_to_static.borrow().get(&id).cloned()
874874
.unwrap_or(false);
875875

876-
// When the corresponding feature isn't toggled, only promote `[T; 0]`.
876+
// Always promote `[T; 0]` (even when e.g. borrowed mutably).
877877
let promotable = match expr_ty.sty {
878878
ty::TyArray(_, 0) => true,
879-
_ => promotable && self.tcx.sess.features.borrow().rvalue_static_promotion,
879+
_ => promotable,
880880
};
881881

882882
// Compute maximum lifetime of this rvalue. This is 'static if

src/libsyntax/feature_gate.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,6 @@ declare_features! (
345345
// Allows `repr(align(u16))` struct attribute (RFC 1358)
346346
(active, repr_align, "1.17.0", Some(33626)),
347347

348-
// See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
349-
(active, rvalue_static_promotion, "1.15.1", Some(38865)),
350-
351348
// Used to preserve symbols (see llvm.used)
352349
(active, used, "1.18.0", Some(40289)),
353350

@@ -457,6 +454,8 @@ declare_features! (
457454
(accepted, associated_consts, "1.20.0", Some(29646)),
458455
// Usage of the `compile_error!` macro
459456
(accepted, compile_error, "1.20.0", Some(40872)),
457+
// See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
458+
(accepted, rvalue_static_promotion, "1.21.0", Some(38865)),
460459
);
461460

462461
// If you change this, please modify src/doc/unstable-book as well. You must

src/test/compile-fail/borrowck/borrowck-borrow-from-temporary.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
// Test lifetimes are linked properly when we take reference
1212
// to interior.
1313

14+
fn id<T>(x: T) -> T { x }
15+
1416
struct Foo(isize);
1517

1618
fn foo<'a>() -> &'a isize {
17-
let &Foo(ref x) = &Foo(3); //~ ERROR borrowed value does not live long enough
19+
let &Foo(ref x) = &id(Foo(3)); //~ ERROR borrowed value does not live long enough
1820
x
1921
}
2022

src/test/compile-fail/feature-gate-rvalue_static_promotion.rs

-15
This file was deleted.

src/test/compile-fail/issue-11493.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
// This file must never have a trailing newline
1212

13+
fn id<T>(x: T) -> T { x }
14+
1315
fn main() {
1416
let x = Some(3);
15-
let y = x.as_ref().unwrap_or(&5); //~ ERROR: borrowed value does not live long enough
17+
let y = x.as_ref().unwrap_or(&id(5)); //~ ERROR: borrowed value does not live long enough
1618
}

src/test/compile-fail/issue-17545.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
#![feature(fn_traits)]
1212

13+
fn id<T>(x: T) -> T { x }
14+
1315
pub fn foo<'a, F: Fn(&'a ())>(bar: F) {
1416
bar.call((
15-
&(), //~ ERROR borrowed value does not live long enough
17+
&id(()), //~ ERROR borrowed value does not live long enough
1618
));
1719
}
1820
fn main() {}

src/test/compile-fail/issue-17718-constants-not-static.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
fn id<T>(x: T) -> T { x }
12+
1113
const FOO: usize = 3;
1214

13-
fn foo() -> &'static usize { &FOO }
15+
fn foo() -> &'static usize { &id(FOO) }
1416
//~^ ERROR: borrowed value does not live long enough
1517

1618
fn main() {

src/test/compile-fail/issue-27592.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Regression test for issue #27591.
11+
// Regression test for issue #27592.
1212

1313
fn write<'a, F: ::std::ops::FnOnce()->::std::fmt::Arguments<'a> + 'a>(fcn: F) {
1414
use std::fmt::Write;
@@ -23,7 +23,7 @@ impl ::std::fmt::Write for Stream {
2323
}
2424

2525
fn main() {
26-
write(|| format_args!("{}", "Hello world"));
26+
write(|| format_args!("{}", String::from("Hello world")));
2727
//~^ ERROR borrowed value does not live long enough
2828
//~| ERROR borrowed value does not live long enough
2929
}

src/test/compile-fail/regions-lifetime-of-struct-or-enum-variant.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,23 @@
1212
// are treated as rvalues and their lifetime is not bounded to
1313
// the static scope.
1414

15+
fn id<T>(x: T) -> T { x }
16+
1517
struct Test;
1618

1719
enum MyEnum {
1820
Variant1
1921
}
2022

2123
fn structLifetime<'a>() -> &'a Test {
22-
let testValue = &Test; //~ ERROR borrowed value does not live long enough
24+
let testValue = &id(Test);
25+
//~^ ERROR borrowed value does not live long enough
2326
testValue
2427
}
2528

2629
fn variantLifetime<'a>() -> &'a MyEnum {
27-
let testValue = &MyEnum::Variant1; //~ ERROR borrowed value does not live long enough
30+
let testValue = &id(MyEnum::Variant1);
31+
//~^ ERROR borrowed value does not live long enough
2832
testValue
2933
}
3034

src/test/compile-fail/regions-ret.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
fn id<T>(x: T) -> T { x }
12+
1113
fn f(_x: &isize) -> &isize {
12-
return &3; //~ ERROR borrowed value does not live long enough
14+
return &id(3); //~ ERROR borrowed value does not live long enough
1315
}
1416

1517
fn main() {

src/test/compile-fail/regions-var-type-out-of-scope.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
fn id<T>(x: T) -> T { x }
12+
1113
fn foo(cond: bool) {
1214
// Here we will infer a type that uses the
1315
// region of the if stmt then block:
1416
let mut x;
1517

1618
if cond {
17-
x = &3; //~ ERROR borrowed value does not live long enough
19+
x = &id(3); //~ ERROR borrowed value does not live long enough
1820
assert_eq!(*x, 3);
1921
}
2022
}

src/test/compile-fail/static-reference-to-fn-2.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
fn id<T>(x: T) -> T { x }
12+
1113
struct StateMachineIter<'a> {
1214
statefn: &'a StateMachineFunc<'a>
1315
}
@@ -23,19 +25,19 @@ impl<'a> Iterator for StateMachineIter<'a> {
2325
}
2426

2527
fn state1(self_: &mut StateMachineIter) -> Option<&'static str> {
26-
self_.statefn = &(state2 as StateMachineFunc);
28+
self_.statefn = &id(state2 as StateMachineFunc);
2729
//~^ ERROR borrowed value does not live long enough
2830
return Some("state1");
2931
}
3032

3133
fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> {
32-
self_.statefn = &(state3 as StateMachineFunc);
34+
self_.statefn = &id(state3 as StateMachineFunc);
3335
//~^ ERROR borrowed value does not live long enough
3436
return Some("state2");
3537
}
3638

3739
fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> {
38-
self_.statefn = &(finished as StateMachineFunc);
40+
self_.statefn = &id(finished as StateMachineFunc);
3941
//~^ ERROR borrowed value does not live long enough
4042
return Some("state3");
4143
}
@@ -46,7 +48,8 @@ fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> {
4648

4749
fn state_iter() -> StateMachineIter<'static> {
4850
StateMachineIter {
49-
statefn: &(state1 as StateMachineFunc) //~ ERROR borrowed value does not live long enough
51+
statefn: &id(state1 as StateMachineFunc)
52+
//~^ ERROR borrowed value does not live long enough
5053
}
5154
}
5255

src/test/compile-fail/static-region-bound.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
#![feature(box_syntax)]
1212

13+
fn id<T>(x: T) -> T { x }
14+
1315
fn f<T:'static>(_: T) {}
1416

1517
fn main() {
1618
let x: Box<_> = box 3;
1719
f(x);
18-
let x = &3; //~ ERROR borrowed value does not live long enough
20+
let x = &id(3); //~ ERROR borrowed value does not live long enough
1921
f(x);
2022
}

src/test/run-pass/rvalue-static-promotion.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rvalue_static_promotion)]
12-
1311
#[allow(unused_variables)]
1412
fn main() {
1513
let x: &'static u32 = &42;

src/test/ui/lifetimes/borrowck-let-suggestion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
fn f() {
12-
let x = [1].iter();
12+
let x = vec![1].iter();
1313
}
1414

1515
fn main() {
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error[E0597]: borrowed value does not live long enough
2-
--> $DIR/borrowck-let-suggestion.rs:12:23
2+
--> $DIR/borrowck-let-suggestion.rs:12:27
33
|
4-
12 | let x = [1].iter();
5-
| --- ^ temporary value dropped here while still borrowed
4+
12 | let x = vec![1].iter();
5+
| ------- ^ temporary value dropped here while still borrowed
66
| |
77
| temporary value created here
88
13 | }
99
| - temporary value needs to live until here
1010
|
1111
= note: consider using a `let` binding to increase its lifetime
12+
= note: this error originates in a macro outside of the current crate
1213

1314
error: aborting due to previous error
1415

src/test/ui/span/borrowck-let-suggestion-suffixes.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
fn id<T>(x: T) -> T { x }
12+
1113
fn f() {
1214
let old = ['o']; // statement 0
1315
let mut v1 = Vec::new(); // statement 1
@@ -21,7 +23,7 @@ fn f() {
2123

2224
let mut v3 = Vec::new(); // statement 5
2325

24-
v3.push(&'x'); // statement 6
26+
v3.push(&id('x')); // statement 6
2527
//~^ ERROR borrowed value does not live long enough
2628
//~| NOTE temporary value created here
2729
//~| NOTE temporary value only lives until here
@@ -31,7 +33,7 @@ fn f() {
3133

3234
let mut v4 = Vec::new(); // (sub) statement 0
3335

34-
v4.push(&'y');
36+
v4.push(&id('y'));
3537
//~^ ERROR borrowed value does not live long enough
3638
//~| NOTE temporary value created here
3739
//~| NOTE temporary value only lives until here
@@ -42,7 +44,7 @@ fn f() {
4244

4345
let mut v5 = Vec::new(); // statement 8
4446

45-
v5.push(&'z');
47+
v5.push(&id('z'));
4648
//~^ ERROR borrowed value does not live long enough
4749
//~| NOTE temporary value created here
4850
//~| NOTE temporary value only lives until here

src/test/ui/span/borrowck-let-suggestion-suffixes.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
error[E0597]: `young[..]` does not live long enough
2-
--> $DIR/borrowck-let-suggestion-suffixes.rs:52:1
2+
--> $DIR/borrowck-let-suggestion-suffixes.rs:54:1
33
|
4-
19 | v2.push(&young[0]); // statement 4
4+
21 | v2.push(&young[0]); // statement 4
55
| -------- borrow occurs here
66
...
7-
52 | }
7+
54 | }
88
| ^ `young[..]` dropped here while still borrowed
99
|
1010
= note: values in a scope are dropped in the opposite order they are created
1111

1212
error[E0597]: borrowed value does not live long enough
13-
--> $DIR/borrowck-let-suggestion-suffixes.rs:24:18
13+
--> $DIR/borrowck-let-suggestion-suffixes.rs:26:22
1414
|
15-
24 | v3.push(&'x'); // statement 6
16-
| --- ^ temporary value dropped here while still borrowed
15+
26 | v3.push(&id('x')); // statement 6
16+
| ------- ^ temporary value dropped here while still borrowed
1717
| |
1818
| temporary value created here
1919
...
20-
52 | }
20+
54 | }
2121
| - temporary value needs to live until here
2222
|
2323
= note: consider using a `let` binding to increase its lifetime
2424

2525
error[E0597]: borrowed value does not live long enough
26-
--> $DIR/borrowck-let-suggestion-suffixes.rs:34:22
26+
--> $DIR/borrowck-let-suggestion-suffixes.rs:36:26
2727
|
28-
34 | v4.push(&'y');
29-
| --- ^ temporary value dropped here while still borrowed
28+
36 | v4.push(&id('y'));
29+
| ------- ^ temporary value dropped here while still borrowed
3030
| |
3131
| temporary value created here
3232
...
33-
40 | } // (statement 7)
33+
42 | } // (statement 7)
3434
| - temporary value needs to live until here
3535
|
3636
= note: consider using a `let` binding to increase its lifetime
3737

3838
error[E0597]: borrowed value does not live long enough
39-
--> $DIR/borrowck-let-suggestion-suffixes.rs:45:18
39+
--> $DIR/borrowck-let-suggestion-suffixes.rs:47:22
4040
|
41-
45 | v5.push(&'z');
42-
| --- ^ temporary value dropped here while still borrowed
41+
47 | v5.push(&id('z'));
42+
| ------- ^ temporary value dropped here while still borrowed
4343
| |
4444
| temporary value created here
4545
...
46-
52 | }
46+
54 | }
4747
| - temporary value needs to live until here
4848
|
4949
= note: consider using a `let` binding to increase its lifetime

0 commit comments

Comments
 (0)