Skip to content

Commit eba4f27

Browse files
authored
Improve type-checking (#19)
Adds type-checking to the following cases: - block outputs (reflect the type of the last statement, none otherwise) - parameters in def (any if not specified, else the specified type) - if else (reflect the type of the blocks if the blocks are equal, if they are unequal then forbidden, and the if branch type otherwise) --------- Signed-off-by: innocentzero <[email protected]>
1 parent 44fcadf commit eba4f27

35 files changed

+592
-134
lines changed

src/snapshots/[email protected]

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: src/test.rs
3+
expression: evaluate_example(path)
4+
input_file: tests/alias.nu
5+
snapshot_kind: text
6+
---
7+
==== COMPILER ====
8+
0: Name (4 to 7) "foo"
9+
1: Name (9 to 10) "a"
10+
2: Param { name: NodeId(1), ty: None } (9 to 10)
11+
3: Params([NodeId(2)]) (8 to 11)
12+
4: Variable (14 to 16) "$a"
13+
5: Block(BlockId(0)) (12 to 18)
14+
6: Def { name: NodeId(0), params: NodeId(3), return_ty: None, block: NodeId(5) } (0 to 18)
15+
7: Name (20 to 25) "alias"
16+
8: Name (26 to 29) "bar"
17+
9: Garbage (30 to 31)
18+
10: String (32 to 35) "foo"
19+
11: Call { parts: [NodeId(7), NodeId(8), NodeId(9), NodeId(10)] } (26 to 35)
20+
12: Name (37 to 40) "bar"
21+
13: Int (41 to 42) "1"
22+
14: Call { parts: [NodeId(12), NodeId(13)] } (41 to 42)
23+
15: Block(BlockId(1)) (0 to 43)
24+
==== COMPILER ERRORS ====
25+
Error (NodeId 9): incomplete expression

src/snapshots/new_nu_parser__test__node_output@binary_ops_exact.nu.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/binary_ops_exact.nu
5+
snapshot_kind: text
56
---
67
==== COMPILER ====
78
0: Int (0 to 1) "1"
@@ -50,4 +51,4 @@ input_file: tests/binary_ops_exact.nu
5051
18: forbidden
5152
19: bool
5253
20: bool
53-
21: ()
54+
21: bool

src/snapshots/new_nu_parser__test__node_output@binary_ops_mismatch.nu.snap

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/binary_ops_mismatch.nu
5+
snapshot_kind: text
56
---
67
==== COMPILER ====
78
0: String (0 to 3) ""a""
@@ -21,18 +22,18 @@ input_file: tests/binary_ops_mismatch.nu
2122
0: Frame Scope, node_id: NodeId(12) (empty)
2223
==== TYPES ====
2324
0: string
24-
1: forbidden
25+
1: error
2526
2: float
26-
3: unknown
27+
3: error
2728
4: string
28-
5: forbidden
29+
5: error
2930
6: float
30-
7: unknown
31+
7: error
3132
8: bool
32-
9: forbidden
33+
9: error
3334
10: string
34-
11: unknown
35-
12: ()
35+
11: error
36+
12: error
3637
==== TYPE ERRORS ====
3738
Error (NodeId 1): type mismatch: unsupported addition between string and float
3839
Error (NodeId 5): type mismatch: unsupported append between string and float

src/snapshots/new_nu_parser__test__node_output@binary_ops_subtypes.nu.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/binary_ops_subtypes.nu
5+
snapshot_kind: text
56
---
67
==== COMPILER ====
78
0: Int (0 to 1) "1"
@@ -94,4 +95,4 @@ input_file: tests/binary_ops_subtypes.nu
9495
40: list<float>
9596
41: list<list<float>>
9697
42: list<list<number>>
97-
43: ()
98+
43: list<list<number>>

src/snapshots/[email protected]

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/calls.nu
5+
snapshot_kind: text
56
---
67
==== COMPILER ====
78
0: Name (0 to 4) "spam"
@@ -59,21 +60,21 @@ input_file: tests/calls.nu
5960
9: unknown
6061
10: unknown
6162
11: string
62-
12: unknown
63+
12: string
6364
13: unknown
6465
14: unknown
6566
15: string
66-
16: unknown
67+
16: string
6768
17: unknown
6869
18: unknown
6970
19: int
70-
20: unknown
71-
21: unknown
72-
22: unknown
73-
23: unknown
74-
24: unknown
75-
25: list<unknown>
76-
26: ()
71+
20: int
72+
21: forbidden
73+
22: string
74+
23: string
75+
24: int
76+
25: list<any>
77+
26: list<any>
7778
27: ()
7879
28: unknown
7980
29: string
@@ -83,4 +84,4 @@ input_file: tests/calls.nu
8384
33: string
8485
34: int
8586
35: any
86-
36: ()
87+
36: any

src/snapshots/[email protected]

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
source: src/test.rs
3+
expression: evaluate_example(path)
4+
input_file: tests/closure3.nu
5+
snapshot_kind: text
6+
---
7+
==== COMPILER ====
8+
0: Variable (4 to 11) "closure"
9+
1: Name (16 to 17) "a"
10+
2: Name (19 to 22) "int"
11+
3: Type { name: NodeId(2), params: None, optional: false } (19 to 22)
12+
4: Param { name: NodeId(1), ty: Some(NodeId(3)) } (16 to 22)
13+
5: Name (24 to 25) "b"
14+
6: Name (27 to 30) "int"
15+
7: Type { name: NodeId(6), params: None, optional: false } (27 to 30)
16+
8: Param { name: NodeId(5), ty: Some(NodeId(7)) } (24 to 30)
17+
9: Params([NodeId(4), NodeId(8)]) (15 to 31)
18+
10: Variable (32 to 34) "$a"
19+
11: Plus (35 to 36)
20+
12: Variable (37 to 39) "$b"
21+
13: LessThan (40 to 41)
22+
14: Int (42 to 43) "5"
23+
15: BinaryOp { lhs: NodeId(10), op: NodeId(11), rhs: NodeId(12) } (32 to 39)
24+
16: BinaryOp { lhs: NodeId(15), op: NodeId(13), rhs: NodeId(14) } (32 to 43)
25+
17: Block(BlockId(0)) (32 to 43)
26+
18: Closure { params: Some(NodeId(9)), block: NodeId(17) } (14 to 44)
27+
19: Let { variable_name: NodeId(0), ty: None, initializer: NodeId(18), is_mutable: false } (0 to 44)
28+
20: Name (46 to 52) "filter"
29+
21: Variable (53 to 61) "$closure"
30+
22: Call { parts: [NodeId(20), NodeId(21)] } (53 to 61)
31+
23: Block(BlockId(1)) (0 to 62)
32+
==== SCOPE ====
33+
0: Frame Scope, node_id: NodeId(23)
34+
variables: [ closure: NodeId(0) ]
35+
1: Frame Scope, node_id: NodeId(17)
36+
variables: [ a: NodeId(1), b: NodeId(5) ]
37+
==== TYPES ====
38+
0: closure
39+
1: unknown
40+
2: unknown
41+
3: int
42+
4: int
43+
5: unknown
44+
6: unknown
45+
7: int
46+
8: int
47+
9: forbidden
48+
10: int
49+
11: forbidden
50+
12: int
51+
13: forbidden
52+
14: int
53+
15: int
54+
16: bool
55+
17: bool
56+
18: closure
57+
19: ()
58+
20: unknown
59+
21: closure
60+
22: stream<binary>
61+
23: stream<binary>

src/snapshots/[email protected]

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/def.nu
5+
snapshot_kind: text
56
---
67
==== COMPILER ====
78
0: Name (4 to 7) "foo"
@@ -41,22 +42,22 @@ input_file: tests/def.nu
4142
3: unknown
4243
4: unknown
4344
5: int
44-
6: unknown
45+
6: int
4546
7: unknown
4647
8: unknown
4748
9: unknown
4849
10: unknown
4950
11: int
50-
12: unknown
51+
12: forbidden
5152
13: list<int>
52-
14: unknown
53+
14: forbidden
5354
15: list<list<int>>
54-
16: unknown
55-
17: unknown
55+
16: list<list<int>>
56+
17: forbidden
5657
18: unknown
57-
19: unknown
58-
20: unknown
59-
21: list<unknown>
60-
22: ()
58+
19: int
59+
20: list<list<int>>
60+
21: list<any>
61+
22: list<any>
6162
23: ()
6263
24: ()

src/snapshots/[email protected]

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/for.nu
5+
snapshot_kind: text
56
---
67
==== COMPILER ====
78
0: Variable (4 to 5) "x"
@@ -35,7 +36,7 @@ input_file: tests/for.nu
3536
28: BinaryOp { lhs: NodeId(22), op: NodeId(23), rhs: NodeId(27) } (73 to 85)
3637
29: Block(BlockId(1)) (67 to 87)
3738
30: For { variable: NodeId(17), range: NodeId(21), block: NodeId(29) } (49 to 87)
38-
31: Block(BlockId(2)) (0 to 87)
39+
31: Block(BlockId(2)) (0 to 88)
3940
==== SCOPE ====
4041
0: Frame Scope, node_id: NodeId(31)
4142
variables: [ x: NodeId(0) ]
@@ -47,35 +48,32 @@ input_file: tests/for.nu
4748
0: int
4849
1: int
4950
2: ()
50-
3: unknown
51-
4: unknown
52-
5: unknown
53-
6: unknown
54-
7: unknown
55-
8: unknown
56-
9: unknown
57-
10: unknown
58-
11: unknown
59-
12: unknown
60-
13: unknown
61-
14: unknown
62-
15: unknown
63-
16: unknown
64-
17: unknown
65-
18: unknown
66-
19: unknown
67-
20: unknown
68-
21: unknown
69-
22: unknown
70-
23: unknown
71-
24: unknown
72-
25: unknown
73-
26: unknown
74-
27: unknown
75-
28: unknown
76-
29: unknown
77-
30: unknown
51+
3: int
52+
4: int
53+
5: int
54+
6: int
55+
7: list<int>
56+
8: int
57+
9: forbidden
58+
10: int
59+
11: forbidden
60+
12: int
61+
13: int
62+
14: ()
63+
15: ()
64+
16: ()
65+
17: int
66+
18: int
67+
19: int
68+
20: int
69+
21: list<int>
70+
22: int
71+
23: forbidden
72+
24: int
73+
25: forbidden
74+
26: int
75+
27: int
76+
28: ()
77+
29: ()
78+
30: ()
7879
31: ()
79-
==== TYPE ERRORS ====
80-
Error (NodeId 16): unsupported ast node 'For { variable: NodeId(3), range: NodeId(7), block: NodeId(15) }' in typechecker
81-
Error (NodeId 30): unsupported ast node 'For { variable: NodeId(17), range: NodeId(21), block: NodeId(29) }' in typechecker

src/snapshots/new_nu_parser__test__node_output@for_break_continue.nu.snap

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
source: src/test.rs
33
expression: evaluate_example(path)
44
input_file: tests/for_break_continue.nu
5+
snapshot_kind: text
56
---
67
==== COMPILER ====
78
0: Variable (4 to 5) "x"
@@ -47,34 +48,35 @@ input_file: tests/for_break_continue.nu
4748
0: int
4849
1: int
4950
2: ()
50-
3: unknown
51-
4: unknown
52-
5: unknown
53-
6: unknown
54-
7: unknown
55-
8: unknown
56-
9: unknown
57-
10: unknown
58-
11: unknown
51+
3: int
52+
4: int
53+
5: int
54+
6: int
55+
7: list<int>
56+
8: int
57+
9: forbidden
58+
10: int
59+
11: bool
5960
12: unknown
6061
13: unknown
61-
14: unknown
62-
15: unknown
63-
16: unknown
64-
17: unknown
65-
18: unknown
62+
14: oneof<(), unknown>
63+
15: int
64+
16: forbidden
65+
17: int
66+
18: bool
6667
19: unknown
6768
20: unknown
68-
21: unknown
69-
22: unknown
70-
23: unknown
71-
24: unknown
72-
25: unknown
73-
26: unknown
74-
27: unknown
75-
28: unknown
76-
29: unknown
77-
30: unknown
69+
21: oneof<(), unknown>
70+
22: int
71+
23: forbidden
72+
24: int
73+
25: forbidden
74+
26: int
75+
27: int
76+
28: ()
77+
29: ()
78+
30: ()
7879
31: ()
7980
==== TYPE ERRORS ====
80-
Error (NodeId 30): unsupported ast node 'For { variable: NodeId(3), range: NodeId(7), block: NodeId(29) }' in typechecker
81+
Error (NodeId 12): unsupported ast node 'Break' in typechecker
82+
Error (NodeId 19): unsupported ast node 'Continue' in typechecker

0 commit comments

Comments
 (0)