@@ -1122,3 +1122,86 @@ fn test_read_ref_no_arg() {
11221122 let fun_context = get_fun_context ( & module) ;
11231123 let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
11241124}
1125+
1126+
1127+ #[ test]
1128+ fn test_write_ref_correct_type ( ) {
1129+ let code = vec ! [ Bytecode :: LdU64 ( 42 ) , Bytecode :: MutBorrowLoc ( 0 ) , Bytecode :: WriteRef ] ;
1130+ let module = make_module_with_local ( code, SignatureToken :: U64 ) ;
1131+ let fun_context = get_fun_context ( & module) ;
1132+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1133+ assert ! ( result. is_ok( ) ) ;
1134+
1135+ let code = vec ! [
1136+ Bytecode :: LdU32 ( 42 ) ,
1137+ Bytecode :: Pack ( StructDefinitionIndex ( 0 ) ) ,
1138+ Bytecode :: MutBorrowLoc ( 0 ) ,
1139+ Bytecode :: WriteRef
1140+ ] ;
1141+ let mut module = make_module_with_local ( code, SignatureToken :: Struct ( StructHandleIndex ( 0 ) ) ) ;
1142+ add_simple_struct_with_abilities ( & mut module, AbilitySet :: PRIMITIVES ) ;
1143+ let fun_context = get_fun_context ( & module) ;
1144+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1145+ assert ! ( result. is_ok( ) ) ;
1146+
1147+ }
1148+
1149+ #[ test]
1150+ fn test_write_ref_wrong_type ( ) {
1151+ let code = vec ! [ Bytecode :: LdU64 ( 42 ) , Bytecode :: ImmBorrowLoc ( 0 ) , Bytecode :: WriteRef ] ;
1152+ let module = make_module_with_local ( code, SignatureToken :: U64 ) ;
1153+ let fun_context = get_fun_context ( & module) ;
1154+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1155+ assert_eq ! (
1156+ result. unwrap_err( ) . major_status( ) ,
1157+ StatusCode :: WRITEREF_NO_MUTABLE_REFERENCE_ERROR
1158+ ) ;
1159+ }
1160+
1161+ #[ test]
1162+ fn test_write_ref_mismatched_types ( ) {
1163+ let code = vec ! [ Bytecode :: LdU32 ( 42 ) , Bytecode :: MutBorrowLoc ( 0 ) , Bytecode :: WriteRef ] ;
1164+ let module = make_module_with_local ( code, SignatureToken :: U64 ) ;
1165+ let fun_context = get_fun_context ( & module) ;
1166+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1167+ assert_eq ! (
1168+ result. unwrap_err( ) . major_status( ) ,
1169+ StatusCode :: WRITEREF_TYPE_MISMATCH_ERROR
1170+ ) ;
1171+ }
1172+
1173+ #[ test]
1174+ fn test_write_ref_no_drop ( ) {
1175+ let code = vec ! [
1176+ Bytecode :: LdU32 ( 42 ) ,
1177+ Bytecode :: Pack ( StructDefinitionIndex ( 0 ) ) ,
1178+ Bytecode :: MutBorrowLoc ( 0 ) ,
1179+ Bytecode :: WriteRef
1180+ ] ;
1181+ let mut module = make_module_with_local ( code, SignatureToken :: Struct ( StructHandleIndex ( 0 ) ) ) ;
1182+ add_simple_struct_with_abilities ( & mut module, AbilitySet :: EMPTY ) ;
1183+ let fun_context = get_fun_context ( & module) ;
1184+ let result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1185+ assert_eq ! (
1186+ result. unwrap_err( ) . major_status( ) ,
1187+ StatusCode :: WRITEREF_WITHOUT_DROP_ABILITY
1188+ ) ;
1189+ }
1190+
1191+ #[ test]
1192+ #[ should_panic]
1193+ fn test_write_ref_too_few_args ( ) {
1194+ let code = vec ! [ Bytecode :: MutBorrowLoc ( 0 ) , Bytecode :: WriteRef ] ;
1195+ let module = make_module_with_local ( code, SignatureToken :: U32 ) ;
1196+ let fun_context = get_fun_context ( & module) ;
1197+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1198+ }
1199+
1200+ #[ test]
1201+ #[ should_panic]
1202+ fn test_write_ref_no_args ( ) {
1203+ let code = vec ! [ Bytecode :: WriteRef ] ;
1204+ let module = make_module_with_local ( code, SignatureToken :: U64 ) ;
1205+ let fun_context = get_fun_context ( & module) ;
1206+ let _result = type_safety:: verify ( & module, & fun_context, & mut DummyMeter ) ;
1207+ }
0 commit comments