1
Fork 0

Migrate unstable-in-stable diagnostic

This commit is contained in:
Michael Goulet 2022-06-28 21:26:05 -07:00
parent 34d6f08f4d
commit 934079fd9e
5 changed files with 30 additions and 20 deletions

View file

@ -0,0 +1,21 @@
use rustc_macros::SessionDiagnostic;
use rustc_span::Span;
#[derive(SessionDiagnostic)]
#[error(const_eval::unstable_in_stable)]
pub(crate) struct UnstableInStable {
pub gate: String,
#[primary_span]
pub span: Span,
#[suggestion(
const_eval::unstable_sugg,
code = "#[rustc_const_unstable(feature = \"...\", issue = \"...\")]\n",
applicability = "has-placeholders"
)]
#[suggestion(
const_eval::bypass_sugg,
code = "#[rustc_allow_const_fn_unstable({gate})]\n",
applicability = "has-placeholders"
)]
pub attr_span: Span,
}

View file

@ -31,6 +31,7 @@ extern crate tracing;
extern crate rustc_middle; extern crate rustc_middle;
pub mod const_eval; pub mod const_eval;
mod errors;
pub mod interpret; pub mod interpret;
pub mod transform; pub mod transform;
pub mod util; pub mod util;

View file

@ -1,6 +1,6 @@
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations. //! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
use rustc_errors::{Applicability, Diagnostic, ErrorGuaranteed}; use rustc_errors::{Diagnostic, ErrorGuaranteed};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_index::bit_set::BitSet; use rustc_index::bit_set::BitSet;
@ -24,6 +24,7 @@ use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop, NeedsNonConstDro
use super::resolver::FlowSensitiveAnalysis; use super::resolver::FlowSensitiveAnalysis;
use super::{ConstCx, Qualif}; use super::{ConstCx, Qualif};
use crate::const_eval::is_unstable_const_fn; use crate::const_eval::is_unstable_const_fn;
use crate::errors::UnstableInStable;
type QualifResults<'mir, 'tcx, Q> = type QualifResults<'mir, 'tcx, Q> =
rustc_mir_dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'mir, 'mir, 'tcx, Q>>; rustc_mir_dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'mir, 'mir, 'tcx, Q>>;
@ -1026,23 +1027,5 @@ fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) { fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) {
let attr_span = ccx.tcx.def_span(ccx.def_id()).shrink_to_lo(); let attr_span = ccx.tcx.def_span(ccx.def_id()).shrink_to_lo();
ccx.tcx ccx.tcx.sess.emit_err(UnstableInStable { gate: gate.to_string(), span, attr_span });
.sess
.struct_span_err(
span,
&format!("const-stable function cannot use `#[feature({})]`", gate.as_str()),
)
.span_suggestion(
attr_span,
"if it is not part of the public API, make this function unstably const",
concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n'),
Applicability::HasPlaceholders,
)
.span_suggestion(
attr_span,
"otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks",
format!("#[rustc_allow_const_fn_unstable({})]\n", gate),
Applicability::MaybeIncorrect,
)
.emit();
} }

View file

@ -0,0 +1,4 @@
const-eval-unstable-in-stable =
const-stable function cannot use `#[feature({$gate})]`
.unstable-sugg = if it is not part of the public API, make this function unstably const
.bypass-sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks

View file

@ -37,6 +37,7 @@ fluent_messages! {
parser => "../locales/en-US/parser.ftl", parser => "../locales/en-US/parser.ftl",
privacy => "../locales/en-US/privacy.ftl", privacy => "../locales/en-US/privacy.ftl",
typeck => "../locales/en-US/typeck.ftl", typeck => "../locales/en-US/typeck.ftl",
const_eval => "../locales/en-US/const_eval.ftl",
} }
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES}; pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};