mentioned_items: avoid adding str/slice unsizing casts

This commit is contained in:
Ralf Jung 2024-03-20 11:16:11 +01:00
parent 682991d2c7
commit feeffaeff9

View File

@ -1,6 +1,6 @@
use rustc_middle::mir::visit::Visitor; use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{self, Location, MentionedItem, MirPass}; use rustc_middle::mir::{self, Location, MentionedItem, MirPass};
use rustc_middle::ty::{adjustment::PointerCoercion, TyCtxt}; use rustc_middle::ty::{self, adjustment::PointerCoercion, TyCtxt};
use rustc_session::Session; use rustc_session::Session;
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
@ -76,14 +76,21 @@ fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) {
) )
| mir::Rvalue::Cast(mir::CastKind::DynStar, ref operand, target_ty) => { | mir::Rvalue::Cast(mir::CastKind::DynStar, ref operand, target_ty) => {
// This isn't monomorphized yet so we can't tell what the actual types are -- just // This isn't monomorphized yet so we can't tell what the actual types are -- just
// add everything. // add everything that may involve a vtable.
self.mentioned_items.push(Spanned { let source_ty = operand.ty(self.body, self.tcx);
node: MentionedItem::UnsizeCast { let may_involve_vtable = match (
source_ty: operand.ty(self.body, self.tcx), source_ty.builtin_deref(true).map(|t| t.ty.kind()),
target_ty, target_ty.builtin_deref(true).map(|t| t.ty.kind()),
}, ) {
span: span(), (Some(ty::Array(..)), Some(ty::Str | ty::Slice(..))) => false, // &str/&[T] unsizing
}); _ => true,
};
if may_involve_vtable {
self.mentioned_items.push(Spanned {
node: MentionedItem::UnsizeCast { source_ty, target_ty },
span: span(),
});
}
} }
// Similarly, record closures that are turned into function pointers. // Similarly, record closures that are turned into function pointers.
mir::Rvalue::Cast( mir::Rvalue::Cast(