Skip to content

Commit

Permalink
Auto merge of #115759 - oli-obk:open_drop_from_non-ADT, r=<try>
Browse files Browse the repository at this point in the history
Reveal opaque types before drop elaboration

fixes #113594

r? `@cjgillot`

cc `@JakobDegen`

This pass was introduced in #110714

I moved it before drop elaboration (which only cares about the hidden types of things, not the opaque TAIT or RPIT type) and set it to run unconditionally (instead of depending on the optimization level and whether the inliner is active)
  • Loading branch information
bors committed Sep 11, 2023
2 parents 68c2f5b + a1e7d38 commit e3f388f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
7 changes: 3 additions & 4 deletions compiler/rustc_mir_dataflow/src/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ where
D: DropElaborator<'b, 'tcx>,
'tcx: 'b,
{
#[instrument(level = "trace", skip(self), ret)]
fn place_ty(&self, place: Place<'tcx>) -> Ty<'tcx> {
place.ty(self.elaborator.body(), self.tcx()).ty
}
Expand All @@ -220,11 +221,9 @@ where
//
// FIXME: I think we should just control the flags externally,
// and then we do not need this machinery.
#[instrument(level = "debug")]
pub fn elaborate_drop(&mut self, bb: BasicBlock) {
debug!("elaborate_drop({:?}, {:?})", bb, self);
let style = self.elaborator.drop_style(self.path, DropFlagMode::Deep);
debug!("elaborate_drop({:?}, {:?}): live - {:?}", bb, self, style);
match style {
match self.elaborator.drop_style(self.path, DropFlagMode::Deep) {
DropStyle::Dead => {
self.elaborator
.patch()
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_mir_transform/src/elaborate_drops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ impl<'a, 'tcx> DropElaborator<'a, 'tcx> for Elaborator<'a, '_, 'tcx> {
self.ctxt.param_env()
}

#[instrument(level = "debug", skip(self), ret)]
fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle {
let ((maybe_live, maybe_dead), multipart) = match mode {
DropFlagMode::Shallow => (self.ctxt.init_data.maybe_live_dead(path), false),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let passes: &[&dyn MirPass<'tcx>] = &[
// These next passes must be executed together
&add_call_guards::CriticalCallEdges,
&reveal_all::RevealAll, // has to be done before drop elaboration, since we need to drop opaque types, too.
&elaborate_drops::ElaborateDrops,
// This will remove extraneous landing pads which are no longer
// necessary as well as well as forcing any call in a non-unwinding
Expand Down Expand Up @@ -526,7 +527,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
body,
&[
&check_alignment::CheckAlignment,
&reveal_all::RevealAll, // has to be done before inlining, since inlined code is in RevealAll mode.
&lower_slice_len::LowerSliceLenCalls, // has to be done before inlining, otherwise actual call will be almost always inlined. Also simple, so can just do first
&unreachable_prop::UnreachablePropagation,
&uninhabited_enum_branching::UninhabitedEnumBranching,
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_mir_transform/src/reveal_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ use rustc_middle::ty::{self, Ty, TyCtxt};
pub struct RevealAll;

impl<'tcx> MirPass<'tcx> for RevealAll {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 3 || super::inline::Inline.is_enabled(sess)
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// Do not apply this transformation to generators.
if body.generator.is_some() {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/polymorphization/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ where

#[rustc_polymorphize_error]
pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
//~^ ERROR item has unused generic parameters
|| {
//~^ ERROR item has unused generic parameters
yield 1;
Expand All @@ -57,6 +58,7 @@ pub fn used_type_in_return<R: Default>() -> impl Generator<(), Yield = u32, Retu

#[rustc_polymorphize_error]
pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
//~^ ERROR item has unused generic parameters
|| {
//~^ ERROR item has unused generic parameters
yield 1;
Expand Down
20 changes: 17 additions & 3 deletions tests/ui/polymorphization/generators.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@ LL | #![feature(generic_const_exprs, generators, generator_trait, rustc_attrs)]
= note: `#[warn(incomplete_features)]` on by default

error: item has unused generic parameters
--> $DIR/generators.rs:35:5
--> $DIR/generators.rs:36:5
|
LL | pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
| - generic parameter `T` is unused
LL |
LL | || {
| ^^

error: item has unused generic parameters
--> $DIR/generators.rs:60:5
--> $DIR/generators.rs:34:8
|
LL | pub fn unused_type<T>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
| ^^^^^^^^^^^ - generic parameter `T` is unused

error: item has unused generic parameters
--> $DIR/generators.rs:62:5
|
LL | pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
| ------------ generic parameter `T` is unused
LL |
LL | || {
| ^^

error: aborting due to 2 previous errors; 1 warning emitted
error: item has unused generic parameters
--> $DIR/generators.rs:60:8
|
LL | pub fn unused_const<const T: u32>() -> impl Generator<(), Yield = u32, Return = u32> + Unpin {
| ^^^^^^^^^^^^ ------------ generic parameter `T` is unused

error: aborting due to 4 previous errors; 1 warning emitted

18 changes: 18 additions & 0 deletions tests/ui/type-alias-impl-trait/destructure_tait-ice-113594.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// build-pass

#![feature(type_alias_impl_trait)]

pub struct Foo {
/// This type must have nontrivial drop glue
field: String,
}

pub type Tait = impl Sized;

pub fn ice_cold(beverage: Tait) {
// Must destructure at least one field of `Foo`
let Foo { field } = beverage;
_ = field;
}

fn main() {}

0 comments on commit e3f388f

Please sign in to comment.