Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8353010

Enhance com.sun.tools.javac.jvm.Gen to provide more specific error information

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      across all OS
      Java version used: 17.0.8

      A DESCRIPTION OF THE PROBLEM :

      The Java compiler (javac), specifically the com.sun.tools.javac.jvm.Gen class, can encounter a StackOverflowError during bytecode generation when processing very complex expressions, often involving deeply nested binary operations (e.g., +, -, *, /, &&, ||).

      Currently, when this error occurs, the stack trace points to Gen.java and the visitBinary method, but it lacks specific information about:

      - The specific expression or code location that triggered the error. The user must manually simplify the code to isolate the expression
      - The depth of nesting or complexity of the expression that exceeded the compiler's capabilities
      - The type of binary operation that is causing the problem

      This lack of detail makes it difficult for developers to quickly identify and resolve the issue, forcing them to use a time-consuming process of elimination to simplify the code. It also makes it harder to determine whether the error is due to a genuine compiler bug or simply an overly complex expression.

      A sample code snipped is attached (hugeExpression.txt) that caused the compilation issue.
      Also, find the Stack Trace produced by the huge expression (StackTrace.txt)
      NOTE: These files are attached in the report because of the extension of both messages.

      STEPS TO REPRODUCE :

      - Create a Java class with a method containing a very long and complex expression using nested binary operators.
      - Compile the class using javac
      - If the expression is complex enough, the compiler will throw a java.lang.StackOverflowError with a stack trace pointing to com.sun.tools.javac.jvm.Gen.visitBinary

      ACTUAL VS EXPECTED BEHAVIOR
      EXPECTED -
      The compiler should provide more detailed error information to aid in debugging. This could include:

      - Precise Code Location: The line number and potentially a snippet of the code within the method where the excessive expression is located
      - Complexity Metric (Optional): A measure of the expression's complexity (e.g., nesting depth, number of operations) that triggered the error
      - Operation Type: The type of binary operation that caused the issue (e.g., +, &&, ||)
      - Suggestion for Simplification: A hint suggesting that the developer break down the expression into smaller parts and use temporary variables

      ACTUAL -
      The compiler throws a java.lang.StackOverflowError with a stack trace that provides limited context about the source of the error.

      ENHACENMENT REQUEST JUSTIFICATION :

      Providing more specific error information in this scenario would significantly improve the developer experience by:

      - Reducing Debugging Time: Developers can quickly pinpoint the problematic code and apply appropriate simplification techniques
      - Distinguishing Compiler Bugs from Code Complexity Issues: Clearer error messages would help developers determine whether the error is due to a compiler limitation or an overly complex expression in their code
      - Facilitating Compiler Optimization: More detailed information about the types of expressions that cause stack overflows could help the Java compiler team identify areas for optimization in the bytecode generation process

      PROPOSED SOLUTION :

      Modify the com.sun.tools.javac.jvm.Gen.visitBinary method to:

      - Track the depth of nested binary operations during expression traversal
      - When a StackOverflowError is caught, include the current code location (line number), the nesting depth, and the type of binary operation in the error message
      - Potentially, add a check for excessive expression complexity before the stack overflows, throwing a more informative exception with a suggestion to simplify the code.

      IMPACT :

      This enhancement would not change the behavior of the compiler in cases where expressions are within acceptable complexity limits. It would only add more detailed error information when a StackOverflowError occurs during bytecode generation of complex expressions.

      ADDITIONAL POINTS :

      I was able to resolve the error by manually simplifying the code, breaking down a long expression into smaller parts and assigning intermediate results to variables. This workaround suggests that the compiler's bytecode generation process can be susceptible to stack overflows when dealing with highly complex expressions.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: