diff --git a/compiler/rustc_mir/src/transform/check_consts/ops.rs b/compiler/rustc_mir/src/transform/check_consts/ops.rs index c111bdd707c..3aa9aca97c2 100644 --- a/compiler/rustc_mir/src/transform/check_consts/ops.rs +++ b/compiler/rustc_mir/src/transform/check_consts/ops.rs @@ -1,6 +1,6 @@ //! Concrete error types for all operations which may be invalid in a certain const context. -use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder}; +use rustc_errors::{struct_span_err, DiagnosticBuilder}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_session::config::nightly_options; @@ -10,56 +10,6 @@ use rustc_span::{Span, Symbol}; use super::ConstCx; -/// Emits an error and returns `true` if `op` is not allowed in the given const context. -pub fn non_const(ccx: &ConstCx<'_, '_>, op: O, span: Span) -> bool { - debug!("illegal_op: op={:?}", op); - - let gate = match op.status_in_item(ccx) { - Status::Allowed => return false, - - Status::Unstable(gate) if ccx.tcx.features().enabled(gate) => { - let unstable_in_stable = ccx.is_const_stable_const_fn() - && !super::allow_internal_unstable(ccx.tcx, ccx.def_id.to_def_id(), gate); - - if unstable_in_stable { - ccx.tcx.sess - .struct_span_err( - span, - &format!("const-stable function cannot use `#[feature({})]`", gate.as_str()), - ) - .span_suggestion( - ccx.body.span, - "if it is not part of the public API, make this function unstably const", - concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n').to_owned(), - Applicability::HasPlaceholders, - ) - .span_suggestion( - ccx.body.span, - "otherwise `#[allow_internal_unstable]` can be used to bypass stability checks", - format!("#[allow_internal_unstable({})]", gate), - Applicability:: MaybeIncorrect, - ) - .emit(); - } - - return unstable_in_stable; - } - - Status::Unstable(gate) => Some(gate), - Status::Forbidden => None, - }; - - if ccx.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you { - ccx.tcx.sess.miri_unleashed_feature(span, gate); - return false; - } - - let mut err = op.build_error(ccx, span); - assert!(err.is_error()); - err.emit(); - true -} - #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Status { Allowed, diff --git a/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs b/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs index 0c171bbc464..9b2568d5abb 100644 --- a/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs +++ b/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs @@ -4,7 +4,7 @@ use rustc_middle::mir::{self, BasicBlock, Location}; use rustc_middle::ty::TyCtxt; use rustc_span::Span; -use super::ops; +use super::ops::{self, NonConstOp}; use super::qualifs::{NeedsDrop, Qualif}; use super::validation::Qualifs; use super::ConstCx; @@ -56,7 +56,7 @@ impl std::ops::Deref for CheckLiveDrops<'mir, 'tcx> { impl CheckLiveDrops<'mir, 'tcx> { fn check_live_drop(&self, span: Span) { - ops::non_const(self.ccx, ops::LiveDrop { dropped_at: None }, span); + ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit(); } } diff --git a/compiler/rustc_mir/src/transform/check_consts/validation.rs b/compiler/rustc_mir/src/transform/check_consts/validation.rs index bee6aa60360..4266dfb4965 100644 --- a/compiler/rustc_mir/src/transform/check_consts/validation.rs +++ b/compiler/rustc_mir/src/transform/check_consts/validation.rs @@ -1,6 +1,6 @@ //! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations. -use rustc_errors::struct_span_err; +use rustc_errors::{struct_span_err, Applicability}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{self as hir, HirId, LangItem}; use rustc_infer::infer::TyCtxtInferExt; @@ -11,13 +11,13 @@ use rustc_middle::ty::subst::GenericArgKind; use rustc_middle::ty::{ self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt, TypeAndMut, }; -use rustc_span::{sym, Span}; +use rustc_span::{sym, Span, Symbol}; use rustc_trait_selection::traits::error_reporting::InferCtxtExt; use rustc_trait_selection::traits::{self, TraitEngine}; use std::ops::Deref; -use super::ops::{self, NonConstOp}; +use super::ops::{self, NonConstOp, Status}; use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop}; use super::resolver::FlowSensitiveAnalysis; use super::{is_lang_panic_fn, ConstCx, Qualif}; @@ -277,8 +277,33 @@ impl Validator<'mir, 'tcx> { return; } - let err_emitted = ops::non_const(self.ccx, op, span); - if err_emitted && O::STOPS_CONST_CHECKING { + let gate = match op.status_in_item(self.ccx) { + Status::Allowed => return, + + Status::Unstable(gate) if self.tcx.features().enabled(gate) => { + let unstable_in_stable = self.ccx.is_const_stable_const_fn() + && !super::allow_internal_unstable(self.tcx, self.def_id.to_def_id(), gate); + if unstable_in_stable { + emit_unstable_in_stable_error(self.ccx, span, gate); + } + + return; + } + + Status::Unstable(gate) => Some(gate), + Status::Forbidden => None, + }; + + if self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you { + self.tcx.sess.miri_unleashed_feature(span, gate); + return; + } + + let mut err = op.build_error(self.ccx, span); + assert!(err.is_error()); + err.emit(); + + if O::STOPS_CONST_CHECKING { self.const_checking_stopped = true; } } @@ -892,3 +917,25 @@ fn is_async_fn(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { .fn_sig_by_hir_id(hir_id) .map_or(false, |sig| sig.header.asyncness == hir::IsAsync::Async) } + +fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) { + ccx.tcx + .sess + .struct_span_err( + span, + &format!("const-stable function cannot use `#[feature({})]`", gate.as_str()), + ) + .span_suggestion( + ccx.body.span, + "if it is not part of the public API, make this function unstably const", + concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n').to_owned(), + Applicability::HasPlaceholders, + ) + .span_suggestion( + ccx.body.span, + "otherwise `#[allow_internal_unstable]` can be used to bypass stability checks", + format!("#[allow_internal_unstable({})]", gate), + Applicability::MaybeIncorrect, + ) + .emit(); +}