Skip to content

Commit

Permalink
Apply SectionsToBinOp pass to arguments of Section (#8655)
Browse files Browse the repository at this point in the history
Fixes #8436 by making sure that `SectionsToBinOp` pass is applied also to arguments of `Section`.
  • Loading branch information
JaroslavTulach authored Jan 3, 2024
1 parent 2e7d71d commit 5e3480b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ case object SectionsToBinOp extends IRPass {
)

ir.transformExpressions { case sec: Section =>
desugarSections(sec, freshNameSupply)
desugarSections(sec, freshNameSupply, inlineContext)
}
}

Expand All @@ -97,7 +97,8 @@ case object SectionsToBinOp extends IRPass {
*/
private def desugarSections(
section: Section,
freshNameSupply: FreshNameSupply
freshNameSupply: FreshNameSupply,
inlineContext: InlineContext
): Expression = {
section match {
case Section.Left(arg, op, loc, passData, diagnostics) =>
Expand Down Expand Up @@ -145,9 +146,11 @@ case object SectionsToBinOp extends IRPass {
)

} else {
val newArg = arg.mapExpressions(runExpression(_, inlineContext))

Application.Prefix(
function = op,
arguments = List(arg),
arguments = List(newArg),
hasDefaultsSuspended = false,
location = loc,
passData,
Expand Down Expand Up @@ -265,9 +268,11 @@ case object SectionsToBinOp extends IRPass {
loc
)
} else {
val newArg = arg.mapExpressions(runExpression(_, inlineContext))

val opCall = Application.Prefix(
function = op,
arguments = List(leftCallArg, arg),
arguments = List(leftCallArg, newArg),
hasDefaultsSuspended = false,
location = None,
passData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,46 @@ public void testCaseOfWithNegativeConstant() throws Exception {
assertEquals("none", none.asString());
}

@Test
public void testDesugarOperators() throws Exception {
var module = ctx.eval("enso", """
main =
ma ==ums==
""");
try {
var run = module.invokeMember("eval_expression", "main");
fail("Unexpected result: " + run);
} catch (PolyglotException ex) {
assertEquals("Compile error: The name `ma` could not be found.", ex.getMessage());
}
}

@Test
public void testDesugarOperatorsLeftRight() throws Exception {
var module = ctx.eval("enso", """
main = (+ (2 *))
""");
try {
var run = module.invokeMember("eval_expression", "main");
fail("Unexpected result: " + run);
} catch (PolyglotException ex) {
assertEquals("Method `+` of type Unnamed could not be found.", ex.getMessage());
}
}

@Test
public void testDesugarOperatorsRightLeft() throws Exception {
var module = ctx.eval("enso", """
main = ((* 2) +)
""");
try {
var run = module.invokeMember("eval_expression", "main");
fail("Unexpected result: " + run);
} catch (PolyglotException ex) {
assertEquals("Method `+` of type Function could not be found.", ex.getMessage());
}
}

@Test
public void testHalfAssignment() throws Exception {
var module =
Expand Down

0 comments on commit 5e3480b

Please sign in to comment.