]> git.lizzy.rs Git - rust.git/commitdiff
Migrate StaticAccess diagnostic
authorMichael Goulet <michael@errs.io>
Wed, 29 Jun 2022 05:32:32 +0000 (22:32 -0700)
committerMichael Goulet <michael@errs.io>
Fri, 8 Jul 2022 03:47:46 +0000 (03:47 +0000)
compiler/rustc_const_eval/src/errors.rs
compiler/rustc_const_eval/src/transform/check_consts/ops.rs
compiler/rustc_error_messages/locales/en-US/const_eval.ftl

index 40398011f1f77fc1d2a2146e2f1944ee20f8cdf0..3a498f84ec58e6d414a8078c015643a2641fd5e9 100644 (file)
@@ -1,3 +1,4 @@
+use rustc_hir::ConstContext;
 use rustc_macros::SessionDiagnostic;
 use rustc_span::Span;
 
@@ -26,3 +27,15 @@ pub(crate) struct NonConstOpErr {
     #[primary_span]
     pub span: Span,
 }
+
+#[derive(SessionDiagnostic)]
+#[error(const_eval::static_access, code = "E0013")]
+#[help]
+pub(crate) struct StaticAccessErr {
+    #[primary_span]
+    pub span: Span,
+    pub kind: ConstContext,
+    #[note(const_eval::teach_note)]
+    #[help(const_eval::teach_help)]
+    pub teach: Option<()>,
+}
index 01080560021ccfc0e5fc8e8e88e670fb0d6532f9..d104fdd59c5ee18e6142aa2b55b8fa0d807ef085 100644 (file)
@@ -1,7 +1,9 @@
 //! Concrete error types for all operations which may be invalid in a certain const context.
 
 use hir::def_id::LocalDefId;
-use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed};
+use rustc_errors::{
+    error_code, struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed,
+};
 use rustc_hir as hir;
 use rustc_hir::def_id::DefId;
 use rustc_infer::infer::TyCtxtInferExt;
@@ -20,7 +22,7 @@
 use rustc_trait_selection::traits::SelectionContext;
 
 use super::ConstCx;
-use crate::errors::NonConstOpErr;
+use crate::errors::{NonConstOpErr, StaticAccessErr};
 use crate::util::{call_kind, CallDesugaringKind, CallKind};
 
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -731,24 +733,11 @@ fn build_error(
         ccx: &ConstCx<'_, 'tcx>,
         span: Span,
     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
-        let mut err = struct_span_err!(
-            ccx.tcx.sess,
+        ccx.tcx.sess.create_err(StaticAccessErr {
             span,
-            E0013,
-            "{}s cannot refer to statics",
-            ccx.const_kind()
-        );
-        err.help(
-            "consider extracting the value of the `static` to a `const`, and referring to that",
-        );
-        if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
-            err.note(
-                "`static` and `const` variables can refer to other `const` variables. \
-                    A `const` variable, however, cannot refer to a `static` variable.",
-            );
-            err.help("To fix this, the value can be extracted to a `const` and then used.");
-        }
-        err
+            kind: ccx.const_kind(),
+            teach: ccx.tcx.sess.teach(&error_code!(E0013)).then_some(()),
+        })
     }
 }
 
index 897ddce7e99ba30943a31f427e1af39742517968..66058aa1769eddc6ab34d7e1853774c2b8e232be 100644 (file)
@@ -4,4 +4,14 @@ const-eval-unstable-in-stable =
     .bypass-sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
 
 const-eval-thread-local-access =
-    thread-local statics cannot be accessed at compile-time
\ No newline at end of file
+    thread-local statics cannot be accessed at compile-time
+
+const-eval-static-access =
+    { $kind -> 
+        [constant function] constant functions
+        [static] statics
+        *[constant] constants
+    } cannot refer to statics
+    .help = consider extracting the value of the `static` to a `const`, and referring to that
+    .teach-note = `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
+    .teach-help = To fix this, the value can be extracted to a `const` and then used.