Skip to content

Commit 3ed1403

Browse files
committed
Reinstate the error-code error over the feature gate error
1 parent 4158e58 commit 3ed1403

File tree

13 files changed

+23
-29
lines changed

13 files changed

+23
-29
lines changed

compiler/rustc_error_codes/src/error_codes/E0492.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ A borrow of a constant containing interior mutability was attempted.
33
Erroneous code example:
44

55
```compile_fail,E0492
6-
#![feature(const_refs_to_cell)]
76
use std::sync::atomic::AtomicUsize;
87
98
const A: AtomicUsize = AtomicUsize::new(0);
@@ -31,7 +30,6 @@ static B: &'static AtomicUsize = &A; // ok!
3130
You can also have this error while using a cell type:
3231

3332
```compile_fail,E0492
34-
#![feature(const_refs_to_cell)]
3533
use std::cell::Cell;
3634
3735
const A: Cell<usize> = Cell::new(1);

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,18 @@ impl NonConstOp for LiveDrop {
209209
}
210210

211211
#[derive(Debug)]
212-
pub struct CellBorrowBehindRef;
213-
impl NonConstOp for CellBorrowBehindRef {
212+
/// A borrow of a type that contains an `UnsafeCell` somewhere. The borrow never escapes to
213+
/// the final value of the constant.
214+
pub struct TransientCellBorrow;
215+
impl NonConstOp for TransientCellBorrow {
214216
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
215217
Status::Unstable(sym::const_refs_to_cell)
216218
}
219+
fn importance(&self) -> DiagnosticImportance {
220+
// The cases that cannot possibly work will already emit a `CellBorrow`, so we should
221+
// not additionally emit a feature gate error if activating the feature gate won't work.
222+
DiagnosticImportance::Secondary
223+
}
217224
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
218225
feature_err(
219226
&ccx.tcx.sess.parse_sess,
@@ -225,19 +232,18 @@ impl NonConstOp for CellBorrowBehindRef {
225232
}
226233

227234
#[derive(Debug)]
235+
/// A borrow of a type that contains an `UnsafeCell` somewhere. The borrow escapes to
236+
/// the final value of the constant, and thus we cannot allow this (for now). We may allow
237+
/// it in the future for static items.
228238
pub struct CellBorrow;
229239
impl NonConstOp for CellBorrow {
230240
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
231241
match ccx.const_kind() {
232-
// The borrow checker does a much better job at handling these than we do
233-
hir::ConstContext::ConstFn => Status::Allowed,
242+
// The borrow checker does a much better job at handling these than we do.
243+
hir::ConstContext::ConstFn => Status::Unstable(sym::const_refs_to_cell),
234244
_ => Status::Forbidden,
235245
}
236246
}
237-
fn importance(&self) -> DiagnosticImportance {
238-
// The problematic cases will already emit a `CellBorrowBehindRef`
239-
DiagnosticImportance::Secondary
240-
}
241247
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
242248
struct_span_err!(
243249
ccx.tcx.sess,

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
587587
// Note: This is only sound if every local that has a `StorageDead` has a
588588
// `StorageDead` in every control flow path leading to a `return` terminator.
589589
if self.local_has_storage_dead(place.local) {
590-
self.check_op(ops::CellBorrowBehindRef);
590+
self.check_op(ops::TransientCellBorrow);
591591
} else {
592592
self.check_op(ops::CellBorrow);
593593
}

src/test/ui/consts/partial_qualif.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_refs_to_cell)]
2-
31
use std::cell::Cell;
42

53
const FOO: &(Cell<usize>, bool) = {

src/test/ui/consts/partial_qualif.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2-
--> $DIR/partial_qualif.rs:8:5
2+
--> $DIR/partial_qualif.rs:6:5
33
|
44
LL | &{a}
55
| ^^^^

src/test/ui/consts/qualif_overwrite.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_refs_to_cell)]
2-
31
use std::cell::Cell;
42

53
// this is overly conservative. The reset to `None` should clear `a` of all qualifications

src/test/ui/consts/qualif_overwrite.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2-
--> $DIR/qualif_overwrite.rs:12:5
2+
--> $DIR/qualif_overwrite.rs:10:5
33
|
44
LL | &{a}
55
| ^^^^

src/test/ui/consts/qualif_overwrite_2.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_refs_to_cell)]
2-
31
use std::cell::Cell;
42

53
// const qualification is not smart enough to know about fields and always assumes that there might

src/test/ui/consts/qualif_overwrite_2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2-
--> $DIR/qualif_overwrite_2.rs:10:5
2+
--> $DIR/qualif_overwrite_2.rs:8:5
33
|
44
LL | &{a.0}
55
| ^^^^^^

src/test/ui/error-codes/E0492.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_refs_to_cell)]
2-
31
use std::sync::atomic::AtomicUsize;
42

53
const A: AtomicUsize = AtomicUsize::new(0);

src/test/ui/error-codes/E0492.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2-
--> $DIR/E0492.rs:6:33
2+
--> $DIR/E0492.rs:4:33
33
|
44
LL | const B: &'static AtomicUsize = &A;
55
| ^^
66

77
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
8-
--> $DIR/E0492.rs:7:34
8+
--> $DIR/E0492.rs:5:34
99
|
1010
LL | static C: &'static AtomicUsize = &A;
1111
| ^^

src/test/ui/issues/issue-17718-const-borrow.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(const_refs_to_cell)]
2-
31
use std::cell::UnsafeCell;
42

53
const A: UnsafeCell<usize> = UnsafeCell::new(1);

src/test/ui/issues/issue-17718-const-borrow.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2-
--> $DIR/issue-17718-const-borrow.rs:6:39
2+
--> $DIR/issue-17718-const-borrow.rs:4:39
33
|
44
LL | const B: &'static UnsafeCell<usize> = &A;
55
| ^^
66

77
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
8-
--> $DIR/issue-17718-const-borrow.rs:11:39
8+
--> $DIR/issue-17718-const-borrow.rs:9:39
99
|
1010
LL | const E: &'static UnsafeCell<usize> = &D.a;
1111
| ^^^^
1212

1313
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
14-
--> $DIR/issue-17718-const-borrow.rs:13:23
14+
--> $DIR/issue-17718-const-borrow.rs:11:23
1515
|
1616
LL | const F: &'static C = &D;
1717
| ^^

0 commit comments

Comments
 (0)