Skip to content

Commit

Permalink
[move-package] Use source files for dependencies
Browse files Browse the repository at this point in the history
- Source files are now used for dependencies
- This will allow for source language only features to be used across packages
- Relaxed the constraint that named addresses are not interchangeable with their value

Closes: #145
  • Loading branch information
Todd Nowacki authored and bors-diem committed Mar 28, 2022
1 parent 1562fbd commit ac094f2
Show file tree
Hide file tree
Showing 54 changed files with 421 additions and 548 deletions.
3 changes: 1 addition & 2 deletions language/benchmarks/src/move_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use criterion::{measurement::Measurement, Criterion};
use move_binary_format::CompiledModule;
use move_compiler::{compiled_unit::AnnotatedCompiledUnit, Compiler, Flags};
use move_compiler::{compiled_unit::AnnotatedCompiledUnit, Compiler};
use move_core_types::{
account_address::AccountAddress,
identifier::{IdentStr, Identifier},
Expand Down Expand Up @@ -39,7 +39,6 @@ fn compile_modules() -> Vec<CompiledModule> {
vec![(src_files, move_stdlib::move_stdlib_named_addresses())],
vec![],
)
.set_flags(Flags::empty().set_sources_shadow_deps(false))
.build_and_report()
.expect("Error compiling...");
compiled_units
Expand Down
6 changes: 3 additions & 3 deletions language/evm/move-to-yul/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ pub fn run_to_yul_errors_to_stderr(options: Options) -> anyhow::Result<()> {
/// Run move-to-yul compiler and print errors to given writer.
pub fn run_to_yul<W: WriteColor>(error_writer: &mut W, options: Options) -> anyhow::Result<()> {
// Run the model builder.
let addrs = parse_addresses_from_options(options.named_address_mapping.clone())?;
let env = run_model_builder_with_options(
&options.sources,
&options.dependencies,
vec![(options.sources.clone(), addrs.clone())],
vec![(options.dependencies.clone(), addrs)],
ModelBuilderOptions::default(),
parse_addresses_from_options(options.named_address_mapping.clone())?,
)?;
// If the model contains any errors, report them now and exit.
check_errors(
Expand Down
8 changes: 5 additions & 3 deletions language/evm/move-to-yul/tests/dispatcher_testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,12 @@ fn compile_yul_to_bytecode_bytes(filename: &str) -> Result<Vec<u8>> {
NumericalAddress::parse_str("0x2").unwrap(),
);
let env = run_model_builder_with_options(
&[contract_path(filename).to_string_lossy().to_string()],
&deps,
vec![(
vec![contract_path(filename).to_string_lossy().to_string()],
named_address_mapping.clone(),
)],
vec![(deps, named_address_mapping)],
ModelBuilderOptions::default(),
named_address_mapping,
)?;
let options = Options::default();
let (_, out) = Generator::run(&options, &env);
Expand Down
10 changes: 5 additions & 5 deletions language/evm/move-to-yul/tests/testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use move_compiler::shared::NumericalAddress;
use move_model::{
model::{FunId, GlobalEnv, QualifiedId},
options::ModelBuilderOptions,
run_model_builder_with_options,
run_model_builder_with_options_and_compilation_flags,
};
use move_prover_test_utils::{baseline_test::verify_or_update_baseline, extract_test_directives};
use move_stdlib::move_stdlib_named_addresses;
Expand Down Expand Up @@ -39,11 +39,11 @@ fn test_runner(path: &Path) -> datatest_stable::Result<()> {
"Evm".to_string(),
NumericalAddress::parse_str("0x2").unwrap(),
);
let env = run_model_builder_with_options(
&sources,
&deps,
let env = run_model_builder_with_options_and_compilation_flags(
vec![(sources, named_address_mapping.clone())],
vec![(deps, named_address_mapping)],
ModelBuilderOptions::default(),
named_address_mapping,
move_compiler::Flags::empty().set_sources_shadow_deps(true),
)?;
for exp in std::iter::once(String::new()).chain(experiments.into_iter()) {
let mut options = Options {
Expand Down
47 changes: 25 additions & 22 deletions language/move-compiler/src/command_line/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,29 @@ pub struct FullyCompiledProgram {
//**************************************************************************************************

impl<'a> Compiler<'a> {
pub fn new(targets: Vec<AddressScopedFiles>, deps: Vec<AddressScopedFiles>) -> Self {
fn rc_scopes(
pub fn new<Paths: Into<Symbol>, NamedAddress: Into<Symbol>>(
targets: Vec<AddressScopedFiles<Paths, NamedAddress>>,
deps: Vec<AddressScopedFiles<Paths, NamedAddress>>,
) -> Self {
fn indexed_scopes(
maps: &mut NamedAddressMaps,
all_paths: Vec<AddressScopedFiles>,
all_paths: Vec<AddressScopedFiles<impl Into<Symbol>, impl Into<Symbol>>>,
) -> Vec<AddressScopedFileIndexed> {
let mut idx_paths = vec![];
for (paths, mapping) in all_paths {
let idx = maps.insert(
mapping
.into_iter()
.map(|(k, v)| (Symbol::from(k), v))
.map(|(k, v)| (k.into(), v))
.collect::<NamedAddressMap>(),
);
idx_paths.extend(paths.into_iter().map(|p| (p, idx)));
idx_paths.extend(paths.into_iter().map(|p| (p.into(), idx)));
}
idx_paths
}
let mut maps = NamedAddressMaps::new();
let targets = rc_scopes(&mut maps, targets);
let deps = rc_scopes(&mut maps, deps);
let targets = indexed_scopes(&mut maps, targets);
let deps = indexed_scopes(&mut maps, deps);

Self {
maps,
Expand Down Expand Up @@ -383,15 +386,16 @@ impl<'a> SteppedCompiler<'a, PASS_COMPILATION> {

/// Given a set of dependencies, precompile them and save the ASTs so that they can be used again
/// to compile against without having to recompile these dependencies
pub fn construct_pre_compiled_lib(
deps: Vec<AddressScopedFiles>,
pub fn construct_pre_compiled_lib<Paths: Into<Symbol>, NamedAddress: Into<Symbol>>(
deps: Vec<AddressScopedFiles<Paths, NamedAddress>>,
interface_files_dir_opt: Option<String>,
flags: Flags,
) -> anyhow::Result<Result<FullyCompiledProgram, (FilesSourceText, Diagnostics)>> {
let (files, pprog_and_comments_res) = Compiler::new(vec![], deps)
.set_interface_files_dir_opt(interface_files_dir_opt)
.set_flags(flags)
.run::<PASS_PARSER>()?;
let (files, pprog_and_comments_res) =
Compiler::new(Vec::<AddressScopedFiles<Paths, NamedAddress>>::new(), deps)
.set_interface_files_dir_opt(interface_files_dir_opt)
.set_flags(flags)
.run::<PASS_PARSER>()?;

let (_comments, stepped) = match pprog_and_comments_res {
Err(errors) => return Ok(Err((files, errors))),
Expand Down Expand Up @@ -585,18 +589,18 @@ pub fn generate_interface_files(
) -> anyhow::Result<Vec<AddressScopedFileIndexed>> {
let mv_files = {
let mut v = vec![];
let (mv_magic_files, other_file_locations): (Vec<_>, Vec<_>) = mv_file_locations
.iter()
.cloned()
.partition(|s| Path::new(&s.0).is_file() && has_compiled_module_magic_number(&s.0));
let (mv_magic_files, other_file_locations): (Vec<_>, Vec<_>) =
mv_file_locations.iter().cloned().partition(|s| {
Path::new(s.0.as_str()).is_file() && has_compiled_module_magic_number(&s.0)
});
v.extend(mv_magic_files);
for (file, address_mapping) in other_file_locations {
v.extend(
find_filenames(&[file], |path| {
find_filenames(&[file.as_str()], |path| {
extension_equals(path, MOVE_COMPILED_EXTENSION)
})?
.into_iter()
.map(|f| (f, address_mapping)),
.map(|f| (Symbol::from(f), address_mapping)),
);
}
v
Expand All @@ -619,7 +623,7 @@ pub fn generate_interface_files(
mv_files.len().hash(&mut hasher);
HASH_DELIM.hash(&mut hasher);
for (mv_file, _) in &mv_files {
std::fs::read(mv_file)?.hash(&mut hasher);
std::fs::read(mv_file.as_str())?.hash(&mut hasher);
HASH_DELIM.hash(&mut hasher);
}

Expand All @@ -637,7 +641,7 @@ pub fn generate_interface_files(
let addr_dir = dir_path!(all_addr_dir.clone(), format!("{}", id.address()));
let file_path = file_path!(addr_dir.clone(), format!("{}", id.name()), MOVE_EXTENSION);
result.push((
file_path.clone().into_os_string().into_string().unwrap(),
Symbol::from(file_path.clone().into_os_string().into_string().unwrap()),
idx,
));
// it's possible some files exist but not others due to multithreaded environments
Expand Down Expand Up @@ -712,7 +716,6 @@ fn run(

match cur {
PassResult::Parser(prog) => {
let prog = parser::sources_shadow_deps::program(compilation_env, prog);
let prog = parser::merge_spec_modules::program(compilation_env, prog);
let prog = unit_test::filter_test_members::program(compilation_env, prog);
let eprog = expansion::translate::program(compilation_env, pre_compiled_lib, prog);
Expand Down
4 changes: 2 additions & 2 deletions language/move-compiler/src/command_line/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub const OUT_DIR: &str = "out-dir";
pub const OUT_DIR_SHORT: char = 'o';
pub const DEFAULT_OUTPUT_DIR: &str = "build";

pub const NO_SHADOW: &str = "no-shadow";
pub const NO_SHADOW_SHORT: char = 'S';
pub const SHADOW: &str = "shadow";
pub const SHADOW_SHORT: char = 'S';

pub const SOURCE_MAP: &str = "source-map";
pub const SOURCE_MAP_SHORT: char = 'm';
Expand Down
6 changes: 2 additions & 4 deletions language/move-compiler/src/compiled_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ pub type AnnotatedCompiledUnit = CompiledUnitEnum<AnnotatedCompiledModule, Annot
impl AnnotatedCompiledModule {
pub fn module_ident(&self) -> ModuleIdent {
use crate::expansion::ast::Address;
let address = match self.address_name {
None => Address::Anonymous(sp(self.loc, self.named_module.address)),
Some(n) => Address::Named(n, Some(self.named_module.address)),
};
let address =
Address::Numerical(self.address_name, sp(self.loc, self.named_module.address));
sp(
self.loc,
ModuleIdent_::new(
Expand Down
36 changes: 16 additions & 20 deletions language/move-compiler/src/expansion/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ pub struct Script {

#[derive(Clone, Copy)]
pub enum Address {
Anonymous(Spanned<NumericalAddress>),
Named(Name, Option<NumericalAddress>),
Numerical(Option<Name>, Spanned<NumericalAddress>),
NamedUnassigned(Name),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ModuleIdent_ {
Expand Down Expand Up @@ -494,8 +494,8 @@ impl fmt::Debug for Address {
impl PartialEq for Address {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Anonymous(l), Self::Anonymous(r)) => l == r,
(Self::Named(l, _), Self::Named(r, _)) => l == r,
(Self::Numerical(_, l), Self::Numerical(_, r)) => l == r,
(Self::NamedUnassigned(l), Self::NamedUnassigned(r)) => l == r,
_ => false,
}
}
Expand All @@ -514,24 +514,20 @@ impl Ord for Address {
use std::cmp::Ordering;

match (self, other) {
(Self::Anonymous(_), Self::Named(_, _)) => Ordering::Less,
(Self::Named(_, _), Self::Anonymous(_)) => Ordering::Greater,
(Self::Numerical(_, _), Self::NamedUnassigned(_)) => Ordering::Less,
(Self::NamedUnassigned(_), Self::Numerical(_, _)) => Ordering::Greater,

(Self::Anonymous(l), Self::Anonymous(r)) => l.cmp(r),
(Self::Named(l, _), Self::Named(r, _)) => l.cmp(r),
(Self::Numerical(_, l), Self::Numerical(_, r)) => l.cmp(r),
(Self::NamedUnassigned(l), Self::NamedUnassigned(r)) => l.cmp(r),
}
}
}

impl Hash for Address {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
match self {
Self::Anonymous(sp!(_, bytes)) => bytes.hash(state),
Self::Named(n, None) => n.hash(state),
Self::Named(n, Some(bytes)) => {
n.hash(state);
bytes.hash(state)
}
Self::Numerical(_, sp!(_, bytes)) => bytes.hash(state),
Self::NamedUnassigned(name) => name.hash(state),
}
}
}
Expand All @@ -542,14 +538,13 @@ impl Hash for Address {

impl Address {
pub const fn anonymous(loc: Loc, address: NumericalAddress) -> Self {
Self::Anonymous(sp(loc, address))
Self::Numerical(None, sp(loc, address))
}

pub fn into_addr_bytes(self) -> NumericalAddress {
match self {
Self::Anonymous(sp!(_, bytes)) => bytes,
Self::Named(_, Some(bytes)) => bytes,
Self::Named(_, None) => NumericalAddress::DEFAULT_ERROR_ADDRESS,
Self::Numerical(_, sp!(_, bytes)) => bytes,
Self::NamedUnassigned(_) => NumericalAddress::DEFAULT_ERROR_ADDRESS,
}
}
}
Expand Down Expand Up @@ -720,8 +715,9 @@ impl<'a> IntoIterator for AbilitySet {
impl fmt::Display for Address {
fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
match self {
Self::Anonymous(sp!(_, bytes)) => write!(f, "{}", bytes),
Self::Named(n, _) => write!(f, "{}", n),
Self::Numerical(None, sp!(_, bytes)) => write!(f, "{}", bytes),
Self::Numerical(Some(name), sp!(_, bytes)) => write!(f, "({}={})", name, bytes),
Self::NamedUnassigned(name) => write!(f, "{}", name),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion language/move-compiler/src/expansion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ mod byte_string;
mod dependency_ordering;
mod hex_string;
pub(crate) mod translate;
mod unique_modules_after_mapping;
Loading

0 comments on commit ac094f2

Please sign in to comment.