8328536: javac - crash on unknown type referenced in yield statement

Co-authored-by: Jan Lahoda <jlahoda@openjdk.org>
Reviewed-by: jlahoda
This commit is contained in:
Hannes Greule 2024-06-26 23:17:32 +00:00 committed by Chen Liang
parent 07bc523df8
commit 3796fdfced
6 changed files with 51 additions and 7 deletions

View File

@ -4646,9 +4646,6 @@ public class Attr extends JCTree.Visitor {
Type pt,
Env<AttrContext> env,
ResultInfo resultInfo) {
if (pt.isErroneous()) {
return types.createErrorType(site);
}
Type owntype; // The computed type of this identifier occurrence.
switch (sym.kind) {
case TYP:
@ -4757,6 +4754,10 @@ public class Attr extends JCTree.Visitor {
chk.checkPreview(tree.pos(), env.info.scope.owner, sym);
}
if (pt.isErroneous()) {
owntype = types.createErrorType(owntype);
}
// If symbol is a variable, check that its type and
// kind are compatible with the prototype and protokind.
return check(tree, owntype, sym.kind.toSelector(), resultInfo);

View File

@ -27,7 +27,7 @@
* 8167128 8154513 8170015 8170368 8172102 8172103 8165405 8173073 8173848
* 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154
* 8192979 8191842 8198573 8198801 8210596 8210959 8215099 8199623 8236715
* 8239536 8247456 8246774 8238173 8292625 8306560
* 8239536 8247456 8246774 8238173 8292625 8306560 8328536
* @summary Simple jshell tool tests
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.main
@ -964,6 +964,13 @@ public class ToolSimpleTest extends ReplToolTesting {
);
}
@Test
public void testSwitchExpressionYieldUnknownType() {
test(a -> assertCommandOutputContains(a,
"I m(I i, int x) { return switch (x) { default -> i; }; } ",
"created method m(I,int), however, it cannot be referenced until class I is declared"));
}
@Test
public void testSelfReference() {
test(

View File

@ -1,7 +1,12 @@
T7188968.java:20:20: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
T7188968.java:20:9: compiler.warn.unchecked.call.mbr.of.raw.type: T7188968.Foo(java.util.List<X>,java.lang.Object), T7188968.Foo
T7188968.java:21:20: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
T7188968.java:21:29: compiler.warn.unchecked.call.mbr.of.raw.type: T7188968.Foo(java.util.List<X>,java.lang.Object), T7188968.Foo
T7188968.java:22:22: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
T7188968.java:22:9: compiler.warn.unchecked.meth.invocation.applied: kindname.constructor, <init>, java.util.List<X>,java.lang.Object, java.util.List,unknown, kindname.class, T7188968.Foo
T7188968.java:22:19: compiler.warn.prob.found.req: (compiler.misc.unchecked.assign), java.util.List, java.util.List<X>
T7188968.java:23:24: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
T7188968.java:23:20: compiler.warn.unchecked.meth.invocation.applied: kindname.method, makeFoo, java.util.List<Z>,java.lang.Object, java.util.List,unknown, kindname.class, T7188968.Foo
T7188968.java:23:21: compiler.warn.prob.found.req: (compiler.misc.unchecked.assign), java.util.List, java.util.List<Z>
4 errors
1 warning
6 warnings

View File

@ -1,5 +1,5 @@
MethodReference23.java:52:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
MethodReference23.java:57:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
MethodReference23.java:58:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
MethodReference23.java:72:9: compiler.err.ref.ambiguous: call3, kindname.method, call3(MethodReference23.SAM21), MethodReference23, kindname.method, call3(MethodReference23.SAM22), MethodReference23

View File

@ -1,2 +1,2 @@
MethodRefToInnerWithoutOuter.java:22:31: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: TestString, , MethodRefToInnerBase)
MethodRefToInnerWithoutOuter.java:22:31: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: TestString, java.lang.String, MethodRefToInnerBase)
1 error

View File

@ -203,4 +203,35 @@ public class AttrRecovery extends TestRunner {
}
}
@Test
public void testErroneousTarget() throws Exception {
String code = """
public class C {
public Undefined g(Undefined u) {
return switch (0) {
default -> u;
};
}
}
""";
Path curPath = Path.of(".");
List<String> actual = new JavacTask(tb)
.options("-XDrawDiagnostics")
.sources(code)
.outdir(curPath)
.run(Expect.FAIL, 1)
.writeAll()
.getOutputLines(OutputKind.DIRECT);
List<String> expected = List.of(
"C.java:2:24: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, C, null)",
"C.java:2:12: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, C, null)",
"2 errors"
);
if (!Objects.equals(actual, expected)) {
error("Expected: " + expected + ", but got: " + actual);
}
}
}