Skip to content

Commit 15fd529

Browse files
committed
8366196: Crash in AbstractMethodCheck.getMethodCheckFailure
Reviewed-by: liach, mcimadamore
1 parent 667744c commit 15fd529

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/AttrRecover.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import com.sun.tools.javac.code.TypeTag;
3535
import com.sun.tools.javac.code.Types;
3636
import com.sun.tools.javac.comp.Attr.ResultInfo;
37+
import com.sun.tools.javac.comp.Check.CheckContext;
38+
import com.sun.tools.javac.comp.Check.NestedCheckContext;
3739
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
3840
import com.sun.tools.javac.tree.JCTree;
3941
import com.sun.tools.javac.tree.JCTree.JCBlock;
@@ -66,6 +68,7 @@ public class AttrRecover {
6668
protected static final Context.Key<AttrRecover> attrRepairKey = new Context.Key<>();
6769

6870
final Attr attr;
71+
final Check chk;
6972
final DeferredAttr deferredAttr;
7073
final Names names;
7174
final TreeMaker make;
@@ -84,6 +87,7 @@ protected AttrRecover(Context context) {
8487
context.put(attrRepairKey, this);
8588

8689
attr = Attr.instance(context);
90+
chk = Check.instance(context);
8791
deferredAttr = DeferredAttr.instance(context);
8892
names = Names.instance(context);
8993
make = TreeMaker.instance(context);
@@ -192,8 +196,14 @@ public void visitClassDef(JCClassDecl tree) {
192196
while (pats.length() < args.length()) {
193197
pats = pats.append(syms.errType);
194198
}
199+
CheckContext recoveryCheckContext = new NestedCheckContext(todo.resultInfo.checkContext) {
200+
@Override
201+
public void report(JCDiagnostic.DiagnosticPosition pos, JCDiagnostic details) {
202+
chk.basicHandler.report(pos, details);
203+
}
204+
};
195205
owntype = attr.checkMethod(todo.site, todo.candSym,
196-
attr.new ResultInfo(todo.resultInfo.pkind, todo.resultInfo.pt.getReturnType(), todo.resultInfo.checkContext, todo.resultInfo.checkMode),
206+
attr.new ResultInfo(todo.resultInfo.pkind, todo.resultInfo.pt.getReturnType(), recoveryCheckContext, todo.resultInfo.checkMode),
197207
todo.env, args, pats,
198208
todo.resultInfo.pt.getTypeArguments());
199209
rollback.forEach(Runnable::run);

test/langtools/tools/javac/recovery/AttrRecovery.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8301580 8322159 8333107 8332230 8338678 8351260
26+
* @bug 8301580 8322159 8333107 8332230 8338678 8351260 8366196
2727
* @summary Verify error recovery w.r.t. Attr
2828
* @library /tools/lib
2929
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -340,4 +340,58 @@ void test(List<@AlphaChars <@StringLength(int value = 5)String> s){
340340
.run(Expect.FAIL)
341341
.writeAll();
342342
}
343+
344+
345+
@Test //JDK-8366196
346+
public void testInferenceFailure() throws Exception {
347+
String code = """
348+
import module java.base;
349+
public class Test {
350+
public void test(Consumer<String> c) {
351+
List.of("")
352+
.stream()
353+
.filter(
354+
buildPredicate(
355+
String.class,
356+
//missing supplier
357+
c,
358+
buildSupplier(
359+
// Missing: Class,
360+
Integer.class,
361+
String.class,
362+
i -> { int check; return i; })));
363+
}
364+
365+
private static <T> Predicate<T> buildPredicate(
366+
Class<T> tClass,
367+
Supplier<T> bSupplier,
368+
Consumer<T> cConsumer,
369+
Supplier<T> dSupplier) {
370+
return null;
371+
}
372+
373+
private static <T, A, B> Supplier<String> buildSupplier(
374+
Class<T> tClass, Class<A> aClass, Class<B> bClass,
375+
Function<A, B> function) {
376+
return null;
377+
}
378+
}""";
379+
Path curPath = Path.of(".");
380+
List<String> actual = new JavacTask(tb)
381+
.options("-XDrawDiagnostics", "-XDshould-stop.at=FLOW")
382+
.sources(code)
383+
.outdir(curPath)
384+
.run(Expect.FAIL)
385+
.writeAll()
386+
.getOutputLines(OutputKind.DIRECT);
387+
388+
List<String> expected = List.of(
389+
"Test.java:7:29: compiler.err.prob.found.req: (compiler.misc.infer.no.conforming.assignment.exists: T, (compiler.misc.inconvertible.types: java.util.function.Consumer<java.lang.String>, java.util.function.Supplier<T>))",
390+
"1 error"
391+
);
392+
393+
if (!Objects.equals(actual, expected)) {
394+
error("Expected: " + expected + ", but got: " + actual);
395+
}
396+
}
343397
}

0 commit comments

Comments
 (0)