diff --git a/analyzer/src/context.rs b/analyzer/src/context.rs index d71f1acb1f..5356d6eee9 100644 --- a/analyzer/src/context.rs +++ b/analyzer/src/context.rs @@ -52,7 +52,7 @@ pub struct ContractAttributes { pub init_function: Option, /// Events that have been defined by the user. pub events: Vec, - /// List expressions that the contract uses + /// Static strings that the contract defines pub string_literals: BTreeSet, /// Structs that have been defined by the user pub structs: Vec, diff --git a/compiler/src/lowering/mappers/module.rs b/compiler/src/lowering/mappers/module.rs index 0de6408c14..0a76950ec7 100644 --- a/compiler/src/lowering/mappers/module.rs +++ b/compiler/src/lowering/mappers/module.rs @@ -1,5 +1,5 @@ use crate::lowering::context::{Context, ModuleContext}; -use crate::lowering::mappers::contracts; +use crate::lowering::mappers::{contracts, types}; use crate::lowering::names; use crate::lowering::utils::ZeroSpanNode; use fe_analyzer::context::Context as AnalyzerContext; @@ -16,13 +16,19 @@ pub fn module(analysis: &AnalyzerContext, module: fe::Module) -> fe::Module { .into_iter() .map(|stmt| match stmt { fe::ModuleStmt::Pragma(_) => stmt, - fe::ModuleStmt::TypeAlias(_) => stmt, // XXX lower tuples? + fe::ModuleStmt::TypeAlias(inner) => fe::ModuleStmt::TypeAlias(Node::new( + fe::TypeAlias { + name: inner.kind.name, + typ: types::type_desc(&mut Context::new(&mut module_context), inner.kind.typ), + }, + inner.span, + )), fe::ModuleStmt::Struct(_) => stmt, fe::ModuleStmt::Import(_) => stmt, - fe::ModuleStmt::Contract(inner) => { - let mut context = Context::new(&mut module_context); - fe::ModuleStmt::Contract(contracts::contract_def(&mut context, inner)) - } + fe::ModuleStmt::Contract(inner) => fe::ModuleStmt::Contract(contracts::contract_def( + &mut Context::new(&mut module_context), + inner, + )), }) .collect::>(); diff --git a/compiler/src/lowering/mappers/types.rs b/compiler/src/lowering/mappers/types.rs index 56057f5642..477f0f0a82 100644 --- a/compiler/src/lowering/mappers/types.rs +++ b/compiler/src/lowering/mappers/types.rs @@ -17,14 +17,6 @@ pub fn type_desc(context: &mut Context, desc: Node) -> Node .as_tuple() .expect("expected tuple type"); - // (u8, (u8, u8)) should become - // struct __Tuple1: - // item0: u8 - // item1: u8 - // struct __Tuple2: - // item0: u8 - // item1: __Tuple1 - for item in items.into_iter() { type_desc(context, item); } diff --git a/newsfragments/459.feature.md b/newsfragments/459.feature.md new file mode 100644 index 0000000000..f0987dbca0 --- /dev/null +++ b/newsfragments/459.feature.md @@ -0,0 +1,4 @@ +Type aliases can now include tuples. Example: +``` +type InternetPoints = (address, u256) +``` diff --git a/tests/fixtures/lowering/type_alias_tuple.fe b/tests/fixtures/lowering/type_alias_tuple.fe new file mode 100644 index 0000000000..8166372f0a --- /dev/null +++ b/tests/fixtures/lowering/type_alias_tuple.fe @@ -0,0 +1,4 @@ +type Tup = (u8, address) + +contract Foo: + tup: Tup diff --git a/tests/fixtures/lowering/type_alias_tuple_lowered.fe b/tests/fixtures/lowering/type_alias_tuple_lowered.fe new file mode 100644 index 0000000000..9ea58254d1 --- /dev/null +++ b/tests/fixtures/lowering/type_alias_tuple_lowered.fe @@ -0,0 +1,8 @@ +struct tuple_u8_address_: + item0: u8 + item1: address + +type Tup = tuple_u8_address_ + +contract Foo: + tup: Tup diff --git a/tests/src/lowering.rs b/tests/src/lowering.rs index 76242833d4..a1e4e759a1 100644 --- a/tests/src/lowering.rs +++ b/tests/src/lowering.rs @@ -52,7 +52,8 @@ fn replace_spans(input: String) -> String { case("init"), case("custom_empty_type"), case("nested_tuple"), - case("map_tuple") + case("map_tuple"), + case("type_alias_tuple") // case("array_tuple") // TODO: analysis fails on "arrays can only hold primitive types" )] fn test_lowering(fixture: &str) {