Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
aa2cf0d
GROOVY-12510: STC: reject a private superclass method called through …
codeconsole Oct 25, 2026
50c436e
GROOVY-12509: STC: allow private methods and constructors of another …
codeconsole Oct 25, 2026
5a679be
GROOVY-12509: STC: allow private accessors of another class of the sa…
codeconsole Oct 25, 2026
bf75cb4
GROOVY-12511: SC: use the enclosing instance for a private member of …
codeconsole Oct 25, 2026
ee9162d
GROOVY-12513: STC: allow a private field of a variable's declared typ…
codeconsole Nov 22, 2026
9015ff5
GROOVY-12509, GROOVY-12510, GROOVY-12511: document private access bet…
codeconsole Oct 25, 2026
d73831d
GROOVY-12511: keep a private static field of the superclass reachable…
codeconsole Oct 26, 2026
bbe3b81
GROOVY-12523: STC: reject x.@name for a private field of a superclass…
codeconsole Oct 26, 2026
fb8c270
GROOVY-12523: say which receivers reach a private superclass field di…
codeconsole Nov 8, 2026
d043522
GROOVY-12510: mark StaticTypeCheckingSupport.isInheritedPrivateMethod…
codeconsole Nov 8, 2026
f5b98e5
GROOVY-12513: use a private member of a variable's declared type only…
codeconsole Nov 22, 2026
63f431c
GROOVY-12513: find a private method of a variable's declared type for…
codeconsole Nov 22, 2026
e4e2928
GROOVY-12511: under type checking, let a skipped private field of a s…
codeconsole Nov 22, 2026
f312d8a
GROOVY-12511: resolve this.name for a private accessor of the enclosi…
codeconsole Nov 22, 2026
70c5e07
GROOVY-12523: say in the guide which mode reaches a private superclas…
codeconsole Nov 22, 2026
be77774
GROOVY-12511: under type checking, find an interface constant past a …
codeconsole Nov 22, 2026
f781c36
GROOVY-12523: also reject Sub.this.@name for a private field of the s…
codeconsole Nov 22, 2026
af0a768
GROOVY-12511: SC: call a private method of the enclosing class on the…
codeconsole Nov 22, 2026
9c85a73
GROOVY-12513: state in the guide which receivers reach a private supe…
codeconsole Nov 22, 2026
d88fdcc
GROOVY-12511: keep this.@name out of the enclosing-instance property …
codeconsole Nov 22, 2026
dee4934
GROOVY-12510: invent a property accessor only where Verifier will emi…
codeconsole Nov 22, 2026
12687c7
GROOVY-12510: check the second pass for a closure shared variable in …
codeconsole Nov 22, 2026
e0e8fd4
GROOVY-12513: SC: accept a private setter of a variable's declared ty…
codeconsole Nov 22, 2026
40a571a
GROOVY-12511: SC: call any method of an enclosing instance of the rec…
codeconsole Nov 22, 2026
d025abb
GROOVY-12511: under type checking, find a private accessor of the enc…
codeconsole Nov 22, 2026
79b952e
GROOVY-12511: say in the guide where an inner class reaches the enclo…
codeconsole Nov 22, 2026
08c62ac
GROOVY-12510, GROOVY-12523, GROOVY-12637: mark the regression tests t…
codeconsole Nov 22, 2026
57228ef
GROOVY-12511: give the guide's sub.name sentence its subject, and des…
codeconsole Nov 22, 2026
af52046
GROOVY-12510, GROOVY-12511: document the member rules of findMethod a…
codeconsole Nov 22, 2026
2e37bb9
GROOVY-12510: document findMethodWithDeclaredType
codeconsole Nov 22, 2026
921ac87
GROOVY-12511: ++, -- and compound assignment with an operator method …
codeconsole Nov 23, 2026
343dd5c
GROOVY-12510: findVisibleMethods: an instance with no enclosing insta…
codeconsole Nov 23, 2026
034df1e
GROOVY-12511: x op= y uses a *Assign method of the receiver only, and…
codeconsole Nov 23, 2026
2c03ea1
GROOVY-12510: method pointers and references keep a private method of…
codeconsole Nov 23, 2026
2889534
GROOVY-12511: follow a closure-valued this$0 to the closure's this ob…
codeconsole Nov 23, 2026
d8c93cb
GROOVY-12513: b.p = v selects the setter of a narrowed variable as b.…
codeconsole Nov 23, 2026
e024391
GROOVY-12511: look up methods of an enclosing instance from the neare…
codeconsole Nov 23, 2026
57b71aa
GROOVY-12509: a private isX that a receiver only inherits leaves the …
codeconsole Nov 23, 2026
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 @@ -18,6 +18,7 @@
*/
package org.codehaus.groovy.classgen.asm.sc;

