Skip to content

Commit

Permalink
[move-compiler][sui-mode] Add init and one-time witness rules (#13335)
Browse files Browse the repository at this point in the history
## Description 

- Added init and one-time witness rules to the compiler's Sui mode

## Test Plan 

- Migrated tests

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [X] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

In this release, the Move compiler will now error on incorrect
declarations (or usage) of `init` functions and one-time witnesses.
  • Loading branch information
tnowacki authored and damirka committed Aug 22, 2023
1 parent 8aaa626 commit bd051dd
Show file tree
Hide file tree
Showing 61 changed files with 1,582 additions and 426 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#[test_only]
/// Tests if normally illegal (in terms of Sui bytecode verification) code is allowed in tests.
module sui::verifier_tests {
struct VERIFIER_TESTS has drop {}

#[allow(unused_function)]
fun init(otw: VERIFIER_TESTS, _: &mut sui::tx_context::TxContext) {
assert!(sui::types::is_one_time_witness(&otw), 0);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-move-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl BuildConfig {
SelfTransferVerifier.visitor(),
CustomStateChangeVerifier.visitor(),
CoinFieldVisitor.visitor(),
FreezeWrappedVisitor::default().visitor(),
FreezeWrappedVisitor.visitor(),
CollectionEqualityVisitor.visitor(),
];
let (filter_attr_name, filters) = known_filters();
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-move-build/src/linters/coin_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use move_compiler::{
expansion::ast as E,
naming::ast as N,
shared::{CompilationEnv, Identifier},
typing::{ast as T, core::ProgramInfo, visitor::TypingVisitor},
typing::{ast as T, core::TypingProgramInfo, visitor::TypingVisitor},
};
use move_core_types::account_address::AccountAddress;
use move_ir_types::location::Loc;
Expand All @@ -36,7 +36,7 @@ impl TypingVisitor for CoinFieldVisitor {
fn visit(
&mut self,
env: &mut CompilationEnv,
_program_info: &ProgramInfo,
_program_info: &TypingProgramInfo,
program: &mut T::Program,
) {
for (_, _, mdef) in program.modules.iter() {
Expand Down
39 changes: 30 additions & 9 deletions crates/sui-move-build/src/linters/collection_equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use move_compiler::{
naming::ast as N,
parser::ast as P,
shared::{CompilationEnv, Identifier},
typing::{ast as T, core::ProgramInfo, visitor::TypingVisitor},
typing::{
ast as T,
core::TypingProgramInfo,
visitor::{TypingVisitorConstructor, TypingVisitorContext},
},
};

use super::{
Expand Down Expand Up @@ -50,15 +54,24 @@ const COLLECTION_TYPES: &[(&str, &str, &str)] = &[
];

pub struct CollectionEqualityVisitor;
pub struct Context<'a> {
env: &'a mut CompilationEnv,
}

impl TypingVisitorConstructor for CollectionEqualityVisitor {
type Context<'a> = Context<'a>;

impl TypingVisitor for CollectionEqualityVisitor {
fn visit_exp_custom(
&mut self,
exp: &T::Exp,
env: &mut CompilationEnv,
_program_info: &ProgramInfo,
fn context<'a>(
env: &'a mut CompilationEnv,
_program_info: &'a TypingProgramInfo,
_program: &T::Program,
) -> bool {
) -> Self::Context<'a> {
Context { env }
}
}

impl TypingVisitorContext for Context<'_> {
fn visit_exp_custom(&mut self, exp: &mut T::Exp) -> bool {
use T::UnannotatedExp_ as E;
if let E::BinopExp(_, op, t, _) = &exp.exp.value {
if op.value != P::BinOp_::Eq && op.value != P::BinOp_::Neq {
Expand Down Expand Up @@ -87,10 +100,18 @@ impl TypingVisitor for CollectionEqualityVisitor {
format!("Equality for collections of type '{caddr}::{cmodule}::{cname}' IS NOT a structural check based on content");
let mut d = diag!(COLLECTIONS_EQUALITY_DIAG, (op.loc, msg),);
d.add_note(note_msg);
env.add_diag(d);
self.env.add_diag(d);
return true;
}
}
false
}

fn add_warning_filter_scope(&mut self, filter: move_compiler::diagnostics::WarningFilters) {
self.env.add_warning_filter_scope(filter)
}

fn pop_warning_filter_scope(&mut self) {
self.env.pop_warning_filter_scope()
}
}
Loading

0 comments on commit bd051dd

Please sign in to comment.