Skip to content

Restrict query recursion in needs_significant_drop #93449

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

Merged
merged 1 commit into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 17 additions & 19 deletions compiler/rustc_ty_utils/src/needs_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,11 @@ fn drop_tys_helper<'tcx>(
fn with_query_cache<'tcx>(
tcx: TyCtxt<'tcx>,
iter: impl IntoIterator<Item = Ty<'tcx>>,
only_significant: bool,
) -> NeedsDropResult<Vec<Ty<'tcx>>> {
iter.into_iter().try_fold(Vec::new(), |mut vec, subty| {
match subty.kind() {
ty::Adt(adt_id, subst) => {
for subty in if only_significant {
tcx.adt_significant_drop_tys(adt_id.did)?
} else {
tcx.adt_drop_tys(adt_id.did)?
} {
for subty in tcx.adt_drop_tys(adt_id.did)? {
vec.push(subty.subst(tcx, subst));
}
}
Expand All @@ -234,25 +229,28 @@ fn drop_tys_helper<'tcx>(
// Since the destructor is insignificant, we just want to make sure all of
// the passed in type parameters are also insignificant.
// Eg: Vec<T> dtor is insignificant when T=i32 but significant when T=Mutex.
with_query_cache(tcx, substs.types(), only_significant)
Ok(substs.types().collect())
}
}
} else if adt_def.is_union() {
debug!("drop_tys_helper: `{:?}` is a union", adt_def);
Ok(Vec::new())
} else {
with_query_cache(
tcx,
adt_def.all_fields().map(|field| {
let r = tcx.type_of(field.did).subst(tcx, substs);
debug!(
"drop_tys_helper: Subst into {:?} with {:?} gettng {:?}",
field, substs, r
);
r
}),
only_significant,
)
let field_tys = adt_def.all_fields().map(|field| {
let r = tcx.type_of(field.did).subst(tcx, substs);
debug!("drop_tys_helper: Subst into {:?} with {:?} gettng {:?}", field, substs, r);
r
});
if only_significant {
// We can't recurse through the query system here because we might induce a cycle
Ok(field_tys.collect())
} else {
// We can use the query system if we consider all drops significant. In that case,
// ADTs are `needs_drop` exactly if they `impl Drop` or if any of their "transitive"
// fields do. There can be no cycles here, because ADTs cannot contain themselves as
// fields.
with_query_cache(tcx, field_tys)
}
}
.map(|v| v.into_iter())
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ICEs if checking if there is a significant destructor causes a query cycle
// check-pass

#![warn(rust_2021_incompatible_closure_captures)]
pub struct Foo(Bar);
pub struct Bar(Baz);
pub struct Baz(Vec<Foo>);

impl Foo {
pub fn baz(self, v: Baz) -> Baz {
(|| v)()
}
}
fn main() {}