[Feature Request] Introduce verifier annotation, such as private_generics #1043
Description
🚀 Feature Request
Motivation
In Move, the verifier ensures that the generic parameters of the global storage instructions must be defined in the caller's module.
Such as
move_to<T>|move_from<T>|borrow_global<T>|borrow_global_mut<T>
The type T
must be defined in the caller's current module.
The move developer also needs this feature to restrict the function's generic type. Still, this feature is hardcode in the verifier, and the developer can not use this feature for their function.
So, if we introduce some verifier annotation, we can provide this feature to the developer.
Pitch
Describe the solution you'd like
- Introduce a verifier annotation, such as
#[private_generics(T)]
, indicating the function's generic parameterT
is private, which requires the same restriction toT
as global storage instructions. - Implement a verifier extension for the annotation, like sui-verifier's private_generics.
UseCase
- TypeTable
#140 The TypeTable also requires this feature to ensure Type Safety.
- Provide
bcs::from_bytes
module std::bcs{
#[private_generics(T)]
public fun from_bytes<T>(bytes: vector<u8>): T
}
As described in the issue(rooch-network/rooch#145) mentioned, relying on private_generics
alone cannot guarantee type safety. We need two other annotations,#[self_struct]
and #[require_self_struct(T)]
. The name for the annotations is yet to be determined.
- #[self_struct]: A struct with this annotation indicates that all its fields are either of primitive type or defined in the current module.
- #[require_self_struct(T)]: A function with annotation indicates that the generic type
T
must have#[self_struct]
annotation.
module std::bcs{
#[private_generics(T), require_self_struct(T)]
public fun from_bytes<T>(bytes: vector<u8>): T
}
module example::my_module{
#[self_struct]
struct MyStruct {
f_number: u64,
f_struct: StructB,
}
struct StructB {
f: u64,
}
public fun decode(bytes: vector<u8>): MyStruct{
std::bcs::from_bytes<MyStruct>(bytes)
}
}
Are you willing to open a pull request? (See CONTRIBUTING)
Yes