Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public Error disabledError(JavaFileObject classfile, int majorVersion) {
* @return true iff sym has been declared using a preview language feature
*/
public boolean declaredUsingPreviewFeature(Symbol sym) {
return false;
return sym.isValueClass();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this include records too given how its semantics are changed? Maybe in another RFE?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not completely certain, but I think this should not include records.

My understanding is that for records, only the implementation of the record's constructor changes, and that this is transparent(?) for the users of the record. I.e. the change not in the declaration/signatures, but in the implementation. If a class, e.g. PM, uses a preview feature inside an implementation only (i.e. not in a declaration), we don't taint another class, e.g. Use, that only uses PM.

For example:

$ cat PM.java 
public class PM {
    private boolean test(Object o) {
        return o instanceof byte b;
    }
}
$ cat Use.java 
public class Use {
    PM pm;
}
$ javac --enable-preview --source 28 -Xlint:preview PM.java Use.java
PM.java:3: warning: [preview] primitive patterns are a preview feature and may be removed in a future release.
        return o instanceof byte b;
                            ^
1 warning

There's no warning for the use of PM in Use, and Use.class is not marked a preview classfile.

Even though PM uses a preview feature (primitive patterns), it only does so inside an implementation, and so the use of the preview feature is invisible to the use site, leading to no warning.

My understanding (possibly wrong) is that the change in record constructors is similar to the use of pattern matching in the implementation of PM.

}

public void checkSourceLevel(DiagnosticPosition pos, Feature feature) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,9 +1002,6 @@ void writeField(VarSymbol v) {
Type fldType = v.erasure(types);
if (fldType.requiresLoadableDescriptors(v.owner)) {
poolWriter.enterLoadableDescriptorsClass(fldType.tsym);
if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
preview.markUsesPreview(null);
}
}
int acountIdx = beginAttrs();
int acount = 0;
Expand Down Expand Up @@ -1035,17 +1032,11 @@ void writeMethod(MethodSymbol m) {
for (Type t : mtype.getParameterTypes()) {
if (t.requiresLoadableDescriptors(m.owner)) {
poolWriter.enterLoadableDescriptorsClass(t.tsym);
if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
preview.markUsesPreview(null);
}
}
}
Type returnType = mtype.getReturnType();
if (returnType.requiresLoadableDescriptors(m.owner)) {
poolWriter.enterLoadableDescriptorsClass(returnType.tsym);
if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
preview.markUsesPreview(null);
}
}
int acountIdx = beginAttrs();
int acount = 0;
Expand Down
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/patterns/DominationWithPP.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ Domination.java:193:18: compiler.err.pattern.dominated
Domination.java:202:18: compiler.err.pattern.dominated
Domination.java:211:18: compiler.err.pattern.dominated
Domination.java:228:18: compiler.err.pattern.dominated
- compiler.note.preview.filename: Domination.java, DEFAULT
- compiler.note.preview.recompile
13 errors
1 change: 0 additions & 1 deletion test/langtools/tools/javac/patterns/T8309054.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* @test /nodynamiccopyright/
* @bug 8309054
* @summary Parsing of erroneous patterns succeeds
* @enablePreview
* @compile/fail/ref=T8309054.out -XDrawDiagnostics --should-stop=at=FLOW T8309054.java
*/

