Skip to content

Commit

Permalink
Auto merge of #43838 - eddyb:stable-rvalue-promotion, r=arielb1
Browse files Browse the repository at this point in the history
Stabilize rvalue promotion to 'static.

Closes #38865.

Documentation PR at rust-lang/reference#98.
  • Loading branch information
bors committed Aug 16, 2017
2 parents 3f94b71 + 014333f commit 7ac979d
Show file tree
Hide file tree
Showing 26 changed files with 88 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/doc/reference

This file was deleted.

4 changes: 2 additions & 2 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,10 +873,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
let promotable = self.tcx.rvalue_promotable_to_static.borrow().get(&id).cloned()
.unwrap_or(false);

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

// Compute maximum lifetime of this rvalue. This is 'static if
Expand Down
5 changes: 2 additions & 3 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ declare_features! (
// Allows `repr(align(u16))` struct attribute (RFC 1358)
(active, repr_align, "1.17.0", Some(33626)),

// See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
(active, rvalue_static_promotion, "1.15.1", Some(38865)),

// Used to preserve symbols (see llvm.used)
(active, used, "1.18.0", Some(40289)),

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

// If you change this, please modify src/doc/unstable-book as well. You must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
// Test lifetimes are linked properly when we take reference
// to interior.

fn id<T>(x: T) -> T { x }

struct Foo(isize);

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

Expand Down
15 changes: 0 additions & 15 deletions src/test/compile-fail/feature-gate-rvalue_static_promotion.rs

This file was deleted.

4 changes: 3 additions & 1 deletion src/test/compile-fail/issue-11493.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

// This file must never have a trailing newline

fn id<T>(x: T) -> T { x }

fn main() {
let x = Some(3);
let y = x.as_ref().unwrap_or(&5); //~ ERROR: borrowed value does not live long enough
let y = x.as_ref().unwrap_or(&id(5)); //~ ERROR: borrowed value does not live long enough
}
4 changes: 3 additions & 1 deletion src/test/compile-fail/issue-17545.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

#![feature(fn_traits)]

fn id<T>(x: T) -> T { x }

pub fn foo<'a, F: Fn(&'a ())>(bar: F) {
bar.call((
&(), //~ ERROR borrowed value does not live long enough
&id(()), //~ ERROR borrowed value does not live long enough
));
}
fn main() {}
4 changes: 3 additions & 1 deletion src/test/compile-fail/issue-17718-constants-not-static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn id<T>(x: T) -> T { x }

const FOO: usize = 3;

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

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-27592.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Regression test for issue #27591.
// Regression test for issue #27592.

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

