From e3adde368b8e1080767219d2173b871a290ae17e Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Fri, 10 Apr 2026 10:43:28 -0700 Subject: [PATCH] Fix aliasAs generating wrong type for the alias variable. When using Elm.Arg.aliasAs on a record pattern, the alias variable was given a fresh type variable instead of inheriting the underlying pattern's type. Before: Arg.record ... |> Arg.field "name" |> Arg.aliasAs "person" generated: { name : name, age : age } -> ( name, person ) (person is wrong - should be the record type) After: generated: { name : name, age : age } -> ( name, { name : name, age : age } ) Root cause: aliasAs was calling `val` which creates a fresh `GenericType` for the alias name. The fix uses the inner pattern's annotation (the actual record/tuple/etc type) for the alias value. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/Internal/Arg.elm | 16 ++++++++++++++-- tests/TypeChecking.elm | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Internal/Arg.elm b/src/Internal/Arg.elm index 8b2f574..eb8b901 100644 --- a/src/Internal/Arg.elm +++ b/src/Internal/Arg.elm @@ -182,8 +182,20 @@ aliasAs aliasName (Arg toArgDetails) = innerArgDetails = toArgDetails index - ( aliasVal, _, _ ) = - val innerArgDetails.index aliasName + -- The alias refers to the same value as the underlying + -- pattern, so it should have the same type. Use the + -- inner pattern's annotation (the record type, tuple + -- type, etc.) rather than creating a fresh type variable. + aliasVal : Compiler.Expression + aliasVal = + Compiler.Expression <| + \_ -> + { expression = + Exp.FunctionOrValue [] + (Format.sanitize aliasName) + , annotation = innerArgDetails.details.annotation + , imports = [] + } in { details = { imports = innerArgDetails.details.imports diff --git a/tests/TypeChecking.elm b/tests/TypeChecking.elm index 25a9ab3..1517698 100644 --- a/tests/TypeChecking.elm +++ b/tests/TypeChecking.elm @@ -271,6 +271,26 @@ generatedCode = ( 1 + 2, x ) """ ] + , describe "aliasAs pattern type" + [ test "aliasAs on record pattern uses the underlying record type" <| + \_ -> + Elm.Declare.fn "describe" + (Arg.record (\a b -> ( a, b )) + |> Arg.field "name" + |> Arg.field "age" + |> Arg.aliasAs "person" + ) + (\( ( name, _ ), person ) -> + Elm.tuple name person + ) + |> .declaration + |> Elm.Expect.declarationAs + """ + describe : { name : name, age : age } -> ( name, { name : name, age : age } ) + describe ({ name, age } as person) = + ( name, person ) + """ + ] , test "Triple with mixed Float and Int infers correct types" <| \_ -> Elm.declaration "myTriple"