Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower list expressions #388

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analyzer/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum ContractTypeMethod {
Create2,
}

#[derive(Debug, PartialEq, EnumString)]
#[derive(strum::ToString, Debug, PartialEq, EnumString)]
#[strum(serialize_all = "lowercase")]
pub enum Object {
Block,
Expand Down
5 changes: 4 additions & 1 deletion analyzer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::builtins::GlobalMethod;
use crate::errors::SemanticError;
use crate::namespace::events::EventDef;
use crate::namespace::scopes::{ContractFunctionDef, ContractScope, ModuleScope, Shared};
use crate::namespace::types::{Contract, FixedSize, Struct, Tuple, Type};
use crate::namespace::types::{Array, Contract, FixedSize, Struct, Tuple, Type};
use fe_common::Span;
use fe_parser::ast as fe;
use fe_parser::node::{Node, NodeId};
Expand Down Expand Up @@ -49,6 +49,8 @@ pub struct ContractAttributes {
pub init_function: Option<FunctionAttributes>,
/// Events that have been defined by the user.
pub events: Vec<EventDef>,
/// List expressions that the contract uses
pub list_expressions: BTreeSet<Array>,
/// Static strings that the contract defines
pub string_literals: BTreeSet<String>,
/// Structs that have been defined by the user
Expand Down Expand Up @@ -116,6 +118,7 @@ impl From<Shared<ContractScope>> for ContractAttributes {
.values()
.map(|event| event.to_owned())
.collect::<Vec<EventDef>>(),
list_expressions: scope.borrow().list_expressions.clone(),
string_literals: scope.borrow().string_defs.clone(),
structs,
external_contracts,
Expand Down
9 changes: 8 additions & 1 deletion analyzer/src/namespace/scopes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::errors::SemanticError;
use crate::namespace::events::EventDef;
use crate::namespace::types::{FixedSize, Tuple, Type};
use crate::namespace::types::{Array, FixedSize, Tuple, Type};
use std::cell::RefCell;
use std::collections::btree_map::Entry;
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -41,6 +41,7 @@ pub struct ContractScope {
pub event_defs: BTreeMap<String, EventDef>,
pub field_defs: BTreeMap<String, ContractFieldDef>,
pub function_defs: BTreeMap<String, ContractFunctionDef>,
pub list_expressions: BTreeSet<Array>,
pub string_defs: BTreeSet<String>,
pub created_contracts: BTreeSet<String>,
num_fields: usize,
Expand Down Expand Up @@ -122,6 +123,7 @@ impl ContractScope {
string_defs: BTreeSet::new(),
interface: vec![],
created_contracts: BTreeSet::new(),
list_expressions: BTreeSet::new(),
num_fields: 0,
}))
}
Expand Down Expand Up @@ -209,6 +211,11 @@ impl ContractScope {
pub fn add_created_contract(&mut self, name: &str) {
self.created_contracts.insert(name.to_owned());
}

/// Add the array type of a list expression that was used within the contract.
pub fn add_used_list_expression(&mut self, typ: Array) {
self.list_expressions.insert(typ);
}
}

impl BlockScope {
Expand Down
6 changes: 6 additions & 0 deletions analyzer/src/namespace/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ impl From<Base> for Type {
}
}

impl From<Base> for FixedSize {
fn from(value: Base) -> Self {
FixedSize::Base(value)
}
}

impl FixedSize {
pub fn empty_tuple() -> Self {
FixedSize::Tuple(Tuple::empty())
Expand Down
30 changes: 20 additions & 10 deletions analyzer/src/traversal/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,20 @@ pub fn expr_list(
// TODO: Right now we are only supporting Base type arrays
// Potential we can support the tuples as well.
if let Type::Base(base) = attribute_to_be_matched.typ {
let array_typ = Array {
size: elts.len(),
inner: base,
};

scope
.borrow()
.contract_scope()
.borrow_mut()
.add_used_list_expression(array_typ.clone());

return Ok(ExpressionAttributes {
typ: Type::Array(Array {
size: elts.len(),
inner: base,
}),
location: attribute_to_be_matched.location,
typ: Type::Array(array_typ),
location: Location::Memory,
move_location: None,
});
}
Expand Down Expand Up @@ -641,15 +649,17 @@ fn expr_call_self_attribute(
func_name: &str,
args: &Node<Vec<Node<fe::CallArg>>>,
) -> Result<ExpressionAttributes, SemanticError> {
let called_func = scope
.borrow()
.contract_scope()
.borrow()
.function_def(func_name);

if let Some(ContractFunctionDef {
params,
return_type,
..
}) = scope
.borrow()
.contract_scope()
.borrow()
.function_def(func_name)
}) = called_func
Copy link
Collaborator Author

@cburgdorf cburgdorf May 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this out of the if let was needed to mitigate a borrow_mut() violation...this took me way to long to find 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, Ive ran into this too

{
let argument_attributes = expr_call_args(Rc::clone(&scope), Rc::clone(&context), args)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6067,6 +6067,7 @@ note:
],
},
],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ note:
indexed_fields: [],
},
],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
3 changes: 3 additions & 0 deletions analyzer/tests/snapshots/analysis__case_003_uniswap.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25779,6 +25779,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down Expand Up @@ -26652,6 +26653,7 @@ note:
],
},
],
list_expressions: {},
string_literals: {
"UniswapV2: FORBIDDEN",
"UniswapV2: INSUFFICIENT_INPUT_AMOUNT",
Expand Down Expand Up @@ -26952,6 +26954,7 @@ note:
],
},
],
list_expressions: {},
string_literals: {
"UniswapV2: FORBIDDEN",
"UniswapV2: IDENTICAL_ADDRESSES",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
1 change: 1 addition & 0 deletions analyzer/tests/snapshots/analysis__case_005_assert.snap
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {
"Must be greater than five",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2294,6 +2294,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13353,6 +13353,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ note:
},
),
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down Expand Up @@ -403,6 +404,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down Expand Up @@ -369,6 +370,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down Expand Up @@ -364,6 +365,7 @@ note:
},
),
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down
1 change: 1 addition & 0 deletions analyzer/tests/snapshots/analysis__case_016_empty.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ note:
public_functions: [],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
1 change: 1 addition & 0 deletions analyzer/tests/snapshots/analysis__case_017_events.snap
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ note:
],
},
],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1912,6 +1912,7 @@ note:
indexed_fields: [],
},
],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down Expand Up @@ -2097,6 +2098,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
1 change: 1 addition & 0 deletions analyzer/tests/snapshots/analysis__case_025_keccak.snap
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
1 change: 1 addition & 0 deletions analyzer/tests/snapshots/analysis__case_026_math.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ note:
],
init_function: None,
events: [],
list_expressions: {},
string_literals: {},
structs: [],
external_contracts: [],
Expand Down
Loading