Migrate StaticAccess diagnostic
This commit is contained in:
parent
ff9fd36aa4
commit
c48f482813
3 changed files with 32 additions and 20 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
use rustc_hir::ConstContext;
|
||||||
use rustc_macros::SessionDiagnostic;
|
use rustc_macros::SessionDiagnostic;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
|
@ -26,3 +27,15 @@ pub(crate) struct NonConstOpErr {
|
||||||
#[primary_span]
|
#[primary_span]
|
||||||
pub span: 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<()>,
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
//! Concrete error types for all operations which may be invalid in a certain const context.
|
//! Concrete error types for all operations which may be invalid in a certain const context.
|
||||||
|
|
||||||
use hir::def_id::LocalDefId;
|
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 as hir;
|
||||||
use rustc_hir::def_id::DefId;
|
use rustc_hir::def_id::DefId;
|
||||||
use rustc_infer::infer::TyCtxtInferExt;
|
use rustc_infer::infer::TyCtxtInferExt;
|
||||||
|
@ -20,7 +22,7 @@ use rustc_span::{BytePos, Pos, Span, Symbol};
|
||||||
use rustc_trait_selection::traits::SelectionContext;
|
use rustc_trait_selection::traits::SelectionContext;
|
||||||
|
|
||||||
use super::ConstCx;
|
use super::ConstCx;
|
||||||
use crate::errors::NonConstOpErr;
|
use crate::errors::{NonConstOpErr, StaticAccessErr};
|
||||||
use crate::util::{call_kind, CallDesugaringKind, CallKind};
|
use crate::util::{call_kind, CallDesugaringKind, CallKind};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
@ -731,24 +733,11 @@ impl<'tcx> NonConstOp<'tcx> for StaticAccess {
|
||||||
ccx: &ConstCx<'_, 'tcx>,
|
ccx: &ConstCx<'_, 'tcx>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
|
||||||
let mut err = struct_span_err!(
|
ccx.tcx.sess.create_err(StaticAccessErr {
|
||||||
ccx.tcx.sess,
|
|
||||||
span,
|
span,
|
||||||
E0013,
|
kind: ccx.const_kind(),
|
||||||
"{}s cannot refer to statics",
|
teach: ccx.tcx.sess.teach(&error_code!(E0013)).then_some(()),
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,3 +5,13 @@ const-eval-unstable-in-stable =
|
||||||
|
|
||||||
const-eval-thread-local-access =
|
const-eval-thread-local-access =
|
||||||
thread-local statics cannot be accessed at compile-time
|
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.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue