Skip to content

Commit d050aee

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#40441 - tschottdorf:promotable-rfc, r=eddyb
Add feature gate for rvalue-static-promotion Probably needs more tests (which ones?) and there may be other things that need to be done. Also not sure whether the version that introduces the flag is really `1.15.1`. See rust-lang/rfcs#1414. Updates rust-lang#38865.
2 parents f340b0b + f06b049 commit d050aee

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

src/doc/unstable-book/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
- [repr_simd](repr-simd.md)
7272
- [rustc_attrs](rustc-attrs.md)
7373
- [rustc_diagnostic_macros](rustc-diagnostic-macros.md)
74+
- [rvalue_static_promotion](rvalue-static-promotion.md)
7475
- [sanitizer_runtime](sanitizer-runtime.md)
7576
- [simd](simd.md)
7677
- [simd_ffi](simd-ffi.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `rvalue_static_promotion`
2+
3+
The tracking issue for this feature is: [#38865]
4+
5+
[#38865]: https://github.com/rust-lang/rust/issues/38865
6+
7+
------------------------
8+
9+
The `rvalue_static_promotion` feature allows directly creating `'static` references to
10+
constant `rvalue`s, which in particular allowing for more concise code in the common case
11+
in which a `'static` reference is all that's needed.
12+
13+
14+
## Examples
15+
16+
```rust
17+
#![feature(rvalue_static_promotion)]
18+
19+
fn main() {
20+
let DEFAULT_VALUE: &'static u32 = &42;
21+
assert_eq!(*DEFAULT_VALUE, 42);
22+
}
23+
```

src/librustc/middle/mem_categorization.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -843,11 +843,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
843843
let promotable = self.tcx().rvalue_promotable_to_static.borrow().get(&id).cloned()
844844
.unwrap_or(false);
845845

846-
// Only promote `[T; 0]` before an RFC for rvalue promotions
847-
// is accepted.
846+
// When the corresponding feature isn't toggled, only promote `[T; 0]`.
848847
let promotable = match expr_ty.sty {
849848
ty::TyArray(_, 0) => true,
850-
_ => promotable & false
849+
_ => promotable && self.tcx().sess.features.borrow().rvalue_static_promotion,
851850
};
852851

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

src/libsyntax/feature_gate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ declare_features! (
342342

343343
// Allows the `catch {...}` expression
344344
(active, catch_expr, "1.17.0", Some(31436)),
345+
346+
// See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
347+
(active, rvalue_static_promotion, "1.15.1", Some(38865)),
345348
);
346349

347350
declare_features! (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[allow(unused_variables)]
12+
fn main() {
13+
let x: &'static u32 = &42; //~ error: does not live long enough
14+
let y: &'static Option<u32> = &None; //~ error: does not live long enough
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rvalue_static_promotion)]
12+
13+
#[allow(unused_variables)]
14+
fn main() {
15+
let x: &'static u32 = &42;
16+
let y: &'static Option<u32> = &None;
17+
}

0 commit comments

Comments
 (0)