Skip to content

Commit ff59532

Browse files
committed
8338678: Erroneous parameterized type represented as <any>
Reviewed-by: vromero
1 parent 0b4a7d5 commit ff59532

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,7 +2338,7 @@ public ErrorType(Type originalType, TypeSymbol tsym) {
23382338
this.originalType = (originalType == null ? noType : originalType);
23392339
}
23402340

2341-
private ErrorType(Type originalType, TypeSymbol tsym,
2341+
public ErrorType(Type originalType, TypeSymbol tsym,
23422342
List<TypeMetadata> metadata) {
23432343
super(noType, List.nil(), null, metadata);
23442344
this.tsym = tsym;
@@ -2393,10 +2393,6 @@ public <R,S> R accept(Type.Visitor<R,S> v, S s) {
23932393
public boolean isCompound() { return false; }
23942394
public boolean isInterface() { return false; }
23952395

2396-
public List<Type> allparams() { return List.nil(); }
2397-
@DefinedBy(Api.LANGUAGE_MODEL)
2398-
public List<Type> getTypeArguments() { return List.nil(); }
2399-
24002396
@DefinedBy(Api.LANGUAGE_MODEL)
24012397
public TypeKind getKind() {
24022398
return TypeKind.ERROR;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5081,6 +5081,14 @@ public void visitTypeApply(JCTypeApply tree) {
50815081
}
50825082
owntype = types.createErrorType(tree.type);
50835083
}
5084+
} else if (clazztype.hasTag(ERROR)) {
5085+
ErrorType parameterizedErroneous =
5086+
new ErrorType(clazztype.getOriginalType(),
5087+
clazztype.tsym,
5088+
clazztype.getMetadata());
5089+
5090+
parameterizedErroneous.typarams_field = actuals;
5091+
owntype = parameterizedErroneous;
50845092
}
50855093
result = check(tree, owntype, KindSelector.TYP, resultInfo);
50865094
}

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

Lines changed: 91 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
26+
* @bug 8301580 8322159 8333107 8332230 8338678
2727
* @summary Verify error recovery w.r.t. Attr
2828
* @library /tools/lib
2929
* @enablePreview
@@ -34,9 +34,23 @@
3434
* @run main AttrRecovery
3535
*/
3636

37+
import com.sun.source.tree.VariableTree;
38+
import com.sun.source.util.TaskEvent;
39+
import com.sun.source.util.TaskListener;
40+
import com.sun.source.util.TreePathScanner;
41+
import com.sun.source.util.Trees;
3742
import java.nio.file.Path;
43+
import java.util.IdentityHashMap;
3844
import java.util.List;
45+
import java.util.Map;
3946
import java.util.Objects;
47+
import java.util.stream.Collectors;
48+
import javax.lang.model.element.Element;
49+
import javax.lang.model.element.VariableElement;
50+
import javax.lang.model.type.DeclaredType;
51+
import javax.lang.model.type.TypeKind;
52+
import javax.lang.model.type.TypeMirror;
53+
import javax.tools.Diagnostic;
4054

4155
import toolbox.JavacTask;
4256
import toolbox.Task.Expect;
@@ -234,4 +248,80 @@ public Undefined g(Undefined u) {
234248
}
235249
}
236250

251+
@Test
252+
public void testParameterizedErroneousType() throws Exception {
253+
String code = """
254+
public class C {
255+
Undefined1<Undefined2, Undefined3> variable1;
256+
}
257+
""";
258+
Path curPath = Path.of(".");
259+
List<String> actual = new JavacTask(tb)
260+
.options("-XDrawDiagnostics")
261+
.sources(code)
262+
.outdir(curPath)
263+
.callback(task -> {
264+
task.addTaskListener(new TaskListener() {
265+
@Override
266+
public void finished(TaskEvent e) {
267+
Trees trees = Trees.instance(task);
268+
269+
if (e.getKind() == TaskEvent.Kind.ANALYZE) {
270+
new TreePathScanner<Void, Void>() {
271+
@Override
272+
public Void visitVariable(VariableTree tree, Void p) {
273+
VariableElement var = (VariableElement) trees.getElement(getCurrentPath());
274+
275+
trees.printMessage(Diagnostic.Kind.NOTE, type2String(var.asType()), tree, e.getCompilationUnit());
276+
277+
return super.visitVariable(tree, p);
278+
}
279+
}.scan(e.getCompilationUnit(), null);
280+
}
281+
}
282+
Map<Element, Integer> identityRename = new IdentityHashMap<>();
283+
String type2String(TypeMirror type) {
284+
StringBuilder result = new StringBuilder();
285+
286+
result.append(type.getKind());
287+
result.append(":");
288+
result.append(type.toString());
289+
290+
if (type.getKind() == TypeKind.DECLARED ||
291+
type.getKind() == TypeKind.ERROR) {
292+
DeclaredType dt = (DeclaredType) type;
293+
Element el = task.getTypes().asElement(dt);
294+
result.append(":");
295+
result.append(el.toString());
296+
if (!dt.getTypeArguments().isEmpty()) {
297+
result.append(dt.getTypeArguments()
298+
.stream()
299+
.map(tm -> type2String(tm))
300+
.collect(Collectors.joining(", ", "<", ">")));
301+
}
302+
} else {
303+
throw new AssertionError(type.getKind().name());
304+
}
305+
306+
return result.toString();
307+
}
308+
});
309+
})
310+
.run(Expect.FAIL)
311+
.writeAll()
312+
.getOutputLines(OutputKind.DIRECT);
313+
314+
List<String> expected = List.of(
315+
"C.java:2:5: compiler.err.cant.resolve.location: kindname.class, Undefined1, , , (compiler.misc.location: kindname.class, C, null)",
316+
"C.java:2:16: compiler.err.cant.resolve.location: kindname.class, Undefined2, , , (compiler.misc.location: kindname.class, C, null)",
317+
"C.java:2:28: compiler.err.cant.resolve.location: kindname.class, Undefined3, , , (compiler.misc.location: kindname.class, C, null)",
318+
"C.java:2:40: compiler.note.proc.messager: ERROR:Undefined1<Undefined2,Undefined3>:Undefined1<ERROR:Undefined2:Undefined2, ERROR:Undefined3:Undefined3>",
319+
"3 errors"
320+
);
321+
322+
if (!Objects.equals(actual, expected)) {
323+
error("Expected: " + expected + ", but got: " + actual);
324+
}
325+
}
326+
237327
}

0 commit comments

Comments
 (0)