Skip to content

Commit

Permalink
Verify error with primitive patterns involving boxed types
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-sankaran committed Nov 26, 2024
1 parent a589ad2 commit e1e6a71
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.jdt.internal.compiler.ast;

import org.eclipse.jdt.internal.compiler.ast.Pattern.PrimitiveConversionRoute;
import org.eclipse.jdt.internal.compiler.codegen.BranchLabel;
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
import org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
Expand All @@ -25,7 +24,7 @@
*/
interface IGenerateTypeCheck {

default void generateTypeCheck(TypeBinding providedType, TypeReference expectedTypeRef, BlockScope scope, CodeStream codeStream, BranchLabel falseLabel, PrimitiveConversionRoute route) {
default void generateTypeCheck(TypeBinding providedType, TypeReference expectedTypeRef, BlockScope scope, CodeStream codeStream, PrimitiveConversionRoute route) {
switch (route) {
case IDENTITY_CONVERSION -> {
consumeProvidedValue(providedType, codeStream);
Expand All @@ -46,8 +45,7 @@ default void generateTypeCheck(TypeBinding providedType, TypeReference expectedT
}
case WIDENING_REFERENCE_AND_UNBOXING_COVERSION,
WIDENING_REFERENCE_AND_UNBOXING_COVERSION_AND_WIDENING_PRIMITIVE_CONVERSION -> {
codeStream.ifnull(falseLabel);
codeStream.iconst_1();
codeStream.instance_of(scope.getJavaLangObject());
setPatternIsTotalType();
}
case NARROWING_AND_UNBOXING_CONVERSION -> {
Expand All @@ -56,8 +54,7 @@ default void generateTypeCheck(TypeBinding providedType, TypeReference expectedT
}
case UNBOXING_CONVERSION,
UNBOXING_AND_WIDENING_PRIMITIVE_CONVERSION -> {
codeStream.ifnull(falseLabel);
codeStream.iconst_1();
codeStream.instance_of(scope.getJavaLangObject());
setPatternIsTotalType();
}
case NO_CONVERSION_ROUTE -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void generateOptimizedBoolean(BlockScope currentScope, CodeStream codeStr
route = this.testContextRecord.route();
providedType = this.testContextRecord.right();
}
generateTypeCheck(providedType, this.type, currentScope, codeStream, internalFalseLabel, route);
generateTypeCheck(providedType, this.type, currentScope, codeStream, route);

if (this.pattern != null) {
codeStream.ifeq(internalFalseLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ original record instance as receiver - leaving the stack drained.
if (!p.isUnnamed())
codeStream.dup(componentType); // lastComponent ? named ? ([C, C] : [R, C, C]) : ([C] : [R, C])
if (p instanceof TypePattern) {
((TypePattern) p).generateTypeCheck(currentScope, codeStream, matchFailLabel);
((TypePattern) p).generateTypeCheck(currentScope, codeStream);
} else {
codeStream.instance_of(p.resolvedType); // lastComponent ? named ? ([C, boolean] : [R, C, boolean]) : ([boolean] : [R, boolean])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream, BranchL
}
}

public void generateTypeCheck(BlockScope scope, CodeStream codeStream, BranchLabel internalFalseLabel) {
generateTypeCheck(this.outerExpressionType, getType(), scope, codeStream, internalFalseLabel,
public void generateTypeCheck(BlockScope scope, CodeStream codeStream) {
generateTypeCheck(this.outerExpressionType, getType(), scope, codeStream,
Pattern.findPrimitiveConversionRoute(this.resolvedType, this.accessorMethod.returnType, scope));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7281,6 +7281,56 @@ public static void main(String[] args) {
},
"int:30");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3181
// VerifyError during conversion between T extends Long and double
public void testGH3181() {
runConformTest(new String[] {
"X.java",
"""
record Record<T extends Long>(T t) {
}
public class X {
public static <T extends Long> double foo(Record<T> s) {
return switch (s) {
case Record(double s1) -> s1 + 1.0;
default -> 0;
};
}
public static void main(String[] args) {
System.out.println(foo(new Record<>(42L)));
}
}
"""
},
"43.0");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3129
// VerifyError with instanceof record patterns with conversion from double to Long
public void testGH3129() {
runConformTest(new String[] {
"X.java",
"""
record Record(Long i) {
}
public class X {
public static double convert(Record r) {
return r instanceof Record(double d) ? d + 1.0 : 0;
}
public static void main(String[] args) {
System.out.println(convert(new Record(42L)));
}
}
"""
},
"43.0");
}

public void _testSpec00X() {
runNegativeTest(new String[] {
"X.java",
Expand Down

0 comments on commit e1e6a71

Please sign in to comment.