Skip to content

Commit

Permalink
Fix bug in the compilation of nested case-of expressions
Browse files Browse the repository at this point in the history
Fix a bug in the Elm compiler that caused defective code to be emitted in some cases where a case-of expression was contained in another case-of expression.
Also, add a regression test with a scenario that failed in the previous compiler implementation.
  • Loading branch information
Viir committed May 9, 2024
1 parent ef72746 commit 09f08c7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
38 changes: 23 additions & 15 deletions implement/pine/ElmTime/compile-elm-program/src/FirCompiler.elm
Original file line number Diff line number Diff line change
Expand Up @@ -908,17 +908,31 @@ parseFunctionParameters expression =

emitReferenceExpression : String -> EmitStack -> Result String Pine.Expression
emitReferenceExpression name compilation =
case
emitApplyFunctionFromCurrentEnvironment
compilation
{ functionName = name }
[]
of
Just functionApplicationResult ->
functionApplicationResult
{-
Prioritize environmentDeconstructions before environmentFunctions here to
support shadowing for function parameters.
A source language like Elm does not support shadowing anyway, but the current
implementation of the Elm compiler sometimes lowers to Fir code that introduces declarations,
which can result in shadowing when nested.
An example is the `pseudoParamName` in `compileElmSyntaxCaseBlock`
-}
case Dict.get name compilation.environmentDeconstructions of
Just deconstruction ->
Ok
(pineExpressionForDeconstructions deconstruction
(listItemFromIndexExpression_Pine 1 Pine.environmentExpr)
)

Nothing ->
case Dict.get name compilation.environmentDeconstructions of
case
emitApplyFunctionFromCurrentEnvironment
compilation
{ functionName = name }
[]
of
Just functionApplicationResult ->
functionApplicationResult

Nothing ->
Err
(String.join ""
Expand All @@ -935,12 +949,6 @@ emitReferenceExpression name compilation =
]
)

Just deconstruction ->
Ok
(pineExpressionForDeconstructions deconstruction
(listItemFromIndexExpression_Pine 1 Pine.environmentExpr)
)


listTransitiveDependenciesOfExpression : EmitStack -> Expression -> Set.Set String
listTransitiveDependenciesOfExpression dependenciesRelations expression =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ gamma = 79 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
let
first_case_of list =
case list of
[] ->
Err "is empty"

_ ->
Ok { alfa = [ { beta = 79 } ] }

second_case_of list =
case list of
[] ->
Err "is empty"

first :: _ ->
Ok { gamma = first.beta }

in
case first_case_of [ 1, 3 ] of
Err _ ->
"err in first"

Ok firstRecord ->
case second_case_of firstRecord.alfa of
Err _ ->
"err in second"

Ok secondRecord ->
secondRecord

0 comments on commit 09f08c7

Please sign in to comment.