Skip to content

Commit d6082ee

Browse files
authored
Rollup merge of rust-lang#120933 - RalfJung:const-check-misc, r=oli-obk
check_consts: fix duplicate errors, make importance consistent This is stuff I noticed while working on rust-lang#120932, but it's orthogonal to that PR. r? `@oli-obk`
2 parents ed0aee9 + e13de31 commit d6082ee

14 files changed

+15
-164
lines changed

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

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

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,6 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
409409
fn status_in_item(&self, _: &ConstCx<'_, 'tcx>) -> Status {
410410
Status::Unstable(sym::const_refs_to_cell)
411411
}
412-
fn importance(&self) -> DiagnosticImportance {
413-
// The cases that cannot possibly work will already emit a `CellBorrow`, so we should
414-
// not additionally emit a feature gate error if activating the feature gate won't work.
415-
DiagnosticImportance::Secondary
416-
}
417412
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
418413
ccx.tcx
419414
.sess
@@ -427,6 +422,11 @@ impl<'tcx> NonConstOp<'tcx> for TransientCellBorrow {
427422
/// it in the future for static items.
428423
pub struct CellBorrow;
429424
impl<'tcx> NonConstOp<'tcx> for CellBorrow {
425+
fn importance(&self) -> DiagnosticImportance {
426+
// Most likely the code will try to do mutation with these borrows, which
427+
// triggers its own errors. Only show this one if that does not happen.
428+
DiagnosticImportance::Secondary
429+
}
430430
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
431431
// FIXME: Maybe a more elegant solution to this if else case
432432
if let hir::ConstContext::Static(_) = ccx.const_kind() {
@@ -459,8 +459,8 @@ impl<'tcx> NonConstOp<'tcx> for MutBorrow {
459459
}
460460

461461
fn importance(&self) -> DiagnosticImportance {
462-
// If there were primary errors (like non-const function calls), do not emit further
463-
// errors about mutable references.
462+
// Most likely the code will try to do mutation with these borrows, which
463+
// triggers its own errors. Only show this one if that does not happen.
464464
DiagnosticImportance::Secondary
465465
}
466466

tests/ui/consts/issue-17718-const-bad-values.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const C1: &'static mut [usize] = &mut [];
44
static mut S: usize = 3;
55
const C2: &'static mut usize = unsafe { &mut S };
66
//~^ ERROR: referencing statics in constants
7-
//~| ERROR: referencing statics in constants
87
//~| WARN mutable reference of mutable static is discouraged [static_mut_ref]
98

109
fn main() {}

tests/ui/consts/issue-17718-const-bad-values.stderr

+1-14
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,7 @@ LL | const C2: &'static mut usize = unsafe { &mut S };
3131
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
3232
= help: to fix this, the value can be extracted to a `const` and then used.
3333

34-
error[E0658]: referencing statics in constants is unstable
35-
--> $DIR/issue-17718-const-bad-values.rs:5:46
36-
|
37-
LL | const C2: &'static mut usize = unsafe { &mut S };
38-
| ^
39-
|
40-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
41-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
42-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
43-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
44-
= help: to fix this, the value can be extracted to a `const` and then used.
45-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
46-
47-
error: aborting due to 3 previous errors; 1 warning emitted
34+
error: aborting due to 2 previous errors; 1 warning emitted
4835

4936
Some errors have detailed explanations: E0658, E0764.
5037
For more information about an error, try `rustc --explain E0658`.

tests/ui/consts/miri_unleashed/const_refers_to_static.32bit.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ help: skipping check for `const_refs_to_static` feature
4949
|
5050
LL | const READ_MUT: u32 = unsafe { MUTABLE };
5151
| ^^^^^^^
52-
help: skipping check for `const_refs_to_static` feature
53-
--> $DIR/const_refers_to_static.rs:18:32
54-
|
55-
LL | const READ_MUT: u32 = unsafe { MUTABLE };
56-
| ^^^^^^^
5752
help: skipping check for `const_refs_to_static` feature
5853
--> $DIR/const_refers_to_static.rs:24:18
5954
|

tests/ui/consts/miri_unleashed/const_refers_to_static.64bit.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ help: skipping check for `const_refs_to_static` feature
4949
|
5050
LL | const READ_MUT: u32 = unsafe { MUTABLE };
5151
| ^^^^^^^
52-
help: skipping check for `const_refs_to_static` feature
53-
--> $DIR/const_refers_to_static.rs:18:32
54-
|
55-
LL | const READ_MUT: u32 = unsafe { MUTABLE };
56-
| ^^^^^^^
5752
help: skipping check for `const_refs_to_static` feature
5853
--> $DIR/const_refers_to_static.rs:24:18
5954
|

tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.32bit.stderr

-35
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,6 @@ help: skipping check for `const_refs_to_static` feature
8383
|
8484
LL | unsafe { &static_cross_crate::ZERO }
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^
86-
help: skipping check for `const_refs_to_static` feature
87-
--> $DIR/const_refers_to_static_cross_crate.rs:12:15
88-
|
89-
LL | unsafe { &static_cross_crate::ZERO }
90-
| ^^^^^^^^^^^^^^^^^^^^^^^^
91-
help: skipping check for `const_refs_to_static` feature
92-
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
93-
|
94-
LL | unsafe { &static_cross_crate::ZERO[0] }
95-
| ^^^^^^^^^^^^^^^^^^^^^^^^
96-
help: skipping check for `const_refs_to_static` feature
97-
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
98-
|
99-
LL | unsafe { &static_cross_crate::ZERO[0] }
100-
| ^^^^^^^^^^^^^^^^^^^^^^^^
10186
help: skipping check for `const_refs_to_static` feature
10287
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
10388
|
@@ -113,26 +98,6 @@ help: skipping check for `const_refs_to_static` feature
11398
|
11499
LL | match static_cross_crate::OPT_ZERO {
115100
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116-
help: skipping check for `const_refs_to_static` feature
117-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
118-
|
119-
LL | match static_cross_crate::OPT_ZERO {
120-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121-
help: skipping check for `const_refs_to_static` feature
122-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
123-
|
124-
LL | match static_cross_crate::OPT_ZERO {
125-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126-
help: skipping check for `const_refs_to_static` feature
127-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
128-
|
129-
LL | match static_cross_crate::OPT_ZERO {
130-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131-
help: skipping check for `const_refs_to_static` feature
132-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
133-
|
134-
LL | match static_cross_crate::OPT_ZERO {
135-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
136101

137102
error: aborting due to 8 previous errors; 2 warnings emitted
138103

tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.64bit.stderr

-35
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,6 @@ help: skipping check for `const_refs_to_static` feature
8383
|
8484
LL | unsafe { &static_cross_crate::ZERO }
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^
86-
help: skipping check for `const_refs_to_static` feature
87-
--> $DIR/const_refers_to_static_cross_crate.rs:12:15
88-
|
89-
LL | unsafe { &static_cross_crate::ZERO }
90-
| ^^^^^^^^^^^^^^^^^^^^^^^^
91-
help: skipping check for `const_refs_to_static` feature
92-
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
93-
|
94-
LL | unsafe { &static_cross_crate::ZERO[0] }
95-
| ^^^^^^^^^^^^^^^^^^^^^^^^
96-
help: skipping check for `const_refs_to_static` feature
97-
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
98-
|
99-
LL | unsafe { &static_cross_crate::ZERO[0] }
100-
| ^^^^^^^^^^^^^^^^^^^^^^^^
10186
help: skipping check for `const_refs_to_static` feature
10287
--> $DIR/const_refers_to_static_cross_crate.rs:18:15
10388
|
@@ -113,26 +98,6 @@ help: skipping check for `const_refs_to_static` feature
11398
|
11499
LL | match static_cross_crate::OPT_ZERO {
115100
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116-
help: skipping check for `const_refs_to_static` feature
117-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
118-
|
119-
LL | match static_cross_crate::OPT_ZERO {
120-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
121-
help: skipping check for `const_refs_to_static` feature
122-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
123-
|
124-
LL | match static_cross_crate::OPT_ZERO {
125-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
126-
help: skipping check for `const_refs_to_static` feature
127-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
128-
|
129-
LL | match static_cross_crate::OPT_ZERO {
130-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131-
help: skipping check for `const_refs_to_static` feature
132-
--> $DIR/const_refers_to_static_cross_crate.rs:28:15
133-
|
134-
LL | match static_cross_crate::OPT_ZERO {
135-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
136101

137102
error: aborting due to 8 previous errors; 2 warnings emitted
138103

tests/ui/consts/miri_unleashed/mutable_references_err.32bit.stderr

-15
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ help: skipping check for `const_refs_to_static` feature
114114
|
115115
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
116116
| ^^^
117-
help: skipping check for `const_refs_to_static` feature
118-
--> $DIR/mutable_references_err.rs:32:40
119-
|
120-
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
121-
| ^^^
122117
help: skipping check that does not even have a feature gate
123118
--> $DIR/mutable_references_err.rs:32:35
124119
|
@@ -144,16 +139,6 @@ help: skipping check for `const_refs_to_static` feature
144139
|
145140
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
146141
| ^^^^^^^
147-
help: skipping check for `const_refs_to_static` feature
148-
--> $DIR/mutable_references_err.rs:47:44
149-
|
150-
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
151-
| ^^^^^^^
152-
help: skipping check for `const_refs_to_static` feature
153-
--> $DIR/mutable_references_err.rs:51:45
154-
|
155-
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
156-
| ^^^^^^^^^^^
157142
help: skipping check for `const_refs_to_static` feature
158143
--> $DIR/mutable_references_err.rs:51:45
159144
|

tests/ui/consts/miri_unleashed/mutable_references_err.64bit.stderr

-15
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ help: skipping check for `const_refs_to_static` feature
114114
|
115115
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
116116
| ^^^
117-
help: skipping check for `const_refs_to_static` feature
118-
--> $DIR/mutable_references_err.rs:32:40
119-
|
120-
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
121-
| ^^^
122117
help: skipping check that does not even have a feature gate
123118
--> $DIR/mutable_references_err.rs:32:35
124119
|
@@ -144,16 +139,6 @@ help: skipping check for `const_refs_to_static` feature
144139
|
145140
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
146141
| ^^^^^^^
147-
help: skipping check for `const_refs_to_static` feature
148-
--> $DIR/mutable_references_err.rs:47:44
149-
|
150-
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
151-
| ^^^^^^^
152-
help: skipping check for `const_refs_to_static` feature
153-
--> $DIR/mutable_references_err.rs:51:45
154-
|
155-
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
156-
| ^^^^^^^^^^^
157142
help: skipping check for `const_refs_to_static` feature
158143
--> $DIR/mutable_references_err.rs:51:45
159144
|

tests/ui/feature-gates/feature-gate-const-refs-to-static.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const C1_READ: () = {
66
assert!(*C1 == 0);
77
};
88
const C2: *const i32 = unsafe { std::ptr::addr_of!(S_MUT) }; //~ERROR: referencing statics in constants is unstable
9-
//~^ERROR: referencing statics in constants is unstable
109

1110
fn main() {
1211
}

tests/ui/feature-gates/feature-gate-const-refs-to-static.stderr

+1-14
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ LL | const C2: *const i32 = unsafe { std::ptr::addr_of!(S_MUT) };
2222
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
2323
= help: to fix this, the value can be extracted to a `const` and then used.
2424

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

4027
For more information about this error, try `rustc --explain E0658`.

tests/ui/thread-local/thread-local-static.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const fn g(x: &mut [u32; 8]) {
1212
//~^^ ERROR thread-local statics cannot be accessed
1313
//~| ERROR mutable references are not allowed
1414
//~| ERROR use of mutable static is unsafe
15-
//~| referencing statics
1615
}
1716

1817
fn main() {}

tests/ui/thread-local/thread-local-static.stderr

+1-13
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ error[E0625]: thread-local statics cannot be accessed at compile-time
3737
LL | std::mem::swap(x, &mut STATIC_VAR_2)
3838
| ^^^^^^^^^^^^
3939

40-
error[E0658]: referencing statics in constant functions is unstable
41-
--> $DIR/thread-local-static.rs:10:28
42-
|
43-
LL | std::mem::swap(x, &mut STATIC_VAR_2)
44-
| ^^^^^^^^^^^^
45-
|
46-
= note: see issue #119618 <https://github.com/rust-lang/rust/issues/119618> for more information
47-
= help: add `#![feature(const_refs_to_static)]` to the crate attributes to enable
48-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
49-
= note: `static` and `const` variables can refer to other `const` variables. A `const` variable, however, cannot refer to a `static` variable.
50-
= help: to fix this, the value can be extracted to a `const` and then used.
51-
5240
error[E0658]: mutable references are not allowed in constant functions
5341
--> $DIR/thread-local-static.rs:10:23
5442
|
@@ -59,7 +47,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
5947
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
6048
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
6149

62-
error: aborting due to 5 previous errors; 1 warning emitted
50+
error: aborting due to 4 previous errors; 1 warning emitted
6351

6452
Some errors have detailed explanations: E0133, E0625, E0658.
6553
For more information about an error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)