Skip to content

Commit 3796fdf

Browse files
SirYwelllahodaj
authored andcommitted
8328536: javac - crash on unknown type referenced in yield statement
Co-authored-by: Jan Lahoda <[email protected]> Reviewed-by: jlahoda
1 parent 07bc523 commit 3796fdf

File tree

6 files changed

+51
-7
lines changed

6 files changed

+51
-7
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -4646,9 +4646,6 @@ Type checkIdInternal(JCTree tree,
46464646
Type pt,
46474647
Env<AttrContext> env,
46484648
ResultInfo resultInfo) {
4649-
if (pt.isErroneous()) {
4650-
return types.createErrorType(site);
4651-
}
46524649
Type owntype; // The computed type of this identifier occurrence.
46534650
switch (sym.kind) {
46544651
case TYP:
@@ -4757,6 +4754,10 @@ else if (ownOuter.hasTag(CLASS) && site != ownOuter) {
47574754
chk.checkPreview(tree.pos(), env.info.scope.owner, sym);
47584755
}
47594756

4757+
if (pt.isErroneous()) {
4758+
owntype = types.createErrorType(owntype);
4759+
}
4760+
47604761
// If symbol is a variable, check that its type and
47614762
// kind are compatible with the prototype and protokind.
47624763
return check(tree, owntype, sym.kind.toSelector(), resultInfo);

test/langtools/jdk/jshell/ToolSimpleTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* 8167128 8154513 8170015 8170368 8172102 8172103 8165405 8173073 8173848
2828
* 8174041 8173916 8174028 8174262 8174797 8177079 8180508 8177466 8172154
2929
* 8192979 8191842 8198573 8198801 8210596 8210959 8215099 8199623 8236715
30-
* 8239536 8247456 8246774 8238173 8292625 8306560
30+
* 8239536 8247456 8246774 8238173 8292625 8306560 8328536
3131
* @summary Simple jshell tool tests
3232
* @modules jdk.compiler/com.sun.tools.javac.api
3333
* jdk.compiler/com.sun.tools.javac.main
@@ -964,6 +964,13 @@ public void testSwitchStatementExpressionDisambiguation() {
964964
);
965965
}
966966

967+
@Test
968+
public void testSwitchExpressionYieldUnknownType() {
969+
test(a -> assertCommandOutputContains(a,
970+
"I m(I i, int x) { return switch (x) { default -> i; }; } ",
971+
"created method m(I,int), however, it cannot be referenced until class I is declared"));
972+
}
973+
967974
@Test
968975
public void testSelfReference() {
969976
test(
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
T7188968.java:20:20: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
2+
T7188968.java:20:9: compiler.warn.unchecked.call.mbr.of.raw.type: T7188968.Foo(java.util.List<X>,java.lang.Object), T7188968.Foo
23
T7188968.java:21:20: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
34
T7188968.java:21:29: compiler.warn.unchecked.call.mbr.of.raw.type: T7188968.Foo(java.util.List<X>,java.lang.Object), T7188968.Foo
45
T7188968.java:22:22: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
6+
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
7+
T7188968.java:22:19: compiler.warn.prob.found.req: (compiler.misc.unchecked.assign), java.util.List, java.util.List<X>
58
T7188968.java:23:24: compiler.err.cant.resolve.location: kindname.variable, unknown, , , (compiler.misc.location: kindname.class, T7188968, null)
9+
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
10+
T7188968.java:23:21: compiler.warn.prob.found.req: (compiler.misc.unchecked.assign), java.util.List, java.util.List<Z>
611
4 errors
7-
1 warning
12+
6 warnings

test/langtools/tools/javac/lambda/MethodReference23.out

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MethodReference23.java:52:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
2-
MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
2+
MethodReference23.java:53:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, MethodReference23, MethodReference23)
33
MethodReference23.java:57:19: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
44
MethodReference23.java:58:16: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: Inner1, , MethodReference23)
55
MethodReference23.java:72:9: compiler.err.ref.ambiguous: call3, kindname.method, call3(MethodReference23.SAM21), MethodReference23, kindname.method, call3(MethodReference23.SAM22), MethodReference23
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
MethodRefToInnerWithoutOuter.java:22:31: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: TestString, , MethodRefToInnerBase)
1+
MethodRefToInnerWithoutOuter.java:22:31: compiler.err.invalid.mref: kindname.constructor, (compiler.misc.cant.access.inner.cls.constr: TestString, java.lang.String, MethodRefToInnerBase)
22
1 error

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

+31
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,35 @@ public interface FI {
203203
}
204204
}
205205

206+
@Test
207+
public void testErroneousTarget() throws Exception {
208+
String code = """
209+
public class C {
210+
public Undefined g(Undefined u) {
211+
return switch (0) {
212+
default -> u;
213+
};
214+
}
215+
}
216+
""";
217+
Path curPath = Path.of(".");
218+
List<String> actual = new JavacTask(tb)
219+
.options("-XDrawDiagnostics")
220+
.sources(code)
221+
.outdir(curPath)
222+
.run(Expect.FAIL, 1)
223+
.writeAll()
224+
.getOutputLines(OutputKind.DIRECT);
225+
226+
List<String> expected = List.of(
227+
"C.java:2:24: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, C, null)",
228+
"C.java:2:12: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, C, null)",
229+
"2 errors"
230+
);
231+
232+
if (!Objects.equals(actual, expected)) {
233+
error("Expected: " + expected + ", but got: " + actual);
234+
}
235+
}
236+
206237
}

0 commit comments

Comments
 (0)