Skip to content

Commit

Permalink
[compiler] Hack to move-compiler to avoid warning about unknown attri…
Browse files Browse the repository at this point in the history
…butes in aptos_std=0x1.

This avoids surprising warning on currently deployed library code.
  • Loading branch information
brmataptos committed Aug 5, 2023
1 parent c123a0f commit 0dac829
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 8 deletions.
43 changes: 39 additions & 4 deletions third_party/move/move-compiler/src/expansion/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ use crate::{
},
shared::{
known_attributes::{AttributeKind, AttributePosition, KnownAttribute},
parse_u128, parse_u64, parse_u8,
unique_map::UniqueMap,
*,
CompilationEnv, Identifier, Name, NamedAddressMap, NamedAddressMaps, NumericalAddress,
},
FullyCompiledProgram,
};
use move_command_line_common::parser::{parse_u16, parse_u256, parse_u32};
use move_ir_types::location::*;
use move_symbol_pool::Symbol;
use once_cell::sync::Lazy;
use std::{
collections::{BTreeMap, BTreeSet, VecDeque},
iter::IntoIterator,
};
use str;

//**************************************************************************************************
// Context
Expand All @@ -44,6 +47,7 @@ struct Context<'env, 'map> {
in_spec_context: bool,
exp_specs: BTreeMap<SpecId, E::SpecBlock>,
env: &'env mut CompilationEnv,
in_aptos_stdlib: bool, // TODO(https://github.com/aptos-labs/aptos-core/issues/9410) remove after bugfix propagates.
}
impl<'env, 'map> Context<'env, 'map> {
fn new(
Expand All @@ -58,6 +62,7 @@ impl<'env, 'map> Context<'env, 'map> {
aliases: AliasMap::new(),
is_source_definition: false,
in_spec_context: false,
in_aptos_stdlib: false,
exp_specs: BTreeMap::new(),
}
}
Expand Down Expand Up @@ -389,6 +394,32 @@ fn set_sender_address(
})
}

// This is a hack to recognize APTOS StdLib to avoid warnings on some old errors.
// This will be removed after library attributes are cleaned up.
// (See https://github.com/aptos-labs/aptos-core/issues/9410)
fn module_is_in_aptos_stdlib(module_address: Option<Spanned<Address>>) -> bool {
const APTOS_STDLIB_NAME: &str = "aptos_std";
static APTOS_STDLIB_NUMERICAL_ADDRESS: Lazy<NumericalAddress> =
Lazy::new(|| NumericalAddress::parse_str("0x1").unwrap());
match &module_address {
Some(spanned_address) => {
let address = spanned_address.value;
match address {
Address::Numerical(optional_name, spanned_numerical_address) => match optional_name
{
Some(spanned_symbol) => {
(&spanned_symbol.value as &str) == APTOS_STDLIB_NAME
&& (spanned_numerical_address.value == *APTOS_STDLIB_NUMERICAL_ADDRESS)
},
None => false,
},
Address::NamedUnassigned(_) => false,
}
},
None => false,
}
}

