From b43c744318b76934cdbcc0c8a4fdbc7d0091bbd8 Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Sat, 11 Mar 2017 10:54:45 -0500 Subject: [PATCH 1/3] Add feature toggle for rvalue-static-promotion RFC See https://github.com/rust-lang/rfcs/pull/1414. Updates #38865. --- src/doc/unstable-book/src/SUMMARY.md | 1 + .../src/rvalue-static-promotion.md | 5 +++++ src/librustc/middle/mem_categorization.rs | 5 ++--- src/libsyntax/feature_gate.rs | 3 +++ .../feature-gate-rvalue_static_promotion.rs | 15 +++++++++++++++ src/test/run-pass/rvalue-static-promotion.rs | 17 +++++++++++++++++ 6 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/doc/unstable-book/src/rvalue-static-promotion.md create mode 100644 src/test/compile-fail/feature-gate-rvalue_static_promotion.rs create mode 100644 src/test/run-pass/rvalue-static-promotion.rs diff --git a/src/doc/unstable-book/src/SUMMARY.md b/src/doc/unstable-book/src/SUMMARY.md index 5fb323d6ce909..034f9f4eb2523 100644 --- a/src/doc/unstable-book/src/SUMMARY.md +++ b/src/doc/unstable-book/src/SUMMARY.md @@ -71,6 +71,7 @@ - [repr_simd](repr-simd.md) - [rustc_attrs](rustc-attrs.md) - [rustc_diagnostic_macros](rustc-diagnostic-macros.md) +- [rvalue_static_promotion](rvalue-static-promotion.md) - [sanitizer_runtime](sanitizer-runtime.md) - [simd](simd.md) - [simd_ffi](simd-ffi.md) diff --git a/src/doc/unstable-book/src/rvalue-static-promotion.md b/src/doc/unstable-book/src/rvalue-static-promotion.md new file mode 100644 index 0000000000000..3b654960c8e3c --- /dev/null +++ b/src/doc/unstable-book/src/rvalue-static-promotion.md @@ -0,0 +1,5 @@ +# `rvalue_static_promotion` + +The tracking issue for this feature is: [#38865] + +------------------------ diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index b0c85e2ef4cd4..a669ac3a69b98 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -843,11 +843,10 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> { let promotable = self.tcx().rvalue_promotable_to_static.borrow().get(&id).cloned() .unwrap_or(false); - // Only promote `[T; 0]` before an RFC for rvalue promotions - // is accepted. + // When the corresponding feature isn't toggled, only promote `[T; 0]`. let promotable = match expr_ty.sty { ty::TyArray(_, 0) => true, - _ => promotable & false + _ => promotable & self.tcx().sess.features.borrow().rvalue_static_promotion, }; // Compute maximum lifetime of this rvalue. This is 'static if diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 15913d56d162f..59e9ab30e0e8c 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -342,6 +342,9 @@ declare_features! ( // Allows the `catch {...}` expression (active, catch_expr, "1.17.0", Some(31436)), + + // See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work. + (active, rvalue_static_promotion, "1.15.1", Some(38865)), ); declare_features! ( diff --git a/src/test/compile-fail/feature-gate-rvalue_static_promotion.rs b/src/test/compile-fail/feature-gate-rvalue_static_promotion.rs new file mode 100644 index 0000000000000..41dc282be41f0 --- /dev/null +++ b/src/test/compile-fail/feature-gate-rvalue_static_promotion.rs @@ -0,0 +1,15 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[allow(unused_variables)] +fn main() { + let x: &'static u32 = &42; //~ error: does not live long enough + let y: &'static Option = &None; //~ error: does not live long enough +} \ No newline at end of file diff --git a/src/test/run-pass/rvalue-static-promotion.rs b/src/test/run-pass/rvalue-static-promotion.rs new file mode 100644 index 0000000000000..f8d93783877bb --- /dev/null +++ b/src/test/run-pass/rvalue-static-promotion.rs @@ -0,0 +1,17 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// 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; + let y: &'static Option = &None; +} \ No newline at end of file From 20c0f323fc26fb913256301e242f34bccc788e31 Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Sun, 12 Mar 2017 00:24:11 -0500 Subject: [PATCH 2/3] Use `&&` instead of `&` It does not seem valuable to always evaluate the right-hand side here. --- src/librustc/middle/mem_categorization.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index a669ac3a69b98..a1deda0ab3ec1 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -846,7 +846,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> { // When the corresponding feature isn't toggled, only promote `[T; 0]`. let promotable = match expr_ty.sty { ty::TyArray(_, 0) => true, - _ => promotable & self.tcx().sess.features.borrow().rvalue_static_promotion, + _ => promotable && self.tcx().sess.features.borrow().rvalue_static_promotion, }; // Compute maximum lifetime of this rvalue. This is 'static if From f06b04949f7944bfe31405d3735240bb02ee9bd1 Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Sun, 12 Mar 2017 18:31:17 -0400 Subject: [PATCH 3/3] Improve the documentation for `rvalue_static_promotion` --- .../src/rvalue-static-promotion.md | 18 ++++++++++++++++++ .../feature-gate-rvalue_static_promotion.rs | 2 +- src/test/run-pass/rvalue-static-promotion.rs | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/doc/unstable-book/src/rvalue-static-promotion.md b/src/doc/unstable-book/src/rvalue-static-promotion.md index 3b654960c8e3c..2583d350ebe11 100644 --- a/src/doc/unstable-book/src/rvalue-static-promotion.md +++ b/src/doc/unstable-book/src/rvalue-static-promotion.md @@ -2,4 +2,22 @@ The tracking issue for this feature is: [#38865] +[#38865]: https://github.com/rust-lang/rust/issues/38865 + ------------------------ + +The `rvalue_static_promotion` feature allows directly creating `'static` references to +constant `rvalue`s, which in particular allowing for more concise code in the common case +in which a `'static` reference is all that's needed. + + +## Examples + +```rust +#![feature(rvalue_static_promotion)] + +fn main() { + let DEFAULT_VALUE: &'static u32 = &42; + assert_eq!(*DEFAULT_VALUE, 42); +} +``` diff --git a/src/test/compile-fail/feature-gate-rvalue_static_promotion.rs b/src/test/compile-fail/feature-gate-rvalue_static_promotion.rs index 41dc282be41f0..f33d0a71481d2 100644 --- a/src/test/compile-fail/feature-gate-rvalue_static_promotion.rs +++ b/src/test/compile-fail/feature-gate-rvalue_static_promotion.rs @@ -12,4 +12,4 @@ fn main() { let x: &'static u32 = &42; //~ error: does not live long enough let y: &'static Option = &None; //~ error: does not live long enough -} \ No newline at end of file +} diff --git a/src/test/run-pass/rvalue-static-promotion.rs b/src/test/run-pass/rvalue-static-promotion.rs index f8d93783877bb..30643cfc3eb7d 100644 --- a/src/test/run-pass/rvalue-static-promotion.rs +++ b/src/test/run-pass/rvalue-static-promotion.rs @@ -14,4 +14,4 @@ fn main() { let x: &'static u32 = &42; let y: &'static Option = &None; -} \ No newline at end of file +}