Skip to content

Commit

Permalink
[move] New move-model-2 (currently unused) (#20854)
Browse files Browse the repository at this point in the history
## Description 

- Adds a new move-model that works over the compiler's ProgramInfo 

## Test plan 

👀 
Not yet used

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] gRPC:
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:

---------

Co-authored-by: Dario Russi <[email protected]>
  • Loading branch information
tnowacki and dariorussi authored Jan 13, 2025
1 parent 5d469ab commit bac61e3
Show file tree
Hide file tree
Showing 18 changed files with 2,131 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions external-crates/move/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions external-crates/move/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ move-ir-to-bytecode = { path = "crates/move-ir-to-bytecode" }
move-ir-to-bytecode-syntax = { path = "crates/move-ir-to-bytecode-syntax" }
move-ir-types = { path = "crates/move-ir-types" }
move-model = { path = "crates/move-model" }
move-model-2 = { path = "crates/move-model-2" }
move-package = { path = "crates/move-package" }
move-proc-macros = { path = "crates/move-proc-macros"}
move-prover = { path = "crates/move-prover" }
Expand Down
52 changes: 42 additions & 10 deletions external-crates/move/crates/move-binary-format/src/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,24 @@ impl CompiledModule {
result
}

pub fn find_function_def_by_name(
&self,
name: impl AsRef<str>,
) -> Option<(FunctionDefinitionIndex, &FunctionDefinition)> {
let name: &str = name.as_ref();
self.function_defs()
.iter()
.enumerate()
.find_map(|(idx, def)| {
let handle = self.function_handle_at(def.function);
if name == self.identifier_at(handle.name).as_str() {
Some((FunctionDefinitionIndex::new(idx as TableIndex), def))
} else {
None
}
})
}

pub fn module_handles(&self) -> &[ModuleHandle] {
&self.module_handles
}
Expand Down Expand Up @@ -2687,18 +2705,32 @@ impl CompiledModule {
self.enum_defs().iter().find(|d| d.enum_handle == idx)
}