Expand Down
12 changes: 6 additions & 6 deletions test/langtools/tools/javac/patterns/T8309054.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
T8309054.java:12:24: compiler.err.expected2: :, ->
T8309054.java:16:26: compiler.err.expected2: :, ->
T8309054.java:19:35: compiler.err.expected: ')'
T8309054.java:13:13: compiler.err.switch.mixing.case.types
T8309054.java:17:13: compiler.err.switch.mixing.case.types
T8309054.java:21:17: compiler.err.unexpected.type: kindname.variable, kindname.value
T8309054.java:11:24: compiler.err.expected2: :, ->
T8309054.java:15:26: compiler.err.expected2: :, ->
T8309054.java:18:35: compiler.err.expected: ')'
T8309054.java:12:13: compiler.err.switch.mixing.case.types
T8309054.java:16:13: compiler.err.switch.mixing.case.types
T8309054.java:20:17: compiler.err.unexpected.type: kindname.variable, kindname.value
6 errors
1 change: 0 additions & 1 deletion test/langtools/tools/javac/patterns/T8314578.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* @test /nodynamiccopyright/
* @bug 8314578
* @enablePreview
* @summary Parsing of erroneous patterns succeeds
* @compile/fail/ref=T8314578.out -XDrawDiagnostics T8314578.java
*/
Expand Down
6 changes: 3 additions & 3 deletions test/langtools/tools/javac/patterns/T8314578.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
T8314578.java:14:18: compiler.err.flows.through.from.pattern
T8314578.java:15:18: compiler.err.flows.through.to.pattern
T8314578.java:27:18: compiler.err.flows.through.to.pattern
T8314578.java:13:18: compiler.err.flows.through.from.pattern
T8314578.java:14:18: compiler.err.flows.through.to.pattern
T8314578.java:26:18: compiler.err.flows.through.to.pattern
3 errors
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/patterns/T8332463b.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
T8332463b.java:36:18: compiler.err.pattern.dominated
- compiler.note.preview.filename: T8332463b.java, DEFAULT
- compiler.note.preview.recompile
1 error
2 changes: 2 additions & 0 deletions test/langtools/tools/javac/preview/PreviewJRTImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ void test() {
"Test.java:1:16: compiler.warn.sun.proprietary: sun.misc.Unsafe",
"Test.java:5:9: compiler.err.type.found.req: java.lang.Boolean, (compiler.misc.type.req.identity)",
"Test.java:7:9: compiler.warn.sun.proprietary: sun.misc.Unsafe",
"- compiler.note.preview.filename: Test.java, DEFAULT",
"- compiler.note.preview.recompile",
"1 error",
"2 warnings"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, 2026, Oracle and/or its affiliates. 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
Expand Down Expand Up @@ -41,9 +41,11 @@
import java.lang.classfile.ClassFile;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.sun.tools.javac.util.Assert;

import toolbox.Task.OutputKind;
import toolbox.TestRunner;
import toolbox.ToolBox;

Expand Down Expand Up @@ -81,24 +83,53 @@ class Ident {
Path classes = base.resolve("classes");
tb.createDirectories(classes);

new toolbox.JavacTask(tb)
.options("--enable-preview", "-source", Integer.toString(Runtime.version().feature()))
List<String> log;
List<String> expected;

expected = List.of(
"Ident.java:2:5: compiler.warn.declared.using.preview: kindname.class, Val",
"Val.java:1:1: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.value.classes)",
"2 warnings"
);

log = new toolbox.JavacTask(tb)
.options("--enable-preview",
"-source", Integer.toString(Runtime.version().feature()),
"-XDrawDiagnostics",
"-Xlint:preview")
.outdir(classes)
.files(findJavaFiles(src))
.run()
.writeAll();
.writeAll()
.getOutputLines(OutputKind.DIRECT);

tb.checkEqual(log, expected);

Path classFilePath = classes.resolve("Ident.class");
var classFile = ClassFile.of().parse(classFilePath);
Assert.check(classFile.minorVersion() == 65535);
Assert.check(classFile.findAttribute(Attributes.loadableDescriptors()).isPresent());

expected = List.of(
"- compiler.warn.preview.feature.use.classfile: Val.class, 28",
"Ident.java:2:5: compiler.warn.declared.using.preview: kindname.class, Val",
"2 warnings"
);

// now with the value class in the classpath
new toolbox.JavacTask(tb)
.options("--enable-preview", "-source", Integer.toString(Runtime.version().feature()), "-cp", classes.toString())
log = new toolbox.JavacTask(tb)
.options("--enable-preview",
"-source", Integer.toString(Runtime.version().feature()),
"-cp", classes.toString(),
"-XDrawDiagnostics",
"-Xlint:preview")
.outdir(classes)
.files(src.resolve("Ident.java"))
.run()
.writeAll();
.writeAll()
.getOutputLines(OutputKind.DIRECT);

tb.checkEqual(log, expected);

classFilePath = classes.resolve("Ident.class");
classFile = ClassFile.of().parse(classFilePath);
Expand All @@ -121,24 +152,53 @@ void m(Val val) {}
Path classes = base.resolve("classes");
tb.createDirectories(classes);

new toolbox.JavacTask(tb)
.options("--enable-preview", "-source", Integer.toString(Runtime.version().feature()))
List<String> log;
List<String> expected;

expected = List.of(
"Ident.java:2:12: compiler.warn.declared.using.preview: kindname.class, Val",
"Val.java:1:1: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.value.classes)",
"2 warnings"
);

log = new toolbox.JavacTask(tb)
.options("--enable-preview",
"-source", Integer.toString(Runtime.version().feature()),
"-XDrawDiagnostics",
"-Xlint:preview")
.outdir(classes)
.files(findJavaFiles(src))
.run()
.writeAll();
.writeAll()
.getOutputLines(OutputKind.DIRECT);

tb.checkEqual(log, expected);

Path classFilePath = classes.resolve("Ident.class");
var classFile = ClassFile.of().parse(classFilePath);
Assert.check(classFile.minorVersion() == 65535);
Assert.check(classFile.findAttribute(Attributes.loadableDescriptors()).isPresent());

expected = List.of(
"- compiler.warn.preview.feature.use.classfile: Val.class, 28",
"Ident.java:2:12: compiler.warn.declared.using.preview: kindname.class, Val",
"2 warnings"
);

// now with the value class in the classpath
new toolbox.JavacTask(tb)
.options("--enable-preview", "-source", Integer.toString(Runtime.version().feature()), "-cp", classes.toString())
log = new toolbox.JavacTask(tb)
.options("--enable-preview",
"-source", Integer.toString(Runtime.version().feature()),
"-cp", classes.toString(),
"-XDrawDiagnostics",
"-Xlint:preview")
.outdir(classes)
.files(src.resolve("Ident.java"))
.run()
.writeAll();
.writeAll()
.getOutputLines(OutputKind.DIRECT);

tb.checkEqual(log, expected);

classFilePath = classes.resolve("Ident.class");
classFile = ClassFile.of().parse(classFilePath);
Expand All @@ -164,25 +224,52 @@ Val m() {
Path classes = base.resolve("classes");
tb.createDirectories(classes);

new toolbox.JavacTask(tb)
.options("--enable-preview", "-source", Integer.toString(Runtime.version().feature()))
List<String> log;
List<String> expected;

expected = List.of(
"Ident.java:2:5: compiler.warn.declared.using.preview: kindname.class, Val",
"Val.java:1:1: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.value.classes)",
"2 warnings"
);

log = new toolbox.JavacTask(tb)
.options("--enable-preview",
"-source", Integer.toString(Runtime.version().feature()),
"-XDrawDiagnostics",
"-Xlint:preview")
.outdir(classes)
.files(findJavaFiles(src))
.run()
.writeAll();
.writeAll()
.getOutputLines(OutputKind.DIRECT);

tb.checkEqual(log, expected);

Path classFilePath = classes.resolve("Ident.class");
var classFile = ClassFile.of().parse(classFilePath);
Assert.check(classFile.minorVersion() == 65535);
Assert.check(classFile.findAttribute(Attributes.loadableDescriptors()).isPresent());

expected = List.of(
"Ident.java:2:5: compiler.warn.declared.using.preview: kindname.class, Val",
"Val.java:1:1: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.value.classes)",
"2 warnings"
);

// now with the value class in the classpath
new toolbox.JavacTask(tb)
.options("--enable-preview", "-source", Integer.toString(Runtime.version().feature()), "-cp", classes.toString())
.options("--enable-preview",
"-source", Integer.toString(Runtime.version().feature()), "-cp", classes.toString(),
"-XDrawDiagnostics",
"-Xlint:preview")
.outdir(classes)
.files(src.resolve("Ident.java"))
.run()
.writeAll();
.writeAll()
.getOutputLines(OutputKind.DIRECT);

tb.checkEqual(log, expected);

classFilePath = classes.resolve("Ident.class");
classFile = ClassFile.of().parse(classFilePath);
Expand Down