From 3ed14033f7c087492a73c70bb11c1f7bae1b06c9 Mon Sep 17 00:00:00 2001 From: oli Date: Wed, 30 Dec 2020 17:20:38 +0000 Subject: [PATCH] Reinstate the error-code error over the feature gate error --- .../src/error_codes/E0492.md | 2 -- .../src/transform/check_consts/ops.rs | 22 ++++++++++++------- .../src/transform/check_consts/validation.rs | 2 +- src/test/ui/consts/partial_qualif.rs | 2 -- src/test/ui/consts/partial_qualif.stderr | 2 +- src/test/ui/consts/qualif_overwrite.rs | 2 -- src/test/ui/consts/qualif_overwrite.stderr | 2 +- src/test/ui/consts/qualif_overwrite_2.rs | 2 -- src/test/ui/consts/qualif_overwrite_2.stderr | 2 +- src/test/ui/error-codes/E0492.rs | 2 -- src/test/ui/error-codes/E0492.stderr | 4 ++-- .../ui/issues/issue-17718-const-borrow.rs | 2 -- .../ui/issues/issue-17718-const-borrow.stderr | 6 ++--- 13 files changed, 23 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0492.md b/compiler/rustc_error_codes/src/error_codes/E0492.md index 30235b0aeb0..79e7c069a91 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0492.md +++ b/compiler/rustc_error_codes/src/error_codes/E0492.md @@ -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 = Cell::new(1); diff --git a/compiler/rustc_mir/src/transform/check_consts/ops.rs b/compiler/rustc_mir/src/transform/check_consts/ops.rs index 5112c572d0f..764031c0dae 100644 --- a/compiler/rustc_mir/src/transform/check_consts/ops.rs +++ b/compiler/rustc_mir/src/transform/check_consts/ops.rs @@ -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, diff --git a/compiler/rustc_mir/src/transform/check_consts/validation.rs b/compiler/rustc_mir/src/transform/check_consts/validation.rs index c2b702f265b..f0ab3ff0100 100644 --- a/compiler/rustc_mir/src/transform/check_consts/validation.rs +++ b/compiler/rustc_mir/src/transform/check_consts/validation.rs @@ -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); } diff --git a/src/test/ui/consts/partial_qualif.rs b/src/test/ui/consts/partial_qualif.rs index e069a023aa1..32c68e69f4b 100644 --- a/src/test/ui/consts/partial_qualif.rs +++ b/src/test/ui/consts/partial_qualif.rs @@ -1,5 +1,3 @@ -#![feature(const_refs_to_cell)] - use std::cell::Cell; const FOO: &(Cell, bool) = { diff --git a/src/test/ui/consts/partial_qualif.stderr b/src/test/ui/consts/partial_qualif.stderr index 58acde1b58a..221e449b6f9 100644 --- a/src/test/ui/consts/partial_qualif.stderr +++ b/src/test/ui/consts/partial_qualif.stderr @@ -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} | ^^^^ diff --git a/src/test/ui/consts/qualif_overwrite.rs b/src/test/ui/consts/qualif_overwrite.rs index 9c20d94c6dd..9b3f6786ae6 100644 --- a/src/test/ui/consts/qualif_overwrite.rs +++ b/src/test/ui/consts/qualif_overwrite.rs @@ -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 diff --git a/src/test/ui/consts/qualif_overwrite.stderr b/src/test/ui/consts/qualif_overwrite.stderr index a6c8c599ecc..fbaae711d7c 100644 --- a/src/test/ui/consts/qualif_overwrite.stderr +++ b/src/test/ui/consts/qualif_overwrite.stderr @@ -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} | ^^^^ diff --git a/src/test/ui/consts/qualif_overwrite_2.rs b/src/test/ui/consts/qualif_overwrite_2.rs index 5d0843710e2..fa79b5c14a7 100644 --- a/src/test/ui/consts/qualif_overwrite_2.rs +++ b/src/test/ui/consts/qualif_overwrite_2.rs @@ -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 diff --git a/src/test/ui/consts/qualif_overwrite_2.stderr b/src/test/ui/consts/qualif_overwrite_2.stderr index f109b4cea81..a393c4e336d 100644 --- a/src/test/ui/consts/qualif_overwrite_2.stderr +++ b/src/test/ui/consts/qualif_overwrite_2.stderr @@ -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} | ^^^^^^ diff --git a/src/test/ui/error-codes/E0492.rs b/src/test/ui/error-codes/E0492.rs index d8357f0c678..2c735fcc9f9 100644 --- a/src/test/ui/error-codes/E0492.rs +++ b/src/test/ui/error-codes/E0492.rs @@ -1,5 +1,3 @@ -#![feature(const_refs_to_cell)] - use std::sync::atomic::AtomicUsize; const A: AtomicUsize = AtomicUsize::new(0); diff --git a/src/test/ui/error-codes/E0492.stderr b/src/test/ui/error-codes/E0492.stderr index ffc4ff372a3..54306433b87 100644 --- a/src/test/ui/error-codes/E0492.stderr +++ b/src/test/ui/error-codes/E0492.stderr @@ -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; | ^^ diff --git a/src/test/ui/issues/issue-17718-const-borrow.rs b/src/test/ui/issues/issue-17718-const-borrow.rs index 7bde2361f61..b11e83144ed 100644 --- a/src/test/ui/issues/issue-17718-const-borrow.rs +++ b/src/test/ui/issues/issue-17718-const-borrow.rs @@ -1,5 +1,3 @@ -#![feature(const_refs_to_cell)] - use std::cell::UnsafeCell; const A: UnsafeCell = UnsafeCell::new(1); diff --git a/src/test/ui/issues/issue-17718-const-borrow.stderr b/src/test/ui/issues/issue-17718-const-borrow.stderr index 1bfb3982bc3..b4330049689 100644 --- a/src/test/ui/issues/issue-17718-const-borrow.stderr +++ b/src/test/ui/issues/issue-17718-const-borrow.stderr @@ -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 = &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 = &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; | ^^