From e313cbd9a33b22425f1796df8e0156a8b6a09c0e Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Tue, 9 May 2023 10:27:41 -0700 Subject: [PATCH] analyze: util: fix callee having unsubstituted generic elem/pointee types --- c2rust-analyze/src/util.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/c2rust-analyze/src/util.rs b/c2rust-analyze/src/util.rs index ef7488ed3..7988bd576 100644 --- a/c2rust-analyze/src/util.rs +++ b/c2rust-analyze/src/util.rs @@ -7,7 +7,9 @@ use rustc_middle::mir::{ BasicBlock, BasicBlockData, Constant, Field, Local, Location, Mutability, Operand, Place, PlaceElem, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, }; -use rustc_middle::ty::{self, AdtDef, DefIdTree, SubstsRef, Ty, TyCtxt, TyKind, UintTy}; +use rustc_middle::ty::{ + self, AdtDef, DefIdTree, EarlyBinder, Subst, SubstsRef, Ty, TyCtxt, TyKind, UintTy, +}; use std::fmt::Debug; #[derive(Debug)] @@ -168,7 +170,7 @@ pub fn ty_callee<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Callee<'tcx> { ty::FnDef(did, substs) => { if is_trivial() { Callee::Trivial - } else if let Some(callee) = builtin_callee(tcx, did) { + } else if let Some(callee) = builtin_callee(tcx, did, substs) { callee } else if !did.is_local() || tcx.def_kind(tcx.parent(did)) == DefKind::ForeignMod { Callee::UnknownDef { ty } @@ -190,7 +192,7 @@ pub fn ty_callee<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Callee<'tcx> { } } -fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option { +fn builtin_callee<'tcx>(tcx: TyCtxt<'tcx>, did: DefId, substs: SubstsRef<'tcx>) -> Option { let name = tcx.item_name(did); match name.as_str() { @@ -203,7 +205,7 @@ fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option { if tcx.impl_trait_ref(parent_did).is_some() { return None; } - let parent_impl_ty = tcx.type_of(parent_did); + let parent_impl_ty = EarlyBinder(tcx.type_of(parent_did)).subst(tcx, substs); let (pointee_ty, mutbl) = match parent_impl_ty.kind() { TyKind::RawPtr(tm) => (tm.ty, tm.mutbl), _ => return None, @@ -220,7 +222,7 @@ fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option { if tcx.impl_trait_ref(parent_did).is_some() { return None; } - let parent_impl_ty = tcx.type_of(parent_did); + let parent_impl_ty = EarlyBinder(tcx.type_of(parent_did)).subst(tcx, substs); let elem_ty = match *parent_impl_ty.kind() { TyKind::Array(ty, _) => ty, TyKind::Slice(ty) => ty, @@ -276,7 +278,7 @@ fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option { if tcx.impl_trait_ref(parent_did).is_some() { return None; } - let parent_impl_ty = tcx.type_of(parent_did); + let parent_impl_ty = EarlyBinder(tcx.type_of(parent_did)).subst(tcx, substs); let (_pointee_ty, _mutbl) = match parent_impl_ty.kind() { TyKind::RawPtr(tm) => (tm.ty, tm.mutbl), _ => return None,