Artifact 99612301dbc44272e8a2a012f06976cb49974601a8209ca6dcaaa2f304f1d03f:
- File typeparse/tests_tclsh/t66_variantes_meme_forme.tcl — part of check-in [b00991eb89] at 2026-10-23 11:07:57 on branch tip-764_tcl90 — Let’s address the point: "Variants with the same form—if multiple equally strict variants are applicable, trigger an error; the error message should suggest `{variant}value` and `set {S}v(variant) value`, while ensuring the deduction defaults to the strictest variant." This is now implemented: v15m is included in `typeparse.zip`. The item has been marked as complete in the project schedule and noted in `noyau/LISEZMOI.md` as well as `doc/ANNOTATIONS.fr.md` (section 8; it has also been removed from section 17). Regarding the error message: The deduction logic selects the strictest variant (e.g., given `{int}i` and `{string}s`, the value `5` resolves to `i`, as before). If multiple equally strict variants are applicable, an error is raised: `set {A}v 5` ;# the value "5" fits variants `i` and `j` of `A` equally: write `{i}5` or `set {A}v(i) 5` The suggested syntax `set {A}v(i) 5` includes the variable name and only appears when an assignment is made directly to the sum type itself. In the context of a list or a product, the message simply suggests `{i}7`. The same logic applies to variants that have no defined precedence relative to one another (e.g., `{P}p` and `{Q}q` with `{1 2}`). For identical anonymous variants (e.g., `{int}`, `{int}`), since there are no labels to reference, the message instructs the user to "name the variants to choose one." A discovery made along the way: With this error check in place, many previously valid cases began to fail. In v15, these cases had passed because the chosen variant was silently discarded, defaulting back to `i`. This occurred in scenarios such as: assignments within a list (`set {list.A}L $v`); assignments within a product (`set {R}r $v 1`); operations like `incr v`, `set v 8`, `lset`, or `set L(0) …`; and type casts—such as `A {j}5` or even `B {s}5`—which previously resolved to `B.i`. I have also addressed these instances of lost information: Lists and products: an element or field whose content alone does not uniquely identify its variant now retains its annotation. (The validation process was already re-reading this information.) Type casts: the label beneath the type of a sum now explicitly names the variant. A design decision to confirm: for a modification (e.g., `set v 8`, `incr v`, `set L(0) 4`, `lset L 0 9`, `set r(a) 7`), I have configured it to preserve the current variant when multiple variants are equally valid. Otherwise, an operation like `incr v` on an `A.j` would trigger an error. If you would prefer to have an error in all such cases—based on the principle that "a modification is a computation"—this can be changed by removing four lines of code. Tests against v15: The same differences as before, plus `t65`, with no other changes. All expected `.out` outputs pass. The Tcl tests—`set`, `var`, `upvar`, `lset`, `list`, `lindex`, `append`, and `incr`—result in 1,234 successes out of 1,251, with 17 skipped and zero failures (across both trees). `valgrind` reports no issues for the new test cases. New test added: `t66_variantes_meme_forme`. (user: Florentis, size: 2603) [annotate] [blame] [check-ins using]
# t66 : variantes de même forme. Déduction par la plus stricte ; si plusieurs # variantes également strictes conviennent : erreur qui propose {variante}valeur # (et set {S}v(variante) valeur). La variante choisie survit dans les listes, # les produits, et aux modifications (la variante courante départage). proc t {l s} { set rc [catch {uplevel 1 $s} m]; puts [format "%-30s %s %s" $l $rc $m] } type def A {{int}i, {int}j} type def B {{int}i, {double}d, {string}s} type def P {{int}x; {int}y} type def Q {{int}a; {int}b} type def C {{P}p, {Q}q} type def W {{int}, {int}} type def R {{A}a; {int}n} puts "-- déduction" t "B 5 : int, la plus stricte" {set {B}v 5; type get $v} t "B 2.5" {set {B}v 2.5; type get $v} t "B abc" {set {B}v abc; type get $v} t "A 5 : i et j" {set {A}v 5} t "C {1 2} : p et q" {set {C}v {1 2}} t "W 5 : anonymes" {set {W}v 5} t "A {j}5" {set {A}v {j}5; type get $v} t "A v(j) 5" {set {A}v(j) 5; type get $v} t "type cast A 5" {type cast A 5} t "type cast A {j}5" {type get [type cast A {j}5]} t "type cast B {s}5" {type get [type cast B {s}5]} set {A}v {j}5 puts "-- la variante survit" t "copie" {set {A}w $v; type get $w} t "liste" {set {list.A}L $v {i}6; lmap e $L {type get $e}} t "liste nue" {set {list.A}L 7} t "lappend" {set {list.A}L; lappend L $v; type get [lindex $L 0]} t "lappend nu" {set {list.A}L; lappend L 3} t "produit" {set {R}r $v 1; list $r [type get $r(a)]} t "produit {a}" {set {R}r {a}$v {n}1; type get $r(a)} t "B {s}5 dans une liste" {set {B}b {s}5; set {list.B}M $b; type get [lindex $M 0]} puts "-- modifications : la variante courante départage" t "set v 8" {set {A}v {j}5; set v 8; list $v [type get $v]} t "incr v" {set {A}v {j}5; incr v; list $v [type get $v]} t "set v(i) 8" {set {A}v {j}5; set v(i) 8; type get $v} t "set v {i}8" {set {A}v {j}5; set v {i}8; type get $v} t "L(0) 4" {set {A}u {j}5; set {list.A}L $u; set L(0) 4; type get [lindex $L 0]} t "lset L 0 9" {set {A}u {j}5; set {list.A}L $u; lset L 0 9; type get [lindex $L 0]} t "r(a) 7" {set {A}u {j}5; set {R}r $u 1; set r(a) 7; type get $r(a)} t "r(n) 2" {set {A}u {j}5; set {R}r $u 1; set r(n) 2; type get $r(a)}