Commit 6860eb9
committed
feat(typeck): support negative integer literal coercion
Implement proper handling of negative integer literals in the type
checker. Previously, negative literals like -42 were broken because:
1. The parser represents them as unary negation applied to a positive
literal, so type_of_lit only sees the positive value
2. The negativity flag in IntLiteral was always false
This commit fixes the issue by:
- Allowing unary negation on IntLiteral types (they can always be
negated since the result is just a negative literal)
- Propagating the negativity flag when applying unary negation to an
IntLiteral, flipping neg from false to true
- Not propagating the expected type through negation when targeting
signed types, to avoid premature type mismatch errors on the inner
expression
Also simplifies the coercion rule to use strict inequality for both
positive and negative literals, since TypeSize rounding means we can't
reliably distinguish edge cases in either direction.1 parent 16b0732 commit 6860eb9
3 files changed
Lines changed: 64 additions & 9 deletions
File tree
- crates/sema/src/typeck
- tests/ui/typeck
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
482 | 482 | | |
483 | 483 | | |
484 | 484 | | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
485 | 489 | | |
486 | 490 | | |
487 | | - | |
| 491 | + | |
488 | 492 | | |
| 493 | + | |
| 494 | + | |
489 | 495 | | |
490 | 496 | | |
491 | 497 | | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
492 | 504 | | |
493 | 505 | | |
494 | 506 | | |
| |||
1001 | 1013 | | |
1002 | 1014 | | |
1003 | 1015 | | |
1004 | | - | |
1005 | | - | |
1006 | | - | |
1007 | | - | |
1008 | | - | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
1009 | 1032 | | |
1010 | | - | |
1011 | 1033 | | |
1012 | | - | |
| 1034 | + | |
1013 | 1035 | | |
1014 | 1036 | | |
1015 | 1037 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
44 | 65 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
20 | 32 | | |
0 commit comments