@@ -5,10 +5,10 @@ use crate::util::{describe_rvalue, ty_callee, Callee, RvalueDesc};
5
5
use assert_matches:: assert_matches;
6
6
use rustc_hir:: def_id:: DefId ;
7
7
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 ,
10
10
} ;
11
- use rustc_middle:: ty:: { SubstsRef , Ty , TyKind } ;
11
+ use rustc_middle:: ty:: { self , SubstsRef , Ty , TyKind } ;
12
12
13
13
/// Visitor that walks over the MIR, computing types of rvalues/operands/places and generating
14
14
/// constraints as a side effect.
@@ -454,6 +454,21 @@ impl<'tcx> TypeChecker<'tcx, '_> {
454
454
}
455
455
}
456
456
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
+
457
472
pub fn visit < ' tcx > (
458
473
acx : & AnalysisCtxt < ' _ , ' tcx > ,
459
474
mir : & Body < ' tcx > ,
@@ -465,6 +480,11 @@ pub fn visit<'tcx>(
465
480
equiv_constraints : Vec :: new ( ) ,
466
481
} ;
467
482
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
+
468
488
for ( bb, bb_data) in mir. basic_blocks ( ) . iter_enumerated ( ) {
469
489
for ( i, stmt) in bb_data. statements . iter ( ) . enumerate ( ) {
470
490
tc. visit_statement (
@@ -484,5 +504,11 @@ pub fn visit<'tcx>(
484
504
) ;
485
505
}
486
506
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
+
487
513
( tc. constraints , tc. equiv_constraints )
488
514
}
0 commit comments