Skip to content

Commit

Permalink
[move-lang] Make locations based off source hash as opposed to file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Zakian authored and bors-libra committed Oct 28, 2021
1 parent 22ed824 commit 17576de
Show file tree
Hide file tree
Showing 58 changed files with 447 additions and 389 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions execution/executor/tests/storage_integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ fn test_change_publishing_option_to_custom() {
let compiler = Compiler {
deps: diem_framework_releases::current_modules().iter().collect(),
};
compiler
.into_script_blob("file_name", code)
.expect("Failed to compile")
compiler.into_script_blob(code).expect("Failed to compile")
};
let txn5 = get_test_signed_transaction(
genesis_account,
Expand Down
2 changes: 2 additions & 0 deletions language/compiler/bytecode-source-map/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ move-core-types = { path = "../../move-core/types" }
move-ir-types = { path = "../../move-ir/types" }
move-symbol-pool = { path = "../../move-symbol-pool" }
move-binary-format = { path = "../../move-binary-format" }
move-command-line-common = { path = "../../move-command-line-common" }

bcs = "0.1.2"
codespan-reporting = "0.11.1"
serde = { version = "1.0.124", default-features = false }
Expand Down
28 changes: 19 additions & 9 deletions language/compiler/bytecode-source-map/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use move_binary_format::{
MemberCount, ModuleHandleIndex, StructDefinition, StructDefinitionIndex, TableIndex,
},
};
use move_command_line_common::files::FileHash;
use move_core_types::{account_address::AccountAddress, identifier::Identifier};
use move_ir_types::{
ast::{ConstantName, ModuleIdent, ModuleName, NopLabel},
Expand All @@ -27,7 +28,7 @@ pub type SourceName = (String, Loc);
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct StructSourceMap {
/// The source declaration location of the struct
pub decl_location: Loc,
pub definition_location: Loc,

/// Important: type parameters need to be added in the order of their declaration
pub type_parameters: Vec<SourceName>,
Expand All @@ -42,7 +43,7 @@ pub struct FunctionSourceMap {
/// The source location for the definition of this entire function. Note that in certain
/// instances this will have no valid source location e.g. the "main" function for modules that
/// are treated as programs are synthesized and therefore have no valid source location.
pub decl_location: Loc,
pub definition_location: Loc,

/// Note that type parameters need to be added in the order of their declaration
pub type_parameters: Vec<SourceName>,
Expand All @@ -66,6 +67,9 @@ pub struct FunctionSourceMap {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct SourceMap {
/// The source location for the definition of the module or script that this source map is for.
pub definition_location: Loc,

/// The name <address.module_name> for module that this source map is for
/// None if it is a script
pub module_name_opt: Option<(AccountAddress, Identifier)>,
Expand All @@ -81,9 +85,9 @@ pub struct SourceMap {
}

impl StructSourceMap {
pub fn new(decl_location: Loc) -> Self {
pub fn new(definition_location: Loc) -> Self {
Self {
decl_location,
definition_location,
type_parameters: Vec::new(),
fields: Vec::new(),
}
Expand Down Expand Up @@ -128,9 +132,9 @@ impl StructSourceMap {
}

impl FunctionSourceMap {
pub fn new(decl_location: Loc, is_native: bool) -> Self {
pub fn new(definition_location: Loc, is_native: bool) -> Self {
Self {
decl_location,
definition_location,
type_parameters: Vec::new(),
parameters: Vec::new(),
locals: Vec::new(),
Expand Down Expand Up @@ -188,7 +192,7 @@ impl FunctionSourceMap {
// `None`.
if self.is_native {
if code_offset == 0 {
Some(self.decl_location)
Some(self.definition_location)
} else {
None
}
Expand Down Expand Up @@ -256,19 +260,25 @@ impl FunctionSourceMap {
}

impl SourceMap {
pub fn new(module_name_opt: Option<ModuleIdent>) -> Self {
pub fn new(definition_location: Loc, module_name_opt: Option<ModuleIdent>) -> Self {
let module_name_opt = module_name_opt.map(|module_name| {
let ident = Identifier::new(module_name.name.0.as_str()).unwrap();
(module_name.address, ident)
});
Self {
definition_location,
module_name_opt,
struct_map: BTreeMap::new(),
function_map: BTreeMap::new(),
constant_map: BTreeMap::new(),
}
}

pub fn check(&self, file_contents: &str) -> bool {
let file_hash = FileHash::new(file_contents);
self.definition_location.file_hash() == file_hash
}

pub fn add_top_level_function_mapping(
&mut self,
fdef_idx: FunctionDefinitionIndex,
Expand Down Expand Up @@ -486,7 +496,7 @@ impl SourceMap {
Some(ModuleIdent::new(module_name, address))
}
};
let mut empty_source_map = Self::new(module_ident);
let mut empty_source_map = Self::new(default_loc, module_ident);

for (function_idx, function_def) in view.function_defs().into_iter().flatten().enumerate() {
empty_source_map.add_top_level_function_mapping(
Expand Down
10 changes: 7 additions & 3 deletions language/compiler/ir-to-bytecode/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub fn compile_script<'a>(
script: Script,
dependencies: impl IntoIterator<Item = &'a CompiledModule>,
) -> Result<(CompiledScript, SourceMap)> {
let mut context = Context::new(HashMap::new(), None)?;
let mut context = Context::new(script.loc, HashMap::new(), None)?;
for dep in dependencies {
context.add_compiled_dependency(dep)?;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ pub fn compile_module<'a>(
dependencies: impl IntoIterator<Item = &'a CompiledModule>,
) -> Result<(CompiledModule, SourceMap)> {
let current_module = module.identifier;
let mut context = Context::new(HashMap::new(), Some(current_module))?;
let mut context = Context::new(module.loc, HashMap::new(), Some(current_module))?;
for dep in dependencies {
context.add_compiled_dependency(dep)?;
}
Expand Down Expand Up @@ -444,7 +444,11 @@ fn compile_explicit_dependency_declarations(
functions,
} = dependency;
let current_module = outer_context.module_ident(&mname)?;
let mut context = Context::new(dependencies_acc, Some(*current_module))?;
let mut context = Context::new(
outer_context.decl_location(),
dependencies_acc,
Some(*current_module),
)?;
compile_imports(&mut context, imports.clone())?;
let self_module_handle_idx = context.module_handle_index(&mname)?;
for struct_dep in structs {
Expand Down
16 changes: 12 additions & 4 deletions language/compiler/ir-to-bytecode/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ use move_core_types::{
account_address::AccountAddress,
identifier::{IdentStr, Identifier},
};
use move_ir_types::ast::{
BlockLabel, ConstantName, Field_, FunctionName, ModuleIdent, ModuleName, QualifiedStructIdent,
StructName,
use move_ir_types::{
ast::{
BlockLabel, ConstantName, Field_, FunctionName, ModuleIdent, ModuleName,
QualifiedStructIdent, StructName,
},
location::Loc,
};
use std::{clone::Clone, collections::HashMap, hash::Hash};

Expand Down Expand Up @@ -274,6 +277,7 @@ impl<'a> Context<'a> {
/// The current module is a dummy `Self` for CompiledScript.
/// It initializes an "import" of `Self` as the alias for the current_module.
pub fn new(
decl_location: Loc,
dependencies: CompiledDependencies<'a>,
current_module_opt: Option<ModuleIdent>,
) -> Result<Self> {
Expand All @@ -299,7 +303,7 @@ impl<'a> Context<'a> {
address_identifiers: HashMap::new(),
constant_pool: HashMap::new(),
current_function_index: FunctionDefinitionIndex::new(0),
source_map: SourceMap::new(current_module_opt),
source_map: SourceMap::new(decl_location, current_module_opt),
};

Ok(context)
Expand Down Expand Up @@ -866,4 +870,8 @@ impl<'a> Context<'a> {
self.ensure_function_declared(m, f.clone())?;
Ok(self.function_handles.get(&(m, f)).unwrap())
}

pub fn decl_location(&self) -> Loc {
self.source_map.definition_location
}
}
15 changes: 7 additions & 8 deletions language/compiler/ir-to-bytecode/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use codespan_reporting::{
use ir_to_bytecode_syntax::syntax::{self, ParseError};
use move_command_line_common::character_sets::is_permitted_char;
use move_ir_types::{ast, location::*};
use move_symbol_pool::Symbol;

// We restrict strings to only ascii visual characters (0x20 <= c <= 0x7E) or a permitted newline
// character--\n--or a tab--\t. Checking each character in the input string is more fool-proof
Expand All @@ -36,23 +35,23 @@ fn verify_string(string: &str) -> Result<()> {

/// Given the raw input of a file, creates a `ScriptOrModule` enum
/// Fails with `Err(_)` if the text cannot be parsed`
pub fn parse_script_or_module(file_name: Symbol, s: &str) -> Result<ast::ScriptOrModule> {
pub fn parse_script_or_module(s: &str) -> Result<ast::ScriptOrModule> {
verify_string(s)?;
syntax::parse_script_or_module_string(file_name, s).or_else(|e| handle_error(e, s))
syntax::parse_script_or_module_string(s).or_else(|e| handle_error(e, s))
}

/// Given the raw input of a file, creates a `Script` struct
/// Fails with `Err(_)` if the text cannot be parsed
pub fn parse_script(file_name: Symbol, script_str: &str) -> Result<ast::Script> {
pub fn parse_script(script_str: &str) -> Result<ast::Script> {
verify_string(script_str)?;
syntax::parse_script_string(file_name, script_str).or_else(|e| handle_error(e, script_str))
syntax::parse_script_string(script_str).or_else(|e| handle_error(e, script_str))
}

/// Given the raw input of a file, creates a single `ModuleDefinition` struct
/// Fails with `Err(_)` if the text cannot be parsed
pub fn parse_module(file_name: Symbol, modules_str: &str) -> Result<ast::ModuleDefinition> {
pub fn parse_module(modules_str: &str) -> Result<ast::ModuleDefinition> {
verify_string(modules_str)?;
syntax::parse_module_string(file_name, modules_str).or_else(|e| handle_error(e, modules_str))
syntax::parse_module_string(modules_str).or_else(|e| handle_error(e, modules_str))
}

fn handle_error<T>(e: syntax::ParseError<Loc, anyhow::Error>, code_str: &str) -> Result<T> {
Expand All @@ -61,7 +60,7 @@ fn handle_error<T>(e: syntax::ParseError<Loc, anyhow::Error>, code_str: &str) ->
ParseError::User { location, .. } => location,
};
let mut files = SimpleFiles::new();
let id = files.add(location.file(), code_str.to_string());
let id = files.add(location.file_hash(), code_str.to_string());
let lbl = match &e {
ParseError::InvalidToken { .. } => {
Label::primary(id, location.usize_range()).with_message("Invalid Token")
Expand Down
1 change: 1 addition & 0 deletions language/compiler/ir-to-bytecode/syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ move-ir-types = { path = "../../../move-ir/types" }
move-core-types = { path = "../../../move-core/types" }
move-symbol-pool = { path = "../../../move-symbol-pool" }
diem-workspace-hack = { path = "../../../../common/workspace-hack" }
move-command-line-common = { path = "../../../move-command-line-common" }

[features]
default = []
14 changes: 7 additions & 7 deletions language/compiler/ir-to-bytecode/syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use crate::syntax::ParseError;
use move_command_line_common::files::FileHash;
use move_ir_types::location::*;
use move_symbol_pool::Symbol;

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Tok {
Expand Down Expand Up @@ -131,7 +131,7 @@ impl Tok {

pub struct Lexer<'input> {
pub spec_mode: bool,
file: Symbol,
file_hash: FileHash,
text: &'input str,
prev_end: usize,
cur_start: usize,
Expand All @@ -140,10 +140,10 @@ pub struct Lexer<'input> {
}

impl<'input> Lexer<'input> {
pub fn new(file: Symbol, s: &'input str) -> Lexer<'input> {
pub fn new(file_hash: FileHash, s: &'input str) -> Lexer<'input> {
Lexer {
spec_mode: false, // read tokens without trailing punctuation during specs.
file,
file_hash,
text: s,
prev_end: 0,
cur_start: 0,
Expand All @@ -160,8 +160,8 @@ impl<'input> Lexer<'input> {
&self.text[self.cur_start..self.cur_end]
}

pub fn file_name(&self) -> Symbol {
self.file
pub fn file_hash(&self) -> FileHash {
self.file_hash
}

pub fn start_loc(&self) -> usize {
Expand Down Expand Up @@ -389,7 +389,7 @@ impl<'input> Lexer<'input> {
']' => (Tok::RSquare, 1), // for vector specs
_ => {
let idx = start_offset as u32;
let location = Loc::new(self.file_name(), idx, idx);
let location = Loc::new(self.file_hash(), idx, idx);
return Err(ParseError::InvalidToken { location });
}
};
Expand Down
Loading

0 comments on commit 17576de

Please sign in to comment.