Skip to content

GROOVY-11916: do not resolve inaccessible types from star imports - #3227

Open
daniellansun wants to merge 8 commits into
apache:masterfrom
daniellansun:GROOVY-11916
Open

daniellansun wants to merge 8 commits into
apache:masterfrom
daniellansun:GROOVY-11916

Conversation

@daniellansun

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/GROOVY-11916

Star imports, import module, and the implicit default imports resolve only types accessible from the importing class. import java.util.* no longer binds package-private java.util.TimSort; a type use of that simple name fails at compile time.

Same-package package-private types, protected member types used from a subclass, and private member types used from the enclosing nest stay visible. An explicit single-type import and a fully-qualified name still resolve, and Class.forName is unchanged. Static field imports have required accessibility since Groovy 5 and are not changed here.

Testing

  • bugs.Groovy11916

@codecov-commenter

codecov-commenter commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.60870% with 24 lines in your changes missing coverage. Please review.
βœ… Project coverage is 74.6377%. Comparing base (e853f4d) to head (b6bb2c2).

Files with missing lines Patch % Lines
...va/org/codehaus/groovy/control/ResolveVisitor.java 83.4586% 11 Missing and 11 partials ⚠️
...aus/groovy/ast/decompiled/DecompiledClassNode.java 60.0000% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3227        +/-   ##
==================================================
+ Coverage     74.6267%   74.6377%   +0.0110%     
- Complexity      45464      45523        +59     
==================================================
  Files            1708       1708                
  Lines          153429     153531       +102     
  Branches        29526      29566        +40     
==================================================
+ Hits           114499     114592        +93     
- Misses          28408      28414         +6     
- Partials        10522      10525         +3     
Files with missing lines Coverage Ξ”
...va/org/apache/groovy/parser/antlr4/AstBuilder.java 87.1813% <ΓΈ> (ΓΈ)
...aus/groovy/ast/decompiled/DecompiledClassNode.java 87.7358% <60.0000%> (-1.3731%) ⬇️
...va/org/codehaus/groovy/control/ResolveVisitor.java 89.9316% <83.4586%> (-0.9972%) ⬇️

... and 12 files with indirect coverage changes

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • πŸ“¦ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

ClassNode outer = importNode.getType();
if (outer != null) {
ClassNode tmp = new ConstructedNestedClass(outer, name);
if (resolve(tmp, false, false, true) && Modifier.isStatic(tmp.getModifiers())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it is unclear why here only a static class is valid. It may be an issue of context, but I think it needs some explanation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

outer != null covers both import static Type.* and import Type.* when that name resolves to a class (visitClass then stores the class on the import). Both forms look up a member type, and only a static member type is in scope, the same rule as a single static import. A non-static inner class is not part of that star. A member type of an interface is implicitly static, so Map.Entry still matches.

Comment thread src/test/groovy/bugs/Groovy11916.groovy Resolved
Comment thread src/test/groovy/bugs/Groovy11916.groovy Resolved
Comment thread src/test/groovy/bugs/Groovy11916.groovy Resolved
Comment thread src/test/groovy/bugs/Groovy11916.groovy Resolved
@testlens-app

This comment has been minimized.

positioned.sort((a, b) -> {
int byLine = Integer.compare(a.line, b.line);
if (byLine != 0) return byLine;
return Integer.compare(a.column, b.column);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This still gives me a headache, I think we should not depend on the line information for this. But I do not think it is worth blocking the issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi Jochen,

Agreed β€” line numbers are only a stand-in. Source on-demand imports live in three lists, and import module stamps the statement position only on the last expanded package, so the current walk sorts by that position and treats line <= 0 as a customizer import. A proper fix would record declaration order and origin when the import is added, rather than recovering them from the AST coordinates. I would rather not hold this change for that, as you said, and can follow up separately if you would like that cleaned up.

Star imports, module imports, and the implicit default imports now bind
only types accessible from the importing class. A package-private type
such as java.util.TimSort is no longer resolved by import java.util.*,
so a type use fails at compile time.

Same-package package-private types, protected member types used from a
subclass, and private member types used from the enclosing nest remain
visible. Explicit single-type imports and fully-qualified names are
unchanged, as is Class.forName. Static field imports have required
accessibility since Groovy 5.
Type-import-on-demand and static-import-on-demand, including the
implicit java.lang import, bind a simple name only when exactly one
accessible type is imported. Two or more accessible types are a
compile-time error. An inaccessible type is not imported.

Two programs that depended on the first accessible star winning now
name the type they meant: the Guice example qualifies Module, and
NamedVariantTransformTest single-type-imports groovy.transform.ASTTest.
On-demand simple names follow the Groovy specification. A source
import shadows every default import, including java.lang and star
imports added by ImportCustomizer. Two source on-demand imports
bind the first accessible type in source order, and a module import
is that same kind of star import placed at its statement. An
inaccessible type is not a candidate.

An explicit single-type import, a fully qualified name, and
Class.forName can still name a type that is not public.
Explain why a type star binds only a static member type, and why
on-demand order is two lists rather than one sort of the raw positions.
Drop the sequence key, since List.sort is stable, and do not cache the
lists on the module. Read @PackageScope while resolve still sees a
synthetic public class. Say in the tests what the modifier checks, the
TimSort script, and the three source-order matrices are checking.
ConstructedNestedClass does not override getOuterClass(): resolve redirects
the node first, and returning the construction-time outer would change later
callers that still hold that node. The fixture comments now separate the
flag lock from the accessibility policy, and state why an explicit import of
a non-public type still resolves.
An earlier source on-demand import that finds a type but cannot access
it no longer lets a later source import bind a different type. The name
falls through to customizer imports and the language defaults. Two
accessible source stars still bind the earlier type.

Membership of a class read from bytecode comes from the InnerClasses
attribute, or from Class.getDeclaringClass when the class was loaded.
A top-level class whose name contains '$' is not treated as nested.
Move the DecompiledClassNode import next to the other ast imports.
Customizer stars are tried when no source statement bound the name,
including when one claimed an inaccessible type. Document that
getMemberDeclaringClassName reads the last InnerClasses entry.
Describe one module import as a single statement. Rename the
source-star matrix test and close its temporary class loader.

This branch has not been deployed

No deployments
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