Commit 61f662b
committed
v3: accept bare generic struct literals where a concrete instance is expected
`fn make() Box[int] { return Box{...} }` (and the `&Box{...}` heap form, and
bare literals passed/assigned where a concrete generic instance is expected)
previously failed the checker with `cannot return Box as Box[int]`, because the
return/assignment path resolves the value through `resolve_expr(expected)` and
that had no `struct_init` case — a bare `Box{...}` stayed the unspecialised base
`Box`, incompatible with `Box[int]`.
resolve_expr now adopts a matching concrete expected instance for a bare generic
struct literal (`bare_generic_literal_adopts`: base short-names match and the
base is a known generic struct), covering both the value and `&` heap forms. The
cgen side already materialises the concrete `Box_int` / `(Box_int*)memdup(...)`
in return position via the `in_return`/`cur_fn_ret` fallback.
To avoid silently adopting an unrelated literal (and emitting broken C), each
named field initializer is checked against the instantiation
(`generic_literal_fields_compatible`, substituting the type params); a definite
mismatch like `Box{v: 'str'}` for `Box[int]` is rejected as a type error rather
than reaching the C compiler, while valid coercions (`Vec[f64]{x: 1}`) are kept.
Adds regression tests (value/heap/multi-param adoption; field mismatch rejected).
Verified: matches V1, self-host v5 == v6 byte-identical, no vlib/v3/tests
regressions, doka still builds and renders.1 parent 2e3655c commit 61f662b
2 files changed
Lines changed: 101 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
439 | 439 | | |
440 | 440 | | |
441 | 441 | | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3484 | 3484 | | |
3485 | 3485 | | |
3486 | 3486 | | |
| 3487 | + | |
| 3488 | + | |
| 3489 | + | |
| 3490 | + | |
| 3491 | + | |
| 3492 | + | |
| 3493 | + | |
| 3494 | + | |
| 3495 | + | |
| 3496 | + | |
| 3497 | + | |
| 3498 | + | |
| 3499 | + | |
| 3500 | + | |
| 3501 | + | |
| 3502 | + | |
| 3503 | + | |
| 3504 | + | |
| 3505 | + | |
| 3506 | + | |
| 3507 | + | |
| 3508 | + | |
| 3509 | + | |
| 3510 | + | |
| 3511 | + | |
| 3512 | + | |
| 3513 | + | |
| 3514 | + | |
| 3515 | + | |
| 3516 | + | |
| 3517 | + | |
| 3518 | + | |
| 3519 | + | |
| 3520 | + | |
| 3521 | + | |
| 3522 | + | |
| 3523 | + | |
| 3524 | + | |
| 3525 | + | |
| 3526 | + | |
| 3527 | + | |
| 3528 | + | |
| 3529 | + | |
| 3530 | + | |
| 3531 | + | |
| 3532 | + | |
| 3533 | + | |
| 3534 | + | |
| 3535 | + | |
| 3536 | + | |
| 3537 | + | |
| 3538 | + | |
| 3539 | + | |
| 3540 | + | |
| 3541 | + | |
| 3542 | + | |
| 3543 | + | |
| 3544 | + | |
| 3545 | + | |
| 3546 | + | |
| 3547 | + | |
| 3548 | + | |
| 3549 | + | |
| 3550 | + | |
3487 | 3551 | | |
3488 | 3552 | | |
3489 | 3553 | | |
| |||
4736 | 4800 | | |
4737 | 4801 | | |
4738 | 4802 | | |
| 4803 | + | |
| 4804 | + | |
| 4805 | + | |
| 4806 | + | |
| 4807 | + | |
| 4808 | + | |
| 4809 | + | |
| 4810 | + | |
| 4811 | + | |
| 4812 | + | |
| 4813 | + | |
| 4814 | + | |
| 4815 | + | |
| 4816 | + | |
| 4817 | + | |
| 4818 | + | |
| 4819 | + | |
4739 | 4820 | | |
4740 | 4821 | | |
4741 | 4822 | | |
| |||
0 commit comments