fn main() {
write(|| format_args!("{}", "Hello world"));
write(|| format_args!("{}", String::from("Hello world")));
//~^ ERROR borrowed value does not live long enough
//~| ERROR borrowed value does not live long enough
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
// are treated as rvalues and their lifetime is not bounded to
// the static scope.

fn id<T>(x: T) -> T { x }

struct Test;

enum MyEnum {
Variant1
}

fn structLifetime<'a>() -> &'a Test {
let testValue = &Test; //~ ERROR borrowed value does not live long enough
let testValue = &id(Test);
//~^ ERROR borrowed value does not live long enough
testValue
}

fn variantLifetime<'a>() -> &'a MyEnum {
let testValue = &MyEnum::Variant1; //~ ERROR borrowed value does not live long enough
let testValue = &id(MyEnum::Variant1);
//~^ ERROR borrowed value does not live long enough
testValue
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/compile-fail/regions-ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn id<T>(x: T) -> T { x }

fn f(_x: &isize) -> &isize {
return &3; //~ ERROR borrowed value does not live long enough
return &id(3); //~ ERROR borrowed value does not live long enough
}

fn main() {
Expand Down
4 changes: 3 additions & 1 deletion src/test/compile-fail/regions-var-type-out-of-scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn id<T>(x: T) -> T { x }

fn foo(cond: bool) {
// Here we will infer a type that uses the
// region of the if stmt then block:
let mut x;

if cond {
x = &3; //~ ERROR borrowed value does not live long enough
x = &id(3); //~ ERROR borrowed value does not live long enough
assert_eq!(*x, 3);
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/test/compile-fail/static-reference-to-fn-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn id<T>(x: T) -> T { x }

struct StateMachineIter<'a> {
statefn: &'a StateMachineFunc<'a>
}
Expand All @@ -23,19 +25,19 @@ impl<'a> Iterator for StateMachineIter<'a> {
}

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

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

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

fn state_iter() -> StateMachineIter<'static> {
StateMachineIter {
statefn: &(state1 as StateMachineFunc) //~ ERROR borrowed value does not live long enough
statefn: &id(state1 as StateMachineFunc)
//~^ ERROR borrowed value does not live long enough
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/compile-fail/static-region-bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

#![feature(box_syntax)]

fn id<T>(x: T) -> T { x }

fn f<T:'static>(_: T) {}

fn main() {
let x: Box<_> = box 3;
f(x);
let x = &3; //~ ERROR borrowed value does not live long enough
let x = &id(3); //~ ERROR borrowed value does not live long enough
f(x);
}
2 changes: 0 additions & 2 deletions src/test/run-pass/rvalue-static-promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rvalue_static_promotion)]

#[allow(unused_variables)]
fn main() {
let x: &'static u32 = &42;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lifetimes/borrowck-let-suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// except according to those terms.

fn f() {
let x = [1].iter();
let x = vec![1].iter();
}

fn main() {
Expand Down
7 changes: 4 additions & 3 deletions src/test/ui/lifetimes/borrowck-let-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
error[E0597]: borrowed value does not live long enough
--> $DIR/borrowck-let-suggestion.rs:12:23
--> $DIR/borrowck-let-suggestion.rs:12:27
|
12 | let x = [1].iter();
| --- ^ temporary value dropped here while still borrowed
12 | let x = vec![1].iter();
| ------- ^ temporary value dropped here while still borrowed
| |
| temporary value created here
13 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
= note: this error originates in a macro outside of the current crate

error: aborting due to previous error

8 changes: 5 additions & 3 deletions src/test/ui/span/borrowck-let-suggestion-suffixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn id<T>(x: T) -> T { x }

fn f() {
let old = ['o']; // statement 0
let mut v1 = Vec::new(); // statement 1
Expand All @@ -21,7 +23,7 @@ fn f() {

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

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

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

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

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

v5.push(&'z');
v5.push(&id('z'));
//~^ ERROR borrowed value does not live long enough
//~| NOTE temporary value created here
//~| NOTE temporary value only lives until here
Expand Down
30 changes: 15 additions & 15 deletions src/test/ui/span/borrowck-let-suggestion-suffixes.stderr
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
error[E0597]: `young[..]` does not live long enough
--> $DIR/borrowck-let-suggestion-suffixes.rs:52:1
--> $DIR/borrowck-let-suggestion-suffixes.rs:54:1
|
19 | v2.push(&young[0]); // statement 4
21 | v2.push(&young[0]); // statement 4
| -------- borrow occurs here
...
52 | }
54 | }
| ^ `young[..]` dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created

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

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

error[E0597]: borrowed value does not live long enough
--> $DIR/borrowck-let-suggestion-suffixes.rs:45:18
--> $DIR/borrowck-let-suggestion-suffixes.rs:47:22
|
45 | v5.push(&'z');
| --- ^ temporary value dropped here while still borrowed
47 | v5.push(&id('z'));
| ------- ^ temporary value dropped here while still borrowed
| |
| temporary value created here
...
52 | }
54 | }
| - temporary value needs to live until here
|
= note: consider using a `let` binding to increase its lifetime
Expand Down
Loading

0 comments on commit 7ac979d

Please sign in to comment.