Tue, 02 Jul 2013 22:49:40 +0100
6326693: variable x might already have been assigned, when assignment is in catch block
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Flow.java Mon Jul 01 16:36:08 2013 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java Tue Jul 02 22:49:40 2013 +0100 1.3 @@ -1945,10 +1945,17 @@ 1.4 } 1.5 } 1.6 1.7 + /* The analysis of each catch should be independent. 1.8 + * Each one should have the same initial values of inits and 1.9 + * uninits. 1.10 + */ 1.11 + final Bits initsCatchPrev = new Bits(initsTry); 1.12 + final Bits uninitsCatchPrev = new Bits(uninitsTry); 1.13 + 1.14 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { 1.15 JCVariableDecl param = l.head.param; 1.16 - inits.assign(initsTry); 1.17 - uninits.assign(uninitsTry); 1.18 + inits.assign(initsCatchPrev); 1.19 + uninits.assign(uninitsCatchPrev); 1.20 scan(param); 1.21 inits.incl(param.sym.adr); 1.22 uninits.excl(param.sym.adr);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/T6326693/FinalVariableAssignedToInCatchBlockTest.java Tue Jul 02 22:49:40 2013 +0100 2.3 @@ -0,0 +1,94 @@ 2.4 +/* 2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/* 2.28 + * @test 2.29 + * @bug 6356530 2.30 + * @summary -Xlint:serial does not flag abstract classes with concrete methods/members 2.31 + * @compile/fail/ref=FinalVariableAssignedToInCatchBlockTest.out -XDrawDiagnostics FinalVariableAssignedToInCatchBlockTest.java 2.32 + */ 2.33 + 2.34 +import java.io.IOException; 2.35 + 2.36 +public class FinalVariableAssignedToInCatchBlockTest { 2.37 + public void m1(int o) 2.38 + { 2.39 + final int i; 2.40 + try { 2.41 + if (o == 1) { 2.42 + throw new IOException(); 2.43 + } else if (o == 2) { 2.44 + throw new InterruptedException(); 2.45 + } else { 2.46 + throw new Exception(); 2.47 + } 2.48 + } catch (IOException e) { 2.49 + i = 1; 2.50 + } catch (InterruptedException ie) { 2.51 + i = 2; 2.52 + } catch (Exception e) { 2.53 + i = 3; 2.54 + } finally { 2.55 + i = 4; 2.56 + } 2.57 + } 2.58 + 2.59 + public void m2(int o) 2.60 + { 2.61 + final int i; 2.62 + try { 2.63 + if (o == 1) { 2.64 + throw new IOException(); 2.65 + } else if (o == 2) { 2.66 + throw new InterruptedException(); 2.67 + } else { 2.68 + throw new Exception(); 2.69 + } 2.70 + } catch (IOException e) { 2.71 + i = 1; 2.72 + } catch (InterruptedException ie) { 2.73 + i = 2; 2.74 + } catch (Exception e) { 2.75 + i = 3; 2.76 + } 2.77 + } 2.78 + 2.79 + public void m3(int o) throws Exception 2.80 + { 2.81 + final int i; 2.82 + try { 2.83 + if (o == 1) { 2.84 + throw new IOException(); 2.85 + } else if (o == 2) { 2.86 + throw new InterruptedException(); 2.87 + } else { 2.88 + throw new Exception(); 2.89 + } 2.90 + } catch (IOException e) { 2.91 + i = 1; 2.92 + } catch (InterruptedException ie) { 2.93 + i = 2; 2.94 + } 2.95 + i = 3; 2.96 + } 2.97 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/T6326693/FinalVariableAssignedToInCatchBlockTest.out Tue Jul 02 22:49:40 2013 +0100 3.3 @@ -0,0 +1,3 @@ 3.4 +FinalVariableAssignedToInCatchBlockTest.java:52:13: compiler.err.var.might.already.be.assigned: i 3.5 +FinalVariableAssignedToInCatchBlockTest.java:92:9: compiler.err.var.might.already.be.assigned: i 3.6 +2 errors