Skip to content

Commit fec08df

Browse files
committed
(c2rust-analyze) Move fn const_perms to pub fn PermissionSet::for_const.
1 parent 01708f4 commit fec08df

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

c2rust-analyze/src/context.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::mir::{
1414
Place, PlaceElem, PlaceRef, Rvalue,
1515
};
1616
use rustc_middle::ty::adjustment::PointerCast;
17-
use rustc_middle::ty::{AdtDef, FieldDef, Ty, TyCtxt, TyKind};
17+
use rustc_middle::ty::{AdtDef, FieldDef, Ty, TyCtxt, TyKind, self};
1818
use std::collections::HashMap;
1919
use std::ops::Index;
2020

@@ -52,6 +52,23 @@ bitflags! {
5252
}
5353
}
5454

55+
impl PermissionSet {
56+
pub fn for_const(constant: ConstantKind) -> PermissionSet {
57+
let ref_ty = constant.ty();
58+
let ty = match ref_ty.kind() {
59+
ty::Ref(_, ty, _) => ty,
60+
_ => panic!("expected only `Ref`s for constants: {ref_ty:?}"),
61+
};
62+
if ty.is_array() || ty.is_str() {
63+
PermissionSet::READ | PermissionSet::OFFSET_ADD
64+
} else if ty.is_primitive_ty() {
65+
PermissionSet::READ
66+
} else {
67+
panic!("expected an array, str, or primitive type: {ty:?}");
68+
}
69+
}
70+
}
71+
5572
bitflags! {
5673
/// Additional flags describing a given pointer type. These are mainly derived from
5774
/// `PermissionSet`, but don't follow the normal subtyping rules and propagation algorithm.

c2rust-analyze/src/dataflow/type_check.rs

Lines changed: 5 additions & 20 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, ConstantKind, Location, Mutability, Operand, Place, PlaceRef,
9-
ProjectionElem, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
8+
AggregateKind, BinOp, Body, Location, Mutability, Operand, Place, PlaceRef, ProjectionElem,
9+
Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
1010
};
11-
use rustc_middle::ty::{self, SubstsRef, Ty, TyKind};
11+
use rustc_middle::ty::{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,21 +454,6 @@ 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-
472457
pub fn visit<'tcx>(
473458
acx: &AnalysisCtxt<'_, 'tcx>,
474459
mir: &Body<'tcx>,
@@ -482,7 +467,7 @@ pub fn visit<'tcx>(
482467

483468
for (&constant, const_lty) in &acx.const_ref_tys {
484469
tc.constraints
485-
.add_all_perms(const_lty.label, const_perms(constant));
470+
.add_all_perms(const_lty.label, PermissionSet::for_const(constant));
486471
}
487472

488473
for (bb, bb_data) in mir.basic_blocks().iter_enumerated() {
@@ -506,7 +491,7 @@ pub fn visit<'tcx>(
506491

507492
for (&constant, const_lty) in &acx.const_ref_tys {
508493
let _ptr_id = const_lty.label;
509-
let _expected_perms = const_perms(constant);
494+
let _expected_perms = PermissionSet::for_const(constant);
510495
// TODO: check that perms match the expected ones
511496
}
512497

0 commit comments

Comments
 (0)