Skip to content

Commit 354e510

Browse files
committed
Fix cell checks in const fn
1 parent 3a44a20 commit 354e510

File tree

2 files changed

+9
-0
lines changed
  • compiler/rustc_mir/src/transform/check_consts
  • src/test/ui/consts/std

2 files changed

+9
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ impl NonConstOp for CellBorrowBehindRef {
227227
#[derive(Debug)]
228228
pub struct CellBorrow;
229229
impl NonConstOp for CellBorrow {
230+
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
231+
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,
234+
_ => Status::Forbidden,
235+
}
236+
}
230237
fn importance(&self) -> DiagnosticImportance {
231238
// The problematic cases will already emit a `CellBorrowBehindRef`
232239
DiagnosticImportance::Secondary

src/test/ui/consts/std/cell.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ unsafe impl<T> Sync for Wrap<T> {}
3636

3737
static BAR_PTR: Wrap<*mut u32> = Wrap(BAR.0.get());
3838

39+
const fn fst_ref<T, U>(x: &(T, U)) -> &T { &x.0 }
40+
3941
fn main() {}

0 commit comments

Comments
 (0)