import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
Expand Down Expand Up @@ -98,6 +99,7 @@
import static org.codehaus.groovy.ast.tools.GeneralUtils.attrX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.bytecodeX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.inSamePackage;
Expand Down Expand Up @@ -260,7 +262,20 @@ private static MethodNode getObjectMethod(final MethodNode method) {
return objectMethod != null && objectMethod.isPublic() && !objectMethod.isStatic() ? objectMethod : null;
}

/**
* Returns the expression for the instance of {@code target} that code in {@code source} uses, which
* may be an enclosing instance of a subclass of {@code target}, see {@link #thisObjectExpression(ClassNode, ClassNode, boolean)}.
*/
private Expression thisObjectExpression(final ClassNode source, final ClassNode target) {
return thisObjectExpression(source, target, false);
}

/**
* Returns the expression for the instance of {@code target} that code in
* {@code source} uses: "this", or an enclosing instance. With {@code exact},
* the enclosing instance must be of {@code target} itself, not a subclass.
*/
private Expression thisObjectExpression(final ClassNode source, final ClassNode target, final boolean exact) {
ClassNode thisType = source;
while (ClassHelper.isGeneratedFunction(thisType)) {
thisType = thisType.getOuterClass();
Expand All @@ -273,21 +288,57 @@ private Expression thisObjectExpression(final ClassNode source, final ClassNode
} else {
thisExpr = varX("thisObject", thisType);
// adjust for multiple levels of nesting
while (!isSubtype(target, thisType)) {
FieldNode thisZero = thisType.getDeclaredField("this$0");
if (thisZero != null) {
thisExpr = attrX(thisExpr, "this$0");
thisExpr.setType(thisZero.getType());
thisType = thisType.getOuterClass();
if (thisType != null) continue;
}
break;
while (exact ? !thisType.equals(target) : !isSubtype(target, thisType)) {
Expression outer = enclosingInstanceOf(thisExpr, thisType);
if (outer == null) break;
thisExpr = outer;
thisType = thisType.getOuterClass();
if (thisType == null) break;
}
}
return thisExpr;
}


/**
* Returns the expression for the enclosing instance of {@code receiver} that
* has {@code method}: the chain of {@code this$0} fields from the receiver's
* class out to that outer class. {@code receiverType} is the type that the
* receiver is written with here, {@code selectedType} the type that the type
* checker selected the method for, which a safe or spread call writes as a
* wider type.
*/
private static Expression enclosingInstanceExpression(final Expression receiver, final ClassNode receiverType, final ClassNode selectedType, final MethodNode method) {
ClassNode outer = StaticTypeCheckingSupport.findEnclosingInstanceTypeWithMethod(selectedType, method);
if (outer == null) throw new GroovyBugError("no enclosing instance with " + method.getTypeDescriptor() + " for " + selectedType.getName());
Expression expr = receiverType.equals(selectedType) ? receiver : castX(selectedType, receiver);
for (ClassNode type = selectedType; !type.equals(outer); type = type.getOuterClass()) {
expr = enclosingInstanceOf(expr, type);
if (expr == null) throw new GroovyBugError("no enclosing instance of " + outer.getName() + " for " + selectedType.getName());
}
return expr;
}

/**
* Returns the expression for the enclosing instance of {@code expr}, an instance
* of {@code type}: its {@code this$0} field or, for a class created in a closure,
* the this object of that closure, as for {@code Outer.this}; or {@code null} if
* {@code type} has no enclosing instance.
*/
private static Expression enclosingInstanceOf(final Expression expr, final ClassNode type) {
FieldNode thisZero = type.getDeclaredField("this$0");
if (thisZero == null) return null;
Expression outer = attrX(expr, "this$0");
outer.setType(thisZero.getType());
if (ClassHelper.CLOSURE_TYPE.equals(thisZero.getType()) && type.getOuterClass() != null) {
MethodCallExpression thisObject = callX(outer, "getThisObject");
thisObject.setImplicitThis(false);
thisObject.setMethodTarget(ClassHelper.CLOSURE_TYPE.getMethod("getThisObject", Parameter.EMPTY_ARRAY));
outer = castX(type.getOuterClass(), thisObject);
}
return outer;
}

/**
* Attempts to make a direct method call on a bridge method, if it exists.
*/
Expand Down Expand Up @@ -433,22 +484,30 @@ protected boolean writeDirectMethodCall(final MethodNode target, final boolean i
boolean isThisImplicit = implicitThis;
Expression theReceiver = receiver;
MethodNode theTarget = target;
if (implicitThis && isThisExpression(receiver)
&& (!isSubtype(declaringClass, enclosingClass)
ClassNode thisType = enclosingClass;
while (ClassHelper.isGeneratedFunction(thisType)) {
thisType = thisType.getOuterClass();
}
boolean enclosingPrivate = isPrivateMethodOfEnclosingInstance(target, thisType);
if (isThisExpression(receiver) && (implicitThis
? !isSubtype(declaringClass, enclosingClass) || enclosingPrivate
// a method of Object called in a closure or lambda is not called on it,
// unless it was resolved against the closure itself (TO_SELF)
|| (ClassHelper.isObjectType(declaringClass) && controller.isInGeneratedFunction()
&& !isSubtype(ClassHelper.CLOSURE_TYPE, receiverType)))) {
&& !isSubtype(ClassHelper.CLOSURE_TYPE, receiverType))
: !controller.isStaticContext() && (!isSubtype(declaringClass, thisType) || enclosingPrivate)
&& isMemberOfOuterClass(declaringClass, thisType, enclosingPrivate))) {
if (target.isStatic()) {
theReceiver = classX(declaringClass);
} else if (controller.isStaticMethod() && !controller.isInGeneratedFunction()) {
theReceiver = classX(enclosingClass); // method of the Class object, like getName()
} else if (!controller.isInGeneratedFunction()) {
ClassNode outerType = getImplicitReceiverType(enclosingClass, declaringClass);
// a private method is called on the instance of its declaring class, not of a subclass
ClassNode outerType = enclosingPrivate ? null : getImplicitReceiverType(enclosingClass, declaringClass);
theReceiver = propX(classX(outerType != null ? outerType : declaringClass), "this");
} else {
theReceiver = thisObjectExpression(enclosingClass, declaringClass);
MethodNode objectMethod = getObjectMethod(target);
theReceiver = thisObjectExpression(enclosingClass, declaringClass, enclosingPrivate);
MethodNode objectMethod = implicitThis ? getObjectMethod(target) : null;
if (objectMethod != null && !enclosingClass.implementsInterface(ClassHelper.GENERATED_LAMBDA_TYPE)) {
// a closure without a this object, e.g. after rehydrate(), uses its owner's method,
// or its own method when it has no owner either, e.g. after dehydrate()
Expand All @@ -458,6 +517,11 @@ protected boolean writeDirectMethodCall(final MethodNode target, final boolean i
}
if (!(theReceiver instanceof VariableExpression)) isThisImplicit = false;
theReceiver.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, theTarget.getDeclaringClass());
} else if (receiver != null && !target.isStatic() && currentCall != null
&& currentCall.getNodeMetaData(StaticTypesMarker.ENCLOSING_INSTANCE_OF_RECEIVER) instanceof ClassNode selectedType) {
// GROOVY-12511: the method is one of an enclosing instance of the receiver, not of the receiver
theReceiver = enclosingInstanceExpression(receiver, receiverType, selectedType, target);
theReceiver.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, theReceiver.getType());
}
if (theReceiver != null && !target.isStatic() && !isSuperExpression(receiver) && !isClassWithSuper(receiver)
&& currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER) == null) {
Expand All @@ -484,6 +548,31 @@ protected boolean writeDirectMethodCall(final MethodNode target, final boolean i
}
}

/**
* Determines if a call on "this", in code of {@code thisType}, to the given
* method is a call to a private method of an enclosing class. A private
* method is not inherited (JLS 8.2), so a nested class that extends the
* declaring class calls the method on its enclosing instance, not on itself.
* A nested class with no enclosing instance, such as an enum constant body,
* calls it on itself.
*/
private static boolean isPrivateMethodOfEnclosingInstance(final MethodNode target, final ClassNode thisType) {
return target.isPrivate() && !target.isStatic() && !target.getDeclaringClass().equals(thisType)
&& thisType.getDeclaredField("this$0") != null;
}

/**
* Determines if an outer class of {@code enclosingClass} is {@code declaringClass}
* or, unless {@code exact}, one of its subclasses: then this.name() in an inner
* class names a method of an enclosing instance, as in dynamic Groovy.
*/
private static boolean isMemberOfOuterClass(final ClassNode declaringClass, final ClassNode enclosingClass, final boolean exact) {
for (ClassNode outer = enclosingClass.getOuterClass(); outer != null; outer = outer.getOuterClass()) {
if (exact ? outer.equals(declaringClass) : isSubtype(declaringClass, outer)) return true;
}
return false;
}

/**
* Returns the trait helper method behind {@code target} when {@code target} is a
* static method of a trait, which the trait interface forwards to its helper with
Expand Down Expand Up @@ -811,6 +900,7 @@ public void makeCall(final Expression origin, final Expression receiver, final E
newMCE.setImplicitThis(false);
MethodNode target = oldMCE.getMethodTarget();
newMCE.setMethodTarget(target);
newMCE.putNodeMetaData(StaticTypesMarker.ENCLOSING_INSTANCE_OF_RECEIVER, oldMCE.getNodeMetaData(StaticTypesMarker.ENCLOSING_INSTANCE_OF_RECEIVER));
if (target == null || !target.isVoidMethod())
newMCE.setNodeMetaData(StaticTypesMarker.INFERRED_TYPE, valuesType);
newMCE.setSafe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.codehaus.groovy.classgen.AsmClassGenerator.ELIDE_EXPRESSION_VALUE;
import static org.codehaus.groovy.syntax.Types.LEFT_SQUARE_BRACKET;
import static org.codehaus.groovy.transform.sc.StaticCompilationVisitor.ARRAYLIST_ADD_METHOD;
import static org.codehaus.groovy.transform.sc.StaticCompilationVisitor.ARRAYLIST_CLASSNODE;
import static org.codehaus.groovy.transform.sc.StaticCompilationVisitor.ARRAYLIST_CONSTRUCTOR;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isAssignment;
import static org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.inferLoopElementType;
import static org.codehaus.groovy.transform.stc.StaticTypesMarker.COMPOUND_ASSIGN_TARGET;
import static org.codehaus.groovy.transform.stc.StaticTypesMarker.DIRECT_METHOD_CALL_TARGET;
import static org.codehaus.groovy.transform.stc.StaticTypesMarker.ENCLOSING_INSTANCE_OF_RECEIVER;
import static org.codehaus.groovy.transform.stc.StaticTypesMarker.INFERRED_TYPE;
import static org.codehaus.groovy.transform.stc.StaticTypesMarker.SUPER_MOP_METHOD_REQUIRED;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
Expand Down Expand Up @@ -116,6 +118,8 @@ protected void writePostOrPrefixMethod(final int op, final String method, final
call.setMethodTarget(mn);
call.setImplicitThis(false);
call.setSourcePosition(orig);
// GROOVY-12511: next() or previous() of an enclosing instance of the operand is called on that instance
call.putNodeMetaData(ENCLOSING_INSTANCE_OF_RECEIVER, orig.getNodeMetaData(ENCLOSING_INSTANCE_OF_RECEIVER));
call.visit(controller.getAcg());
controller.getCompileStack().removeVar(slot);
return;
Expand Down Expand Up @@ -148,6 +152,29 @@ private static void visitInsnByType(final ClassNode top, final MethodVisitor mv,
}
}

/**
* Writes the read of {@code x[i]} in {@code x[i]++} or {@code x[i] += v} as the
* {@code getAt} of an enclosing instance of {@code x} where the type checker
* selected one (GROOVY-12511), as dynamic Groovy reads it; the write stays the
* {@code putAt} of {@code x}.
*/
@Override
protected void evaluateBinaryExpression(final String message, final BinaryExpression binExp) {
if (binExp.getOperation().getType() == LEFT_SQUARE_BRACKET
&& binExp.getNodeMetaData(ENCLOSING_INSTANCE_OF_RECEIVER) != null
&& binExp.getNodeMetaData(DIRECT_METHOD_CALL_TARGET) instanceof MethodNode getAt) {
MethodCallExpression call = callX(binExp.getLeftExpression(), "getAt", binExp.getRightExpression());
call.setImplicitThis(false);
call.setSafe(binExp.isSafe());
call.setMethodTarget(getAt);
call.putNodeMetaData(ENCLOSING_INSTANCE_OF_RECEIVER, binExp.getNodeMetaData(ENCLOSING_INSTANCE_OF_RECEIVER));
call.setSourcePosition(binExp);
call.visit(controller.getAcg());
return;
}
super.evaluateBinaryExpression(message, binExp);
}

/** {@inheritDoc} */
@Override
protected void evaluateBinaryExpressionWithAssignment(final String method, final BinaryExpression expression) {
Expand Down
Loading
Loading