Skip to content

Commit c48f482

Browse files
Migrate StaticAccess diagnostic
1 parent ff9fd36 commit c48f482

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

compiler/rustc_const_eval/src/errors.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_hir::ConstContext;
12
use rustc_macros::SessionDiagnostic;
23
use rustc_span::Span;
34

@@ -26,3 +27,15 @@ pub(crate) struct NonConstOpErr {
2627
#[primary_span]
2728
pub span: Span,
2829
}
30+
31+
#[derive(SessionDiagnostic)]
32+
#[error(const_eval::static_access, code = "E0013")]
33+
#[help]
34+
pub(crate) struct StaticAccessErr {
35+
#[primary_span]
36+
pub span: Span,
37+
pub kind: ConstContext,
38+
#[note(const_eval::teach_note)]
39+
#[help(const_eval::teach_help)]
40+
pub teach: Option<()>,
41+
}

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

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Concrete error types for all operations which may be invalid in a certain const context.
22
33
use hir::def_id::LocalDefId;
4-
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
4+
use rustc_errors::{
5+
error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
6+
};
57
use rustc_hir as hir;
68
use rustc_hir::def_id::DefId;
79
use rustc_infer::infer::TyCtxtInferExt;
@@ -20,7 +22,7 @@ use rustc_span::{BytePos, Pos, Span, Symbol};
2022
use rustc_trait_selection::traits::SelectionContext;
2123

2224
use super::ConstCx;
23-
use crate::errors::NonConstOpErr;
25+
use crate::errors::{NonConstOpErr, StaticAccessErr};
2426
use crate::util::{call_kind, CallDesugaringKind, CallKind};
2527

2628
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -731,24 +733,11 @@ impl<'tcx> NonConstOp<'tcx> for StaticAccess {
731733
ccx: &ConstCx<'_, 'tcx>,
732734
span: Span,
733735
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
734-
let mut err = struct_span_err!(
735-
ccx.tcx.sess,
736+
ccx.tcx.sess.create_err(StaticAccessErr {
736737
span,
737-
E0013,
738-
"{}s cannot refer to statics",
739-
ccx.const_kind()
740-
);
741-
err.help(
742-
"consider extracting the value of the `static` to a `const`, and referring to that",
743-
);
744-
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
745-
err.note(
746-
"`static` and `const` variables can refer to other `const` variables. \
747-
A `const` variable, however, cannot refer to a `static` variable.",
748-
);
749-
err.help("To fix this, the value can be extracted to a `const` and then used.");
750-
}
751-
err
738+
kind: ccx.const_kind(),
739+
teach: ccx.tcx.sess.teach(&error_code!(E0013)).then_some(()),
740+
})
752741
}
753742
}
754743

compiler/rustc_error_messages/locales/en-US/const_eval.ftl

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,14 @@ const-eval-unstable-in-stable =
44
.bypass-sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
55
66
const-eval-thread-local-access =
7-
thread-local statics cannot be accessed at compile-time
7+
thread-local statics cannot be accessed at compile-time
8+
9+
const-eval-static-access =
10+
{ $kind ->
11+
[constant function] constant functions
12+
[static] statics
13+
*[constant] constants
14+
} cannot refer to statics
15+
.help = consider extracting the value of the `static` to a `const`, and referring to that
16+
.teach-note = `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
17+
.teach-help = To fix this, the value can be extracted to a `const` and then used.

0 commit comments

Comments
 (0)