Skip to content

Commit

Permalink
Avoid stackoverflow in Atom.toString by not printing the self argumen…
Browse files Browse the repository at this point in the history
…t of to_text function
  • Loading branch information
JaroslavTulach committed Aug 23, 2023
1 parent b1d58d9 commit ea23fc9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class ReplTest
result.toString shouldEqual "Error in method `to_text` of [Bar 1]: Expected Text but got 42"
}
inside(executor.evaluate("C.Baz 1")) { case Right(result) =>
result.toString shouldEqual "Error in method `to_text` of [Baz 1]: Expected Text but got C.to_text[Test:18-40]"
result.toString shouldEqual "Error in method `to_text` of [Baz 1]: Expected Text but got C.to_text[Test:18:1-29]"
}
inside(executor.evaluate("Pattern.compile 'foo'")) {
case Right(result) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ Text toDisplayString(
} else if (TypesGen.isText(result)) {
return TypesGen.asText(result);
} else {
msg = this.toString("Error in method `to_text` of [", 10, "]: Expected Text but got ", result);
var txt = result instanceof Function fn ? fn.toString(false) : result.toString();
msg = this.toString("Error in method `to_text` of [", 10, "]: Expected Text but got ", txt);
}
} catch (AbstractTruffleException | UnsupportedMessageException | ArityException | UnknownIdentifierException |
UnsupportedTypeException panic) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,12 @@ String toDisplayString(boolean sideEffects) {
}

@Override
@CompilerDirectives.TruffleBoundary
public String toString() {
return toString(true);
}

@CompilerDirectives.TruffleBoundary
public final String toString(boolean includeArguments) {
var n = callTarget.getRootNode();
var ss = n.getSourceSection();
if (ss == null) {
Expand All @@ -422,19 +426,22 @@ public String toString() {
sb.append("-").append(end);
}
sb.append("]");
for (var i = 0; i < schema.getArgumentsCount(); i++) {
sb.append(" ").append(schema.getArgumentInfos()[i].getName()).append("=");
if (preAppliedArguments != null && preAppliedArguments[i] != null) {
sb.append(iop.toDisplayString(preAppliedArguments[i], false));
} else {
sb.append("_");
if (includeArguments) {
for (var i = 0; i < schema.getArgumentsCount(); i++) {
var name = schema.getArgumentInfos()[i].getName();
sb.append(" ").append(name).append("=");
if (preAppliedArguments != null && preAppliedArguments[i] != null) {
sb.append(iop.toDisplayString(preAppliedArguments[i], false));
} else {
sb.append("_");
}
}
}
if (schema.getOversaturatedArguments() != null) {
for (var i = 0; i < schema.getOversaturatedArguments().length; i++) {
if (oversaturatedArguments != null && oversaturatedArguments[i] != null) {
sb.append(" +").append(schema.getOversaturatedArguments()[i].getName()).append("=");
sb.append(iop.toDisplayString(oversaturatedArguments[i], false));
if (schema.getOversaturatedArguments() != null) {
for (var i = 0; i < schema.getOversaturatedArguments().length; i++) {
if (oversaturatedArguments != null && oversaturatedArguments[i] != null) {
sb.append(" +").append(schema.getOversaturatedArguments()[i].getName()).append("=");
sb.append(iop.toDisplayString(oversaturatedArguments[i], false));
}
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion test/Tests/src/Semantic/Error_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,19 @@ spec =
Test.specify "try to apply two arguments with over-saturated" <|
r = Panic.recover Type_Error <| neg (my_func z=10)
r . should_fail_with Type_Error
r.to_display_text.contains "Try to apply x, y arguments" . should_be_true
r.to_display_text . should_contain "Try to apply x, y arguments"

Test.specify "types and unapplied arguments" <|
c = C.Baz C.to_text
r = Panic.recover Type_Error <| neg (c.to_num c=3)
r . should_fail_with Type_Error
r.to_display_text . should_contain "Try to apply a, b arguments"



type C
Baz x

C.to_num self a b c = a+b+c

main = Test_Suite.run_main spec

0 comments on commit ea23fc9

Please sign in to comment.