Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit de35c42

Browse files
Remove ops::non_const
This helper function was meant to reduce code duplication between const-checking pre- and post-drop-elaboration. Most of the functionality is only relevant for the pre-drop-elaboration pass.
1 parent ce50939 commit de35c42

File tree

3 files changed

+55
-58
lines changed

3 files changed

+55
-58
lines changed

compiler/rustc_mir/src/transform/check_consts/ops.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
3-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
3+
use rustc_errors::{struct_span_err, DiagnosticBuilder};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::DefId;
66
use rustc_session::config::nightly_options;
@@ -10,56 +10,6 @@ use rustc_span::{Span, Symbol};
1010

1111
use super::ConstCx;
1212

13-
/// Emits an error and returns `true` if `op` is not allowed in the given const context.
14-
pub fn non_const<O: NonConstOp>(ccx: &ConstCx<'_, '_>, op: O, span: Span) -> bool {
15-
debug!("illegal_op: op={:?}", op);
16-
17-
let gate = match op.status_in_item(ccx) {
18-
Status::Allowed => return false,
19-
20-
Status::Unstable(gate) if ccx.tcx.features().enabled(gate) => {
21-
let unstable_in_stable = ccx.is_const_stable_const_fn()
22-
&& !super::allow_internal_unstable(ccx.tcx, ccx.def_id.to_def_id(), gate);
23-
24-
if unstable_in_stable {
25-
ccx.tcx.sess
26-
.struct_span_err(
27-
span,
28-
&format!("const-stable function cannot use `#[feature({})]`", gate.as_str()),
29-
)
30-
.span_suggestion(
31-
ccx.body.span,
32-
"if it is not part of the public API, make this function unstably const",
33-
concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n').to_owned(),
34-
Applicability::HasPlaceholders,
35-
)
36-
.span_suggestion(
37-
ccx.body.span,
38-
"otherwise `#[allow_internal_unstable]` can be used to bypass stability checks",
39-
format!("#[allow_internal_unstable({})]", gate),
40-
Applicability:: MaybeIncorrect,
41-
)
42-
.emit();
43-
}
44-
45-
return unstable_in_stable;
46-
}
47-
48-
Status::Unstable(gate) => Some(gate),
49-
Status::Forbidden => None,
50-
};
51-
52-
if ccx.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you {
53-
ccx.tcx.sess.miri_unleashed_feature(span, gate);
54-
return false;
55-
}
56-
57-
let mut err = op.build_error(ccx, span);
58-
assert!(err.is_error());
59-
err.emit();
60-
true
61-
}
62-
6313
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6414
pub enum Status {
6515
Allowed,

compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::mir::{self, BasicBlock, Location};
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::Span;
66

7-
use super::ops;
7+
use super::ops::{self, NonConstOp};
88
use super::qualifs::{NeedsDrop, Qualif};
99
use super::validation::Qualifs;
1010
use super::ConstCx;
@@ -56,7 +56,7 @@ impl std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
5656

5757
impl CheckLiveDrops<'mir, 'tcx> {
5858
fn check_live_drop(&self, span: Span) {
59-
ops::non_const(self.ccx, ops::LiveDrop { dropped_at: None }, span);
59+
ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
6060
}
6161
}
6262

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
3-
use rustc_errors::struct_span_err;
3+
use rustc_errors::{struct_span_err, Applicability};
44
use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_hir::{self as hir, HirId, LangItem};
66
use rustc_infer::infer::TyCtxtInferExt;
@@ -11,13 +11,13 @@ use rustc_middle::ty::subst::GenericArgKind;
1111
use rustc_middle::ty::{
1212
self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt, TypeAndMut,
1313
};
14-
use rustc_span::{sym, Span};
14+
use rustc_span::{sym, Span, Symbol};
1515
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
1616
use rustc_trait_selection::traits::{self, TraitEngine};
1717

1818
use std::ops::Deref;
1919

20-
use super::ops::{self, NonConstOp};
20+
use super::ops::{self, NonConstOp, Status};
2121
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop};
2222
use super::resolver::FlowSensitiveAnalysis;
2323
use super::{is_lang_panic_fn, ConstCx, Qualif};
@@ -277,8 +277,33 @@ impl Validator<'mir, 'tcx> {
277277
return;
278278
}
279279

280-
let err_emitted = ops::non_const(self.ccx, op, span);
281-
if err_emitted && O::STOPS_CONST_CHECKING {
280+
let gate = match op.status_in_item(self.ccx) {
281+
Status::Allowed => return,
282+
283+
Status::Unstable(gate) if self.tcx.features().enabled(gate) => {
284+
let unstable_in_stable = self.ccx.is_const_stable_const_fn()
285+
&& !super::allow_internal_unstable(self.tcx, self.def_id.to_def_id(), gate);
286+
if unstable_in_stable {
287+
emit_unstable_in_stable_error(self.ccx, span, gate);
288+
}
289+
290+
return;
291+
}
292+
293+
Status::Unstable(gate) => Some(gate),
294+
Status::Forbidden => None,
295+
};
296+
297+
if self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you {
298+
self.tcx.sess.miri_unleashed_feature(span, gate);
299+
return;
300+
}
301+
302+
let mut err = op.build_error(self.ccx, span);
303+
assert!(err.is_error());
304+
err.emit();
305+
306+
if O::STOPS_CONST_CHECKING {
282307
self.const_checking_stopped = true;
283308
}
284309
}
@@ -892,3 +917,25 @@ fn is_async_fn(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
892917
.fn_sig_by_hir_id(hir_id)
893918
.map_or(false, |sig| sig.header.asyncness == hir::IsAsync::Async)
894919
}
920+
921+
fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) {
922+
ccx.tcx
923+
.sess
924+
.struct_span_err(
925+
span,
926+
&format!("const-stable function cannot use `#[feature({})]`", gate.as_str()),
927+
)
928+
.span_suggestion(
929+
ccx.body.span,
930+
"if it is not part of the public API, make this function unstably const",
931+
concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n').to_owned(),
932+
Applicability::HasPlaceholders,
933+
)
934+
.span_suggestion(
935+
ccx.body.span,
936+
"otherwise `#[allow_internal_unstable]` can be used to bypass stability checks",
937+
format!("#[allow_internal_unstable({})]", gate),
938+
Applicability::MaybeIncorrect,
939+
)
940+
.emit();
941+
}

0 commit comments

Comments
 (0)