Skip to content

Commit

Permalink
update in response to Vineeth's review
Browse files Browse the repository at this point in the history
  • Loading branch information
brmataptos committed Aug 20, 2024
1 parent 03ba099 commit d3ea6cf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
5 changes: 1 addition & 4 deletions third_party/move/move-model/src/builder/exp_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5040,10 +5040,7 @@ impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'mo
self.error(loc, "macro invocation cannot have type arguments");
self.new_error_exp()
} else if let EA::ModuleAccess_::Name(name) = &maccess.value {
let expansion = self
.parent
.parent
.expand_macro(maccess.loc, name.value.as_str(), args);
let expansion = self.expand_macro(maccess.loc, name.value.as_str(), args);
self.translate_exp(&expansion, expected_type)
} else {
let qsym = self.parent.module_access_to_qualified(maccess);
Expand Down
31 changes: 21 additions & 10 deletions third_party/move/move-model/src/builder/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
// SPDX-License-Identifier: Apache-2.0

//! Module for expanding macros, as `assert!(cond, code)`. This are expanded to
//! the input AST before type checking.
//! the input AST before type checking. We also allow `assert!(cond)`, for Move 2,
//! which generates the "well-known" abort code `UNSPECIFIED_ABORT_CODDE`,
//! which is `(0xD8CA26CBD9BE << 16)`.

use crate::{builder::model_builder::ModelBuilder, well_known::UNSPECIFIED_ABORT_CODE};
use crate::{
builder::exp_builder::ExpTranslator, well_known::UNSPECIFIED_ABORT_CODE, LanguageVersion,
};
use move_compiler::expansion::ast as EA;
use move_ir_types::location::{sp, Loc, Spanned};

impl<'env> ModelBuilder<'env> {
impl<'env, 'translator, 'module_translator> ExpTranslator<'env, 'translator, 'module_translator> {
pub fn expand_macro(&self, loc: Loc, name: &str, args: &Spanned<Vec<EA::Exp>>) -> EA::Exp {
// Currently, there is only the assert! macro, and no user definable ones.
let expansion_ = match name {
Expand All @@ -24,13 +28,20 @@ impl<'env> ModelBuilder<'env> {

fn expand_assert(&self, loc: Loc, args: &Spanned<Vec<EA::Exp>>) -> EA::Exp_ {
let (cond, abort_code) = match args.value.len() {
1 => (
args.value[0].clone(),
sp(
loc,
EA::Exp_::Value(sp(loc, EA::Value_::U64(UNSPECIFIED_ABORT_CODE))),
),
),
1 => {
self.check_language_version(
&self.to_loc(&loc),
"single-argument `assert!` macro",
LanguageVersion::V2_0,
);
(
args.value[0].clone(),
sp(
loc,
EA::Exp_::Value(sp(loc, EA::Value_::U64(UNSPECIFIED_ABORT_CODE))),
),
)
},
2 => (args.value[0].clone(), args.value[1].clone()),
_ => {
self.error(
Expand Down
4 changes: 4 additions & 0 deletions third_party/move/move-model/src/well_known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,9 @@ pub const RECEIVER_PARAM_NAME: &str = "self";
/// TODO: add a check at runtime that user is not clashing with reserved
/// codes?
pub const WELL_KNOWN_ABORT_CODE_BASE: u64 = 0xD8CA26CBD9BE << 16;

// Used when user omits an abort code.
pub const UNSPECIFIED_ABORT_CODE: u64 = WELL_KNOWN_ABORT_CODE_BASE;

// Used when a runtime value falls through a match.
pub const INCOMPLETE_MATCH_ABORT_CODE: u64 = WELL_KNOWN_ABORT_CODE_BASE | 0x1;

0 comments on commit d3ea6cf

Please sign in to comment.