Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[move-compiler] public init for unit testing in debug build #20481

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions external-crates/move/crates/move-compiler/src/typing/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,25 +1727,25 @@ fn check_function_visibility(
pub enum PublicForTesting {
/// The function is entry, so it can be called in unit tests
Entry(Loc),
// TODO we should allow calling init in unit tests, but this would need Sui bytecode verifier
// support. Or we would need to name dodge init in unit tests
// SuiInit(Loc),
/// We allow calling init in unit tests in Sui mode
SuiInit,
}

pub fn public_testing_visibility(
env: &CompilationEnv,
_package: Option<Symbol>,
_callee_name: &FunctionName,
package: Option<Symbol>,
callee_name: &FunctionName,
callee_entry: Option<Loc>,
) -> Option<PublicForTesting> {
// is_testing && (is_entry || is_sui_init)
if !env.flags().is_testing() {
return None;
}

// TODO support sui init functions
// let flavor = env.package_config(package).flavor;
// flavor == Flavor::Sui && callee_name.value() == INIT_FUNCTION_NAME
let is_sui_mode = env.package_config(package).flavor == crate::editions::Flavor::Sui;
if is_sui_mode && callee_name.value() == crate::sui_mode::INIT_FUNCTION_NAME {
return Some(PublicForTesting::SuiInit);
}
callee_entry.map(PublicForTesting::Entry)
}

Expand Down Expand Up @@ -1781,6 +1781,16 @@ fn report_visibility_error_(
);
(entry_loc, entry_msg)
}
PublicForTesting::SuiInit => {
let entry_msg = format!(
"'{}' functions can be called in tests, \
but only from testing contexts, e.g. '#[{}]' or '#[{}]'",
crate::sui_mode::INIT_FUNCTION_NAME,
TestingAttribute::TEST,
TestingAttribute::TEST_ONLY,
);
(Loc::invalid(), entry_msg)
}
};
diag.add_secondary_label((test_loc, test_msg))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ fn function(context: &mut Context, name: FunctionName, f: N::Function) -> T::Fun
let compiled_visibility =
match public_testing_visibility(context.env, context.current_package, &name, entry) {
Some(PublicForTesting::Entry(loc)) => Visibility::Public(loc),
Some(PublicForTesting::SuiInit) => Visibility::Public(Loc::invalid()),
None => visibility,
};
function_signature(context, macro_, &signature);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// check that fun `init` can be used cross module in sui-mode
module 0x1::M {
fun init(_ctx: &mut sui::tx_context::TxContext) { }
}

module 0x1::Tests {
#[test]
fun tester() {
use 0x1::M;
let ctx = sui::tx_context::TxContext {};
M::init(&ctx);
}
}

module sui::tx_context {
struct TxContext has drop {}
}
Loading