Skip to content

Commit aaee3f2

Browse files
committed
Don't mix feature gates and hard errors, decide on one per op and stick with it
1 parent 3ed1403 commit aaee3f2

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,6 @@ impl NonConstOp for TransientCellBorrow {
237237
/// it in the future for static items.
238238
pub struct CellBorrow;
239239
impl NonConstOp for CellBorrow {
240-
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
241-
match ccx.const_kind() {
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),
244-
_ => Status::Forbidden,
245-
}
246-
}
247240
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
248241
struct_span_err!(
249242
ccx.tcx.sess,

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,14 +582,24 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
582582
);
583583

584584
if borrowed_place_has_mut_interior {
585-
// Locals without StorageDead follow the "enclosing scope" rule, meaning
586-
// they are essentially anonymous static items themselves.
587-
// Note: This is only sound if every local that has a `StorageDead` has a
588-
// `StorageDead` in every control flow path leading to a `return` terminator.
589-
if self.local_has_storage_dead(place.local) {
590-
self.check_op(ops::TransientCellBorrow);
591-
} else {
592-
self.check_op(ops::CellBorrow);
585+
match self.const_kind() {
586+
// In a const fn all borrows are transient or point to the places given via
587+
// references in the arguments. The borrow checker guarantees that.
588+
// NOTE: Once we have heap allocations during CTFE we need to figure out
589+
// how to prevent `const fn` to create long-lived allocations that point
590+
// to (interior) mutable memory.
591+
hir::ConstContext::ConstFn => self.check_op(ops::TransientCellBorrow),
592+
_ => {
593+
// Locals without StorageDead follow the "enclosing scope" rule, meaning
594+
// they are essentially anonymous static items themselves.
595+
// Note: This is only sound if every local that has a `StorageDead` has a
596+
// `StorageDead` in every control flow path leading to a `return` terminator.
597+
if self.local_has_storage_dead(place.local) {
598+
self.check_op(ops::TransientCellBorrow);
599+
} else {
600+
self.check_op(ops::CellBorrow);
601+
}
602+
}
593603
}
594604
}
595605
}

0 commit comments

Comments
 (0)