Skip to content

Commit

Permalink
No equality between unions that contain structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Feb 23, 2022
1 parent 4f09554 commit 8adf1d7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
7 changes: 4 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ or implement some kind of resource-awareness in the language itself.

Other backends (Python? Java? CIL? Scheme?)

### Design ###
Test framework: collect the backend-independent tests into a single
file, and only test it once. Run all the *other* tests on every
backend.

Disallow equality checking for union types too (maybe except where
all member types are simple.)
### Design ###

Don't output final value. Command-line arguments passed to `main`. (`sysmain`?)

Expand Down
2 changes: 2 additions & 0 deletions src/castile/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def type_of(self, ast):
self.assert_eq(type1, type2)
if isinstance(type1, Struct):
raise CastileTypeError("structs cannot be compared")
if isinstance(type1, Union) and type1.contains_instance_of(Struct):
raise CastileTypeError("unions containing structs cannot be compared")
ast.type = Boolean()
elif ast.tag == 'Not':
type1 = self.type_of(ast.children[0])
Expand Down
6 changes: 6 additions & 0 deletions src/castile/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def contains(self, type_):
return True
return False

def contains_instance_of(self, cls):
for member in self.content_types:
if isinstance(member, cls):
return True
return False

def __str__(self):
h = "union("
h += ', '.join(sorted([str(t) for t in self.content_types]))
Expand Down
16 changes: 15 additions & 1 deletion tests/Castile.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ Equality cannot be checked between two values of different types.
| }
? mismatch

Equality can be checked between unions.
Equality can be checked between unions, as long as they are
unions entirely of simple (non-struct) types.

| fun main() {
| a = 40 as string|integer
Expand Down Expand Up @@ -633,6 +634,19 @@ Equality cannot be tested between two disjoint unions.
| }
? mismatch

Equality cannot be tested between values of a union type
that contains a struct type as one of its members.

| struct person { name: string; age: integer }
| fun main() {
| a = 40 as person|integer
| b = 40 as person|integer
| if a == b {
| print("it is")
| }
| }
? struct

### Builtins ###

The usual.
Expand Down

0 comments on commit 8adf1d7

Please sign in to comment.