Skip to content

Commit

Permalink
Lower tuples in type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
sbillig committed Jun 22, 2021
1 parent bad7cc0 commit 6807996
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion analyzer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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
/// Static strings that the contract defines
pub string_literals: BTreeSet<String>,
/// Structs that have been defined by the user
pub structs: Vec<Struct>,
Expand Down
18 changes: 12 additions & 6 deletions compiler/src/lowering/mappers/module.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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::<Vec<_>>();

Expand Down
8 changes: 0 additions & 8 deletions compiler/src/lowering/mappers/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ pub fn type_desc(context: &mut Context, desc: Node<TypeDesc>) -> Node<TypeDesc>
.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);
}
Expand Down
4 changes: 4 additions & 0 deletions newsfragments/459.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Type aliases can now include tuples. Example:
```
type InternetPoints = (address, u256)
```
4 changes: 4 additions & 0 deletions tests/fixtures/lowering/type_alias_tuple.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Tup = (u8, address)

contract Foo:
tup: Tup
8 changes: 8 additions & 0 deletions tests/fixtures/lowering/type_alias_tuple_lowered.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct tuple_u8_address_:
item0: u8
item1: address

type Tup = tuple_u8_address_

contract Foo:
tup: Tup
3 changes: 2 additions & 1 deletion tests/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6807996

Please sign in to comment.