Repository navigation
Description
Affects PMD Version: 7.27.0 (latest release) and 7.23.0.
Description:
PMD reports IllegalArgumentException: <?> cannot be a wildcard bound when analyzing a lambda passed to Iterable.forEach through a type variable whose intersection bound is Iterable<?> & Marker.
The complete reproducer is seven lines, uses only JDK types and an empty marker interface, and compiles without warnings. No external dependencies, auxiliary classpath, build tool, or custom rules are needed.
Code sample — failing/Reproducer.java:
class Reproducer {
interface Marker { }
static <C extends Iterable<?> & Marker> void visit(C values) {
values.forEach(value -> { });
}
}Similar exception for method refs:
import java.util.function.Consumer;
class Reproducer {
interface Marker { }
static <C extends Iterable<?> & Marker> void visit(C values) {
Consumer<Consumer<Object>> f = values::forEach;
}
}Steps to reproduce:
Download and extract the official PMD 7.27.0 binary distribution. Save the code above as failing/Reproducer.java, then run from the directory containing failing and pmd-bin-7.27.0:
javac --release 17 -Xlint:all -Werror -d classes/failing failing/Reproducer.java
./pmd-bin-7.27.0/bin/pmd check \
-d failing/Reproducer.java \
-R category/java/errorprone.xml/CloseResource \
-f xml --no-cache --threads 1 --no-progress \
> failing-report.xml 2> failing-stderr.txtjavac exits 0 with empty stdout/stderr. PMD exits 5. Its XML report on stdout contains one <error> and no violations. Stderr ends with:
[INFO] Found no violations. There was 1 processing error.
Expected: analysis completes without a processing error. CloseResource is a single built-in rule used to trigger analysis; this example has no resource usage.
Workaround / passing control — workaround/Reproducer.java:
Introduce a local variable of type Iterable<?>, retaining the method signature and lambda:
class Reproducer {
interface Marker { }
static <C extends Iterable<?> & Marker> void visit(C values) {
Iterable<?> direct = values;
direct.forEach(value -> { });
}
}javac --release 17 -Xlint:all -Werror -d classes/workaround workaround/Reproducer.java
./pmd-bin-7.27.0/bin/pmd check \
-d workaround/Reproducer.java \
-R category/java/errorprone.xml/CloseResource \
-f xml --no-cache --threads 1 --no-progress \
> workaround-report.xml 2> workaround-stderr.txtBoth commands exit 0. PMD's XML report contains no errors or violations; stderr ends with [INFO] Found no violations.
Two further controls also compile cleanly and pass PMD 7.27.0: remove & Marker from the failing signature, or replace Iterable<?> with Iterable<String>.
Verified environment and results:
Official PMD CLI binary distributions on macOS 26.6.2 / arm64. The same JDK was used for javac and PMD in each row; all compilations used --release 17 -Xlint:all -Werror.
| PMD | JDK runtime | Failing PMD exit | Workaround PMD exit |
|---|---|---|---|
| 7.27.0 | Temurin 17.0.20+8 | 5 (1 error) | 0 |
| 7.27.0 | Corretto 25.0.2+10-LTS | 5 (1 error) | 0 |
| 7.23.0 | Temurin 17.0.20+8 | 5 (1 error) | 0 |
| 7.23.0 | Corretto 25.0.2+10-LTS | 5 (1 error) | 0 |
Exception stacktrace (7.27.0 / JDK 17):
Full processing-error stacktrace
org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.IllegalArgumentException: <?> cannot be a wildcard bound
Exception Context:
[1:Resolving type of=!debug only! [MethodCall:5:9]values.forEach(value -> { })]
[2:Location=!debug only! failing/Reproducer.java:5:9]
---------------------------------
at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:265)
at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:54)
at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror(ASTMethodCall.java:22)
at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:39)
at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.getTypeMirror(ASTMethodCall.java:22)
at net.sourceforge.pmd.lang.java.ast.InternalApiBridge.lambda$forceTypeResolutionPhase$0(InternalApiBridge.java:99)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
at net.sourceforge.pmd.lang.ast.internal.IteratorBasedNStream.forEach(IteratorBasedNStream.java:101)
at net.sourceforge.pmd.lang.java.ast.InternalApiBridge.forceTypeResolutionPhase(InternalApiBridge.java:97)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.lambda$process$4(JavaAstProcessor.java:146)
at net.sourceforge.pmd.benchmark.TimeTracker.bench(TimeTracker.java:163)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:146)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:177)
at net.sourceforge.pmd.lang.java.internal.JavaAstProcessor.process(JavaAstProcessor.java:161)
at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl(JavaParser.java:70)
at net.sourceforge.pmd.lang.java.ast.JavaParser.parseImpl(JavaParser.java:25)
at net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeParserAdapter.parse(JjtreeParserAdapter.java:36)
at net.sourceforge.pmd.lang.impl.PmdRunnable.parse(PmdRunnable.java:112)
at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:132)
at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:840)
Caused by: java.lang.IllegalArgumentException: <?> cannot be a wildcard bound
at net.sourceforge.pmd.lang.java.types.TypeSystem.wildcard(TypeSystem.java:662)
at net.sourceforge.pmd.lang.java.types.WildcardTypeImpl.subst(WildcardTypeImpl.java:36)
at net.sourceforge.pmd.lang.java.types.WildcardTypeImpl.subst(WildcardTypeImpl.java:17)
at net.sourceforge.pmd.lang.java.types.TypeOps.lambda$subst$0(TypeOps.java:982)
at net.sourceforge.pmd.lang.java.types.TypeOps.mapPreservingSelf(TypeOps.java:1009)
at net.sourceforge.pmd.lang.java.types.TypeOps.subst(TypeOps.java:982)
at net.sourceforge.pmd.lang.java.types.JClassType.subst(JClassType.java:78)
at net.sourceforge.pmd.lang.java.types.JClassType.subst(JClassType.java:54)
at net.sourceforge.pmd.lang.java.types.TypeOps.lambda$subst$0(TypeOps.java:982)
at net.sourceforge.pmd.lang.java.types.TypeOps.mapPreservingSelf(TypeOps.java:1009)
at net.sourceforge.pmd.lang.java.types.TypeOps.subst(TypeOps.java:982)
at net.sourceforge.pmd.lang.java.symbols.internal.asm.ExecutableStub.getFormalParameterTypes(ExecutableStub.java:106)
at net.sourceforge.pmd.lang.java.types.ClassMethodSigImpl.getFormalParameters(ClassMethodSigImpl.java:109)
at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.isPotentiallyApplicable(Infer.java:1045)
at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.computeCompileTimeDecl(Infer.java:306)
at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.getCompileTimeDecl(Infer.java:283)
at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.goToInvocationWithFallback(Infer.java:212)
at net.sourceforge.pmd.lang.java.types.internal.infer.Infer.inferInvocationRecursively(Infer.java:174)
at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.inferInvocation(PolyResolution.java:268)
at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.polyTypeOtherCtx(PolyResolution.java:140)
at net.sourceforge.pmd.lang.java.types.ast.internal.PolyResolution.computePolyType(PolyResolution.java:130)
at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.handlePoly(LazyTypeResolver.java:360)
at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit(LazyTypeResolver.java:365)
at net.sourceforge.pmd.lang.java.types.ast.internal.LazyTypeResolver.visit(LazyTypeResolver.java:103)
at net.sourceforge.pmd.lang.java.ast.ASTMethodCall.acceptVisitor(ASTMethodCall.java:69)
at net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode.getTypeMirror(AbstractJavaTypeNode.java:51)
... 23 more
Related reports checked:
#5493 reports the same exception with a different example and was closed by #5529. #5167, #4771, and #4753 are also closed. The open #4436 primarily concerns a different disambiguation error; a maintainer identified the wildcard-bound exception mentioned there as a different bug. I did not find an open issue matching this intersection-bound reproducer. These links are historical context; I have not established that this has the same root cause as any of those fixes.
Running PMD through: CLI.
Activity
[-][java] IllegalArgumentException with wildcard in intersection-bound Iterable.forEach lambda[/-][+][java] IllegalArgumentException with wildcard in intersection-bound Iterable.forEach lambda/method refs[/+][-][java] IllegalArgumentException with wildcard in intersection-bound Iterable.forEach lambda/method refs[/-][+][java] IllegalArgumentException with wildcard in the upper bound of a type variable, in a lambda or method reference[/+][java] Fix pmd#7054: Capture wildcard components of intersection boun…