From 7164b6acb34f78c7d81b72947f271b01667093ef Mon Sep 17 00:00:00 2001 From: Teng Zhang Date: Sun, 7 Apr 2024 19:38:16 -0700 Subject: [PATCH] check field --- .../move-compiler-v2/src/function_checker.rs | 132 +++++++++++++++++- .../{ => v1-typing}/invalid_type_acquire.exp | 12 +- .../{ => v1-typing}/invalid_type_acquire.move | 0 .../checking/inlining/resources_invalid.exp | 28 +--- .../checking/typing/borrow_field_internal.exp | 38 +++-- .../typing/global_builtins_script.exp | 39 +++--- .../implicit_deref_borrow_field_internal.exp | 38 +++-- .../checking/typing/mutate_field_internal.exp | 38 +++-- .../move/move-compiler-v2/tests/testsuite.rs | 2 +- .../move/move-compiler-v2/tests/v1.matched | 36 +---- .../move/move-compiler-v2/tests/v1.unmatched | 17 --- .../visibility-checker/global_operator.exp | 73 ++++++++++ .../visibility-checker/global_operator.move | 31 ++++ .../pack_unpack_structs.exp | 51 +++++++ .../pack_unpack_structs.move | 24 ++++ .../visibility-checker/resources_invalid.exp | 12 ++ .../visibility-checker/resources_invalid.move | 20 +++ .../v1-typing/pack_private_with_field.exp | 15 ++ .../v1-typing/pack_private_with_field.move | 28 ++++ .../v1-typing/pack_unpack_private.exp | 17 +++ .../v1-typing/pack_unpack_private.move | 13 ++ .../v1-typing/pack_unpack_private_script.exp | 18 +++ .../v1-typing/pack_unpack_private_script.move | 15 ++ .../tools/testdiff/src/main.rs | 4 + 24 files changed, 542 insertions(+), 159 deletions(-) rename third_party/move/move-compiler-v2/tests/checking-lang-v1/{ => v1-typing}/invalid_type_acquire.exp (69%) rename third_party/move/move-compiler-v2/tests/checking-lang-v1/{ => v1-typing}/invalid_type_acquire.move (100%) create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.exp create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.move create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.exp create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.move create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.exp create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.move create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.exp create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.move create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.exp create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.move create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.exp create mode 100644 third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.move diff --git a/third_party/move/move-compiler-v2/src/function_checker.rs b/third_party/move/move-compiler-v2/src/function_checker.rs index fb4d3e7dd7f97..64335cf12e30c 100644 --- a/third_party/move/move-compiler-v2/src/function_checker.rs +++ b/third_party/move/move-compiler-v2/src/function_checker.rs @@ -7,7 +7,8 @@ use crate::Options; use codespan_reporting::diagnostic::Severity; use move_binary_format::file_format::Visibility; use move_model::{ - model::{FunId, FunctionEnv, GlobalEnv, Loc, NodeId, QualifiedId}, + ast::{ExpData, Operation, Pattern}, + model::{FunId, FunctionEnv, GlobalEnv, Loc, ModuleId, NodeId, QualifiedId}, ty::Type, }; use std::{collections::BTreeSet, iter::Iterator, vec::Vec}; @@ -56,6 +57,131 @@ pub fn check_for_function_typed_parameters(env: &mut GlobalEnv) { } } +fn access_error(env: &GlobalEnv, fun_loc: &Loc, id: &NodeId, oper: &str, msg: String) { + let call_details: Vec<_> = [*id] + .iter() + .map(|node_id| (env.get_node_loc(*node_id), format!("{} here", oper))) + .collect(); + env.diag_with_labels(Severity::Error, fun_loc, &msg, call_details); +} + +/// check a struct can only be accessed within the module that defines it. +fn check_access_and_use_of_structs(env: &GlobalEnv, fun_env: &FunctionEnv) { + if let Some(fun_body) = fun_env.get_def() { + let caller_module_id = fun_env.module_env.get_id(); + fun_body.visit_pre_order(&mut |exp: &ExpData| { + match exp { + ExpData::Call(id, oper, _) => match oper { + Operation::Exists(_) + | Operation::BorrowGlobal(_) + | Operation::MoveFrom + | Operation::MoveTo + if env.get_node_instantiation(*id)[0].get_struct(env).is_some() => + { + let inst = env.get_node_instantiation(*id); + let (mid, sid, _) = inst[0].require_struct(); + if mid != caller_module_id { + let qualified_struct_id = mid.qualified(sid); + let struct_env = env.get_struct(qualified_struct_id); + access_error( + env, + &fun_env.get_id_loc(), + id, + "called", + format!( + "Invalid storage operation on external type `{}`", + struct_env.get_full_name_str() + ), + ); + } + }, + Operation::Select(mid, sid, fid) if *mid != caller_module_id => { + let qualified_struct_id = mid.qualified(*sid); + let struct_env = env.get_struct(qualified_struct_id); + access_error( + env, + &fun_env.get_id_loc(), + id, + "accessed", + format!( + "Invalid access of field `{}` on external type `{}`", + fid.symbol().display(struct_env.symbol_pool()), + struct_env.get_full_name_str() + ), + ); + }, + Operation::Pack(mid, sid) => { + if *mid != caller_module_id { + let qualified_struct_id = mid.qualified(*sid); + let struct_env = env.get_struct(qualified_struct_id); + access_error( + env, + &fun_env.get_id_loc(), + id, + "packed", + format!( + "Invalid pack operation on external type {}", + struct_env.get_full_name_str() + ), + ); + } + }, + _ => {}, + }, + ExpData::Assign(_, pat, _) | ExpData::Block(_, pat, _, _) => { + check_access_and_use_of_structs_in_pattern( + env, + &fun_env.get_id_loc(), + &caller_module_id, + pat, + ); + }, + // access in specs is not restricted + ExpData::SpecBlock(_, _) => { + return false; + }, + _ => {}, + } + true + }); + } +} + +fn check_access_and_use_of_structs_in_pattern( + env: &GlobalEnv, + fun_loc: &Loc, + mid: &ModuleId, + pat: &Pattern, +) { + match pat { + Pattern::Struct(id, str, args) => { + let module_id = str.module_id; + if module_id != *mid { + let struct_env = env.get_struct(str.to_qualified_id()); + access_error( + env, + fun_loc, + id, + "unpacked", + format!( + "Invalid unpack operation on external type {}", + struct_env.get_full_name_str() + ), + ); + } + for pat in args { + check_access_and_use_of_structs_in_pattern(env, fun_loc, mid, pat); + } + }, + Pattern::Tuple(_, pats) => { + for pat in pats { + check_access_and_use_of_structs_in_pattern(env, fun_loc, mid, pat); + } + }, + _ => {}, + } +} + /// For all function in target modules: /// /// If `before_inlining`, then @@ -63,6 +189,7 @@ pub fn check_for_function_typed_parameters(env: &mut GlobalEnv) { /// - warn about unused private functions /// Otherwise (`!before_inlining`): /// - check that all function calls *not* involving inline functions are accessible. +/// - check structs are not accessed across the module boundary. pub fn check_access_and_use(env: &mut GlobalEnv, before_inlining: bool) { // For each function seen, we record whether it has an accessible caller. let mut functions_with_callers: BTreeSet = BTreeSet::new(); @@ -77,6 +204,9 @@ pub fn check_access_and_use(env: &mut GlobalEnv, before_inlining: bool) { let caller_module_has_friends = !caller_module.has_no_friends(); let caller_module_is_script = caller_module.get_name().is_script(); for caller_func in caller_module.get_functions() { + if !before_inlining { + check_access_and_use_of_structs(env, &caller_func); + } let caller_qfid = caller_func.get_qualified_id(); // During first pass, record private functions for later diff --git a/third_party/move/move-compiler-v2/tests/checking-lang-v1/invalid_type_acquire.exp b/third_party/move/move-compiler-v2/tests/checking-lang-v1/v1-typing/invalid_type_acquire.exp similarity index 69% rename from third_party/move/move-compiler-v2/tests/checking-lang-v1/invalid_type_acquire.exp rename to third_party/move/move-compiler-v2/tests/checking-lang-v1/v1-typing/invalid_type_acquire.exp index b3ed2abde218f..2c8415cbb4dbd 100644 --- a/third_party/move/move-compiler-v2/tests/checking-lang-v1/invalid_type_acquire.exp +++ b/third_party/move/move-compiler-v2/tests/checking-lang-v1/v1-typing/invalid_type_acquire.exp @@ -1,31 +1,31 @@ Diagnostics: error: invalid access specifier - ┌─ tests/checking-lang-v1/invalid_type_acquire.move:18:9 + ┌─ tests/checking-lang-v1/v1-typing/invalid_type_acquire.move:18:9 │ 18 │ T, │ ^ error: not supported before language version `2.0-unstable`: address and wildcard access specifiers. Only resource type names can be provided. - ┌─ tests/checking-lang-v1/invalid_type_acquire.move:18:9 + ┌─ tests/checking-lang-v1/v1-typing/invalid_type_acquire.move:18:9 │ 18 │ T, │ ^ error: invalid access specifier - ┌─ tests/checking-lang-v1/invalid_type_acquire.move:19:9 + ┌─ tests/checking-lang-v1/v1-typing/invalid_type_acquire.move:19:9 │ 19 │ u64, │ ^^^ error: not supported before language version `2.0-unstable`: address and wildcard access specifiers. Only resource type names can be provided. - ┌─ tests/checking-lang-v1/invalid_type_acquire.move:19:9 + ┌─ tests/checking-lang-v1/v1-typing/invalid_type_acquire.move:19:9 │ 19 │ u64, │ ^^^ error: type `u64` is missing required ability `key` - ┌─ tests/checking-lang-v1/invalid_type_acquire.move:32:36 + ┌─ tests/checking-lang-v1/v1-typing/invalid_type_acquire.move:32:36 │ 32 │ destroy(account, move_from(a)); │ ^^^ @@ -33,7 +33,7 @@ error: type `u64` is missing required ability `key` = required by instantiating type parameter `T:key` of function `move_from` error: type `S` is missing required ability `key` - ┌─ tests/checking-lang-v1/invalid_type_acquire.move:34:36 + ┌─ tests/checking-lang-v1/v1-typing/invalid_type_acquire.move:34:36 │ 34 │ destroy(account, move_from(a)); │ ^ diff --git a/third_party/move/move-compiler-v2/tests/checking-lang-v1/invalid_type_acquire.move b/third_party/move/move-compiler-v2/tests/checking-lang-v1/v1-typing/invalid_type_acquire.move similarity index 100% rename from third_party/move/move-compiler-v2/tests/checking-lang-v1/invalid_type_acquire.move rename to third_party/move/move-compiler-v2/tests/checking-lang-v1/v1-typing/invalid_type_acquire.move diff --git a/third_party/move/move-compiler-v2/tests/checking/inlining/resources_invalid.exp b/third_party/move/move-compiler-v2/tests/checking/inlining/resources_invalid.exp index 289dc8cf4f230..71735bd677231 100644 --- a/third_party/move/move-compiler-v2/tests/checking/inlining/resources_invalid.exp +++ b/third_party/move/move-compiler-v2/tests/checking/inlining/resources_invalid.exp @@ -1,28 +1,12 @@ -// -- Model dump before bytecode pipeline -module 0x42::objects { - struct ReaderRef { - addr: address, - } -} // end 0x42::objects -module 0x42::token { - use 0x42::objects as obj; // resolved as: 0x42::objects - struct Token { - val: u64, - } - public fun get_value(ref: &objects::ReaderRef): u64 - acquires token::Token(*) - { - select token::Token.val<&token::Token>({ - let (ref: &objects::ReaderRef): (&objects::ReaderRef) = Tuple(ref); - BorrowGlobal(Immutable)(select objects::ReaderRef.addr<&objects::ReaderRef>(ref)) - }) - } -} // end 0x42::token - Diagnostics: -bug: struct not defined +error: Invalid access of field `addr` on external type `objects::ReaderRef` ┌─ tests/checking/inlining/resources_invalid.move:17:16 │ + 8 │ borrow_global(ref.addr) + │ -------- accessed here + · 17 │ public fun get_value(ref: &obj::ReaderRef): u64 acquires Token { │ ^^^^^^^^^ +18 │ obj::reader(ref).val + │ ---------------- from a call inlined at this callsite diff --git a/third_party/move/move-compiler-v2/tests/checking/typing/borrow_field_internal.exp b/third_party/move/move-compiler-v2/tests/checking/typing/borrow_field_internal.exp index 7cf7ab7d87c3a..fb604346b4956 100644 --- a/third_party/move/move-compiler-v2/tests/checking/typing/borrow_field_internal.exp +++ b/third_party/move/move-compiler-v2/tests/checking/typing/borrow_field_internal.exp @@ -1,20 +1,18 @@ -// -- Model dump before bytecode pipeline -module 0x2::X { - struct S { - f: u64, - } - public fun s(): X::S { - pack X::S(0) - } -} // end 0x2::X -module 0x2::M { - use 0x2::X; // resolved as: 0x2::X - private fun t0() { - Borrow(Immutable)(select X::S.f(X::s())); - { - let s: &X::S = Borrow(Immutable)(X::s()); - Borrow(Immutable)(select X::S.f<&X::S>(s)); - Abort(0) - } - } -} // end 0x2::M + +Diagnostics: +error: Invalid access of field `f` on external type `X::S` + ┌─ tests/checking/typing/borrow_field_internal.move:12:9 + │ +12 │ fun t0() { + │ ^^ +13 │ (&X::s().f: &u64); + │ -------- accessed here + +error: Invalid access of field `f` on external type `X::S` + ┌─ tests/checking/typing/borrow_field_internal.move:12:9 + │ +12 │ fun t0() { + │ ^^ + · +15 │ (&s.f: &u64); + │ --- accessed here diff --git a/third_party/move/move-compiler-v2/tests/checking/typing/global_builtins_script.exp b/third_party/move/move-compiler-v2/tests/checking/typing/global_builtins_script.exp index 8089a2fe7cb57..0bc68d9074959 100644 --- a/third_party/move/move-compiler-v2/tests/checking/typing/global_builtins_script.exp +++ b/third_party/move/move-compiler-v2/tests/checking/typing/global_builtins_script.exp @@ -1,20 +1,19 @@ -// -- Model dump before bytecode pipeline -module 0x42::M { - struct R { - dummy_field: bool, - } - public fun new(): M::R { - pack M::R(false) - } -} // end 0x42::M -module _0 { - use 0x42::M; // resolved as: 0x42::M - private fun test(account: signer) { - { - let r: M::R = M::new(); - BorrowGlobal(Immutable)(0x1); - MoveTo(Borrow(Immutable)(account), r); - Tuple() - } - } -} // end _0 + +Diagnostics: +error: Invalid storage operation on external type `M::R` + ┌─ tests/checking/typing/global_builtins_script.move:14:5 + │ +14 │ fun test(account: signer) { + │ ^^^^ +15 │ let r = M::new(); +16 │ borrow_global(@0x1); + │ ------------------------- called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/checking/typing/global_builtins_script.move:14:5 + │ +14 │ fun test(account: signer) { + │ ^^^^ + · +17 │ move_to(&account, r); + │ -------------------- called here diff --git a/third_party/move/move-compiler-v2/tests/checking/typing/implicit_deref_borrow_field_internal.exp b/third_party/move/move-compiler-v2/tests/checking/typing/implicit_deref_borrow_field_internal.exp index 5a9034982e31b..cddb1c2240748 100644 --- a/third_party/move/move-compiler-v2/tests/checking/typing/implicit_deref_borrow_field_internal.exp +++ b/third_party/move/move-compiler-v2/tests/checking/typing/implicit_deref_borrow_field_internal.exp @@ -1,20 +1,18 @@ -// -- Model dump before bytecode pipeline -module 0x2::X { - struct S { - f: u64, - } - public fun s(): X::S { - pack X::S(0) - } -} // end 0x2::X -module 0x2::M { - use 0x2::X; // resolved as: 0x2::X - private fun t0() { - select X::S.f(X::s()); - { - let s: &X::S = Borrow(Immutable)(X::s()); - select X::S.f<&X::S>(s); - Abort(0) - } - } -} // end 0x2::M + +Diagnostics: +error: Invalid access of field `f` on external type `X::S` + ┌─ tests/checking/typing/implicit_deref_borrow_field_internal.move:12:9 + │ +12 │ fun t0() { + │ ^^ +13 │ (X::s().f: u64); + │ -------- accessed here + +error: Invalid access of field `f` on external type `X::S` + ┌─ tests/checking/typing/implicit_deref_borrow_field_internal.move:12:9 + │ +12 │ fun t0() { + │ ^^ + · +15 │ (s.f: u64); + │ --- accessed here diff --git a/third_party/move/move-compiler-v2/tests/checking/typing/mutate_field_internal.exp b/third_party/move/move-compiler-v2/tests/checking/typing/mutate_field_internal.exp index 49ad3d6aef41a..45888935bf969 100644 --- a/third_party/move/move-compiler-v2/tests/checking/typing/mutate_field_internal.exp +++ b/third_party/move/move-compiler-v2/tests/checking/typing/mutate_field_internal.exp @@ -1,20 +1,18 @@ -// -- Model dump before bytecode pipeline -module 0x2::X { - struct S { - f: u64, - } - public fun s(): X::S { - pack X::S(0) - } -} // end 0x2::X -module 0x2::M { - use 0x2::X; // resolved as: 0x2::X - private fun t0() { - select X::S.f(X::s()) = 0; - { - let s: &mut X::S = Borrow(Mutable)(X::s()); - select X::S.f<&mut X::S>(s) = 0; - Abort(0) - } - } -} // end 0x2::M + +Diagnostics: +error: Invalid access of field `f` on external type `X::S` + ┌─ tests/checking/typing/mutate_field_internal.move:12:9 + │ +12 │ fun t0() { + │ ^^ +13 │ X::s().f = 0; + │ -------- accessed here + +error: Invalid access of field `f` on external type `X::S` + ┌─ tests/checking/typing/mutate_field_internal.move:12:9 + │ +12 │ fun t0() { + │ ^^ + · +15 │ s.f = 0; + │ --- accessed here diff --git a/third_party/move/move-compiler-v2/tests/testsuite.rs b/third_party/move/move-compiler-v2/tests/testsuite.rs index f227a4f4c0eda..d2ab7c9d81765 100644 --- a/third_party/move/move-compiler-v2/tests/testsuite.rs +++ b/third_party/move/move-compiler-v2/tests/testsuite.rs @@ -333,7 +333,7 @@ const TEST_CONFIGS: Lazy> = Lazy::new(|| { include: vec!["/acquires-checker/"], exclude: vec![], exp_suffix: None, - options: opts.clone(), + options: opts.clone().set_experiment(Experiment::ACCESS_CHECK, false), // Run the full compiler pipeline to double-check the result. stop_after: StopAfter::FileFormat, dump_ast: DumpLevel::None, diff --git a/third_party/move/move-compiler-v2/tests/v1.matched b/third_party/move/move-compiler-v2/tests/v1.matched index aaf091180c6c3..ff88c6b752220 100644 --- a/third_party/move/move-compiler-v2/tests/v1.matched +++ b/third_party/move/move-compiler-v2/tests/v1.matched @@ -319,7 +319,7 @@ move-compiler/tests/move_check/typing/implicit_deref_borrow_field_internal.exp move-compiler/tests/move_check/typing/implicit_deref_borrow_field_missing.exp move-compiler-v2/tests/checking/typing/implicit_deref_borrow_field_missing.exp move-compiler/tests/move_check/typing/implicit_deref_borrow_field_non_ref_non_local_root.exp move-compiler-v2/tests/checking/typing/implicit_deref_borrow_field_non_ref_non_local_root.exp move-compiler/tests/move_check/typing/implicit_deref_borrow_field_non_ref_root.exp move-compiler-v2/tests/checking/typing/implicit_deref_borrow_field_non_ref_root.exp -move-compiler/tests/move_check/typing/invalid_type_acquire.exp move-compiler-v2/tests/acquires-checker/v1-tests/invalid_type_acquire.exp +move-compiler/tests/move_check/typing/invalid_type_acquire.exp move-compiler-v2/tests/checking-lang-v1/v1-typing/invalid_type_acquire.exp move-compiler/tests/move_check/typing/lambda.exp move-compiler-v2/tests/checking/typing/lambda.exp move-compiler/tests/move_check/typing/large_binop.exp move-compiler-v2/tests/checking/typing/large_binop.exp move-compiler/tests/move_check/typing/loop_body.exp move-compiler-v2/tests/checking/typing/loop_body.exp @@ -363,8 +363,11 @@ move-compiler/tests/move_check/typing/pack.exp move-compiler-v2/tests/checking move-compiler/tests/move_check/typing/pack_invalid_argument.exp move-compiler-v2/tests/checking/typing/pack_invalid_argument.exp move-compiler/tests/move_check/typing/pack_missing_field.exp move-compiler-v2/tests/checking/typing/pack_missing_field.exp move-compiler/tests/move_check/typing/pack_multiple.exp move-compiler-v2/tests/checking/typing/pack_multiple.exp +move-compiler/tests/move_check/typing/pack_private_with_field.exp move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.exp move-compiler/tests/move_check/typing/pack_reference.exp move-compiler-v2/tests/checking/typing/pack_reference.exp move-compiler/tests/move_check/typing/pack_unit.exp move-compiler-v2/tests/checking/typing/pack_unit.exp +move-compiler/tests/move_check/typing/pack_unpack_private.exp move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.exp +move-compiler/tests/move_check/typing/pack_unpack_private_script.exp move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.exp move-compiler/tests/move_check/typing/phantom_param_op_abilities.exp move-compiler-v2/tests/checking/abilities/v1/phantom_param_op_abilities.exp move-compiler/tests/move_check/typing/phantom_param_op_abilities_invalid.exp move-compiler-v2/tests/checking/abilities/v1/phantom_param_op_abilities_invalid.exp move-compiler/tests/move_check/typing/phantom_params_constraint_abilities.exp move-compiler-v2/tests/checking/abilities/v1/phantom_params_constraint_abilities.exp @@ -430,34 +433,3 @@ move-compiler/tests/move_check/typing/while_body.exp move-compiler-v2/tests/ch move-compiler/tests/move_check/typing/while_body_invalid.exp move-compiler-v2/tests/checking/typing/while_body_invalid.exp move-compiler/tests/move_check/typing/while_condition.exp move-compiler-v2/tests/checking/typing/while_condition.exp move-compiler/tests/move_check/typing/while_condition_invalid.exp move-compiler-v2/tests/checking/typing/while_condition_invalid.exp -move-compiler/tests/move_check/parser/aptos_stdlib_attributes.exp move-compiler-v2/tests/checking/attributes/aptos_stdlib_attributes.exp -move-compiler/tests/move_check/parser/aptos_stdlib_attributes2.exp move-compiler-v2/tests/checking/attributes/aptos_stdlib_attributes2.exp -move-compiler/tests/move_check/parser/attribute_no_closing_bracket.exp move-compiler-v2/tests/checking/attributes/attribute_no_closing_bracket.exp -move-compiler/tests/move_check/parser/attribute_num_sign_no_bracket.exp move-compiler-v2/tests/checking/attributes/attribute_num_sign_no_bracket.exp -move-compiler/tests/move_check/parser/attribute_placement.exp move-compiler-v2/tests/checking/attributes/attribute_placement.exp -move-compiler/tests/move_check/parser/attribute_variants.exp move-compiler-v2/tests/checking/attributes/attribute_variants.exp -move-compiler/tests/move_check/parser/duplicate_attributes.exp move-compiler-v2/tests/checking/attributes/duplicate_attributes.exp -move-compiler/tests/move_check/skip_attribute_checks/aptos_stdlib_attributes.exp move-compiler-v2/tests/skip_attribute_checks/aptos_stdlib_attributes.exp -move-compiler/tests/move_check/skip_attribute_checks/attribute_no_closing_bracket.exp move-compiler-v2/tests/skip_attribute_checks/attribute_no_closing_bracket.exp -move-compiler/tests/move_check/skip_attribute_checks/duplicate_attributes.exp move-compiler-v2/tests/skip_attribute_checks/duplicate_attributes.exp -move-compiler/tests/move_check/unit_testattribute_location_invalid.exp move-compiler-v2/tests/unit_test/notest/attribute_location_invalid.exp -move-compiler/tests/move_check/unit_testcross_module_members_non_test_function.exp move-compiler-v2/tests/unit_test/notest/cross_module_members_non_test_function.exp -move-compiler/tests/move_check/unit_testcross_module_test_only_module.exp move-compiler-v2/tests/unit_test/notest/cross_module_test_only_module.exp -move-compiler/tests/move_check/unit_testexpected_failure_on_non_function.exp move-compiler-v2/tests/unit_test/notest/expected_failure_on_non_function.exp -move-compiler/tests/move_check/unit_testscript_with_multiple_on_main.exp move-compiler-v2/tests/unit_test/notest/script_with_multiple_on_main.exp -move-compiler/tests/move_check/unit_testscript_with_multiple_top_level.exp move-compiler-v2/tests/unit_test/notest/script_with_multiple_top_level.exp -move-compiler/tests/move_check/unit_testscript_with_test_on_main.exp move-compiler-v2/tests/unit_test/notest/script_with_test_on_main.exp -move-compiler/tests/move_check/unit_testscript_with_test_top_level.exp move-compiler-v2/tests/unit_test/notest/script_with_test_top_level.exp -move-compiler/tests/move_check/unit_testtest_filter_function.exp move-compiler-v2/tests/unit_test/notest/test_filter_function.exp -move-compiler/tests/move_check/unit_testtest_filter_struct.exp move-compiler-v2/tests/unit_test/notest/test_filter_struct.exp -move-compiler/tests/move_check/unit_test/attribute_location_invalid.unit_test.exp move-compiler-v2/tests/unit_test/test/attribute_location_invalid.exp -move-compiler/tests/move_check/unit_test/expected_failure_on_non_function.unit_test.exp move-compiler-v2/tests/unit_test/test/expected_failure_on_non_function.exp -move-compiler/tests/move_check/unit_test/extra_attributes2.unit_test.exp move-compiler-v2/tests/unit_test/test/extra_attributes2.exp -move-compiler/tests/move_check/unit_test/multiple_errors.unit_test.exp move-compiler-v2/tests/unit_test/test/multiple_errors.exp -move-compiler/tests/move_check/unit_test/multiple_test_annotations.unit_test.exp move-compiler-v2/tests/unit_test/test/multiple_test_annotations.exp -move-compiler/tests/move_check/unit_test/named_address_no_value_in_annotation.unit_test.exp move-compiler-v2/tests/unit_test/test/named_address_no_value_in_annotation.exp -move-compiler/tests/move_check/unit_test/other_failures_invalid_location_module.unit_test.exp move-compiler-v2/tests/unit_test/test/other_failures_invalid_location_module.exp -move-compiler/tests/move_check/unit_test/script_with_multiple_on_main.unit_test.exp move-compiler-v2/tests/unit_test/test/script_with_multiple_on_main.exp -move-compiler/tests/move_check/unit_test/script_with_multiple_top_level.unit_test.exp move-compiler-v2/tests/unit_test/test/script_with_multiple_top_level.exp -move-compiler/tests/move_check/unit_test/script_with_test_on_main.unit_test.exp move-compiler-v2/tests/unit_test/test/script_with_test_on_main.exp -move-compiler/tests/move_check/unit_test/script_with_test_top_level.unit_test.exp move-compiler-v2/tests/unit_test/test/script_with_test_top_level.exp diff --git a/third_party/move/move-compiler-v2/tests/v1.unmatched b/third_party/move/move-compiler-v2/tests/v1.unmatched index 0600fe2512c01..a8870f551fdf9 100644 --- a/third_party/move/move-compiler-v2/tests/v1.unmatched +++ b/third_party/move/move-compiler-v2/tests/v1.unmatched @@ -205,9 +205,6 @@ move-compiler/tests/move_check/typing/{ mutable_eq_and_neq_invalid.move, non_phantom_in_phantom_pos.move, pack_constraint_not_satisfied.move, - pack_private_with_field.move, - pack_unpack_private.move, - pack_unpack_private_script.move, pay_me_a_river.move, phantom_param_struct_decl.move, phantom_param_struct_decl_invalid.move, @@ -225,17 +222,3 @@ move-compiler/tests/move_check/verification/{ single_module_invalid.move, single_module_valid.move, } -move-compiler/tests/move_check/unit_test/{ - cross_module_members_non_test_function.move - expected_failure_bad_value.unit_test - expected_failure_constants_invalid.unit_test - expected_failure_invalid_literals.unit_test - expected_failure_not_test.unit_test - expected_failure_out_of_range_value.unit_test - extra_attributes.unit_test - invalid_expected_code_name.unit_test - invalid_expected_failure_name.unit_test - other_failures_invalid_assignment.unit_test - other_failures_invalid_location.unit_test - test_and_test_only_annotation.unit_test -} diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.exp b/third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.exp new file mode 100644 index 0000000000000..851b31a1f3ceb --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.exp @@ -0,0 +1,73 @@ + +Diagnostics: +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:10:9 + │ +10 │ fun test(account: signer) { + │ ^^^^ +11 │ let r = M::new(); +12 │ borrow_global(@0x1); + │ ------------------------- called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:10:9 + │ +10 │ fun test(account: signer) { + │ ^^^^ + · +13 │ borrow_global_mut(@0x1); + │ ----------------------------- called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:10:9 + │ +10 │ fun test(account: signer) { + │ ^^^^ + · +14 │ exists(@0x1); + │ ------------------ called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:10:9 + │ +10 │ fun test(account: signer) { + │ ^^^^ + · +15 │ move_to(&account, r); + │ -------------------- called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:24:9 + │ +24 │ fun test(account: signer) { + │ ^^^^ +25 │ let r = M::new(); +26 │ borrow_global(@0x1); + │ ------------------------- called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:24:9 + │ +24 │ fun test(account: signer) { + │ ^^^^ + · +27 │ borrow_global_mut(@0x1); + │ ----------------------------- called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:24:9 + │ +24 │ fun test(account: signer) { + │ ^^^^ + · +28 │ exists(@0x1); + │ ------------------ called here + +error: Invalid storage operation on external type `M::R` + ┌─ tests/visibility-checker/global_operator.move:24:9 + │ +24 │ fun test(account: signer) { + │ ^^^^ + · +29 │ move_to(&account, r); + │ -------------------- called here diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.move b/third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.move new file mode 100644 index 0000000000000..6ad6a8f361e8a --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/global_operator.move @@ -0,0 +1,31 @@ +address 0x42 { +module M { + struct R has key {} + public fun new(): R { + R {} + } +} +module M2 { + use 0x42::M; + fun test(account: signer) { + let r = M::new(); + borrow_global(@0x1); + borrow_global_mut(@0x1); + exists(@0x1); + move_to(&account, r); + } +} +} + + +script { + use 0x42::M; + + fun test(account: signer) { + let r = M::new(); + borrow_global(@0x1); + borrow_global_mut(@0x1); + exists(@0x1); + move_to(&account, r); + } +} diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.exp b/third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.exp new file mode 100644 index 0000000000000..c5456806edf8a --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.exp @@ -0,0 +1,51 @@ + +Diagnostics: +error: Invalid pack operation on external type D::G + ┌─ tests/visibility-checker/pack_unpack_structs.move:13:16 + │ +13 │ public fun foo(): C::T { + │ ^^^ +14 │ C::T {g: G{}} + │ --- packed here + +error: Invalid pack operation on external type C::T + ┌─ tests/visibility-checker/pack_unpack_structs.move:13:16 + │ +13 │ public fun foo(): C::T { + │ ^^^ +14 │ C::T {g: G{}} + │ ------------- packed here + +error: Invalid unpack operation on external type C::T + ┌─ tests/visibility-checker/pack_unpack_structs.move:16:16 + │ +16 │ public fun bar(c: C::T) { + │ ^^^ +17 │ let C::T { g } = c; + │ ---------- unpacked here + +error: Invalid unpack operation on external type D::G + ┌─ tests/visibility-checker/pack_unpack_structs.move:16:16 + │ +16 │ public fun bar(c: C::T) { + │ ^^^ +17 │ let C::T { g } = c; +18 │ let G {} = g; + │ ---- unpacked here + +error: Invalid unpack operation on external type C::T + ┌─ tests/visibility-checker/pack_unpack_structs.move:20:16 + │ +20 │ public fun bar_ref(c: &C::T) { + │ ^^^^^^^ +21 │ let C::T { g } = c; + │ ---------- unpacked here + +error: Invalid unpack operation on external type D::G + ┌─ tests/visibility-checker/pack_unpack_structs.move:20:16 + │ +20 │ public fun bar_ref(c: &C::T) { + │ ^^^^^^^ +21 │ let C::T { g } = c; +22 │ let G {} = g; + │ ---- unpacked here diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.move b/third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.move new file mode 100644 index 0000000000000..90894329128cb --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/pack_unpack_structs.move @@ -0,0 +1,24 @@ +module 0x43::C { + use 0x41::D; + struct T {g: D::G} +} + +module 0x41::D { + struct G {} +} + +module 0x42::B { + use 0x43::C; + use 0x41::D::G; + public fun foo(): C::T { + C::T {g: G{}} + } + public fun bar(c: C::T) { + let C::T { g } = c; + let G {} = g; + } + public fun bar_ref(c: &C::T) { + let C::T { g } = c; + let G {} = g; + } +} diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.exp b/third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.exp new file mode 100644 index 0000000000000..e3773b13ab257 --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.exp @@ -0,0 +1,12 @@ + +Diagnostics: +error: Invalid access of field `addr` on external type `objects::ReaderRef` + ┌─ tests/visibility-checker/resources_invalid.move:17:16 + │ + 8 │ borrow_global(ref.addr) + │ -------- accessed here + · +17 │ public fun get_value(ref: &obj::ReaderRef): u64 { + │ ^^^^^^^^^ +18 │ obj::reader(ref).val + │ ---------------- from a call inlined at this callsite diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.move b/third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.move new file mode 100644 index 0000000000000..3445aa71d15d9 --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/resources_invalid.move @@ -0,0 +1,20 @@ +module 0x42::objects { + + struct ReaderRef has store { + addr: address + } + + public inline fun reader(ref: &ReaderRef): &T { + borrow_global(ref.addr) + } +} + +module 0x42::token { + use 0x42::objects as obj; + + struct Token has key { val: u64 } + + public fun get_value(ref: &obj::ReaderRef): u64 { + obj::reader(ref).val + } +} diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.exp b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.exp new file mode 100644 index 0000000000000..1d29e9dae1adb --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.exp @@ -0,0 +1,15 @@ + +Diagnostics: +error: Invalid pack operation on external type m::S + ┌─ tests/visibility-checker/v1-typing/pack_private_with_field.move:18:5 + │ +18 │ fun f() { + │ ^ +19 │ let s = S { + │ ╭───────────' +20 │ │ f1: 0, +21 │ │ f4: 0, +22 │ │ f2: 0, +23 │ │ f3: 0, +24 │ │ }; + │ ╰───' packed here diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.move b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.move new file mode 100644 index 0000000000000..fb46af0a1be8c --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_private_with_field.move @@ -0,0 +1,28 @@ +module 0x42::m { + +struct S { + f1: u64, + f2: u64, + f3: u64, + f4: u64, +} + +public fun consume(_: S) { abort 0 } + +} + +module 0x42::n { + +use 0x42::m::S; + +fun f() { + let s = S { + f1: 0, + f4: 0, + f2: 0, + f3: 0, + }; + 0x42::m::consume(s); +} + +} diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.exp b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.exp new file mode 100644 index 0000000000000..00dddf8463fda --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.exp @@ -0,0 +1,17 @@ + +Diagnostics: +error: Invalid pack operation on external type C::T + ┌─ tests/visibility-checker/v1-typing/pack_unpack_private.move:7:16 + │ +7 │ public fun foo(): C::T { + │ ^^^ +8 │ C::T {} + │ ------- packed here + +error: Invalid unpack operation on external type C::T + ┌─ tests/visibility-checker/v1-typing/pack_unpack_private.move:10:16 + │ +10 │ public fun bar(c: C::T) { + │ ^^^ +11 │ let C::T {} = c; + │ ------- unpacked here diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.move b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.move new file mode 100644 index 0000000000000..ecf581684f7d7 --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private.move @@ -0,0 +1,13 @@ +module 0x43::C { + struct T {} +} + +module 0x42::B { + use 0x43::C; + public fun foo(): C::T { + C::T {} + } + public fun bar(c: C::T) { + let C::T {} = c; + } +} diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.exp b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.exp new file mode 100644 index 0000000000000..a9e9b00c4d83b --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.exp @@ -0,0 +1,18 @@ + +Diagnostics: +error: Invalid pack operation on external type M::T + ┌─ tests/visibility-checker/v1-typing/pack_unpack_private_script.move:11:9 + │ +11 │ fun main() { + │ ^^^^ +12 │ let t = T { }; + │ ------ packed here + +error: Invalid unpack operation on external type M::T + ┌─ tests/visibility-checker/v1-typing/pack_unpack_private_script.move:11:9 + │ +11 │ fun main() { + │ ^^^^ +12 │ let t = T { }; +13 │ let T {} = t; + │ ---- unpacked here diff --git a/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.move b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.move new file mode 100644 index 0000000000000..1aa3e3bae8137 --- /dev/null +++ b/third_party/move/move-compiler-v2/tests/visibility-checker/v1-typing/pack_unpack_private_script.move @@ -0,0 +1,15 @@ +module 0x42::M { + struct T {} + +} + + +script { + + use 0x42::M::T; + + fun main() { + let t = T { }; + let T {} = t; + } +} diff --git a/third_party/move/move-compiler-v2/tools/testdiff/src/main.rs b/third_party/move/move-compiler-v2/tools/testdiff/src/main.rs index 4829b9ce2c04c..486e411f13087 100644 --- a/third_party/move/move-compiler-v2/tools/testdiff/src/main.rs +++ b/third_party/move/move-compiler-v2/tools/testdiff/src/main.rs @@ -115,6 +115,10 @@ static UNIT_PATH_REMAP: Lazy> = Lazy::new(|| { "acquires-checker/v1-tests".to_string(), "typing".to_string(), ); + map.insert( + "checking-lang-v1/v1-typing".to_string(), + "typing".to_string(), + ); map });