Skip to content

Commit 8731fb6

Browse files
authored
Merge pull request #136 from dillonkearns/fix/aliasAs-type
Fix aliasAs generating wrong type for the alias variable
2 parents f963186 + 2f45bd9 commit 8731fb6

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/Internal/Arg.elm

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,20 @@ aliasAs aliasName (Arg toArgDetails) =
182182
innerArgDetails =
183183
toArgDetails index
184184

185-
( aliasVal, _, _ ) =
186-
val innerArgDetails.index aliasName
185+
-- The alias refers to the same value as the underlying
186+
-- pattern, so it should have the same type. Use the
187+
-- inner pattern's annotation (the record type, tuple
188+
-- type, etc.) rather than creating a fresh type variable.
189+
aliasVal : Compiler.Expression
190+
aliasVal =
191+
Compiler.Expression <|
192+
\_ ->
193+
{ expression =
194+
Exp.FunctionOrValue []
195+
(Format.sanitize aliasName)
196+
, annotation = innerArgDetails.details.annotation
197+
, imports = []
198+
}
187199
in
188200
{ details =
189201
{ imports = innerArgDetails.details.imports

tests/TypeChecking.elm

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ generatedCode =
272272
( 1 + 2, x )
273273
"""
274274
]
275+
, describe "aliasAs pattern type"
276+
[ test "aliasAs on record pattern uses the underlying record type" <|
277+
\_ ->
278+
Elm.Declare.fn "describe"
279+
(Arg.record (\a b -> ( a, b ))
280+
|> Arg.field "name"
281+
|> Arg.field "age"
282+
|> Arg.aliasAs "person"
283+
)
284+
(\( ( name, _ ), person ) ->
285+
Elm.tuple name person
286+
)
287+
|> .declaration
288+
|> Elm.Expect.declarationAs
289+
"""
290+
describe : { name : name, age : age } -> ( name, { name : name, age : age } )
291+
describe ({ name, age } as person) =
292+
( name, person )
275293
, test "Elm.Let.fn declaration has a type annotation" <|
276294
\_ ->
277295
Elm.declaration "useLetFn"

0 commit comments

Comments
 (0)