6707032: Division by zero warning not suppressed properly in some cases
authorjlahoda
Mon, 30 Jun 2014 17:08:06 +0200
changeset 25301 e5da086c7d43
parent 25300 3b8a5067fe29
child 25302 7d48ff633279
6707032: Division by zero warning not suppressed properly in some cases Summary: Delay reporting of the division by zero warning until annotations are resolved, so that @SuppressWarnings works correctly. Reviewed-by: vromero
langtools/src/share/classes/com/sun/tools/javac/comp/Check.java
langtools/test/tools/javac/warnings/suppress/T6707032.java
langtools/test/tools/javac/warnings/suppress/T6707032.out
langtools/test/tools/javac/warnings/suppress/VerifySuppressWarnings.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Fri Jun 27 17:54:54 2014 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Check.java	Mon Jun 30 17:08:06 2014 +0200
@@ -271,6 +271,14 @@
             log.warning(LintCategory.STATIC, pos, msg, args);
     }
 
+    /** Warn about division by integer constant zero.
+     *  @param pos        Position to be used for error reporting.
+     */
+    void warnDivZero(DiagnosticPosition pos) {
+        if (lint.isEnabled(LintCategory.DIVZERO))
+            log.warning(LintCategory.DIVZERO, pos, "div.zero");
+    }
+
     /**
      * Report any deferred diagnostics.
      */
@@ -3393,15 +3401,19 @@
      *  @param operator      The operator for the expression
      *  @param operand       The right hand operand for the expression
      */
-    void checkDivZero(DiagnosticPosition pos, Symbol operator, Type operand) {
+    void checkDivZero(final DiagnosticPosition pos, Symbol operator, Type operand) {
         if (operand.constValue() != null
-            && lint.isEnabled(LintCategory.DIVZERO)
             && operand.getTag().isSubRangeOf(LONG)
             && ((Number) (operand.constValue())).longValue() == 0) {
             int opc = ((OperatorSymbol)operator).opcode;
             if (opc == ByteCodes.idiv || opc == ByteCodes.imod
                 || opc == ByteCodes.ldiv || opc == ByteCodes.lmod) {
-                log.warning(LintCategory.DIVZERO, pos, "div.zero");
+                deferredLintHandler.report(new DeferredLintHandler.LintLogger() {
+                    @Override
+                    public void report() {
+                        warnDivZero(pos);
+                    }
+                });
             }
         }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/warnings/suppress/T6707032.java	Mon Jun 30 17:08:06 2014 +0200
@@ -0,0 +1,18 @@
+/**
+ * @test /nodynamiccopyright/
+ * @bug 6707032
+ * @summary Verify that \\@SuppressWarnings("divzero") works for constant initializers
+ * @build VerifySuppressWarnings
+ * @compile/ref=T6707032.out -XDrawDiagnostics -Xlint:divzero T6707032.java
+ * @run main VerifySuppressWarnings T6707032.java
+ */
+
+public class T6707032 {
+    public static final int D1 = T6707032b.D0;
+    public static final int D2 = 1/0;
+}
+
+class T6707032b {
+    public static final int D0 = 1/0;
+    public static final int D3 = T6707032.D2;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/warnings/suppress/T6707032.out	Mon Jun 30 17:08:06 2014 +0200
@@ -0,0 +1,3 @@
+T6707032.java:12:36: compiler.warn.div.zero
+T6707032.java:16:36: compiler.warn.div.zero
+2 warnings
--- a/langtools/test/tools/javac/warnings/suppress/VerifySuppressWarnings.java	Fri Jun 27 17:54:54 2014 -0700
+++ b/langtools/test/tools/javac/warnings/suppress/VerifySuppressWarnings.java	Mon Jun 30 17:08:06 2014 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -55,11 +55,12 @@
  * the @SuppressWarnings annotation on the declaration and verifies than no
  * warnings are produced inside the declaration, but all are produced outside it.
  *
- * Currently only works with <code>unchecked,deprecation,cast</code> warnings.
+ * Currently only works with <code>unchecked,deprecation,cast,divzero</code> warnings.
  */
 public class VerifySuppressWarnings {
 
-    private static final List<String> STANDARD_PARAMS = Arrays.asList("-Xlint:unchecked,deprecation,cast", "-Xjcov");
+    private static final List<String> STANDARD_PARAMS =
+            Arrays.asList("-Xlint:unchecked,deprecation,cast,divzero");
 
     public static void main(String... args) throws IOException, URISyntaxException {
         if (args.length != 1) throw new IllegalStateException("Must provide class name!");
@@ -133,7 +134,8 @@
         }.scan(cut, null);
 
         for (final int[] declarationSpan : declarationSpans) {
-            final String suppressWarnings = "@SuppressWarnings({\"deprecation\", \"unchecked\", \"serial\"})";
+            final String suppressWarnings =
+                    "@SuppressWarnings({\"deprecation\", \"unchecked\", \"serial\", \"divzero\"})";
             final String updatedContent = testContent.substring(0, declarationSpan[0]) + suppressWarnings + testContent.substring(declarationSpan[0]);
             final List<Diagnostic<?>> foundErrors = new ArrayList<>(diagnostics);
             DiagnosticListener<JavaFileObject> verifyDiagnostics = new DiagnosticListener<JavaFileObject>() {