Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature gate for rvalue-static-promotion #40441

Merged
merged 3 commits into from
Mar 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions src/doc/unstable-book/src/rvalue-static-promotion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# `rvalue_static_promotion`

The tracking issue for this feature is: [#38865]

[#38865]: https:/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);
}
```
5 changes: 2 additions & 3 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! (
Expand Down
15 changes: 15 additions & 0 deletions src/test/compile-fail/feature-gate-rvalue_static_promotion.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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<u32> = &None; //~ error: does not live long enough
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file and the other one need a newline at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

17 changes: 17 additions & 0 deletions src/test/run-pass/rvalue-static-promotion.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rvalue_static_promotion)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even run when I run ./x.py test src/test/run-pass --test-args rval?
I see test [run-pass] run-pass/rvalue-static-promotion.rs ... ok in the output but it would seem magical if it did. I guess it is set up to be magical because other tests do it the same way, but wonder where that magic is documented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I believe it's not covered in src/test/COMPILER_TEST.md, though that doc does talk about "revisions" which seems like that's what you would want, but other tests I've randomly peeked into don't seem to use that, so I see no reason for the feature to be toggled when they run)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eddyb any pointers here?


#[allow(unused_variables)]
fn main() {
let x: &'static u32 = &42;
let y: &'static Option<u32> = &None;
}