Reinstate the error-code error over the feature gate error

This commit is contained in:
oli 2020-12-30 17:20:38 +00:00
parent 4158e58d79
commit 3ed14033f7
13 changed files with 23 additions and 29 deletions

View File

@ -3,7 +3,6 @@ A borrow of a constant containing interior mutability was attempted.
Erroneous code example:
```compile_fail,E0492
#![feature(const_refs_to_cell)]
use std::sync::atomic::AtomicUsize;
const A: AtomicUsize = AtomicUsize::new(0);
@ -31,7 +30,6 @@ static B: &'static AtomicUsize = &A; // ok!
You can also have this error while using a cell type:
```compile_fail,E0492
#![feature(const_refs_to_cell)]
use std::cell::Cell;
const A: Cell<usize> = Cell::new(1);

View File

@ -209,11 +209,18 @@ impl NonConstOp for LiveDrop {
}
#[derive(Debug)]
pub struct CellBorrowBehindRef;
impl NonConstOp for CellBorrowBehindRef {
/// A borrow of a type that contains an `UnsafeCell` somewhere. The borrow never escapes to
/// the final value of the constant.
pub struct TransientCellBorrow;
impl NonConstOp for TransientCellBorrow {
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
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> {
feature_err(
&ccx.tcx.sess.parse_sess,
@ -225,19 +232,18 @@ impl NonConstOp for CellBorrowBehindRef {
}
#[derive(Debug)]
/// A borrow of a type that contains an `UnsafeCell` somewhere. The borrow escapes to
/// the final value of the constant, and thus we cannot allow this (for now). We may allow
/// it in the future for static items.
pub struct CellBorrow;
impl NonConstOp for CellBorrow {
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
match ccx.const_kind() {
// The borrow checker does a much better job at handling these than we do
hir::ConstContext::ConstFn => Status::Allowed,
// The borrow checker does a much better job at handling these than we do.
hir::ConstContext::ConstFn => Status::Unstable(sym::const_refs_to_cell),
_ => Status::Forbidden,
}
}
fn importance(&self) -> DiagnosticImportance {
// The problematic cases will already emit a `CellBorrowBehindRef`
DiagnosticImportance::Secondary
}
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
struct_span_err!(
ccx.tcx.sess,

View File

@ -587,7 +587,7 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
// Note: This is only sound if every local that has a `StorageDead` has a
// `StorageDead` in every control flow path leading to a `return` terminator.
if self.local_has_storage_dead(place.local) {
self.check_op(ops::CellBorrowBehindRef);
self.check_op(ops::TransientCellBorrow);
} else {
self.check_op(ops::CellBorrow);
}

View File

@ -1,5 +1,3 @@
#![feature(const_refs_to_cell)]
use std::cell::Cell;
const FOO: &(Cell<usize>, bool) = {

View File

@ -1,5 +1,5 @@
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/partial_qualif.rs:8:5
--> $DIR/partial_qualif.rs:6:5
|
LL | &{a}
| ^^^^

View File

@ -1,5 +1,3 @@
#![feature(const_refs_to_cell)]
use std::cell::Cell;
// this is overly conservative. The reset to `None` should clear `a` of all qualifications

View File

@ -1,5 +1,5 @@
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/qualif_overwrite.rs:12:5
--> $DIR/qualif_overwrite.rs:10:5
|
LL | &{a}
| ^^^^

View File

@ -1,5 +1,3 @@
#![feature(const_refs_to_cell)]
use std::cell::Cell;
// const qualification is not smart enough to know about fields and always assumes that there might

View File

@ -1,5 +1,5 @@
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/qualif_overwrite_2.rs:10:5
--> $DIR/qualif_overwrite_2.rs:8:5
|
LL | &{a.0}
| ^^^^^^

View File

@ -1,5 +1,3 @@
#![feature(const_refs_to_cell)]
use std::sync::atomic::AtomicUsize;
const A: AtomicUsize = AtomicUsize::new(0);

View File

@ -1,11 +1,11 @@
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/E0492.rs:6:33
--> $DIR/E0492.rs:4:33
|
LL | const B: &'static AtomicUsize = &A;
| ^^
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/E0492.rs:7:34
--> $DIR/E0492.rs:5:34
|
LL | static C: &'static AtomicUsize = &A;
| ^^

View File

@ -1,5 +1,3 @@
#![feature(const_refs_to_cell)]
use std::cell::UnsafeCell;
const A: UnsafeCell<usize> = UnsafeCell::new(1);

View File

@ -1,17 +1,17 @@
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/issue-17718-const-borrow.rs:6:39
--> $DIR/issue-17718-const-borrow.rs:4:39
|
LL | const B: &'static UnsafeCell<usize> = &A;
| ^^
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/issue-17718-const-borrow.rs:11:39
--> $DIR/issue-17718-const-borrow.rs:9:39
|
LL | const E: &'static UnsafeCell<usize> = &D.a;
| ^^^^
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
--> $DIR/issue-17718-const-borrow.rs:13:23
--> $DIR/issue-17718-const-borrow.rs:11:23
|
LL | const F: &'static C = &D;
| ^^