Skip to content

Commit fa765dc

Browse files
authored
Merge pull request #8213 from FabHof/main
2 parents 26faf0e + 1fe3c54 commit fa765dc

5 files changed

Lines changed: 30 additions & 31 deletions

File tree

src/check/Check.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,11 @@ fn checkBinopExpr(self: *Self, expr_idx: CIR.Expr.Idx, expr_region: Region, bino
18231823
var does_fx = try self.checkExpr(binop.lhs);
18241824
does_fx = try self.checkExpr(binop.rhs) or does_fx;
18251825

1826+
// For equality/comparison, operands must be the same type
1827+
const lhs_var = @as(Var, @enumFromInt(@intFromEnum(binop.lhs)));
1828+
const rhs_var = @as(Var, @enumFromInt(@intFromEnum(binop.rhs)));
1829+
_ = try self.unify(lhs_var, rhs_var);
1830+
18261831
// Comparison operators always return Bool
18271832
const expr_var = @as(Var, @enumFromInt(@intFromEnum(expr_idx)));
18281833
const fresh_bool = try self.instantiateVarAnon(ModuleEnv.varFrom(can.Can.BUILTIN_BOOL_TYPE), .{ .explicit = expr_region });

src/check/unify.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,21 @@ fn Unifier(comptime StoreTypeB: type) type {
15761576
.int => return error.TypeMismatch,
15771577
}
15781578
},
1579+
.num_unbound => |b_num_unbound| {
1580+
// Check if the compact type satisfies the requirements
1581+
switch (a_num_compact) {
1582+
.int => |int_prec| {
1583+
const result = self.checkIntPrecisionRequirements(int_prec, b_num_unbound);
1584+
switch (result) {
1585+
.ok => {},
1586+
.negative_unsigned => return error.NegativeUnsignedInt,
1587+
.too_large => return error.NumberDoesNotFit,
1588+
}
1589+
},
1590+
.frac => return error.TypeMismatch,
1591+
}
1592+
self.merge(vars, vars.a.desc.content);
1593+
},
15791594
else => return error.TypeMismatch,
15801595
}
15811596
},

test/snapshots/can_list_number_doesnt_fit.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,19 @@ type=expr
88
[1u8, 2u8, 300]
99
~~~
1010
# EXPECTED
11-
INCOMPATIBLE LIST ELEMENTS - can_list_number_doesnt_fit.md:1:7:1:7
11+
NUMBER DOES NOT FIT IN TYPE - can_list_number_doesnt_fit.md:1:12:1:15
1212
# PROBLEMS
13-
**INCOMPATIBLE LIST ELEMENTS**
14-
The second and third elements in this list have incompatible types:
15-
**can_list_number_doesnt_fit.md:1:7:**
13+
**NUMBER DOES NOT FIT IN TYPE**
14+
The number **300** does not fit in its inferred type:
15+
**can_list_number_doesnt_fit.md:1:12:1:15:**
1616
```roc
1717
[1u8, 2u8, 300]
1818
```
19-
^^^ ^^^
19+
^^^
2020
21-
The second element has this type:
21+
Its inferred type is:
2222
_U8_
2323
24-
However, the third element has this type:
25-
_Num(_size)_
26-
27-
All elements in a list must have compatible types.
28-
29-
Note: You can wrap each element in a tag to make them compatible.
30-
To learn about tags, see <https://www.roc-lang.org/tutorial#tags>
31-
3224
# TOKENS
3325
~~~zig
3426
OpenSquare(1:1-1:2),Int(1:2-1:5),Comma(1:5-1:6),Int(1:7-1:10),Comma(1:10-1:11),Int(1:12-1:15),CloseSquare(1:15-1:16),

test/snapshots/if_then_else/if_then_else_nested_chain.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ NO CHANGE
131131
~~~clojure
132132
(inferred-types
133133
(defs
134-
(patt @3.1-3.12 (type "_arg -> Str")))
134+
(patt @3.1-3.12 (type "Num(_size) -> Str")))
135135
(expressions
136-
(expr @3.15-13.2 (type "_arg -> Str"))))
136+
(expr @3.15-13.2 (type "Num(_size) -> Str"))))
137137
~~~

test/snapshots/type_record_basic.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,9 @@ getName = |_person| "hello"
1313
main! = |_| getName({name: "luke", age:21})
1414
~~~
1515
# EXPECTED
16-
TYPE MISMATCH - type_record_basic.md:6:21:6:43
16+
NIL
1717
# PROBLEMS
18-
**TYPE MISMATCH**
19-
The first argument being passed to this function has the wrong type:
20-
**type_record_basic.md:6:21:6:43:**
21-
```roc
22-
main! = |_| getName({name: "luke", age:21})
23-
```
24-
^^^^^^^^^^^^^^^^^^^^^^
25-
26-
This argument has the type:
27-
_{ name: Str, age: Num(_size) }_
28-
29-
But the function needs the first argument to be:
30-
_{ age: U64, name: Str }_
31-
18+
NIL
3219
# TOKENS
3320
~~~zig
3421
KwApp(1:1-1:4),OpenSquare(1:5-1:6),LowerIdent(1:6-1:11),CloseSquare(1:11-1:12),OpenCurly(1:13-1:14),LowerIdent(1:15-1:17),OpColon(1:17-1:18),KwPlatform(1:19-1:27),StringStart(1:28-1:29),StringPart(1:29-1:50),StringEnd(1:50-1:51),CloseCurly(1:52-1:53),

0 commit comments

Comments
 (0)