Skip to content

Commit 272a191

Browse files
committed
Move NR1S cast handling from Lower to TransTypes
1 parent c351ca9 commit 272a191

File tree

8 files changed

+65
-50
lines changed

8 files changed

+65
-50
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ public static Lower instance(Context context) {
106106
private final boolean optimizeOuterThis;
107107
private final boolean nullCheckOuterThis;
108108
private final boolean useMatchException;
109-
private final boolean allowEnhancedVariableDecls;
110109
private final HashMap<TypePairs, String> typePairToName;
111110
private int variableIndex = 0;
112111

@@ -142,8 +141,6 @@ protected Lower(Context context) {
142141
Preview preview = Preview.instance(context);
143142
useMatchException = Feature.PATTERN_SWITCH.allowedInSource(source) &&
144143
(preview.isEnabled() || !preview.isPreview(Feature.PATTERN_SWITCH));
145-
allowEnhancedVariableDecls = Feature.ENHANCED_VARIABLE_DECLS.allowedInSource(source) &&
146-
(preview.isEnabled() || !preview.isPreview(Feature.ENHANCED_VARIABLE_DECLS));
147144
typePairToName = TypePairs.initialize(syms);
148145
}
149146

@@ -3735,10 +3732,6 @@ public void visitVarDef(JCVariableDecl tree) {
37353732
try {
37363733
if (tree.init != null) {
37373734
tree.init = translate(tree.init, tree.type);
3738-
3739-
if (allowEnhancedVariableDecls && types.isNR1S(tree.init.type, tree.vartype.type)){
3740-
tree = make.VarDef(tree.sym, make.TypeCast(tree.vartype.type, tree.init)) ;
3741-
}
37423735
}
37433736

37443737
result = tree;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
3030
import com.sun.tools.javac.code.*;
3131
import com.sun.tools.javac.code.Attribute.TypeCompound;
32+
import com.sun.tools.javac.code.Source.Feature;
3233
import com.sun.tools.javac.code.Symbol.*;
3334
import com.sun.tools.javac.code.Type.TypeVar;
3435
import com.sun.tools.javac.jvm.Target;
@@ -78,6 +79,7 @@ public static TransTypes instance(Context context) {
7879
private final Resolve resolve;
7980
private final CompileStates compileStates;
8081
private final Target target;
82+
private final boolean allowEnhancedVariableDecls;
8183

8284
@SuppressWarnings("this-escape")
8385
protected TransTypes(Context context) {
@@ -93,6 +95,10 @@ protected TransTypes(Context context) {
9395
annotate = Annotate.instance(context);
9496
attr = Attr.instance(context);
9597
target = Target.instance(context);
98+
Source source = Source.instance(context);
99+
Preview preview = Preview.instance(context);
100+
allowEnhancedVariableDecls = Feature.ENHANCED_VARIABLE_DECLS.allowedInSource(source) &&
101+
(preview.isEnabled() || !preview.isPreview(Feature.ENHANCED_VARIABLE_DECLS));
96102
}
97103

98104
/** Construct an attributed tree for a cast of expression to target type,
@@ -131,7 +137,11 @@ public JCExpression coerce(Env<AttrContext> env, JCExpression tree, Type target)
131137
JCExpression coerce(JCExpression tree, Type target) {
132138
Type btarget = target.baseType();
133139
if (tree.type.isPrimitive() == target.isPrimitive()) {
134-
return types.isAssignable(tree.type, btarget, types.noWarnings)
140+
boolean assignable = types.isAssignable(tree.type, btarget, types.noWarnings);
141+
if (allowEnhancedVariableDecls && types.isNR1S(tree.type, btarget)) {
142+
assignable = false;
143+
}
144+
return assignable
135145
? tree
136146
: cast(tree, btarget);
137147
}

test/langtools/tools/javac/patterns/AssignmentNR1S.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,64 @@
11
/*
22
* @test /nodynamiccopyright/
33
* @summary
4-
* @compile/fail/ref=AssignmentNR1S.out -XDrawDiagnostics AssignmentNR1S.java
5-
* @compile/fail/ref=AssignmentNR1SPreview.out --enable-preview --source ${jdk.version} -XDrawDiagnostics AssignmentNR1S.java
4+
* @compile/fail/ref=AssignmentNR1SNoPreview.out -XDrawDiagnostics AssignmentNR1S.java
5+
* @compile --enable-preview --source ${jdk.version} AssignmentNR1S.java
6+
* @run main/othervm --enable-preview AssignmentNR1S
67
*/
78
import java.util.Objects;
89
import java.util.List;
910

1011
public class AssignmentNR1S {
1112
public static void main(String[] args) {
12-
method0();
13-
method1();
14-
method2();
15-
method3();
16-
method4();
17-
method5();
18-
method6();
13+
assignmentFromSealedSupertype();
14+
arrayStoreFromSealedSupertype();
15+
returnFromSealedSupertype();
16+
genericAssignmentWithTypeVariable();
17+
interfaceToRecordAssignment();
18+
reassignmentFromSealedInterface();
19+
enhancedForElementNarrowing();
20+
returnFromParameterSealedSupertype(new SB1());
1921
}
2022

2123
static sealed abstract class SA1 permits SB1 {}
2224
static final class SB1 extends SA1 {}
23-
static void method0() {
25+
static void assignmentFromSealedSupertype() {
2426
SA1 sa = new SB1();
2527
SB1 sb = sa; // OK
2628
}
27-
static void method1() {
29+
static void arrayStoreFromSealedSupertype() {
2830
SA1 sa = new SB1();
2931
SB1[] sb = new SB1[1];
3032
sb[0] = sa; // ok
3133
}
32-
static SB1 method2() {
34+
static SB1 returnFromSealedSupertype() {
3335
SA1 sa = new SB1();
3436
return sa; // ok
3537
}
3638

3739
static sealed abstract class SA2<T> permits SB2 {}
3840
static final class SB2<T> extends SA2<T> {}
39-
static <T> void method3() {
41+
static <T> void genericAssignmentWithTypeVariable() {
4042
SA2<T> sa = new SB2<>(); // WRC, OK
4143
SB2<T> sb = sa;
4244
}
4345

4446
static record R1(int x) implements IR {}
4547
static sealed interface IR {}
46-
static void method4() {
48+
static void interfaceToRecordAssignment() {
4749
IR ir = new R1(42);
4850
R1 r1 = ir; // OK
4951
}
5052

5153
static sealed interface IFoo permits FooImpl {}
5254
static final class FooImpl implements IFoo {}
53-
static void method5() {
55+
static void reassignmentFromSealedInterface() {
5456
IFoo f = new FooImpl();
5557
FooImpl fi = new FooImpl();
5658
fi = f; // OK
5759
}
5860

59-
static void method6() {
61+
static void enhancedForElementNarrowing() {
6062
List<IFoo> fs = List.of(new FooImpl(), new FooImpl());
6163
int count = 0;
6264
for (FooImpl fi : fs) {
@@ -65,17 +67,8 @@ static void method6() {
6567
assertEquals(2, count);
6668
}
6769

68-
static void method7() {
69-
SA2<Integer> sa = new SB2<>();
70-
SB2<String> sb = sa; // always error: under preview or non-preview
71-
}
72-
73-
static sealed interface SI permits Mid {}
74-
static non-sealed interface Mid extends SI {}
75-
static final class Leaf implements Mid {}
76-
static void method8() {
77-
SI si = new Leaf();
78-
Leaf leaf = si; // always error: under preview or non-preview
70+
static SB1 returnFromParameterSealedSupertype(SA1 sa) {
71+
return sa; // OK
7972
}
8073

8174
static void assertEquals(Object expected, Object actual) {

test/langtools/tools/javac/patterns/AssignmentNR1S.out

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AssignmentNR1S.java:27:18: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.SA1, AssignmentNR1S.SB1)
2+
AssignmentNR1S.java:32:17: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.SA1, AssignmentNR1S.SB1)
3+
AssignmentNR1S.java:36:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.SA1, AssignmentNR1S.SB1)
4+
AssignmentNR1S.java:43:21: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.SA2<T>, AssignmentNR1S.SB2<T>)
5+
AssignmentNR1S.java:50:17: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.IR, AssignmentNR1S.R1)
6+
AssignmentNR1S.java:58:14: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.IFoo, AssignmentNR1S.FooImpl)
7+
AssignmentNR1S.java:64:27: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.IFoo, AssignmentNR1S.FooImpl)
8+
AssignmentNR1S.java:71:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1S.SA1, AssignmentNR1S.SB1)
9+
8 errors

test/langtools/tools/javac/patterns/AssignmentNR1SPreview.out

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @summary Ensure non-NR1S assignments stay rejected in all modes
4+
* @compile/fail/ref=AssignmentNR1SRegressionErrors.out -XDrawDiagnostics AssignmentNR1SRegressionErrors.java
5+
* @compile/fail/ref=AssignmentNR1SRegressionErrors.out --enable-preview --source ${jdk.version} -XDrawDiagnostics AssignmentNR1SRegressionErrors.java
6+
*/
7+
public class AssignmentNR1SRegressionErrors {
8+
static sealed abstract class SA2<T> permits SB2 {}
9+
static final class SB2<T> extends SA2<T> {}
10+
static void rejectMismatchedGenericArguments() {
11+
SA2<Integer> sa = new SB2<>();
12+
SB2<String> sb = sa; // always error
13+
}
14+
15+
static sealed interface SI permits Mid {}
16+
static non-sealed interface Mid extends SI {}
17+
static final class Leaf implements Mid {}
18+
static void rejectIndirectPermittedSubtypeAssignment() {
19+
SI si = new Leaf();
20+
Leaf leaf = si; // always error
21+
}
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
AssignmentNR1SRegressionErrors.java:12:26: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1SRegressionErrors.SA2<java.lang.Integer>, AssignmentNR1SRegressionErrors.SB2<java.lang.String>)
2+
AssignmentNR1SRegressionErrors.java:20:21: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: AssignmentNR1SRegressionErrors.SI, AssignmentNR1SRegressionErrors.Leaf)
3+
2 errors

0 commit comments

Comments
 (0)