Skip to content

Commit

Permalink
Auto merge of #127068 - compiler-errors:stall-drop, r=<try>
Browse files Browse the repository at this point in the history
Stall dropaStall computing instance for drop shim until it has no unsubstituted const params

Stall resolving the drop shim instance for types that still have unsubstituted const params.

## Why?

#127030 ICEs because it tries to inline the drop shim for a type with an unsubstituted const param. In order to generate this shim, this requires calling the drop shim builder, which invokes the trait solver to compute whether constituent types need drop (since we compute if a type is copy to disqualify any `Drop` behavior):

https://github.com/rust-lang/rust/blob/9c3bc805dd9cb84019c124b9a50fdff1e62a7ec9/compiler/rustc_mir_dataflow/src/elaborate_drops.rs#L378

However, since we don't keep the param-env of the instance we resolved the item for, we use the wrong param-env:
https://github.com/rust-lang/rust/blob/9c3bc805dd9cb84019c124b9a50fdff1e62a7ec9/compiler/rustc_mir_transform/src/shim.rs#L278
(which is the param-env of `std::ptr::drop_in_place`)

This param-env is notably missing `ConstParamHasTy` predicates, and since we removed the type from consts in #125958, we literally cannot prove these predicates in this (relatively) empty param-env. This currently happens in places like the MIR inliner, but may happen elsewhere such as in lints that resolve terminators.

## What?

We delay the resolution (`Instance::resolve`) of calls for `drop_in_place` for types that have unsubstituted const params. This should be OK, since all cases that deal with polymorphic code should handle `Instance::resolve` returning `None` gracefully.
  • Loading branch information
bors committed Jun 28, 2024
2 parents 9c3bc80 + d62c70c commit e495e73
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
11 changes: 10 additions & 1 deletion compiler/rustc_ty_utils/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_middle::query::Providers;
use rustc_middle::traits::{BuiltinImplSource, CodegenObligationError};
use rustc_middle::ty::util::AsyncDropGlueMorphology;
use rustc_middle::ty::GenericArgsRef;
use rustc_middle::ty::{self, Instance, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{self, Instance, TyCtxt, TypeFlags, TypeVisitableExt};
use rustc_span::sym;
use rustc_trait_selection::traits;
use rustc_type_ir::ClosureKind;
Expand Down Expand Up @@ -53,6 +53,15 @@ fn resolve_instance<'tcx>(
_ => return Ok(None),
}

// FIXME(#127030): `ConstParamHasTy` has bad interactions with
// the drop shim builder, which does not evaluate predicates in
// the correct param-env for types being dropped. Stall resolving
// the MIR for this instance until all of its const params are
// substituted.
if ty.has_type_flags(TypeFlags::HAS_CT_PARAM) {
return Ok(None);
}

ty::InstanceKind::DropGlue(def_id, Some(ty))
} else {
debug!(" => trivial drop glue");
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/const-generics/polymorphic-drop-shim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ compile-flags: -Zinline-mir=yes --crate-type=lib
//@ build-pass

use std::mem::ManuallyDrop;

pub struct Foo<T, const N: usize>([T; N]);

pub struct Dorp {}

impl Drop for Dorp {
fn drop(&mut self) {}
}

#[inline]
// SAFETY: call this with a valid allocation idk
pub unsafe fn drop<const M: usize>(x: *mut Foo<Dorp, M>) {
std::ptr::drop_in_place(x);
}

0 comments on commit e495e73

Please sign in to comment.