Skip to content

Commit

Permalink
fix bug in rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
aneksteind committed Oct 15, 2023
1 parent e73c2ae commit 5f49628
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
36 changes: 24 additions & 12 deletions c2rust-analyze/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,16 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
}
}

/// Returns the [`LTy`] of an [`Rvalue`] and a boolean indicating whether or
/// Returns the [`LTy`] of an [`Rvalue`]
pub fn type_of_rvalue(&self, rv: &Rvalue<'tcx>, loc: Location) -> LTy<'tcx> {
if let Some(lty) = self.rvalue_tys.get(&loc) {
return lty;
}

self.derived_type_of_rvalue(rv)
}

/// Returns a boolean indicating whether or
/// not the `Rvalue` is a reference or pointer containing a field projection.
/// For example, the following [`Rvalue`]s will satisfy that criteria:
/// - let r1 = std::ptr::addr_of!(x.field);
Expand All @@ -954,13 +963,19 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
/// The following will NOT satisfy that critera:
/// - let r1 = x.field;
/// - let r2 = x.field + y;
pub fn type_of_rvalue(&self, rv: &Rvalue<'tcx>, loc: Location) -> (LTy<'tcx>, bool) {
let mut has_field_projection = false;
if let Some(lty) = self.rvalue_tys.get(&loc) {
return (lty, false);
pub fn has_field_projection(&self, rv: &Rvalue<'tcx>) -> bool {
if let Some(desc) = describe_rvalue(rv) {
match desc {
RvalueDesc::Project { proj, .. } | RvalueDesc::AddrOfLocal { proj, .. } => {
for p in proj {
if let PlaceElem::Field(..) = p {
return true;
}
}
}
}
}

self.derived_type_of_rvalue(rv)
false
}

/// In some cases, we can compute an `LTy` for an `Rvalue` that uses `PointerId`s derived from
Expand Down Expand Up @@ -1003,9 +1018,6 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {

let mut pointee_lty = pointee_lty;
for p in proj {
if let PlaceElem::Field(..) = p {
has_field_projection = true;
}
pointee_lty = self.projection_lty(pointee_lty, p);
}

Expand All @@ -1024,7 +1036,7 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
);

let args = self.lcx().mk_slice(&[pointee_lty]);
return (self.lcx().mk(ty, args, ptr), has_field_projection);
return self.lcx().mk(ty, args, ptr);
}
}

Expand Down Expand Up @@ -1056,7 +1068,7 @@ impl<'a, 'tcx> AnalysisCtxt<'a, 'tcx> {
Rvalue::ShallowInitBox(ref _op, _ty) => todo!("type_of ShallowInitBox: rv = {rv:?}"),
};

(ty, false)
ty
}

pub fn projection_lty(&self, lty: LTy<'tcx>, proj: &PlaceElem<'tcx>) -> LTy<'tcx> {
Expand Down
4 changes: 2 additions & 2 deletions c2rust-analyze/src/dataflow/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ impl<'tcx> TypeChecker<'tcx, '_> {
self.visit_place(pl, Mutability::Mut);
let pl_lty = self.acx.type_of(pl);

let (rv_lty, has_field_projection) = self.acx.type_of_rvalue(rv, loc);
let rv_lty = self.acx.type_of_rvalue(rv, loc);
self.visit_rvalue(rv, rv_lty);

if has_field_projection {
if self.acx.has_field_projection(rv) {
// Fields don't get offset permissions propagated to their base pointer
self.do_assign_except(
pl_lty,
Expand Down
2 changes: 1 addition & 1 deletion c2rust-analyze/src/rewrite/expr/mir_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl<'a, 'tcx> ExprRewriteVisitor<'a, 'tcx> {
_ => {}
};

let (rv_lty, _) = self.acx.type_of_rvalue(rv, loc);
let rv_lty = self.acx.type_of_rvalue(rv, loc);
self.enter_rvalue(|v| v.visit_rvalue(rv, Some(rv_lty)));
// The cast from `rv_lty` to `pl_lty` should be applied to the RHS.
self.enter_rvalue(|v| v.emit_cast_lty_lty(rv_lty, pl_lty));
Expand Down

0 comments on commit 5f49628

Please sign in to comment.