Skip to content
Open
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
43 changes: 41 additions & 2 deletions compiler/rustc_mir_transform/src/impossible_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
//! it's usually never invoked in this way.

use rustc_middle::mir::{Body, START_BLOCK, TerminatorKind};
use rustc_middle::ty::{TyCtxt, TypeFlags, TypeVisitableExt, Unnormalized};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFlags, TypeVisitableExt, Unnormalized};
use rustc_span::def_id::DefId;
use rustc_trait_selection::traits;
use tracing::trace;
Expand All @@ -36,9 +36,48 @@ use crate::pass_manager::MirPass;

pub(crate) struct ImpossiblePredicates;

fn has_impossible_predicates(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
fn is_structurally_unsized<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
match ty.kind() {
ty::Str | ty::Slice(_) | ty::Dynamic(_, _) | ty::Foreign(_) => true,
ty::Tuple(tys) => tys.last().is_some_and(|ty| is_structurally_unsized(tcx, *ty)),
ty::Adt(def, args) => {
def.sizedness_constraint(tcx, ty::SizedTraitKind::Sized).is_some_and(|ty| {
is_structurally_unsized(tcx, ty.instantiate(tcx, args).skip_norm_wip())
})
}
_ => false,
}
}

fn has_structurally_impossible_sized_predicate<'tcx>(
tcx: TyCtxt<'tcx>,
sized_trait: DefId,
predicate: ty::Clause<'tcx>,
) -> bool {
let Some(trait_predicate) = predicate.as_trait_clause() else {
return false;
};
let trait_predicate = trait_predicate.skip_binder();

trait_predicate.polarity == ty::PredicatePolarity::Positive
&& trait_predicate.def_id() == sized_trait
&& is_structurally_unsized(tcx, trait_predicate.self_ty())
}

fn has_impossible_predicates<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
let predicates = tcx.predicates_of(def_id).instantiate_identity(tcx);
tracing::trace!(?predicates);

// Some `Sized` predicates that mention local generics are still impossible
// for every instantiation, e.g. `dyn Trait<T>: Sized`.
if let Some(sized_trait) = tcx.lang_items().sized_trait() {
if predicates.predicates.iter().copied().map(Unnormalized::skip_norm_wip).any(|predicate| {
has_structurally_impossible_sized_predicate(tcx, sized_trait, predicate)
}) {
return true;
}
}

let predicates =
predicates.predicates.into_iter().map(Unnormalized::skip_norm_wip).filter(|p| {
!p.has_type_flags(
Expand Down
22 changes: 20 additions & 2 deletions tests/ui/const_prop/issue-102553.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ compile-flags: --crate-type=lib
//@ check-pass
//@ compile-flags: --crate-type=lib -Zmir-opt-level=3
//@ build-pass

pub trait Widget<E> {
fn boxed<'w>(self) -> Box<dyn WidgetDyn<E> + 'w>
Expand All @@ -22,3 +22,21 @@ impl<E> Widget<E> for dyn WidgetDyn<E> + '_ {
Box::new(self)
}
}

// Regression test for https://github.com/rust-lang/rust/issues/156051.
trait WidgetSize<E> {
fn size<'w>(self) -> usize
where
Self: Sized + 'w;
}

trait WidgetSizeDyn<E> {}

impl<E> WidgetSize<E> for dyn WidgetSizeDyn<E> + '_ {
fn size<'w>(self) -> usize
where
Self: Sized + 'w,
{
core::mem::size_of::<Self>()
}
}
Loading