Skip to content

Commit 934079f

Browse files
Migrate unstable-in-stable diagnostic
1 parent 34d6f08 commit 934079f

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use rustc_macros::SessionDiagnostic;
2+
use rustc_span::Span;
3+
4+
#[derive(SessionDiagnostic)]
5+
#[error(const_eval::unstable_in_stable)]
6+
pub(crate) struct UnstableInStable {
7+
pub gate: String,
8+
#[primary_span]
9+
pub span: Span,
10+
#[suggestion(
11+
const_eval::unstable_sugg,
12+
code = "#[rustc_const_unstable(feature = \"...\", issue = \"...\")]\n",
13+
applicability = "has-placeholders"
14+
)]
15+
#[suggestion(
16+
const_eval::bypass_sugg,
17+
code = "#[rustc_allow_const_fn_unstable({gate})]\n",
18+
applicability = "has-placeholders"
19+
)]
20+
pub attr_span: Span,
21+
}

compiler/rustc_const_eval/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern crate tracing;
3131
extern crate rustc_middle;
3232

3333
pub mod const_eval;
34+
mod errors;
3435
pub mod interpret;
3536
pub mod transform;
3637
pub mod util;

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+3-20
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::{Applicability, Diagnostic, ErrorGuaranteed};
3+
use rustc_errors::{Diagnostic, ErrorGuaranteed};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::DefId;
66
use rustc_index::bit_set::BitSet;
@@ -24,6 +24,7 @@ use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop, NeedsNonConstDro
2424
use super::resolver::FlowSensitiveAnalysis;
2525
use super::{ConstCx, Qualif};
2626
use crate::const_eval::is_unstable_const_fn;
27+
use crate::errors::UnstableInStable;
2728

2829
type QualifResults<'mir, 'tcx, Q> =
2930
rustc_mir_dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'mir, 'mir, 'tcx, Q>>;
@@ -1026,23 +1027,5 @@ fn is_int_bool_or_char(ty: Ty<'_>) -> bool {
10261027
fn emit_unstable_in_stable_error(ccx: &ConstCx<'_, '_>, span: Span, gate: Symbol) {
10271028
let attr_span = ccx.tcx.def_span(ccx.def_id()).shrink_to_lo();
10281029

1029-
ccx.tcx
1030-
.sess
1031-
.struct_span_err(
1032-
span,
1033-
&format!("const-stable function cannot use `#[feature({})]`", gate.as_str()),
1034-
)
1035-
.span_suggestion(
1036-
attr_span,
1037-
"if it is not part of the public API, make this function unstably const",
1038-
concat!(r#"#[rustc_const_unstable(feature = "...", issue = "...")]"#, '\n'),
1039-
Applicability::HasPlaceholders,
1040-
)
1041-
.span_suggestion(
1042-
attr_span,
1043-
"otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks",
1044-
format!("#[rustc_allow_const_fn_unstable({})]\n", gate),
1045-
Applicability::MaybeIncorrect,
1046-
)
1047-
.emit();
1030+
ccx.tcx.sess.emit_err(UnstableInStable { gate: gate.to_string(), span, attr_span });
10481031
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const-eval-unstable-in-stable =
2+
const-stable function cannot use `#[feature({$gate})]`
3+
.unstable-sugg = if it is not part of the public API, make this function unstably const
4+
.bypass-sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks

compiler/rustc_error_messages/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ fluent_messages! {
3737
parser => "../locales/en-US/parser.ftl",
3838
privacy => "../locales/en-US/privacy.ftl",
3939
typeck => "../locales/en-US/typeck.ftl",
40+
const_eval => "../locales/en-US/const_eval.ftl",
4041
}
4142

4243
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};

0 commit comments

Comments
 (0)