Skip to content

Erase integer and float variables before needs_drop calls in HIR generator analysis #103036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/rustc_hir_analysis/src/check/generator_interior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,11 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {

let ty = self.fcx.typeck_results.borrow().expr_ty_adjusted_opt(expr);
let may_need_drop = |ty: Ty<'tcx>| {
// Avoid ICEs in needs_drop.
let ty = self.fcx.resolve_vars_if_possible(ty);
// Regions are not needed to prove if something needs drop
let ty = self.fcx.tcx.erase_regions(ty);
// All int/float types are trivially drop, so those infer variables can be erased
let ty = self.fcx.resolve_numeric_literals_with_default(ty);
if ty.needs_infer() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolve_numeric_literals_with_default probably makes this a bit more accurate, too, since integer variables no longer trigger may_need_drop for no reason.

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ use crate::{
use hir::{def_id::DefId, Body, HirId, HirIdMap};
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_middle::hir::place::{PlaceBase, Projection, ProjectionKind};
use rustc_infer::infer::InferenceLiteralEraser;
use rustc_middle::ty::{ParamEnv, TyCtxt};
use rustc_middle::{
hir::place::{PlaceBase, Projection, ProjectionKind},
ty::{TypeFoldable, TypeVisitable},
};

pub(super) fn find_consumed_and_borrowed<'a, 'tcx>(
fcx: &'a FnCtxt<'a, 'tcx>,
Expand Down Expand Up @@ -196,13 +200,13 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
return;
}

// Regions are not needed to prove if something needs drop
let ty = self.tcx.erase_regions(assignee_place.place.base_ty);
// All int/float types are trivially drop, so those infer variables can be erased
let ty = ty.fold_with(&mut InferenceLiteralEraser { tcx: self.tcx });
// If the type being assigned needs dropped, then the mutation counts as a borrow
// since it is essentially doing `Drop::drop(&mut x); x = new_value;`.
//
// FIXME(drop-tracking): We need to be more responsible about inference
// variables here, since `needs_drop` is a "raw" type query, i.e. it
// basically requires types to have been fully resolved.
if assignee_place.place.base_ty.needs_drop(self.tcx, self.param_env) {
if ty.needs_infer() || ty.needs_drop(self.tcx, self.param_env) {
self.places
.borrowed
.insert(TrackedValue::from_place_with_projections_allowed(assignee_place));
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1840,9 +1840,8 @@ impl<'tcx> TyOrConstInferVar<'tcx> {
}

/// Replace `{integer}` with `i32` and `{float}` with `f64`.
/// Used only for diagnostics.
struct InferenceLiteralEraser<'tcx> {
tcx: TyCtxt<'tcx>,
pub struct InferenceLiteralEraser<'tcx> {
pub tcx: TyCtxt<'tcx>,
}

impl<'tcx> TypeFolder<'tcx> for InferenceLiteralEraser<'tcx> {
Expand Down