pub fn find_struct_def_by_name(&self, name: &IdentStr) -> Option<&StructDefinition> {
self.struct_defs().iter().find(|def| {
let handle = self.datatype_handle_at(def.struct_handle);
name == self.identifier_at(handle.name)
})
pub fn find_struct_def_by_name(
&self,
name: &str,
) -> Option<(StructDefinitionIndex, &StructDefinition)> {
self.struct_defs()
.iter()
.enumerate()
.find(|(_idx, def)| {
let handle = self.datatype_handle_at(def.struct_handle);
name == self.identifier_at(handle.name).as_str()
})
.map(|(idx, def)| (StructDefinitionIndex(idx as TableIndex), def))
}

pub fn find_enum_def_by_name(&self, name: &IdentStr) -> Option<&EnumDefinition> {
self.enum_defs().iter().find(|def| {
let handle = self.datatype_handle_at(def.enum_handle);
name == self.identifier_at(handle.name)
})
pub fn find_enum_def_by_name(
&self,
name: &str,
) -> Option<(EnumDefinitionIndex, &EnumDefinition)> {
self.enum_defs()
.iter()
.enumerate()
.find(|(_idx, def)| {
let handle = self.datatype_handle_at(def.enum_handle);
name == self.identifier_at(handle.name).as_str()
})
.map(|(idx, def)| (EnumDefinitionIndex(idx as TableIndex), def))
}

// Return the `AbilitySet` of a `SignatureToken` given a context.
Expand Down
20 changes: 12 additions & 8 deletions external-crates/move/crates/move-bytecode-utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,17 @@ impl<'a, T: GetModule> SerdeLayoutBuilder<'a, T> {
.map_err(|e| anyhow::format_err!("{:?}", e))?
.expect("Failed to resolve module");
let def = match (
declaring_module.borrow().find_struct_def_by_name(name),
declaring_module.borrow().find_enum_def_by_name(name),
declaring_module
.borrow()
.find_struct_def_by_name(name.as_str()),
declaring_module
.borrow()
.find_enum_def_by_name(name.as_str()),
) {
(Some(struct_def), None) => {
(Some((_, struct_def)), None) => {
Container::Struct(Struct::new(declaring_module.borrow(), struct_def).1)
}
(None, Some(enum_def)) => {
(None, Some((_, enum_def))) => {
Container::Enum(Enum::new(declaring_module.borrow(), enum_def).1)
}
(Some(_), Some(_)) => bail!("Found both struct and enum with name {}", name),
Expand Down Expand Up @@ -608,10 +612,10 @@ impl DatatypeLayoutBuilder {
Ok(Some(m)) => m,
};
match (
module.borrow().find_struct_def_by_name(name),
module.borrow().find_enum_def_by_name(name),
module.borrow().find_struct_def_by_name(name.as_str()),
module.borrow().find_enum_def_by_name(name.as_str()),
) {
(Some(struct_def), None) => Ok(A::MoveDatatypeLayout::Struct(Box::new(
(Some((_, struct_def)), None) => Ok(A::MoveDatatypeLayout::Struct(Box::new(
Self::build_from_struct_definition(
module.borrow(),
struct_def,
Expand All @@ -620,7 +624,7 @@ impl DatatypeLayoutBuilder {
depth,
)?,
))),
(None, Some(enum_def)) => Ok(A::MoveDatatypeLayout::Enum(Box::new(
(None, Some((_, enum_def))) => Ok(A::MoveDatatypeLayout::Enum(Box::new(
Self::build_from_enum_definition(
module.borrow(),
enum_def,
Expand Down
20 changes: 20 additions & 0 deletions external-crates/move/crates/move-compiler/src/cfgir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub fn program(
let mut context = Context::new(compilation_env, &info);

let modules = modules(&mut context, hmodules);
set_constant_value_types(&info, &modules);

let mut program = G::Program {
modules,
Expand All @@ -177,6 +178,25 @@ pub fn program(
program
}

fn set_constant_value_types(
info: &TypingProgramInfo,
modules: &UniqueMap<ModuleIdent, G::ModuleDefinition>,
) {
for (mname, mdef) in modules.key_cloned_iter() {
for (cname, cdef) in mdef.constants.key_cloned_iter() {
if let Some(value) = &cdef.value {
info.module(&mname)
.constants
.get(&cname)
.unwrap()
.value
.set(value.clone())
.unwrap();
}
}
}
}

fn modules(
context: &mut Context,
hmodules: UniqueMap<ModuleIdent, H::ModuleDefinition>,
Expand Down
4 changes: 2 additions & 2 deletions external-crates/move/crates/move-compiler/src/shared/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ impl MappedFiles {
&self.file_name_mapping
}

pub fn filename(&self, fhash: &FileHash) -> &str {
pub fn filename(&self, fhash: &FileHash) -> Symbol {
let file_id = self.file_mapping().get(fhash).unwrap();
self.files().get(*file_id).unwrap().name()
*self.files().get(*file_id).unwrap().name()
}

pub fn file_path(&self, fhash: &FileHash) -> &PathBuf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ use crate::{
typing::ast::{self as T},
FullyCompiledProgram,
};
use move_core_types::runtime_value;
use move_ir_types::location::Loc;
use move_symbol_pool::Symbol;

#[derive(Debug, Clone)]
pub struct FunctionInfo {
pub attributes: Attributes,
pub defined_loc: Loc,
pub full_loc: Loc,
pub visibility: Visibility,
pub entry: Option<Loc>,
pub macro_: Option<Loc>,
Expand All @@ -35,10 +37,13 @@ pub struct ConstantInfo {
pub attributes: Attributes,
pub defined_loc: Loc,
pub signature: Type,
// Set after compilation
pub value: OnceLock<runtime_value::MoveValue>,
}

#[derive(Debug, Clone)]
pub struct ModuleInfo {
pub defined_loc: Loc,
pub target_kind: TargetKind,
pub attributes: Attributes,
pub package: Option<Symbol>,
Expand Down Expand Up @@ -82,6 +87,7 @@ macro_rules! program_info {
let functions = mdef.functions.ref_map(|fname, fdef| FunctionInfo {
attributes: fdef.attributes.clone(),
defined_loc: fname.loc(),
full_loc: fdef.loc,
visibility: fdef.visibility.clone(),
entry: fdef.entry,
macro_: fdef.macro_,
Expand All @@ -91,12 +97,14 @@ macro_rules! program_info {
attributes: cdef.attributes.clone(),
defined_loc: cname.loc(),
signature: cdef.signature.clone(),
value: OnceLock::new(),
});
let use_funs = $module_use_funs
.as_mut()
.map(|module_use_funs| module_use_funs.remove(&mident).unwrap())
.unwrap_or_default();
let minfo = ModuleInfo {
defined_loc: mdef.loc,
target_kind: mdef.target_kind,
attributes: mdef.attributes.clone(),
package: mdef.package_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,7 @@ pub fn make_constant_type(
attributes: _,
defined_loc,
signature,
value: _,
} = context.constant_info(m, c);
(*defined_loc, signature.clone())
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3980,6 +3980,7 @@ fn annotated_error_const(context: &mut Context, e: &mut T::Exp, abort_or_assert_
attributes,
defined_loc,
signature: _,
value: _,
} = context.constant_info(module_ident, constant_name);
const_name = Some((*defined_loc, *constant_name));
let has_error_annotation =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
warning[W01004]: invalid documentation comment
┌─ tests/move_check/parser/doc_comments_placement_invalid.move:4:9
4 │ /// doc not attached
│ ^^^^^^^^^^^^^^^^^^^^ Documentation comment cannot be matched to a language item

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module a::m {

public fun foo() {
/// doc not attached
}

public fun bar() {}
}
Loading

0 comments on commit bac61e3

Please sign in to comment.