From 1fc49f5f4690321853a2c5d12ddd543d85fda130 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Wed, 30 Jun 2021 16:32:57 +0100 Subject: [PATCH] Each of the individual types named in the union type must be unique. --- README.md | 22 +++++++++++----------- TODO.md | 2 -- src/castile/checker.py | 8 +++++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0357670..49213b6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/TODO.md b/TODO.md index b438b7f..fbf0345 100644 --- a/TODO.md +++ b/TODO.md @@ -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`?) diff --git a/src/castile/checker.py b/src/castile/checker.py index c368d90..6870304 100644 --- a/src/castile/checker.py +++ b/src/castile/checker.py @@ -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':