-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #65188 - matthewjasper:stabilize-const-constructor, r=C…
…entril Stabilize `const_constructor` # Stabilization proposal I propose that we stabilize `#![feature(const_constructor)]`. Tracking issue: #61456 Version target: 1.40 (2019-11-05 => beta, 2019-12-19 => stable). ## What is stabilized ### User guide Tuple struct and tuple variant constructors are now considered to be constant functions. As such a call expression where the callee has a tuple struct or variant constructor "function item" type can be called: ```rust const fn make_options() { // These already work because they are special cased: Some(0); (Option::Some)(1); // These also work now: let f = Option::Some; f(2); {Option::Some}(3); <Option<_>>::Some(5); } ``` ### Motivation Consistency with other `const fn`. Consistency between syntactic path forms. This should also ensure that constructors implement `const Fn` traits and can be coerced to `const fn` function pointers, if they are introduced. ## Tests * [ui/consts/const_constructor/const-construct-call.rs](https://github.com/rust-lang/rust/blob/0d75ab2293a106eb674ac01860910cfc1580837e/src/test/ui/consts/const_constructor/const-construct-call.rs) - Tests various syntactic forms, use in both `const fn` and `const` items, and constructors in both the current and extern crates. * [ui/consts/const_constructor/const_constructor_qpath.rs](https://github.com/rust-lang/rust/blob/1850dfcdabf8258a1f023f26c2c59e96b869dd95/src/test/ui/consts/const_constructor/const_constructor_qpath.rs) - Tests that type qualified paths to enum variants are also considered to be `const fn`.(#64247) r? @oli-obk Closes #61456 Closes #64247
- Loading branch information
Showing
8 changed files
with
45 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/test/ui/consts/const_constructor/const_constructor_qpath.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// revisions: min_const_fn const_fn | ||
// run-pass | ||
|
||
#![cfg_attr(const_fn, feature(const_fn))] | ||
|
||
trait ConstDefault { | ||
const DEFAULT: Self; | ||
} | ||
|
||
#[derive(PartialEq)] | ||
enum E { | ||
V(i32), | ||
W(usize), | ||
} | ||
|
||
impl ConstDefault for E { | ||
const DEFAULT: Self = Self::V(23); | ||
} | ||
|
||
impl ConstDefault for Option<i32> { | ||
const DEFAULT: Self = Self::Some(23); | ||
} | ||
|
||
impl E { | ||
const NON_DEFAULT: Self = Self::W(12); | ||
const fn local_fn() -> Self { | ||
Self::V(23) | ||
} | ||
} | ||
|
||
const fn explicit_qpath() -> E { | ||
let _x = <Option<usize>>::Some(23); | ||
<E>::W(12) | ||
} | ||
|
||
fn main() { | ||
assert!(E::DEFAULT == E::local_fn()); | ||
assert!(Option::DEFAULT == Some(23)); | ||
assert!(E::NON_DEFAULT == explicit_qpath()); | ||
} |
34 changes: 0 additions & 34 deletions
34
src/test/ui/consts/const_constructor/feature-gate-const_constructor.const_fn.stderr
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
src/test/ui/consts/const_constructor/feature-gate-const_constructor.min_const_fn.stderr
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
src/test/ui/consts/const_constructor/feature-gate-const_constructor.rs
This file was deleted.
Oops, something went wrong.