Skip to content

Commit

Permalink
Each of the individual types named in the union type must be unique.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Jun 30, 2021
1 parent 681df3a commit 1fc49f5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1067,18 +1067,18 @@ The type after the `as` must be the type of the expression.
| }
= ok

Each of the individual types named in the union type must be unique. (TODO)
Each of the individual types named in the union type must be unique.

/| fun foo(a, b: integer|string) {
/| print("ok")
/| }
/| fun main() {
/| a = 20;
/| b = 30;
/| c = a + b as integer|integer|string
/| foo(a, c)
/| }
/? bad union type
| fun foo(a, b: integer|string) {
| print("ok")
| }
| fun main() {
| a = 20;
| b = 30;
| c = a + b as integer|integer|string
| foo(a, c)
| }
? bad union type

Cannot promote a union type to itself.

Expand Down
2 changes: 0 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Other backends (Python? Java? CIL? Scheme?)

### Design ###

Each of the individual types named in the union type must be unique.

Promote union type to bigger union type (e.g. `string|int` => `string|int|void`)

Don't output final value. Command-line arguments passed to `main`. (`sysmain`?)
Expand Down
8 changes: 7 additions & 1 deletion src/castile/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ def type_of(self, ast):
[self.type_of(c) for c in ast.children[1:]], return_type
)
elif ast.tag == 'UnionType':
ast.type = Union([self.type_of(c) for c in ast.children])
types = []
for c in ast.children:
type_ = self.type_of(c)
if type_ in types:
raise CastileTypeError("bad union type")
types.append(type_)
ast.type = Union(types)
elif ast.tag == 'StructType':
ast.type = Struct(ast.value)
elif ast.tag == 'VarRef':
Expand Down

0 comments on commit 1fc49f5

Please sign in to comment.