GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Lambda block body that cannot complete normally is not treated as value-compatible · Issue #7144 · pmd/pmd

[java] Lambda block body that cannot complete normally is not treated as value-compatible #7144

New issue
Copy link
New issue
Copy link
Closed
#7218
Closed
[java] Lambda block body that cannot complete normally is not treated as value-compatible#7144
#7218
Copy link
Labels
in:symbol-tableAffects the symbol table codein:type-resolutionAffects the type resolution code
Milestone
7.30.0

Description

@saryn17
saryn17
opened on Oct 6, 2026
Issue body actions

Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)

Description:

For an ExecutorService pool, javac compiles pool.submit(() -> { while (true) { } }) to submit(Callable) and PMD resolves it to submit(Runnable). For pool.submit(() -> { if (flag) { throw new IllegalStateException(); } }) it is the other way round. javac selects submit(Runnable), PMD submit(Callable).

LambdaMirrorImpl.isLambdaBodyCompatible treats a block body without return expr; as value-compatible when the block contains a throw statement:

pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/ast/LambdaMirrorImpl.java

Lines 141 to 148 in 5c9271c

private static boolean isLambdaBodyCompatible(ASTBlock body, boolean voidCompatible) {
boolean noReturnsWithExpr = body.descendants(ASTReturnStatement.class).none(it -> it.getExpr() != null);
if (noReturnsWithExpr && !voidCompatible) {
// normally we should be determining whether the block must complete abruptly on all paths
return body.descendants(ASTThrowStatement.class).nonEmpty();
}
return noReturnsWithExpr == voidCompatible;
}

JLS 15.27.2 asks instead whether the block can complete normally (JLS 14.22). The body { while (true) { } } cannot complete normally and is value-compatible. The second body can complete normally and is not. Line 144 has the comment // normally we should be determining whether the block must complete abruptly on all paths. AbruptCompletionAnalysis.canCompleteNormally in symbols/table/internal implements JLS 14.22. It is package-private, and it recognizes a constant true loop condition only as the literal true (line 249), so a body such as { while (1 < 2) { } } needs the constant expression rule of JLS 14.22 as well.

Found while working on #7136.

Code Sample demonstrating the issue:

import java.util.concurrent.ExecutorService;

public class Submit {
    void t(ExecutorService pool, boolean flag) {
        pool.submit(() -> { while (true) { } });
        pool.submit(() -> { if (flag) { throw new IllegalStateException(); } });
    }
}

Steps to reproduce:

  1. javac Submit.java, also with --release 8, then javap -c -p Submit. Submit.t calls submit:(Ljava/util/concurrent/Callable;) on line 5 and submit:(Ljava/lang/Runnable;) on line 6.
  2. Run PMD on the file with the XPath rule //MethodCall[pmd-java:matchesSig("java.util.concurrent.ExecutorService#submit(java.lang.Runnable)")]. It reports line 5 and not line 6.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. saryn17
    added a commit that references this issue on Oct 6, 2026

    [java] UnnecessaryBoxing: rerun the overload resolution without the u…

    15327e0
  2. UncleOwen
    added this to the 7.30.0 milestone on Nov 10, 2026
  3. UncleOwen
    closed this as completedin #7218on Nov 10, 2026
  4. UncleOwen
    added a commit that references this issue on Nov 10, 2026

    [java] Fix pmd#7144: Fix lambda block body compatibility (pmd#7218)

    This issue will close once commit a825ee7 is merged into the 'main' branch.
    a825ee7
  5. saryn17
    added a commit that references this issue on Nov 11, 2026

    [java] UnnecessaryBoxing: enable the test case for pmd#7144

    8647b35
  6. adangel
    added
    in:type-resolutionAffects the type resolution code
    in:symbol-tableAffects the symbol table code
    on Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in:symbol-tableAffects the symbol table codein:type-resolutionAffects the type resolution code

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.30.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.