Skip to content

GROOVY-12620: test a closure or lambda returning an int constant that fits a byte, short or char return type - #3146

Closed
codeconsole wants to merge 3 commits into
apache:masterfrom
codeconsole:stc-lambda-constant-narrowing
Closed

codeconsole wants to merge 3 commits into
apache:masterfrom
codeconsole:stc-lambda-constant-narrowing

Conversation

@codeconsole

@codeconsole codeconsole commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Static type checking used to reject a closure or lambda that returns an int constant for a byte, short or char SAM return type, where Java accepts it (JLS 5.2):

interface S { short get() }
interface B { byte get() }
@groovy.transform.CompileStatic
void test() {
    S s = () -> 1          // Cannot return value of type int for lambda expecting short
    B b = { -> 2 }         // Cannot return value of type int for closure expecting byte
    assert s.get() == (short) 1 && b.get() == (byte) 2
}

4.0.33, 5.0.8 and 6.0.0 reject it. Master now accepts it: the check GROOVY-12616 gave a value returned to a primitive functional interface type allows a constant that fits. So this PR no longer changes the compiler; it adds the tests.

A constant that does not fit a byte or short, and a non-constant int, are reported as "Possible loss of precision". A char is not range checked, so C c = { -> -1 } compiles, as char m() { -1 } does; GROOVY-12648 covers that, so the tests leave it out.

Tests

CoercionSTCTest.testCoerceNarrowingConstantReturnToFunctionalInterface, also run by CoercionStaticCompileTests, covers closures and lambdas for byte, short and char, boundary and negative values, block lambdas with several returns, method arguments, as coercion, static and instance contexts, and the cases that are still rejected. Both classes pass.

@codecov-commenter

codecov-commenter commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.3431%. Comparing base (22bb46c) to head (62d6836).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3146        +/-   ##
==================================================
+ Coverage     74.3343%   74.3431%   +0.0088%     
- Complexity      44299      44312        +13     
==================================================
  Files            1699       1699                
  Lines          150855     150860         +5     
  Branches        28754      28755         +1     
==================================================
+ Hits           112137     112154        +17     
+ Misses          28441      28433         -8     
+ Partials        10277      10273         -4     

see 13 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.

…-narrowing

# Conflicts:
#	src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
#	src/test/groovy/groovy/transform/stc/CoercionSTCTest.groovy
The check that GROOVY-12616 gave a return to a primitive functional interface
type already allows an int constant that fits a byte or short, and char is not
range checked (GROOVY-12648), so the branch and helper here can no longer be
reached. The tests stay, with the messages that check reports for what does not
fit.
@codeconsole codeconsole changed the title GROOVY-12620: accept a closure or lambda returning an int constant that fits a byte, short or char return type GROOVY-12620: test a closure or lambda returning an int constant that fits a byte, short or char return type Nov 6, 2026
@testlens-app

testlens-app Bot commented Nov 6, 2026

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ Build and test / lts (17, windows-latest, 1)

🏷️ Commit: 62d6836
▶️ Tests: 64198 executed
⚪️ Checks: 26/26 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@codeconsole

Copy link
Copy Markdown
Contributor Author

Closing: master already accepts these. The check GROOVY-12616 gave a value returned to a primitive functional interface type allows an int constant that fits a byte or short, so this PR no longer changes the compiler.

@codeconsole codeconsole closed this Nov 7, 2026
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.

2 participants