Skip to content

Commit a989e17

Browse files
authored
analyze: minor fixes (#918)
A couple small fixes split out from the `FIXED` work * 85bd2ac: Makes `pointer_table[PointerId::NONE]` panic with a clearer message (it already panics, but the message is rather opaque). * e02e0b8: The `pointee_ty` field of `Callee::PtrOffset` currently is always set to the `T` from `impl<T> *mut T { ... }`; this commit fixes it to be the actual pointee type at the use site. For example, on `(x: *mut i32).offset(...)`, `pointee_ty` will now be `i32`.
2 parents de612ba + e313cbd commit a989e17

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

c2rust-analyze/src/pointer_id.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ impl<'a, T> PointerTable<'a, T> {
323323
impl<'a, T> Index<PointerId> for PointerTable<'a, T> {
324324
type Output = T;
325325
fn index(&self, id: PointerId) -> &T {
326+
debug_assert!(!id.is_none());
326327
if id.is_global() {
327328
&self.global[id]
328329
} else {
@@ -372,6 +373,7 @@ impl<'a, T> PointerTableMut<'a, T> {
372373
impl<'a, T> Index<PointerId> for PointerTableMut<'a, T> {
373374
type Output = T;
374375
fn index(&self, id: PointerId) -> &T {
376+
debug_assert!(!id.is_none());
375377
if id.is_global() {
376378
&self.global[id]
377379
} else {
@@ -382,6 +384,7 @@ impl<'a, T> Index<PointerId> for PointerTableMut<'a, T> {
382384

383385
impl<'a, T> IndexMut<PointerId> for PointerTableMut<'a, T> {
384386
fn index_mut(&mut self, id: PointerId) -> &mut T {
387+
debug_assert!(!id.is_none());
385388
if id.is_global() {
386389
&mut self.global[id]
387390
} else {
@@ -438,6 +441,7 @@ impl<T> OwnedPointerTable<T> {
438441
impl<T> Index<PointerId> for OwnedPointerTable<T> {
439442
type Output = T;
440443
fn index(&self, id: PointerId) -> &T {
444+
debug_assert!(!id.is_none());
441445
if id.is_global() {
442446
&self.global[id]
443447
} else {
@@ -448,6 +452,7 @@ impl<T> Index<PointerId> for OwnedPointerTable<T> {
448452

449453
impl<T> IndexMut<PointerId> for OwnedPointerTable<T> {
450454
fn index_mut(&mut self, id: PointerId) -> &mut T {
455+
debug_assert!(!id.is_none());
451456
if id.is_global() {
452457
&mut self.global[id]
453458
} else {

c2rust-analyze/src/util.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_middle::mir::{
77
BasicBlock, BasicBlockData, Constant, Field, Local, Location, Mutability, Operand, Place,
88
PlaceElem, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind,
99
};
10-
use rustc_middle::ty::{self, AdtDef, DefIdTree, SubstsRef, Ty, TyCtxt, TyKind, UintTy};
10+
use rustc_middle::ty::{
11+
self, AdtDef, DefIdTree, EarlyBinder, Subst, SubstsRef, Ty, TyCtxt, TyKind, UintTy,
12+
};
1113
use std::fmt::Debug;
1214

1315
#[derive(Debug)]
@@ -168,7 +170,7 @@ pub fn ty_callee<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Callee<'tcx> {
168170
ty::FnDef(did, substs) => {
169171
if is_trivial() {
170172
Callee::Trivial
171-
} else if let Some(callee) = builtin_callee(tcx, did) {
173+
} else if let Some(callee) = builtin_callee(tcx, did, substs) {
172174
callee
173175
} else if !did.is_local() || tcx.def_kind(tcx.parent(did)) == DefKind::ForeignMod {
174176
Callee::UnknownDef { ty }
@@ -190,7 +192,7 @@ pub fn ty_callee<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Callee<'tcx> {
190192
}
191193
}
192194

193-
fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option<Callee> {
195+
fn builtin_callee<'tcx>(tcx: TyCtxt<'tcx>, did: DefId, substs: SubstsRef<'tcx>) -> Option<Callee> {
194196
let name = tcx.item_name(did);
195197

196198
match name.as_str() {
@@ -203,7 +205,7 @@ fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option<Callee> {
203205
if tcx.impl_trait_ref(parent_did).is_some() {
204206
return None;
205207
}
206-
let parent_impl_ty = tcx.type_of(parent_did);
208+
let parent_impl_ty = EarlyBinder(tcx.type_of(parent_did)).subst(tcx, substs);
207209
let (pointee_ty, mutbl) = match parent_impl_ty.kind() {
208210
TyKind::RawPtr(tm) => (tm.ty, tm.mutbl),
209211
_ => return None,
@@ -220,7 +222,7 @@ fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option<Callee> {
220222
if tcx.impl_trait_ref(parent_did).is_some() {
221223
return None;
222224
}
223-
let parent_impl_ty = tcx.type_of(parent_did);
225+
let parent_impl_ty = EarlyBinder(tcx.type_of(parent_did)).subst(tcx, substs);
224226
let elem_ty = match *parent_impl_ty.kind() {
225227
TyKind::Array(ty, _) => ty,
226228
TyKind::Slice(ty) => ty,
@@ -276,7 +278,7 @@ fn builtin_callee(tcx: TyCtxt, did: DefId) -> Option<Callee> {
276278
if tcx.impl_trait_ref(parent_did).is_some() {
277279
return None;
278280
}
279-
let parent_impl_ty = tcx.type_of(parent_did);
281+
let parent_impl_ty = EarlyBinder(tcx.type_of(parent_did)).subst(tcx, substs);
280282
let (_pointee_ty, _mutbl) = match parent_impl_ty.kind() {
281283
TyKind::RawPtr(tm) => (tm.ty, tm.mutbl),
282284
_ => return None,

0 commit comments

Comments
 (0)