Skip to content

Commit 2b169cc

Browse files
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.
1 parent e3b1c12 commit 2b169cc

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 16 additions & 18 deletions
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)