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

Stabilize const_constructor #65188

Merged
merged 1 commit into from
Oct 28, 2019
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
11 changes: 3 additions & 8 deletions src/librustc/ty/constness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::ty::query::Providers;
use crate::hir::def_id::DefId;
use crate::hir;
use crate::ty::TyCtxt;
use syntax_pos::symbol::{sym, Symbol};
use syntax_pos::symbol::Symbol;
use crate::hir::map::blocks::FnLikeNode;
use syntax::attr;

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

/// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
pub fn is_unstable_const_fn(self, def_id: DefId) -> Option<Symbol> {
if self.is_constructor(def_id) {
Some(sym::const_constructor)
} else if self.is_const_fn_raw(def_id) {
if self.is_const_fn_raw(def_id) {
self.lookup_stability(def_id)?.const_stability
} else {
None
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/feature_gate/accepted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ declare_features! (
(accepted, macros_in_extern, "1.40.0", Some(49476), None),
/// Allows future-proofing enums/structs with the `#[non_exhaustive]` attribute (RFC 2008).
(accepted, non_exhaustive, "1.40.0", Some(44109), None),
/// Allows calling constructor functions in `const fn`.
(accepted, const_constructor, "1.40.0", Some(61456), None),

// -------------------------------------------------------------------------
// feature-group-end: accepted features
Expand Down
3 changes: 0 additions & 3 deletions src/libsyntax/feature_gate/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,6 @@ declare_features! (
/// Allows the user of associated type bounds.
(active, associated_type_bounds, "1.34.0", Some(52662), None),

/// Allows calling constructor functions in `const fn`.
(active, const_constructor, "1.37.0", Some(61456), None),

/// Allows `if/while p && let q = r && ...` chains.
(active, let_chains, "1.37.0", Some(53667), None),

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/consts/const_constructor/const-construct-call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

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

#![feature(const_constructor)]

// Ctor(..) is transformed to Ctor { 0: ... } in HAIR lowering, so directly
// calling constructors doesn't require them to be const.

Expand Down
40 changes: 40 additions & 0 deletions src/test/ui/consts/const_constructor/const_constructor_qpath.rs
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());
}

This file was deleted.

This file was deleted.

This file was deleted.