fn module_(
context: &mut Context,
package_name: Option<Symbol>,
Expand All @@ -403,7 +434,9 @@ fn module_(
name,
members,
} = mdef;
context.in_aptos_stdlib = module_is_in_aptos_stdlib(module_address);
let attributes = flatten_attributes(context, AttributePosition::Module, attributes);

assert!(context.address.is_none());
assert!(address.is_none());
set_sender_address(context, &name, module_address);
Expand Down Expand Up @@ -584,10 +617,12 @@ fn unique_attributes(
let flags = &context.env.flags();
if !flags.skip_attribute_checks() {
let known_attributes = &context.env.get_known_attributes();
// TODO(See https://github.com/aptos-labs/aptos-core/issues/9410) remove after bugfix propagates.
if !is_nested && !known_attributes.contains(sym.as_str()) {
let msg = format!("Attribute name '{}' is unknown (use --{} CLI option to ignore); known attributes are '{:?}'.",
sym.as_str(),
SKIP_ATTRIBUTE_CHECKS, known_attributes);
if !context.in_aptos_stdlib {
let msg = format!("Attribute name '{}' is unknown (use --{} CLI option to ignore); known attributes are '{:?}'.",
sym.as_str(),
SKIP_ATTRIBUTE_CHECKS, known_attributes);
context
.env
.add_diag(diag!(Declarations::UnknownAttribute, (nloc, msg)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E02001]: duplicate declaration, item, or annotation
┌─ tests/move_check/parser/aptos_stdlib_attributes.move:2:10
2 │ #[a, a(x = 0)]
│ - ^^^^^^^^ Duplicate attribute 'a' attached to the same item
│ │
│ Attribute previously given here

error[E02001]: duplicate declaration, item, or annotation
┌─ tests/move_check/parser/aptos_stdlib_attributes.move:6:12
6 │ #[b(a, a = 0, a(x = 1))]
│ - ^^^^^ Duplicate attribute 'a' attached to the same item
│ │
│ Attribute previously given here

error[E02001]: duplicate declaration, item, or annotation
┌─ tests/move_check/parser/aptos_stdlib_attributes.move:6:19
6 │ #[b(a, a = 0, a(x = 1))]
│ - ^^^^^^^^ Duplicate attribute 'a' attached to the same item
│ │
│ Attribute previously given here

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module aptos_std::M {
#[a, a(x = 0)]
fun foo() {}

#[testonly]
#[b(a, a = 0, a(x = 1))]
fun bar() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module aptos_std::M {
fun foo() {}

#[testonly]
fun bar() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E02001]: duplicate declaration, item, or annotation
┌─ tests/move_check/skip_attribute_checks/aptos_stdlib_attributes.move:2:10
2 │ #[a, a(x = 0)]
│ - ^^^^^^^^ Duplicate attribute 'a' attached to the same item
│ │
│ Attribute previously given here

error[E02001]: duplicate declaration, item, or annotation
┌─ tests/move_check/skip_attribute_checks/aptos_stdlib_attributes.move:6:12
6 │ #[b(a, a = 0, a(x = 1))]
│ - ^^^^^ Duplicate attribute 'a' attached to the same item
│ │
│ Attribute previously given here

error[E02001]: duplicate declaration, item, or annotation
┌─ tests/move_check/skip_attribute_checks/aptos_stdlib_attributes.move:6:19
6 │ #[b(a, a = 0, a(x = 1))]
│ - ^^^^^^^^ Duplicate attribute 'a' attached to the same item
│ │
│ Attribute previously given here

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module aptos_std::M {
#[a, a(x = 0)]
fun foo() {}

#[testonly]
#[b(a, a = 0, a(x = 1))]
fun bar() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module aptos_std::M {
fun foo() {}

#[testonly]
fun bar() {}
}
10 changes: 6 additions & 4 deletions third_party/move/move-compiler/tests/move_check_testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ const VERIFICATION_EXT: &str = "verification";
const FLAVOR_PATH: &str = "flavors/";

/// Root of tests which require to set skip_attribute_checks flag.
const SKIP_ATTRIBUTE_CHECK_PATH: &str = "skip_attribute_checks/";
const SKIP_ATTRIBUTE_CHECKS_PATH: &str = "skip_attribute_checks/";

fn default_testing_addresses() -> BTreeMap<String, NumericalAddress> {
let mapping = [
("aptos_std", "0x1"),
("std", "0x1"),
("M", "0x1"),
("A", "0x42"),
Expand Down Expand Up @@ -99,7 +100,8 @@ fn move_check_testsuite(path: &Path) -> datatest_stable::Result<()> {
.to_string_lossy()
.to_string();
flags = flags.set_flavor(flavor)
} else if p.contains(SKIP_ATTRIBUTE_CHECK_PATH) {
}
if p.contains(SKIP_ATTRIBUTE_CHECKS_PATH) {
flags = flags.set_skip_attribute_checks(true);
}
};
Expand All @@ -115,9 +117,9 @@ fn run_test(path: &Path, exp_path: &Path, out_path: &Path, flags: Flags) -> anyh
targets,
move_stdlib::move_stdlib_files(),
default_testing_addresses(),
KnownAttribute::get_attribute_names_set(),
flags,
KnownAttribute::get_all_attribute_names(),
)
.set_flags(flags)
.run::<PASS_PARSER>()?;
let diags = move_check_for_errors(comments_and_compiler_res);

Expand Down

0 comments on commit 0dac829

Please sign in to comment.