@@ -142,6 +142,7 @@ import Elm exposing (Expression)
142142import Elm.Syntax.Expression as Exp
143143import Elm.Syntax.Node as Node
144144import Elm.Syntax.Pattern as Pattern
145+ import Elm.Syntax.TypeAnnotation as Annotation
145146import Internal.Arg
146147import Internal.Compiler as Compiler exposing (Module )
147148import Internal.Index as Index
@@ -335,10 +336,13 @@ fn desiredName arg toInnerFn sourceLet =
335336 Elm . apply
336337 ( Compiler . Expression
337338 ( \ _ ->
338- { innerFnDetails
339- | expression =
340- Exp . FunctionOrValue []
341- name
339+ { expression =
340+ Exp . FunctionOrValue [] name
341+ , annotation =
342+ letFnAnnotation
343+ [ argDetails. details. annotation ]
344+ innerFnDetails. annotation
345+ , imports = innerFnDetails. imports
342346 }
343347 )
344348 )
@@ -349,6 +353,45 @@ fn desiredName arg toInnerFn sourceLet =
349353 )
350354
351355
356+ {- | Build the function type annotation for a let-bound function's
357+ reference expression. Takes the arg annotations (in order) and the
358+ body's annotation, and produces `arg1 -> arg2 -> ... -> body`.
359+
360+ This is needed because `Elm.apply` needs a proper function type
361+ annotation to derive the return type when calling the let-bound
362+ function. Without this, the call would use the body's annotation
363+ directly, which is the return type rather than a function type.
364+ -}
365+ letFnAnnotation :
366+ List ( Result ( List Compiler . InferenceError ) Compiler . Inference )
367+ -> Result ( List Compiler . InferenceError ) Compiler . Inference
368+ -> Result ( List Compiler . InferenceError ) Compiler . Inference
369+ letFnAnnotation argAnnotations bodyAnnotation =
370+ List . foldr
371+ ( \ argResult resultSoFar ->
372+ Result . map2
373+ ( \ argAnn soFar ->
374+ { type_ =
375+ Annotation . FunctionTypeAnnotation
376+ ( Compiler . nodify argAnn. type_)
377+ ( Compiler . nodify soFar. type_)
378+ , inferences =
379+ Compiler . mergeInferences
380+ argAnn. inferences
381+ soFar. inferences
382+ , aliases =
383+ Compiler . mergeAliases
384+ argAnn. aliases
385+ soFar. aliases
386+ }
387+ )
388+ argResult
389+ resultSoFar
390+ )
391+ bodyAnnotation
392+ argAnnotations
393+
394+
352395{- | -}
353396fn2 :
354397 String
@@ -399,10 +442,15 @@ fn2 desiredName argOne argTwo toInnerFn sourceLet =
399442 Elm . apply
400443 ( Compiler . Expression
401444 ( \ _ ->
402- { innerFnDetails
403- | expression =
404- Exp . FunctionOrValue []
405- name
445+ { expression =
446+ Exp . FunctionOrValue [] name
447+ , annotation =
448+ letFnAnnotation
449+ [ argOneDetails. details. annotation
450+ , argTwoDetails. details. annotation
451+ ]
452+ innerFnDetails. annotation
453+ , imports = innerFnDetails. imports
406454 }
407455 )
408456 )
@@ -473,10 +521,16 @@ fn3 desiredName argOne argTwo argThree toInnerFn sourceLet =
473521 Elm . apply
474522 ( Compiler . Expression
475523 ( \ _ ->
476- { innerFnDetails
477- | expression =
478- Exp . FunctionOrValue []
479- name
524+ { expression =
525+ Exp . FunctionOrValue [] name
526+ , annotation =
527+ letFnAnnotation
528+ [ argOneDetails. details. annotation
529+ , argTwoDetails. details. annotation
530+ , argThreeDetails. details. annotation
531+ ]
532+ innerFnDetails. annotation
533+ , imports = innerFnDetails. imports
480534 }
481535 )
482536 )
0 commit comments