Skip to content

Don't codegen impossible to satisfy impls #109110

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
Mar 14, 2023
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
15 changes: 15 additions & 0 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,21 @@ fn create_mono_items_for_default_impls<'tcx>(
return;
}

// Unlike 'lazy' monomorphization that begins by collecting items transitively
// called by `main` or other global items, when eagerly monomorphizing impl
// items, we never actually check that the predicates of this impl are satisfied
// in a empty reveal-all param env (i.e. with no assumptions).
//
// Even though this impl has no substitutions, because we don't consider higher-
// ranked predicates such as `for<'a> &'a mut [u8]: Copy` to be trivially false,
// we must now check that the impl has no impossible-to-satisfy predicates.
if tcx.subst_and_check_impossible_predicates((
item.owner_id.to_def_id(),
&InternalSubsts::identity_for_item(tcx, item.owner_id.to_def_id()),
)) {
return;
}

let Some(trait_ref) = tcx.impl_trait_ref(item.owner_id) else {
return;
};
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/codegen/mono-impossible.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// compile-flags: -Clink-dead-code=on --crate-type=lib
// build-pass

// Make sure that we don't monomorphize the impossible method `<() as Visit>::visit`,
// which does not hold under a reveal-all param env.

pub trait Visit {
fn visit() {}
}

pub trait Array<'a> {}

impl Visit for () where (): for<'a> Array<'a> {}