Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] Bad intersection, unrelated class types Child and Parent<? extends Child> #5006

Closed
SamCarlberg opened this issue May 11, 2024 · 0 comments · Fixed by #5008
Closed
Assignees
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Milestone

Comments

@SamCarlberg
Copy link

Affects PMD Version:

7.1.0

Description:

An exception is thrown when processing code using generic wildcards and self-referential generic bounds. This bug does not occur if the parent type is declared as an interface.

class Parent<T extends Parent<T>> {} // Recursive type T

class Child extends Parent<Child> {}

class Box<T extends Parent<T>> {
  public void foo(Box<? extends Child> box) {} // <-- The bug is triggered by this line
}

Exception Stacktrace:

ContextedRuntimeException: java.lang.IllegalArgumentException: Bad intersection, unrelated class types Child and Parent<capture#225 of ? extends Child> in [Parent<capture#225 of ? extends Child>, Child]
Exception Context:
        [1:Rule applied on node=!debug only! [MethodDeclaration:309:15]public void foo(Box<? extends Child> box) {}]

org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.IllegalArgumentException: Bad intersection, unrelated class types Child and Parent<capture#225 of ? extends Child> in [Parent<capture#225 of ? extends Child>, Child]
Exception Context:
        [1:Rule applied on node=!debug only! [MethodDeclaration:309:15]public void foo(Box<? extends Child> box) {}]
---------------------------------
        at net.sourceforge.pmd.util.AssertionUtil.contexted(AssertionUtil.java:236)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:79)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.apply(RuleApplicator.java:57)
        at net.sourceforge.pmd.lang.rule.internal.RuleSets.apply(RuleSets.java:155)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.processSource(PmdRunnable.java:140)
        at net.sourceforge.pmd.lang.impl.PmdRunnable.run(PmdRunnable.java:80)
        at java.base@17.0.8.1/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base@17.0.8.1/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at java.base@17.0.8.1/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base@17.0.8.1/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base@17.0.8.1/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalArgumentException: Bad intersection, unrelated class types Child and Parent<capture#225 of ? extends Child> in [Parent<capture#225 of ? extends Child>, Child]
        at net.sourceforge.pmd.lang.java.types.Lub.glb(Lub.java:344)
        at net.sourceforge.pmd.lang.java.types.TypeSystem.glb(TypeSystem.java:723)
        at net.sourceforge.pmd.lang.java.types.TypeConversion.capture(TypeConversion.java:227)
        at net.sourceforge.pmd.lang.java.types.TypeConversion.capture(TypeConversion.java:154)
        at net.sourceforge.pmd.lang.java.types.TypeOps.isConvertible(TypeOps.java:462)
        at net.sourceforge.pmd.lang.java.types.TypeOps.isConvertible(TypeOps.java:401)
        at net.sourceforge.pmd.lang.java.types.JTypeMirror.isConvertibleTo(JTypeMirror.java:126)
        at net.sourceforge.pmd.lang.java.types.JTypeMirror.isSubtypeOf(JTypeMirror.java:114)
        at net.sourceforge.pmd.lang.java.types.TypeTestUtil.isA(TypeTestUtil.java:158)
        at net.sourceforge.pmd.lang.java.types.TypeTestUtil.isA(TypeTestUtil.java:178)
        at net.sourceforge.pmd.lang.java.types.TypeTestUtil.isA(TypeTestUtil.java:122)
        at net.sourceforge.pmd.lang.java.rule.xpath.internal.BaseContextNodeTestFun.lambda$makeCallExpression$1(BaseContextNodeTestFun.java:57)
        at net.sourceforge.pmd.lang.rule.xpath.internal.SaxonExtensionFunctionDefinitionAdapter$1.call(SaxonExtensionFunctionDefinitionAdapter.java:135)
        at net.sf.saxon.lib.ExtensionFunctionCall.effectiveBooleanValue(ExtensionFunctionCall.java:189)
        at net.sf.saxon.functions.IntegratedFunctionCall.effectiveBooleanValue(IntegratedFunctionCall.java:377)
        at net.sf.saxon.expr.elab.FallbackElaborator.lambda$elaborateForBoolean$3(FallbackElaborator.java:61)
        at net.sf.saxon.expr.FilterExpression$FilterExprElaborator$SimpleFilteredIterator.next(FilterExpression.java:1376)
        at net.sf.saxon.expr.ContextMappingIterator.next(ContextMappingIterator.java:67)
        at net.sf.saxon.functions.Count.count(Count.java:66)
        at net.sf.saxon.functions.Count$CountFnElaborator.lambda$elaborateForItem$0(Count.java:130)
        at net.sf.saxon.expr.ValueComparison$ValueComparisonElaborator.lambda$elaborateForItem$1(ValueComparison.java:546)
        at net.sf.saxon.expr.elab.ItemElaborator.lambda$elaborateForPull$0(ItemElaborator.java:36)
        at net.sf.saxon.expr.FilterExpression$FilterExprElaborator$PositionalFilteredIterator.next(FilterExpression.java:1330)
        at net.sourceforge.pmd.lang.rule.xpath.internal.SaxonXPathRuleQuery.evaluate(SaxonXPathRuleQuery.java:128)
        at net.sourceforge.pmd.lang.rule.xpath.XPathRule.apply(XPathRule.java:101)
        at net.sourceforge.pmd.lang.rule.RuleReference.apply(RuleReference.java:415)
        at net.sourceforge.pmd.lang.rule.internal.RuleApplicator.applyOnIndex(RuleApplicator.java:77)
        ... 9 more
Exception occurred on node !debug only! [MethodDeclaration:309:15]public void foo(Box<? extends Child> box) {}

Code Sample demonstrating the issue:

See above

Steps to reproduce:

Run PMD on the code sample

Running PMD through:

Gradle 8.5, on openjdk 17.0.8.1 2023-08-24

@SamCarlberg SamCarlberg added the a:bug PMD crashes or fails to analyse a file. label May 11, 2024
@oowekyala oowekyala self-assigned this May 12, 2024
@oowekyala oowekyala added this to the 7.2.0 milestone May 12, 2024
@oowekyala oowekyala added the in:type-resolution Affects the type resolution code label May 13, 2024
jsotuyod added a commit that referenced this issue May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:bug PMD crashes or fails to analyse a file. in:type-resolution Affects the type resolution code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants