Curious Boolean evaluation bug

44 views
Skip to first unread message

Yegor Kurbachev

unread,
May 8, 2022, 10:51:00 PM5/8/22
to j2objc-discuss
Hi!

Found a nice Boolean evaluation bug in J2ObjC. Here is my function:

    public int getColor(State state, boolean checked)
    {
      return isDark() == checked == foreground ? 0x000000 : 0xffffff;
    }

Here "isDark()" is whether dark theme is enabled; "checked" is whether the item is checked and "foreground" is whether foreground or background color is needed.
All of these boil down to black or white with a nice looking condition.   :)
Granted, it's not often that you would compare three Booleans like that, but it's an absolutely valid Java code.

This code works in Android and with GWT, but in J2ObjC it causes an "AssertionError: null".
To workaround the error I've simply surrounded the first equality in parenthesis, like this: "(isDark() == checked) == foreground" and it works fine.

Here is the J2ObjC call stack:
com.google.devtools.j2objc.ast.TreeVisitorError: /Users/Yegor/Dropbox/JVEsoft/Java/iOS/Alarm/Shared/com/jvesoft/custom/AdvancedColor.java:102: internal error translating "/Users/Yegor/Dropbox/JVEsoft/Java/iOS/Alarm/Shared/com/jvesoft/custom/AdvancedColor.java"
    at com.google.devtools.j2objc.translate.OperatorRewriter.getInfixFunction(OperatorRewriter.java:428)
    at com.google.devtools.j2objc.translate.OperatorRewriter.endVisit(OperatorRewriter.java:112)
    at com.google.devtools.j2objc.ast.InfixExpression.acceptInner(InfixExpression.java:148)
    at com.google.devtools.j2objc.ast.TreeNode.accept(TreeNode.java:95)
    at com.google.devtools.j2objc.ast.ChildLink.accept(ChildLink.java:80)
    at com.google.devtools.j2objc.ast.ConditionalExpression.acceptInner(ConditionalExpression.java:85)
    at com.google.devtools.j2objc.ast.TreeNode.accept(TreeNode.java:95)
    at com.google.devtools.j2objc.ast.ChildLink.accept(ChildLink.java:80)
    at com.google.devtools.j2objc.ast.ReturnStatement.acceptInner(ReturnStatement.java:52)
    at com.google.devtools.j2objc.ast.TreeNode.accept(TreeNode.java:95)
    at com.google.devtools.j2objc.ast.ChildLink.accept(ChildLink.java:80)
    at com.google.devtools.j2objc.ast.ChildList.accept(ChildList.java:90)
    at com.google.devtools.j2objc.ast.Block.acceptInner(Block.java:55)
    at com.google.devtools.j2objc.ast.TreeNode.accept(TreeNode.java:95)
    at com.google.devtools.j2objc.ast.ChildLink.accept(ChildLink.java:80)
    at com.google.devtools.j2objc.ast.MethodDeclaration.acceptInner(MethodDeclaration.java:135)
    at com.google.devtools.j2objc.ast.TreeNode.accept(TreeNode.java:95)
    at com.google.devtools.j2objc.ast.ChildLink.accept(ChildLink.java:80)
    at com.google.devtools.j2objc.ast.ChildList.accept(ChildList.java:90)
    at com.google.devtools.j2objc.ast.TypeDeclaration.acceptInner(TypeDeclaration.java:95)
    at com.google.devtools.j2objc.ast.TreeNode.accept(TreeNode.java:95)
    at com.google.devtools.j2objc.ast.ChildLink.accept(ChildLink.java:80)
    at com.google.devtools.j2objc.ast.ChildList.accept(ChildList.java:90)
    at com.google.devtools.j2objc.ast.CompilationUnit.acceptInner(CompilationUnit.java:181)
    at com.google.devtools.j2objc.ast.TreeNode.accept(TreeNode.java:95)
    at com.google.devtools.j2objc.ast.UnitTreeVisitor.run(UnitTreeVisitor.java:50)
    at com.google.devtools.j2objc.pipeline.TranslationProcessor.applyMutations(TranslationProcessor.java:286)
    at com.google.devtools.j2objc.pipeline.TranslationProcessor.processConvertedTree(TranslationProcessor.java:107)
    at com.google.devtools.j2objc.pipeline.FileProcessor.processCompiledSource(FileProcessor.java:154)
    at com.google.devtools.j2objc.pipeline.FileProcessor.access$000(FileProcessor.java:39)
    at com.google.devtools.j2objc.pipeline.FileProcessor$1.handleParsedUnit(FileProcessor.java:132)
    at com.google.devtools.j2objc.javac.JavacParser.parseFiles(JavacParser.java:205)
    at com.google.devtools.j2objc.pipeline.FileProcessor.processBatch(FileProcessor.java:137)
    at com.google.devtools.j2objc.pipeline.FileProcessor.processInputs(FileProcessor.java:67)
    at com.google.devtools.j2objc.pipeline.TranslationProcessor.processInputs(TranslationProcessor.java:83)
    at com.google.devtools.j2objc.J2ObjC.run(J2ObjC.java:127)
    at com.google.devtools.j2objc.J2ObjC.main(J2ObjC.java:176)

BTW — LOVE J2ObjC!!!
Thanks!

Tom Ball

unread,
May 9, 2022, 10:59:36 AM5/9/22
to j2objc-...@googlegroups.com
Please file an issue, so when this is fixed you'll be notified. Your example is perfect, as it can be easily included in a unit test. I wish more issues were reported this way!

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/j2objc-discuss/b9b890b6-83db-4b46-9dd8-cc37cd41aa52n%40googlegroups.com.

Yegor Kurbachev

unread,
May 9, 2022, 2:30:05 PM5/9/22
to j2objc-discuss
Done — https://github.com/google/j2objc/issues/1878 — apologies for not doing this upfront.
Reply all
Reply to author
Forward
0 new messages