Rollup merge of #120933 - RalfJung:const-check-misc, r=oli-obk

check_consts: fix duplicate errors, make importance consistent

This is stuff I noticed while working on https://github.com/rust-lang/rust/pull/120932, but it's orthogonal to that PR.

r? ``@oli-obk``
This commit is contained in:
Matthias Krüger 2024-02-12 18:04:09 +01:00 committed by GitHub
commit 3f67169a5f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 15 additions and 164 deletions

View file

@ -619,9 +619,11 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
if base_ty.is_unsafe_ptr() { if base_ty.is_unsafe_ptr() {
if place_ref.projection.is_empty() { if place_ref.projection.is_empty() {
let decl = &self.body.local_decls[place_ref.local]; let decl = &self.body.local_decls[place_ref.local];
if let LocalInfo::StaticRef { def_id, .. } = *decl.local_info() { // If this is a static, then this is not really dereferencing a pointer,
let span = decl.source_info.span; // just directly accessing a static. That is not subject to any feature
self.check_static(def_id, span); // gates (except for the one about whether statics can even be used, but
// that is checked already by `visit_operand`).
if let LocalInfo::StaticRef { .. } = *decl.local_info() {
return; return;
} }
} }

View file

@ -409,11 +409,6 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status { fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
Status::Unstable(sym::const_refs_to_cell) Status::Unstable(sym::const_refs_to_cell)
} }
fn importance(&self) -> DiagnosticImportance {
// The cases that cannot possibly work will already emit a `CellBorrow`, so we should
// not additionally emit a feature gate error if activating the feature gate won't work.
DiagnosticImportance::Secondary
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> { fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
ccx.tcx ccx.tcx
.sess .sess
@ -427,6 +422,11 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
/// it in the future for static items. /// it in the future for static items.
pub struct CellBorrow; pub struct CellBorrow;
impl<'tcx> NonConstOp<'tcx> for CellBorrow { impl<'tcx> NonConstOp<'tcx> for CellBorrow {
fn importance(&self) -> DiagnosticImportance {
// Most likely the code will try to do mutation with these borrows, which
// triggers its own errors. Only show this one if that does not happen.
DiagnosticImportance::Secondary
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> { fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
// FIXME: Maybe a more elegant solution to this if else case // FIXME: Maybe a more elegant solution to this if else case
if let hir::ConstContext::Static(_) = ccx.const_kind() { if let hir::ConstContext::Static(_) = ccx.const_kind() {
@ -459,8 +459,8 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
} }
fn importance(&self) -> DiagnosticImportance { fn importance(&self) -> DiagnosticImportance {
// If there were primary errors (like non-const function calls), do not emit further // Most likely the code will try to do mutation with these borrows, which
// errors about mutable references. // triggers its own errors. Only show this one if that does not happen.
DiagnosticImportance::Secondary DiagnosticImportance::Secondary
} }

View file

@ -4,7 +4,6 @@ const C1: &'static mut [usize] = &mut [];
static mut S: usize = 3; static mut S: usize = 3;
const C2: &'static mut usize = unsafe { &mut S }; const C2: &'static mut usize = unsafe { &mut S };
//~^ ERROR: referencing statics in constants //~^ ERROR: referencing statics in constants
//~| ERROR: referencing statics in constants
//~| WARN mutable reference of mutable static is discouraged [static_mut_ref] //~| WARN mutable reference of mutable static is discouraged [static_mut_ref]
fn main() {} fn main() {}

View file

@ -31,20 +31,7 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable. = note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used. = help: to fix this, the value can be extracted to a `const` and then used.
error[E0658]: referencing statics in constants is unstable error: aborting due to 2 previous errors; 1 warning emitted
--> $DIR/issue-17718-const-bad-values.rs:5:46
|
LL | const C2: &'static mut usize = unsafe { &mut S };
| ^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 3 previous errors; 1 warning emitted
Some errors have detailed explanations: E0658, E0764. Some errors have detailed explanations: E0658, E0764.
For more information about an error, try `rustc --explain E0658`. For more information about an error, try `rustc --explain E0658`.

View file

@ -49,11 +49,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | const READ_MUT: u32 = unsafe { MUTABLE }; LL | const READ_MUT: u32 = unsafe { MUTABLE };
| ^^^^^^^ | ^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static.rs:18:32
|
LL | const READ_MUT: u32 = unsafe { MUTABLE };
| ^^^^^^^
help: skipping check for `const_refs_to_static` feature help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static.rs:24:18 --> $DIR/const_refers_to_static.rs:24:18
| |

View file

@ -49,11 +49,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | const READ_MUT: u32 = unsafe { MUTABLE }; LL | const READ_MUT: u32 = unsafe { MUTABLE };
| ^^^^^^^ | ^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static.rs:18:32
|
LL | const READ_MUT: u32 = unsafe { MUTABLE };
| ^^^^^^^
help: skipping check for `const_refs_to_static` feature help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static.rs:24:18 --> $DIR/const_refers_to_static.rs:24:18
| |

View file

@ -83,21 +83,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | unsafe { &static_cross_crate::ZERO } LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:12:15
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:18:15 --> $DIR/const_refers_to_static_cross_crate.rs:18:15
| |
@ -113,26 +98,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | match static_cross_crate::OPT_ZERO { LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 8 previous errors; 2 warnings emitted error: aborting due to 8 previous errors; 2 warnings emitted

View file

@ -83,21 +83,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | unsafe { &static_cross_crate::ZERO } LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:12:15
|
LL | unsafe { &static_cross_crate::ZERO }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
|
LL | unsafe { &static_cross_crate::ZERO[0] }
| ^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:18:15 --> $DIR/const_refers_to_static_cross_crate.rs:18:15
| |
@ -113,26 +98,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | match static_cross_crate::OPT_ZERO { LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
|
LL | match static_cross_crate::OPT_ZERO {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 8 previous errors; 2 warnings emitted error: aborting due to 8 previous errors; 2 warnings emitted

View file

@ -114,11 +114,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO }; LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
| ^^^ | ^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:32:40
|
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
| ^^^
help: skipping check that does not even have a feature gate help: skipping check that does not even have a feature gate
--> $DIR/mutable_references_err.rs:32:35 --> $DIR/mutable_references_err.rs:32:35
| |
@ -144,16 +139,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE }; LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
| ^^^^^^^ | ^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:47:44
|
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
| ^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:51:45
|
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
| ^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:51:45 --> $DIR/mutable_references_err.rs:51:45
| |

View file

@ -114,11 +114,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO }; LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
| ^^^ | ^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:32:40
|
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
| ^^^
help: skipping check that does not even have a feature gate help: skipping check that does not even have a feature gate
--> $DIR/mutable_references_err.rs:32:35 --> $DIR/mutable_references_err.rs:32:35
| |
@ -144,16 +139,6 @@ help: skipping check for `const_refs_to_static` feature
| |
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE }; LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
| ^^^^^^^ | ^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:47:44
|
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
| ^^^^^^^
help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:51:45
|
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
| ^^^^^^^^^^^
help: skipping check for `const_refs_to_static` feature help: skipping check for `const_refs_to_static` feature
--> $DIR/mutable_references_err.rs:51:45 --> $DIR/mutable_references_err.rs:51:45
| |

View file

@ -6,7 +6,6 @@ const C1_READ: () = {
assert!(*C1 == 0); assert!(*C1 == 0);
}; };
const C2: *const i32 = unsafe { std::ptr::addr_of!(S_MUT) }; //~ERROR: referencing statics in constants is unstable const C2: *const i32 = unsafe { std::ptr::addr_of!(S_MUT) }; //~ERROR: referencing statics in constants is unstable
//~^ERROR: referencing statics in constants is unstable
fn main() { fn main() {
} }

View file

@ -22,19 +22,6 @@ LL | const C2: *const i32 = unsafe { std::ptr::addr_of!(S_MUT) };
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable. = note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used. = help: to fix this, the value can be extracted to a `const` and then used.
error[E0658]: referencing statics in constants is unstable error: aborting due to 2 previous errors
--> $DIR/feature-gate-const-refs-to-static.rs:8:52
|
LL | const C2: *const i32 = unsafe { std::ptr::addr_of!(S_MUT) };
| ^^^^^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0658`. For more information about this error, try `rustc --explain E0658`.

View file

@ -12,7 +12,6 @@ const fn g(x: &mut [u32; 8]) {
//~^^ ERROR thread-local statics cannot be accessed //~^^ ERROR thread-local statics cannot be accessed
//~| ERROR mutable references are not allowed //~| ERROR mutable references are not allowed
//~| ERROR use of mutable static is unsafe //~| ERROR use of mutable static is unsafe
//~| referencing statics
} }
fn main() {} fn main() {}

View file

@ -37,18 +37,6 @@ error[E0625]: thread-local statics cannot be accessed at compile-time
LL | std::mem::swap(x, &mut STATIC_VAR_2) LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error[E0658]: referencing statics in constant functions is unstable
--> $DIR/thread-local-static.rs:10:28
|
LL | std::mem::swap(x, &mut STATIC_VAR_2)
| ^^^^^^^^^^^^
|
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
= help: to fix this, the value can be extracted to a `const` and then used.
error[E0658]: mutable references are not allowed in constant functions error[E0658]: mutable references are not allowed in constant functions
--> $DIR/thread-local-static.rs:10:23 --> $DIR/thread-local-static.rs:10:23
| |
@ -59,7 +47,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable = help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 5 previous errors; 1 warning emitted error: aborting due to 4 previous errors; 1 warning emitted
Some errors have detailed explanations: E0133, E0625, E0658. Some errors have detailed explanations: E0133, E0625, E0658.
For more information about an error, try `rustc --explain E0133`. For more information about an error, try `rustc --explain E0133`.