Commit 037844c
committed
Auto merge of #15271 - lowr:patch/re-castable, r=HKalbasi
Properly infer types with type casts
This PR reenables `Expectation::Castable` (previous attempt at #14104, reverted by #14120) and implements type cast checks, which enable us to infer a bit more.
Castable expectations are relatively weak -- they only influence the inference if we cannot infer the types by other means. Therefore, we need to defer possible type unification with the casted type until we type check all expressions of the body. This PR adds a struct and slots in `InferenceContext` for the deferred cast checks (c.f. [`CastCheck`] in `rustc_hir_typeck`).
I only implemented the bits that affect the inference result. It should be possible to return type adjustments for well-formed casts and report diagnostics for invalid casts, but I'm leaving them for future work for now.
Fixes #11571
Fixes #15246
[`CastCheck`]: https://github.com/rust-lang/rust/blob/da1d099f91ea387a2814a6244dd875a2048b486f/compiler/rustc_hir_typeck/src/cast.rs#L55File tree
5 files changed
+112
-28
lines changed- crates/hir-ty/src
- infer
- tests
5 files changed
+112
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
16 | 25 | | |
17 | 26 | | |
18 | 27 | | |
| |||
60 | 69 | | |
61 | 70 | | |
62 | 71 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 72 | + | |
| 73 | + | |
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| |||
508 | 510 | | |
509 | 511 | | |
510 | 512 | | |
| 513 | + | |
| 514 | + | |
511 | 515 | | |
512 | 516 | | |
513 | 517 | | |
| |||
582 | 586 | | |
583 | 587 | | |
584 | 588 | | |
585 | | - | |
| 589 | + | |
| 590 | + | |
586 | 591 | | |
587 | 592 | | |
588 | 593 | | |
| |||
594 | 599 | | |
595 | 600 | | |
596 | 601 | | |
597 | | - | |
| 602 | + | |
598 | 603 | | |
599 | 604 | | |
600 | 605 | | |
| |||
622 | 627 | | |
623 | 628 | | |
624 | 629 | | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
625 | 637 | | |
626 | 638 | | |
627 | 639 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
| 49 | + | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
574 | 574 | | |
575 | 575 | | |
576 | 576 | | |
577 | | - | |
578 | | - | |
579 | | - | |
580 | | - | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
585 | | - | |
586 | | - | |
| 577 | + | |
| 578 | + | |
587 | 579 | | |
588 | 580 | | |
589 | 581 | | |
| |||
1592 | 1584 | | |
1593 | 1585 | | |
1594 | 1586 | | |
1595 | | - | |
| 1587 | + | |
1596 | 1588 | | |
1597 | 1589 | | |
1598 | 1590 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1978 | 1978 | | |
1979 | 1979 | | |
1980 | 1980 | | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3513 | 3513 | | |
3514 | 3514 | | |
3515 | 3515 | | |
3516 | | - | |
3517 | 3516 | | |
3518 | 3517 | | |
3519 | 3518 | | |
| |||
3538 | 3537 | | |
3539 | 3538 | | |
3540 | 3539 | | |
3541 | | - | |
3542 | | - | |
| 3540 | + | |
| 3541 | + | |
3543 | 3542 | | |
3544 | | - | |
| 3543 | + | |
3545 | 3544 | | |
3546 | 3545 | | |
3547 | 3546 | | |
| |||
3577 | 3576 | | |
3578 | 3577 | | |
3579 | 3578 | | |
| 3579 | + | |
| 3580 | + | |
| 3581 | + | |
| 3582 | + | |
| 3583 | + | |
| 3584 | + | |
| 3585 | + | |
| 3586 | + | |
| 3587 | + | |
| 3588 | + | |
| 3589 | + | |
| 3590 | + | |
| 3591 | + | |
| 3592 | + | |
| 3593 | + | |
3580 | 3594 | | |
3581 | 3595 | | |
3582 | 3596 | | |
| |||
0 commit comments