Skip to content

Commit

Permalink
Auto merge of rust-lang#87737 - LeSeulArtichaut:unsafeck-less-freeze,…
Browse files Browse the repository at this point in the history
… 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`
  • Loading branch information
bors committed Aug 5, 2021
2 parents e21e1d6 + 2b169cc commit 61a941b
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,27 +456,25 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
return; // we have already visited everything by now
}
}
ExprKind::Borrow { borrow_kind, arg } => match borrow_kind {
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
if !self.thir[arg]
.ty
.is_freeze(self.tcx.at(self.thir[arg].span), self.param_env)
{
let mut visitor = LayoutConstrainedPlaceVisitor::new(self.thir, self.tcx);
visit::walk_expr(&mut visitor, expr);
if visitor.found {
self.requires_unsafe(expr.span, BorrowOfLayoutConstrainedField);
ExprKind::Borrow { borrow_kind, arg } => {
let mut visitor = LayoutConstrainedPlaceVisitor::new(self.thir, self.tcx);
visit::walk_expr(&mut visitor, expr);
if visitor.found {
match borrow_kind {
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique
if !self.thir[arg]
.ty
.is_freeze(self.tcx.at(self.thir[arg].span), self.param_env) =>
{
self.requires_unsafe(expr.span, BorrowOfLayoutConstrainedField)
}
BorrowKind::Mut { .. } => {
self.requires_unsafe(expr.span, MutationOfLayoutConstrainedField)
}
BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {}
}
}
BorrowKind::Mut { .. } => {
let mut visitor = LayoutConstrainedPlaceVisitor::new(self.thir, self.tcx);
visit::walk_expr(&mut visitor, expr);
if visitor.found {
self.requires_unsafe(expr.span, MutationOfLayoutConstrainedField);
}
}
},
}
_ => {}
}
visit::walk_expr(self, expr);
Expand Down

0 comments on commit 61a941b

Please sign in to comment.