1
Fork 0

More comments

This commit is contained in:
Michael Goulet 2025-02-22 22:13:23 +00:00
parent bab03bf176
commit 2797936f6d

View file

@ -369,17 +369,22 @@ struct FindSignificantDropper<'a, 'tcx> {
} }
impl<'tcx> FindSignificantDropper<'_, 'tcx> { impl<'tcx> FindSignificantDropper<'_, 'tcx> {
/// Check the scrutinee of an `if let` to see if it promotes any temporary values
/// that would change drop order in edition 2024. Specifically, it checks the value
/// of the scrutinee itself, and also recurses into the expression to find any ref
/// exprs (or autoref) which would promote temporaries that would be scoped to the
/// end of this `if`.
fn check_if_let_scrutinee(&mut self, init: &'tcx hir::Expr<'tcx>) -> ControlFlow<Span> { fn check_if_let_scrutinee(&mut self, init: &'tcx hir::Expr<'tcx>) -> ControlFlow<Span> {
self.check_promoted_temp_with_drop(init)?; self.check_promoted_temp_with_drop(init)?;
self.visit_expr(init) self.visit_expr(init)
} }
// Check that an expression is not a promoted temporary with a significant /// Check that an expression is not a promoted temporary with a significant
// drop impl. /// drop impl.
// ///
// An expression is a promoted temporary if it has an addr taken (i.e. `&expr`) /// An expression is a promoted temporary if it has an addr taken (i.e. `&expr` or autoref)
// or is the scrutinee of the `if let`, *and* the expression is not a place /// or is the scrutinee of the `if let`, *and* the expression is not a place
// expr, and it has a significant drop. /// expr, and it has a significant drop.
fn check_promoted_temp_with_drop(&self, expr: &'tcx hir::Expr<'tcx>) -> ControlFlow<Span> { fn check_promoted_temp_with_drop(&self, expr: &'tcx hir::Expr<'tcx>) -> ControlFlow<Span> {
if !expr.is_place_expr(|base| { if !expr.is_place_expr(|base| {
self.cx self.cx
@ -405,7 +410,8 @@ impl<'tcx> Visitor<'tcx> for FindSignificantDropper<'_, 'tcx> {
fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) -> Self::Result { fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) -> Self::Result {
// Blocks introduce temporary terminating scope for all of its // Blocks introduce temporary terminating scope for all of its
// statements, so just visit the tail expr. // statements, so just visit the tail expr, skipping over any
// statements. This prevents false positives like `{ let x = &Drop; }`.
if let Some(expr) = b.expr { self.visit_expr(expr) } else { ControlFlow::Continue(()) } if let Some(expr) = b.expr { self.visit_expr(expr) } else { ControlFlow::Continue(()) }
} }
@ -415,6 +421,7 @@ impl<'tcx> Visitor<'tcx> for FindSignificantDropper<'_, 'tcx> {
// where `fn as_ref(&self) -> Option<...>`. // where `fn as_ref(&self) -> Option<...>`.
for adj in self.cx.typeck_results().expr_adjustments(expr) { for adj in self.cx.typeck_results().expr_adjustments(expr) {
match adj.kind { match adj.kind {
// Skip when we hit the first deref expr.
Adjust::Deref(_) => break, Adjust::Deref(_) => break,
Adjust::Borrow(_) => { Adjust::Borrow(_) => {
self.check_promoted_temp_with_drop(expr)?; self.check_promoted_temp_with_drop(expr)?;
@ -424,7 +431,7 @@ impl<'tcx> Visitor<'tcx> for FindSignificantDropper<'_, 'tcx> {
} }
match expr.kind { match expr.kind {
// Check for cases like `if let None = Some(&Drop) {} else {}`. // Account for cases like `if let None = Some(&Drop) {} else {}`.
hir::ExprKind::AddrOf(_, _, expr) => { hir::ExprKind::AddrOf(_, _, expr) => {
self.check_promoted_temp_with_drop(expr)?; self.check_promoted_temp_with_drop(expr)?;
intravisit::walk_expr(self, expr) intravisit::walk_expr(self, expr)
@ -438,6 +445,7 @@ impl<'tcx> Visitor<'tcx> for FindSignificantDropper<'_, 'tcx> {
hir::ExprKind::Match(scrut, _, _) => self.visit_expr(scrut), hir::ExprKind::Match(scrut, _, _) => self.visit_expr(scrut),
// Self explanatory. // Self explanatory.
hir::ExprKind::DropTemps(_) => ControlFlow::Continue(()), hir::ExprKind::DropTemps(_) => ControlFlow::Continue(()),
// Otherwise, walk into the expr's parts.
_ => intravisit::walk_expr(self, expr), _ => intravisit::walk_expr(self, expr),
} }
} }