Skip to content

Commit 01708f4

Browse files
committed
(c2rust-analyze) Add known perms for const refs before dataflow typeck.
Also, add skeleton code for checking that these perms are not modified after dataflow typeck, but I'm not yet sure how to lookup the `PermissionSet` for a `PointerId`, so that part is `TODO`ed.
1 parent 84dbaea commit 01708f4

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

c2rust-analyze/src/dataflow/type_check.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use crate::util::{describe_rvalue, ty_callee, Callee, RvalueDesc};
55
use assert_matches::assert_matches;
66
use rustc_hir::def_id::DefId;
77
use rustc_middle::mir::{
8-
AggregateKind, BinOp, Body, Location, Mutability, Operand, Place, PlaceRef, ProjectionElem,
9-
Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
8+
AggregateKind, BinOp, Body, ConstantKind, Location, Mutability, Operand, Place, PlaceRef,
9+
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
1010
};
11-
use rustc_middle::ty::{SubstsRef, Ty, TyKind};
11+
use rustc_middle::ty::{self, SubstsRef, Ty, TyKind};
1212

1313
/// Visitor that walks over the MIR, computing types of rvalues/operands/places and generating
1414
/// constraints as a side effect.
@@ -454,6 +454,21 @@ impl<'tcx> TypeChecker<'tcx, '_> {
454454
}
455455
}
456456

457+
fn const_perms(constant: ConstantKind) -> PermissionSet {
458+
let ref_ty = constant.ty();
459+
let ty = match ref_ty.kind() {
460+
ty::Ref(_, ty, _) => ty,
461+
_ => panic!("expected only `Ref`s for constants: {ref_ty:?}"),
462+
};
463+
if ty.is_array() || ty.is_str() {
464+
PermissionSet::READ | PermissionSet::OFFSET_ADD
465+
} else if ty.is_primitive_ty() {
466+
PermissionSet::READ
467+
} else {
468+
panic!("expected an array, str, or primitive type: {ty:?}");
469+
}
470+
}
471+
457472
pub fn visit<'tcx>(
458473
acx: &AnalysisCtxt<'_, 'tcx>,
459474
mir: &Body<'tcx>,
@@ -465,6 +480,11 @@ pub fn visit<'tcx>(
465480
equiv_constraints: Vec::new(),
466481
};
467482

483+
for (&constant, const_lty) in &acx.const_ref_tys {
484+
tc.constraints
485+
.add_all_perms(const_lty.label, const_perms(constant));
486+
}
487+
468488
for (bb, bb_data) in mir.basic_blocks().iter_enumerated() {
469489
for (i, stmt) in bb_data.statements.iter().enumerate() {
470490
tc.visit_statement(
@@ -484,5 +504,11 @@ pub fn visit<'tcx>(
484504
);
485505
}
486506

507+
for (&constant, const_lty) in &acx.const_ref_tys {
508+
let _ptr_id = const_lty.label;
509+
let _expected_perms = const_perms(constant);
510+
// TODO: check that perms match the expected ones
511+
}
512+
487513
(tc.constraints, tc.equiv_constraints)
488514
}

0 commit comments

Comments
 (0)