Skip to content

Commit

Permalink
Merge pull request #384 from sbillig/generic-map
Browse files Browse the repository at this point in the history
Remove ast::TypeDesc::Map in favor of TypeDesc::Generic
  • Loading branch information
g-r-a-n-t authored May 3, 2021
2 parents 5c25397 + 9fb10f8 commit be3ccf8
Show file tree
Hide file tree
Showing 20 changed files with 369 additions and 237 deletions.
1 change: 1 addition & 0 deletions analyzer/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum ErrorKind {
CannotMove,
CircularDependency,
ContinueWithoutLoop,
MapTypeError,
KeyWordArgsRequired,
MissingEventDefinition,
MissingReturn,
Expand Down
30 changes: 24 additions & 6 deletions analyzer/src/namespace/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::errors::SemanticError;
use crate::errors::{ErrorKind, SemanticError};
use fe_parser::ast as fe;
use fe_parser::node::Node;
use std::collections::{btree_map::Entry, BTreeMap, HashMap};
use std::convert::TryFrom;

Expand Down Expand Up @@ -918,11 +919,28 @@ pub fn type_desc(defs: &HashMap<String, Type>, typ: &fe::TypeDesc) -> Result<Typ
inner: type_desc_base(defs, &typ.kind)?,
size: *dimension,
})),
fe::TypeDesc::Map { from, to } => Ok(Type::Map(Map {
key: type_desc_base(defs, &from.kind)?,
value: Box::new(type_desc(defs, &to.kind)?),
})),
fe::TypeDesc::Generic { .. } => todo!(),
fe::TypeDesc::Generic { base, args } => {
if base.kind == "map" {
match &args[..] {
[Node {
kind: fe::GenericArg::TypeDesc(from),
..
}, Node {
kind: fe::GenericArg::TypeDesc(to),
..
}] => Ok(Type::Map(Map {
key: type_desc_base(defs, &from)?,
value: Box::new(type_desc(defs, &to)?),
})),
_ => Err(SemanticError {
kind: ErrorKind::MapTypeError,
context: vec![],
}),
}
} else {
Err(SemanticError::undefined_value())
}
}
fe::TypeDesc::Tuple { items } => Ok(Type::Tuple(Tuple {
items: items
.iter()
Expand Down
5 changes: 5 additions & 0 deletions compiler/tests/cases/compile_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ use rstest::rstest;
fixture_file,
expected_error,
case("assert_reason_not_string.fe", TypeError),
case("bad_map.fe", MapTypeError),
case("bad_map2.fe", MapTypeError),
case("bad_map3.fe", MapTypeError),
case("bad_map4.fe", MapTypeError),
case("bad_map5.fe", TypeError),
case("break_without_loop_2.fe", BreakWithoutLoop),
case("break_without_loop.fe", BreakWithoutLoop),
case("call_event_with_wrong_types.fe", TypeError),
Expand Down
2 changes: 2 additions & 0 deletions compiler/tests/fixtures/compile_errors/bad_map.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
contract Foo:
x: map<u8, u8, u8>
2 changes: 2 additions & 0 deletions compiler/tests/fixtures/compile_errors/bad_map2.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
contract Foo:
x: map<address, 100>
2 changes: 2 additions & 0 deletions compiler/tests/fixtures/compile_errors/bad_map3.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
contract Foo:
x: map<>
2 changes: 2 additions & 0 deletions compiler/tests/fixtures/compile_errors/bad_map4.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
contract Foo:
x: map<y>
2 changes: 2 additions & 0 deletions compiler/tests/fixtures/compile_errors/bad_map5.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
contract Foo:
x: map<map<u8, u8>, address>
4 changes: 0 additions & 4 deletions parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ pub enum TypeDesc {
typ: Box<Node<TypeDesc>>,
dimension: usize,
},
Map {
from: Box<Node<TypeDesc>>,
to: Box<Node<TypeDesc>>,
},
Tuple {
items: Vec<Node<TypeDesc>>,
},
Expand Down
38 changes: 7 additions & 31 deletions parser/src/grammar/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,26 +261,6 @@ fn parse_generic_args(par: &mut Parser) -> ParseResult<(Vec<Node<GenericArg>>, S
Ok((args, span))
}

// TODO: remove TypeDesc::Map and this fn
fn temporary_backward_compatible_map_type(
mut args: Vec<Node<GenericArg>>,
span: Span,
) -> Node<TypeDesc> {
let to_node = args.pop().unwrap();
let from_node = args.pop().unwrap();
if let (GenericArg::TypeDesc(from), GenericArg::TypeDesc(to)) = (from_node.kind, to_node.kind) {
Node::new(
TypeDesc::Map {
from: Box::new(Node::new(from, from_node.span)),
to: Box::new(Node::new(to, to_node.span)),
},
span,
)
} else {
panic!()
}
}

/// Parse a type description, e.g. `u8` or `map<address, u256>`.
pub fn parse_type_desc(par: &mut Parser) -> ParseResult<Node<TypeDesc>> {
use TokenKind::*;
Expand All @@ -292,17 +272,13 @@ pub fn parse_type_desc(par: &mut Parser) -> ParseResult<Node<TypeDesc>> {
Some(Lt) => {
let (args, argspan) = parse_generic_args(par)?;
let span = name.span + argspan;
if name.text == "map" {
temporary_backward_compatible_map_type(args, span)
} else {
Node::new(
TypeDesc::Generic {
base: name.into(),
args,
},
span,
)
}
Node::new(
TypeDesc::Generic {
base: name.into(),
args,
},
span,
)
}
_ => Node::new(
TypeDesc::Base {
Expand Down
37 changes: 23 additions & 14 deletions parser/tests/cases/snapshots/cases__parse_ast__contract_def.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,34 @@ Node(
),
),
typ: Node(
kind: Map(
from: Node(
kind: Base(
base: "u8",
),
kind: Generic(
base: Node(
kind: "map",
span: Span(
start: 58,
end: 60,
start: 54,
end: 57,
),
),
to: Node(
kind: Base(
base: "address",
args: [
Node(
kind: TypeDesc(Base(
base: "u8",
)),
span: Span(
start: 58,
end: 60,
),
),
span: Span(
start: 62,
end: 69,
Node(
kind: TypeDesc(Base(
base: "address",
)),
span: Span(
start: 62,
end: 69,
),
),
),
],
),
span: Span(
start: 54,
Expand Down
37 changes: 23 additions & 14 deletions parser/tests/cases/snapshots/cases__parse_ast__guest_book.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,34 @@ Node(
),
),
typ: Node(
kind: Map(
from: Node(
kind: Base(
base: "address",
),
kind: Generic(
base: Node(
kind: "map",
span: Span(
start: 72,
end: 79,
start: 68,
end: 71,
),
),
to: Node(
kind: Base(
base: "BookMsg",
args: [
Node(
kind: TypeDesc(Base(
base: "address",
)),
span: Span(
start: 72,
end: 79,
),
),
span: Span(
start: 81,
end: 88,
Node(
kind: TypeDesc(Base(
base: "BookMsg",
)),
span: Span(
start: 81,
end: 88,
),
),
),
],
),
span: Span(
start: 68,
Expand Down
37 changes: 23 additions & 14 deletions parser/tests/cases/snapshots/cases__parse_ast__module_stmts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,34 @@ Node(
),
),
typ: Node(
kind: Map(
from: Node(
kind: Base(
base: "u8",
),
kind: Generic(
base: Node(
kind: "map",
span: Span(
start: 45,
end: 47,
start: 41,
end: 44,
),
),
to: Node(
kind: Base(
base: "u16",
args: [
Node(
kind: TypeDesc(Base(
base: "u8",
)),
span: Span(
start: 45,
end: 47,
),
),
span: Span(
start: 49,
end: 52,
Node(
kind: TypeDesc(Base(
base: "u16",
)),
span: Span(
start: 49,
end: 52,
),
),
),
],
),
span: Span(
start: 41,
Expand Down
37 changes: 23 additions & 14 deletions parser/tests/cases/snapshots/cases__parse_ast__struct_def.snap
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,34 @@ Node(
),
),
typ: Node(
kind: Map(
from: Node(
kind: Base(
base: "u8",
),
kind: Generic(
base: Node(
kind: "map",
span: Span(
start: 68,
end: 70,
start: 64,
end: 67,
),
),
to: Node(
kind: Base(
base: "foo",
args: [
Node(
kind: TypeDesc(Base(
base: "u8",
)),
span: Span(
start: 68,
end: 70,
),
),
span: Span(
start: 72,
end: 75,
Node(
kind: TypeDesc(Base(
base: "foo",
)),
span: Span(
start: 72,
end: 75,
),
),
),
],
),
span: Span(
start: 64,
Expand Down
37 changes: 23 additions & 14 deletions parser/tests/cases/snapshots/cases__parse_ast__type_def.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,34 @@ Node(
),
),
typ: Node(
kind: Map(
from: Node(
kind: Base(
base: "address",
),
kind: Generic(
base: Node(
kind: "map",
span: Span(
start: 13,
end: 20,
start: 9,
end: 12,
),
),
to: Node(
kind: Base(
base: "u256",
args: [
Node(
kind: TypeDesc(Base(
base: "address",
)),
span: Span(
start: 13,
end: 20,
),
),
span: Span(
start: 22,
end: 26,
Node(
kind: TypeDesc(Base(
base: "u256",
)),
span: Span(
start: 22,
end: 26,
),
),
),
],
),
span: Span(
start: 9,
Expand Down
Loading

0 comments on commit be3ccf8

Please sign in to comment.