Skip to content

Commit 03a50ae

Browse files
committed
Auto merge of #65188 - matthewjasper:stabilize-const-constructor, r=Centril
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
2 parents 9285d40 + 170718c commit 03a50ae

8 files changed

+45
-109
lines changed

src/librustc/ty/constness.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ty::query::Providers;
22
use crate::hir::def_id::DefId;
33
use crate::hir;
44
use crate::ty::TyCtxt;
5-
use syntax_pos::symbol::{sym, Symbol};
5+
use syntax_pos::symbol::Symbol;
66
use crate::hir::map::blocks::FnLikeNode;
77
use syntax::attr;
88

@@ -13,14 +13,11 @@ impl<'tcx> TyCtxt<'tcx> {
1313
self.is_const_fn_raw(def_id) && match self.is_unstable_const_fn(def_id) {
1414
Some(feature_name) => {
1515
// has a `rustc_const_unstable` attribute, check whether the user enabled the
16-
// corresponding feature gate, const_constructor is not a lib feature, so has
17-
// to be checked separately.
16+
// corresponding feature gate.
1817
self.features()
1918
.declared_lib_features
2019
.iter()
2120
.any(|&(sym, _)| sym == feature_name)
22-
|| (feature_name == sym::const_constructor
23-
&& self.features().const_constructor)
2421
},
2522
// functions without const stability are either stable user written
2623
// const fn or the user is using feature gates and we thus don't
@@ -31,9 +28,7 @@ impl<'tcx> TyCtxt<'tcx> {
3128

3229
/// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
3330
pub fn is_unstable_const_fn(self, def_id: DefId) -> Option<Symbol> {
34-
if self.is_constructor(def_id) {
35-
Some(sym::const_constructor)
36-
} else if self.is_const_fn_raw(def_id) {
31+
if self.is_const_fn_raw(def_id) {
3732
self.lookup_stability(def_id)?.const_stability
3833
} else {
3934
None

src/libsyntax/feature_gate/accepted.rs

+2
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ declare_features! (
249249
(accepted, macros_in_extern, "1.40.0", Some(49476), None),
250250
/// Allows future-proofing enums/structs with the `#[non_exhaustive]` attribute (RFC 2008).
251251
(accepted, non_exhaustive, "1.40.0", Some(44109), None),
252+
/// Allows calling constructor functions in `const fn`.
253+
(accepted, const_constructor, "1.40.0", Some(61456), None),
252254

253255
// -------------------------------------------------------------------------
254256
// feature-group-end: accepted features

src/libsyntax/feature_gate/active.rs

-3
Original file line numberDiff line numberDiff line change
@@ -488,9 +488,6 @@ declare_features! (
488488
/// Allows the user of associated type bounds.
489489
(active, associated_type_bounds, "1.34.0", Some(52662), None),
490490

491-
/// Allows calling constructor functions in `const fn`.
492-
(active, const_constructor, "1.37.0", Some(61456), None),
493-
494491
/// Allows `if/while p && let q = r && ...` chains.
495492
(active, let_chains, "1.37.0", Some(53667), None),
496493

src/test/ui/consts/const_constructor/const-construct-call.rs

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#![cfg_attr(const_fn, feature(const_fn))]
88

9-
#![feature(const_constructor)]
10-
119
// Ctor(..) is transformed to Ctor { 0: ... } in HAIR lowering, so directly
1210
// calling constructors doesn't require them to be const.
1311

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// revisions: min_const_fn const_fn
2+
// run-pass
3+
4+
#![cfg_attr(const_fn, feature(const_fn))]
5+
6+
trait ConstDefault {
7+
const DEFAULT: Self;
8+
}
9+
10+
#[derive(PartialEq)]
11+
enum E {
12+
V(i32),
13+
W(usize),
14+
}
15+
16+
impl ConstDefault for E {
17+
const DEFAULT: Self = Self::V(23);
18+
}
19+
20+
impl ConstDefault for Option<i32> {
21+
const DEFAULT: Self = Self::Some(23);
22+
}
23+
24+
impl E {
25+
const NON_DEFAULT: Self = Self::W(12);
26+
const fn local_fn() -> Self {
27+
Self::V(23)
28+
}
29+
}
30+
31+
const fn explicit_qpath() -> E {
32+
let _x = <Option<usize>>::Some(23);
33+
<E>::W(12)
34+
}
35+
36+
fn main() {
37+
assert!(E::DEFAULT == E::local_fn());
38+
assert!(Option::DEFAULT == Some(23));
39+
assert!(E::NON_DEFAULT == explicit_qpath());
40+
}

src/test/ui/consts/const_constructor/feature-gate-const_constructor.const_fn.stderr

-34
This file was deleted.

src/test/ui/consts/const_constructor/feature-gate-const_constructor.min_const_fn.stderr

-34
This file was deleted.

src/test/ui/consts/const_constructor/feature-gate-const_constructor.rs

-28
This file was deleted.

0 commit comments

Comments
 (0)