Skip to content

Commit 61a941b

Browse files
committed
Auto merge of rust-lang#87737 - LeSeulArtichaut:unsafeck-less-freeze, r=oli-obk
Only compute `is_freeze` for layout-constrained ADTs Places are usually shallow and quick to visit. By contrast, computing `is_freeze` can be much costlier, involving inference and trait solving. Making sure to call `is_freeze` only when necessary should be beneficial for performance in most cases. See [this comparison](https://perf.rust-lang.org/compare.html?start=81f08a4763e7537b92506fa5a597e6bf774d20cc&end=56a58d347b1c7dd0c2984b8fc3930c408e26fbc2&stat=instructions%3Au) from rust-lang#87710. r? `@oli-obk`
2 parents e21e1d6 + 2b169cc commit 61a941b

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -456,27 +456,25 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
456456
return; // we have already visited everything by now
457457
}
458458
}
459-
ExprKind::Borrow { borrow_kind, arg } => match borrow_kind {
460-
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
461-
if !self.thir[arg]
462-
.ty
463-
.is_freeze(self.tcx.at(self.thir[arg].span), self.param_env)
464-
{
465-
let mut visitor = LayoutConstrainedPlaceVisitor::new(self.thir, self.tcx);
466-
visit::walk_expr(&mut visitor, expr);
467-
if visitor.found {
468-
self.requires_unsafe(expr.span, BorrowOfLayoutConstrainedField);
459+
ExprKind::Borrow { borrow_kind, arg } => {
460+
let mut visitor = LayoutConstrainedPlaceVisitor::new(self.thir, self.tcx);
461+
visit::walk_expr(&mut visitor, expr);
462+
if visitor.found {
463+
match borrow_kind {
464+
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique
465+
if !self.thir[arg]
466+
.ty
467+
.is_freeze(self.tcx.at(self.thir[arg].span), self.param_env) =>
468+
{
469+
self.requires_unsafe(expr.span, BorrowOfLayoutConstrainedField)
469470
}
471+
BorrowKind::Mut { .. } => {
472+
self.requires_unsafe(expr.span, MutationOfLayoutConstrainedField)
473+
}
474+
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {}
470475
}
471476
}
472-
BorrowKind::Mut { .. } => {
473-
let mut visitor = LayoutConstrainedPlaceVisitor::new(self.thir, self.tcx);
474-
visit::walk_expr(&mut visitor, expr);
475-
if visitor.found {
476-
self.requires_unsafe(expr.span, MutationOfLayoutConstrainedField);
477-
}
478-
}
479-
},
477+
}
480478
_ => {}
481479
}
482480
visit::walk_expr(self, expr);

0 commit comments

Comments
 (0)