Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Fix BY_FILE compilation policy
Browse files Browse the repository at this point in the history
When -XDcompilePolicy=BY_FILE is enabled, javac groups symbols by
compilation unit and moves them through the phases of compilation
together. A symbol cannot be desugared until its ancestors have been
desugared, so javac also tracks supertypes and ensures they get
desugared first. The second ordering constraint was overriding the
first, and causing symbols to be desugared separately from their
compilation unit group.

See:
* google/error-prone#781
* https://bugs.openjdk.java.net/browse/JDK-8155674
  • Loading branch information
cushon committed Apr 25, 2019
1 parent ebfbdda commit 94c6b72
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public Env<AttrContext> peek() {
return (size() == 0 ? null : contents.get(0));
}

public Queue<Env<AttrContext>> groupByFile(Env<AttrContext> env) {
return new LinkedList<>(fileMap.get(env.toplevel.sourcefile));
}

public Queue<Queue<Env<AttrContext>>> groupByFile() {
if (contentsByFile == null) {
contentsByFile = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1511,11 +1511,17 @@ public void visitReference(JCMemberReference tree) {
}
ScanNested scanner = new ScanNested();
scanner.scan(env.tree);
for (Env<AttrContext> dep: scanner.dependencies) {
if (!compileStates.isDone(dep, CompileState.FLOW))
desugaredEnvs.put(dep, desugar(flow(attribute(dep))));
if (compilePolicy == CompilePolicy.BY_FILE) {
desugarByFile(scanner.dependencies);
} else {
for (Env<AttrContext> dep: scanner.dependencies) {
if (!compileStates.isDone(dep, CompileState.FLOW)) {
desugaredEnvs.put(dep, desugar(flow(attribute(dep))));
}
}
}


//We need to check for error another time as more classes might
//have been attributed and analyzed at this stage
if (shouldStop(CompileState.TRANSTYPES))
Expand Down Expand Up @@ -1593,6 +1599,20 @@ public void visitReference(JCMemberReference tree) {
}

}
// where
private void desugarByFile(Iterable<Env<AttrContext>> envs) {
Set<JCCompilationUnit> seen = new HashSet<>();
for (Env<AttrContext> env: envs) {
if (compileStates.isDone(env, CompileState.FLOW)) {
continue;
}
if (seen.add(env.toplevel)) {
// process unique compilation units up to flow
flow(attribute(todo.groupByFile(env)));
}
desugaredEnvs.put(env, desugar(ListBuffer.of(env)));
}
}

/** Generates the source or class file for a list of classes.
* The decision to generate a source file or a class file is
Expand Down Expand Up @@ -1652,20 +1672,6 @@ public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<Jav
}

// where
Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) {
// use a LinkedHashMap to preserve the order of the original list as much as possible
Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<>();
for (Env<AttrContext> env: envs) {
Queue<Env<AttrContext>> sublist = map.get(env.toplevel);
if (sublist == null) {
sublist = new ListBuffer<>();
map.put(env.toplevel, sublist);
}
sublist.add(env);
}
return map;
}

JCClassDecl removeMethodBodies(JCClassDecl cdef) {
final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0;
class MethodBodyRemover extends TreeTranslator {
Expand Down
24 changes: 24 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/A.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2016, Google Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

class A extends One {}
26 changes: 26 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/B.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2016, Google Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

class One {}

class Two {}
47 changes: 47 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/B27686620.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2016, Google Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @compile/ref=bytodo.ABC.out -XDverboseCompilePolicy -XDcompilePolicy=bytodo A.java B.java C.java
*/

/*
* @test
* @compile/ref=byfile.ABC.out -XDverboseCompilePolicy -XDcompilePolicy=byfile A.java B.java C.java
*/

/*
* @test
* @compile/ref=bytodo.ADC.out -XDverboseCompilePolicy -XDcompilePolicy=bytodo A.java D.java C.java
*/

/*
* @test
* @compile/ref=byfile.ADC.out -XDverboseCompilePolicy -XDcompilePolicy=byfile A.java D.java C.java
*/

/*
* @test
* @compile/ref=byfile.EF.out -XDverboseCompilePolicy -XDcompilePolicy=byfile E.java F.java
*/
24 changes: 24 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/C.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2016, Google Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

class B extends Two {}
28 changes: 28 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2016, Google Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

class One {}

class Two {}

class Three {}
26 changes: 26 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/E.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2016, Google Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

class One {}

class Two extends Four {}
26 changes: 26 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/F.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2016, Google Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

class Three extends One {}

class Four {}
16 changes: 16 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/byfile.ABC.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[attribute A]
[flow A]
[attribute One]
[attribute Two]
[flow One]
[flow Two]
[desugar One]
[desugar A]
[generate code A]
[desugar Two]
[generate code One]
[generate code Two]
[attribute B]
[flow B]
[desugar B]
[generate code B]
20 changes: 20 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/byfile.ADC.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[attribute A]
[flow A]
[attribute One]
[attribute Two]
[attribute Three]
[flow One]
[flow Two]
[flow Three]
[desugar One]
[desugar A]
[generate code A]
[desugar Two]
[desugar Three]
[generate code One]
[generate code Two]
[generate code Three]
[attribute B]
[flow B]
[desugar B]
[generate code B]
16 changes: 16 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/byfile.EF.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[attribute One]
[attribute Two]
[flow One]
[flow Two]
[desugar One]
[attribute Four]
[attribute Three]
[flow Four]
[flow Three]
[desugar Four]
[desugar Two]
[generate code One]
[generate code Two]
[desugar Three]
[generate code Four]
[generate code Three]
16 changes: 16 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/bytodo.ABC.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[attribute A]
[flow A]
[attribute One]
[flow One]
[desugar One]
[desugar A]
[generate code A]
[generate code One]
[attribute Two]
[flow Two]
[desugar Two]
[generate code Two]
[attribute B]
[flow B]
[desugar B]
[generate code B]
20 changes: 20 additions & 0 deletions test/langtools/tools/javac/policy/B27686620/bytodo.ADC.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[attribute A]
[flow A]
[attribute One]
[flow One]
[desugar One]
[desugar A]
[generate code A]
[generate code One]
[attribute Two]
[flow Two]
[desugar Two]
[generate code Two]
[attribute Three]
[flow Three]
[desugar Three]
[generate code Three]
[attribute B]
[flow B]
[desugar B]
[generate code B]

0 comments on commit 94c6b72

Please sign in to comment.