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
21 changes: 19 additions & 2 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ private void addBindings2Scope(JCStatement introducingStatement,
}

public void visitEnhancedVariableDeclaration(JCEnhancedVariableDeclaration tree) {
attribExpr(tree.expr, env);
attribExpr(tree.expr, env, patternTarget(tree.pattern));
attribExpr(tree.pattern, env, tree.expr.type);

matchBindings.bindingsWhenTrue.forEach(env.info.scope::enter);
Expand Down Expand Up @@ -4306,7 +4306,8 @@ public void visitRecordPattern(JCRecordPattern tree) {
tree.record = syms.errSymbol;
site = tree.type = types.createErrorType(tree.record.type);
} else {
Type type = attribType(tree.deconstructor, env);
Type type = tree.deconstructor.type != null ?
tree.deconstructor.type : attribType(tree.deconstructor, env);
if (type.isRaw() && type.tsym.getTypeParameters().nonEmpty()) {
Type inferred = infer.instantiatePatternType(resultInfo.pt, type.tsym);
if (inferred == null) {
Expand Down Expand Up @@ -5649,6 +5650,22 @@ public void visitModuleDef(JCModuleDecl tree) {
}
}

private Type patternTarget(JCTree pattern) {
if (pattern instanceof JCRecordPattern record) {
if (record.deconstructor.hasTag(VARTYPE)) {
return Type.noType;
}
Type recordType = record.deconstructor.type != null ?
record.deconstructor.type : attribType(record.deconstructor, env);
if (recordType.hasTag(ERROR) ||
recordType.isRaw() && recordType.tsym.getTypeParameters().nonEmpty()) {
return Type.noType;
}
return recordType;
}
return Type.noType;
}

/** Finish the attribution of a class. */
private void attribClassBody(Env<AttrContext> env, ClassSymbol c) {
JCClassDecl tree = (JCClassDecl)env.tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,8 @@ Type generateReturnConstraints(JCTree tree, Attr.ResultInfo resultInfo,
}
}
Type qtype = inferenceContext.asUndetVar(from);
Type to = resultInfo.pt;
Type to = resultCompatibilityTarget(mt.getReturnType(), resultInfo.pt,

@vicente-romero-oracle vicente-romero-oracle Jul 9, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

probably the highest risk of semantic change is when qtype is an UndetVar, we need to test that case and look for any possible semantic change

inferenceContext, rsInfoInfContext);

if (qtype.hasTag(VOID)) {
to = syms.voidType;
Expand All @@ -416,6 +417,33 @@ Type generateReturnConstraints(JCTree tree, Attr.ResultInfo resultInfo,
return from;
}

/**
* Returns the target for invocation result constraints, lifting a proper
* target to the supertype corresponding to the generic return type. Targets
* free in an enclosing inference context are left unchanged.
*/
private Type resultCompatibilityTarget(Type returnType, Type targetType,
InferenceContext inferenceContext, InferenceContext targetInferenceContext) {
if (returnType.hasTag(CLASS) &&
returnType.getTypeArguments().stream()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

inferenceContext.free(returnType)?

.anyMatch(t -> t.containsAny(inferenceContext.inferencevars)) &&
!targetType.hasTag(NONE) &&
!targetType.hasTag(ERROR) &&
targetType.isReference() &&
!targetInferenceContext.free(targetType) &&

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe this can be replaced by targetType.hasTag(CLASS)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(e.g. the issue here is that a type variable might erase to a class type, which might lead you to lift more cases than needed. E.g. if we lift T to C<...> where T was an inference variable, we kind of lose T).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

not sure I follow, erasure during inference?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's the check in the line below -- which checks if |T| <: |R| where T is the target type and R is the return type. Basically, we're checking here if the relationship is "the wrong way around", and, if so, lift T to the same height as R. That's because with the new narrowing sealing conversion you can have legit cases of |T| <: |R| that are valid assignments.

types.isSubtype(types.erasure(targetType), types.erasure(returnType))) {
Type liftedTarget = lift(targetType, returnType);
if (liftedTarget != null) {
return liftedTarget;
}
}
return targetType;
}

private Type lift(Type targetType, Type returnType) {
return types.asSuper(targetType, returnType.tsym);

@vicente-romero-oracle vicente-romero-oracle Jul 8, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

what if we are dealing with arrays? better to use Infer::asSuper

}

private boolean needsEagerInstantiation(UndetVar from, Type to, InferenceContext inferenceContext) {
if (to.isPrimitive()) {
/* T is a primitive type, and one of the primitive wrapper classes is an instantiation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public void testInferredPatternsNonExhaustive() {
assertEval("interface IBox {}");
assertEval("record BoxImpl(int x) implements IBox {}");

assertDeclareFail("Pair(var x, var y) = new Object();", "compiler.err.enhanced.local.variable.declaration.not.exhaustive.on.type");
assertDeclareFail("Pair(var x, var y) = new Object();", "compiler.err.prob.found.req");
assertDeclareFail("Box(var value) = new Object();", "compiler.err.enhanced.local.variable.declaration.not.exhaustive.on.type");
assertDeclareFail("BoxImpl(var x) = (IBox) new BoxImpl(1);", "compiler.err.enhanced.local.variable.declaration.not.exhaustive.on.type");
assertDeclareFail("BoxImpl(var x) = (IBox) new BoxImpl(1);", "compiler.err.prob.found.req");
assertDeclareFail("Holder(Point(var x)) = new Holder(new Point(1));", "compiler.err.enhanced.local.variable.declaration.not.exhaustive.on.type");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
// options: --enable-preview -source ${jdk.version}

class EnhancedLocalVariableDeclarationNotExhaustiveOnType {
void m(Object o) {
Point(var x) = o;
void m() {
Holder(Point(var x)) = new Holder(new Point(1));
}

record Point(int x) {}
record Holder(Object value) {}
}
Loading
Loading