Skip to content

Commit 8440b09

Browse files
authored
Rollup merge of #103226 - compiler-errors:delay-if-need-infer, r=lcnr
Check `needs_infer` before `needs_drop` during HIR generator analysis This is kinda a revival of #103036, but with the understanding that after fallback, a generator-interior type will only have `needs_infer` true if there's an error that prevented int or float variable fallback to occur (modulo region variables, which are erased). Therefore the best choice here is to delay a bug and skip the `needs_drop` call altogether. r? `@lcnr` feel free to reassign though
2 parents e426874 + 134de38 commit 8440b09

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/record_consumed_borrow.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ use crate::{
66
use hir::{def_id::DefId, Body, HirId, HirIdMap};
77
use rustc_data_structures::fx::FxHashSet;
88
use rustc_hir as hir;
9-
use rustc_middle::hir::place::{PlaceBase, Projection, ProjectionKind};
109
use rustc_middle::ty::{ParamEnv, TyCtxt};
10+
use rustc_middle::{
11+
hir::place::{PlaceBase, Projection, ProjectionKind},
12+
ty::TypeVisitable,
13+
};
1114

1215
pub(super) fn find_consumed_and_borrowed<'a, 'tcx>(
1316
fcx: &'a FnCtxt<'a, 'tcx>,
@@ -198,11 +201,13 @@ impl<'tcx> expr_use_visitor::Delegate<'tcx> for ExprUseDelegate<'tcx> {
198201

199202
// If the type being assigned needs dropped, then the mutation counts as a borrow
200203
// since it is essentially doing `Drop::drop(&mut x); x = new_value;`.
201-
//
202-
// FIXME(drop-tracking): We need to be more responsible about inference
203-
// variables here, since `needs_drop` is a "raw" type query, i.e. it
204-
// basically requires types to have been fully resolved.
205-
if assignee_place.place.base_ty.needs_drop(self.tcx, self.param_env) {
204+
let ty = self.tcx.erase_regions(assignee_place.place.base_ty);
205+
if ty.needs_infer() {
206+
self.tcx.sess.delay_span_bug(
207+
self.tcx.hir().span(assignee_place.hir_id),
208+
&format!("inference variables in {ty}"),
209+
);
210+
} else if ty.needs_drop(self.tcx, self.param_env) {
206211
self.places
207212
.borrowed
208213
.insert(TrackedValue::from_place_with_projections_allowed(assignee_place));

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,6 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
377377
debug!("is_borrowed_temporary: {:?}", self.drop_ranges.is_borrowed_temporary(expr));
378378

379379
let ty = self.fcx.typeck_results.borrow().expr_ty_adjusted_opt(expr);
380-
let may_need_drop = |ty: Ty<'tcx>| {
381-
// Avoid ICEs in needs_drop.
382-
let ty = self.fcx.resolve_vars_if_possible(ty);
383-
let ty = self.fcx.tcx.erase_regions(ty);
384-
if ty.needs_infer() {
385-
return true;
386-
}
387-
ty.needs_drop(self.fcx.tcx, self.fcx.param_env)
388-
};
389380

390381
// Typically, the value produced by an expression is consumed by its parent in some way,
391382
// so we only have to check if the parent contains a yield (note that the parent may, for
@@ -403,9 +394,18 @@ impl<'a, 'tcx> Visitor<'tcx> for InteriorVisitor<'a, 'tcx> {
403394
// src/test/ui/generator/drop-tracking-parent-expression.rs.
404395
let scope = if self.drop_ranges.is_borrowed_temporary(expr)
405396
|| ty.map_or(true, |ty| {
406-
let needs_drop = may_need_drop(ty);
407-
debug!(?needs_drop, ?ty);
408-
needs_drop
397+
// Avoid ICEs in needs_drop.
398+
let ty = self.fcx.resolve_vars_if_possible(ty);
399+
let ty = self.fcx.tcx.erase_regions(ty);
400+
if ty.needs_infer() {
401+
self.fcx
402+
.tcx
403+
.sess
404+
.delay_span_bug(expr.span, &format!("inference variables in {ty}"));
405+
true
406+
} else {
407+
ty.needs_drop(self.fcx.tcx, self.fcx.param_env)
408+
}
409409
}) {
410410
self.rvalue_scopes.temporary_scope(self.region_scope_tree, expr.hir_id.local_id)
411411
} else {

0 commit comments

Comments
 (0)