Skip to content

Commit b46ef3d

Browse files
authored
Rollup merge of #70013 - ecstatic-morse:check-consts-feature-gate, r=oli-obk
Return feature gate as a `Symbol` A minor refactoring that will be needed for #68940. That PR is blocked on me changing the error comments in a whole lot of UI tests. r? @oli-obk
2 parents d74c5cd + d7e6649 commit b46ef3d

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

src/librustc_mir/transform/check_consts/ops.rs

+31-28
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use rustc::session::config::nightly_options;
44
use rustc::session::parse::feature_err;
5-
use rustc::ty::TyCtxt;
65
use rustc_errors::struct_span_err;
76
use rustc_hir::def_id::DefId;
87
use rustc_span::symbol::sym;
@@ -15,18 +14,21 @@ pub trait NonConstOp: std::fmt::Debug {
1514
/// Whether this operation can be evaluated by miri.
1615
const IS_SUPPORTED_IN_MIRI: bool = true;
1716

18-
/// Returns a boolean indicating whether the feature gate that would allow this operation is
19-
/// enabled, or `None` if such a feature gate does not exist.
20-
fn feature_gate(_tcx: TyCtxt<'tcx>) -> Option<bool> {
17+
/// Returns the `Symbol` corresponding to the feature gate that would enable this operation,
18+
/// or `None` if such a feature gate does not exist.
19+
fn feature_gate() -> Option<Symbol> {
2120
None
2221
}
2322

2423
/// Returns `true` if this operation is allowed in the given item.
2524
///
2625
/// This check should assume that we are not in a non-const `fn`, where all operations are
2726
/// legal.
27+
///
28+
/// By default, it returns `true` if and only if this operation has a corresponding feature
29+
/// gate and that gate is enabled.
2830
fn is_allowed_in_item(&self, item: &Item<'_, '_>) -> bool {
29-
Self::feature_gate(item.tcx).unwrap_or(false)
31+
Self::feature_gate().map_or(false, |gate| item.tcx.features().enabled(gate))
3032
}
3133

3234
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -55,8 +57,8 @@ pub trait NonConstOp: std::fmt::Debug {
5557
#[derive(Debug)]
5658
pub struct Downcast;
5759
impl NonConstOp for Downcast {
58-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
59-
Some(tcx.features().const_if_match)
60+
fn feature_gate() -> Option<Symbol> {
61+
Some(sym::const_if_match)
6062
}
6163
}
6264

@@ -147,8 +149,8 @@ impl NonConstOp for HeapAllocation {
147149
#[derive(Debug)]
148150
pub struct IfOrMatch;
149151
impl NonConstOp for IfOrMatch {
150-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
151-
Some(tcx.features().const_if_match)
152+
fn feature_gate() -> Option<Symbol> {
153+
Some(sym::const_if_match)
152154
}
153155

154156
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -175,8 +177,8 @@ impl NonConstOp for LiveDrop {
175177
#[derive(Debug)]
176178
pub struct Loop;
177179
impl NonConstOp for Loop {
178-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
179-
Some(tcx.features().const_loop)
180+
fn feature_gate() -> Option<Symbol> {
181+
Some(sym::const_loop)
180182
}
181183

182184
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -203,8 +205,8 @@ impl NonConstOp for CellBorrow {
203205
#[derive(Debug)]
204206
pub struct MutBorrow;
205207
impl NonConstOp for MutBorrow {
206-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
207-
Some(tcx.features().const_mut_refs)
208+
fn feature_gate() -> Option<Symbol> {
209+
Some(sym::const_mut_refs)
208210
}
209211

210212
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -238,8 +240,8 @@ impl NonConstOp for MutBorrow {
238240
#[derive(Debug)]
239241
pub struct MutAddressOf;
240242
impl NonConstOp for MutAddressOf {
241-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
242-
Some(tcx.features().const_mut_refs)
243+
fn feature_gate() -> Option<Symbol> {
244+
Some(sym::const_mut_refs)
243245
}
244246

245247
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -256,16 +258,16 @@ impl NonConstOp for MutAddressOf {
256258
#[derive(Debug)]
257259
pub struct MutDeref;
258260
impl NonConstOp for MutDeref {
259-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
260-
Some(tcx.features().const_mut_refs)
261+
fn feature_gate() -> Option<Symbol> {
262+
Some(sym::const_mut_refs)
261263
}
262264
}
263265

264266
#[derive(Debug)]
265267
pub struct Panic;
266268
impl NonConstOp for Panic {
267-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
268-
Some(tcx.features().const_panic)
269+
fn feature_gate() -> Option<Symbol> {
270+
Some(sym::const_panic)
269271
}
270272

271273
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -282,8 +284,8 @@ impl NonConstOp for Panic {
282284
#[derive(Debug)]
283285
pub struct RawPtrComparison;
284286
impl NonConstOp for RawPtrComparison {
285-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
286-
Some(tcx.features().const_compare_raw_pointers)
287+
fn feature_gate() -> Option<Symbol> {
288+
Some(sym::const_compare_raw_pointers)
287289
}
288290

289291
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -300,8 +302,8 @@ impl NonConstOp for RawPtrComparison {
300302
#[derive(Debug)]
301303
pub struct RawPtrDeref;
302304
impl NonConstOp for RawPtrDeref {
303-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
304-
Some(tcx.features().const_raw_ptr_deref)
305+
fn feature_gate() -> Option<Symbol> {
306+
Some(sym::const_raw_ptr_deref)
305307
}
306308

307309
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -318,8 +320,8 @@ impl NonConstOp for RawPtrDeref {
318320
#[derive(Debug)]
319321
pub struct RawPtrToIntCast;
320322
impl NonConstOp for RawPtrToIntCast {
321-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
322-
Some(tcx.features().const_raw_ptr_to_usize_cast)
323+
fn feature_gate() -> Option<Symbol> {
324+
Some(sym::const_raw_ptr_to_usize_cast)
323325
}
324326

325327
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
@@ -386,11 +388,12 @@ pub struct UnionAccess;
386388
impl NonConstOp for UnionAccess {
387389
fn is_allowed_in_item(&self, item: &Item<'_, '_>) -> bool {
388390
// Union accesses are stable in all contexts except `const fn`.
389-
item.const_kind() != ConstKind::ConstFn || Self::feature_gate(item.tcx).unwrap()
391+
item.const_kind() != ConstKind::ConstFn
392+
|| item.tcx.features().enabled(Self::feature_gate().unwrap())
390393
}
391394

392-
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
393-
Some(tcx.features().const_fn_union)
395+
fn feature_gate() -> Option<Symbol> {
396+
Some(sym::const_fn_union)
394397
}
395398

396399
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {

src/librustc_mir/transform/check_consts/validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl Validator<'a, 'mir, 'tcx> {
213213

214214
// If an operation is supported in miri (and is not already controlled by a feature gate) it
215215
// can be turned on with `-Zunleash-the-miri-inside-of-you`.
216-
let is_unleashable = O::IS_SUPPORTED_IN_MIRI && O::feature_gate(self.tcx).is_none();
216+
let is_unleashable = O::IS_SUPPORTED_IN_MIRI && O::feature_gate().is_none();
217217

218218
if is_unleashable && self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you {
219219
self.tcx.sess.span_warn(span, "skipping const checks");

0 commit comments

Comments
 (0)