Skip to content

GROOVY-11375, GROOVY-11769: SC: lookup type of variable before target#2311

Open
eric-milles wants to merge 1 commit intoGROOVY_4_0_Xfrom
GROOVY-11769
Open

GROOVY-11375, GROOVY-11769: SC: lookup type of variable before target#2311
eric-milles wants to merge 1 commit intoGROOVY_4_0_Xfrom
GROOVY-11769

Conversation

@eric-milles
Copy link
Copy Markdown
Member

There may be some side effects from changing StaticTypesTypeChooser in this manner. I cannot recall exactly why this was not ported to 4.x back in June 2024.

see also GROOVY-9344, GROOVY-9607

@eric-milles
Copy link
Copy Markdown
Member Author

One of the related problems from Grails (see GormEntityTransformation) is this:

if (classNode instanceof InnerClassNode || classNode.isEnum())

The second occurrence of classNode gets cast to InnerClassNode due to improper or handling (see GROOVY-7971).

@paulk-asert
Copy link
Copy Markdown
Contributor

I am happy for this to be merged but we still need to sort out GROOVY-7971. I might try to relook at that again soon.

@eric-milles
Copy link
Copy Markdown
Member Author

I'm reluctant to merge myself because GROOVY-11769 is about bytecode optimization, not failing functionality. And GROOVY-11375 was not strong enough on its own to make this change earlier.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR changes static-compilation type resolution to prefer metadata on the current expression before falling back to the accessed target, and adds regression tests around primitive flow typing and object-to-interface casts. In the Groovy codebase, this primarily affects how statically compiled bytecode decides operand and cast types after STC has attached flow-type metadata.

Changes:

  • Update StaticTypesTypeChooser to read inferred/declaration types from the expression first, then fall back to the accessed variable target.
  • Add/override regression tests for primitive flow typing in n-ary expressions and object-to-interface casting under static compilation.
  • Replace a former @NotYetImplemented instanceof test with a new GROOVY-11769 scenario.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/main/java/org/codehaus/groovy/classgen/asm/sc/StaticTypesTypeChooser.java Changes SC type lookup order between expression metadata and accessed-variable metadata.
src/test/groovy/groovy/transform/stc/STCAssignmentTest.groovy Adds assignment/cast regression coverage for object-to-interface conversion.
src/test/groovy/org/codehaus/groovy/classgen/asm/sc/AssignmentsStaticCompileTest.groovy Adds bytecode-level assertion for the cast regression under static compilation.
src/test/groovy/groovy/transform/stc/STCnAryExpressionTest.groovy Adds regression tests for flow-typed primitive shift and power operators.
src/test/groovy/groovy/transform/stc/TypeInferenceSTCTest.groovy Replaces an old instanceof regression test with a new scenario intended to cover GROOVY-11769.

if (inferredType == null) {
inferredType = target.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
ASTNode node = getTarget(exp); // GROOVY-9344, GROOVY-9607
inferredType = node.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
Comment on lines +244 to 245
// GROOVY-11769
void testInstanceOf10() {

void test(Foo foo) {
if (foo instanceof Bar || foo.isBaz()) {
foo.toString()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants