Skip to content

Kotlin printer: Optional.empty() and Optional.of(x) into an Optional<T> compile - #148

Closed
tomtourwe wants to merge 1 commit into
develfrom
fix/optional-empty-of-type-variable
Closed

tomtourwe wants to merge 1 commit into
develfrom
fix/optional-empty-of-type-variable

Conversation

@tomtourwe

Copy link
Copy Markdown

What

1014d98 casts return Optional.empty(); into an Optional<T> as
(Optional.empty() as Optional<T>), and Optional.of(value) likewise; kotlinc cannot infer T
from a cast, and Kotlin's Optional.of takes a non-null T.

Now: (Optional.empty<T>() as Optional<T>) and (Optional.of(value!!) as Optional<T>); the !!
changes nothing, Java's Optional.of(null) throws too (reported as ASSERT_AT_DEREFERENCE).
Test: TestJavaToKotlinStructure.optionalEmptyAndOfIntoTypeVariable (output compiles with kotlinc).

How it was found

A mutation-testing run (PIT) over the lines that maddi's latest 100 commits changed listed the changed code no
existing test checks. Tests written for those lines, from what each commit says the code should do, exposed this
bug in the lines 1014d98 changed, which lists empty and of but handled only ofNullable.

Tests

On devel 07b926b: the new test fails without the fix and passes with it.
With this fix and the nine others of this series applied together, every maddi test passes
(run on 7bc974b: 2,743 tests, 0 failures).

🤖 Generated with Claude Code

…T> compile

1014d98 casts `return Optional.empty();` into an `Optional<T>` as
`(Optional.empty() as Optional<T>)`, and `Optional.of(value)` likewise; kotlinc cannot infer T
from a cast, and Kotlin's Optional.of takes a non-null T.

Now: `(Optional.empty<T>() as Optional<T>)` and `(Optional.of(value!!) as Optional<T>)`; the `!!`
changes nothing, Java's Optional.of(null) throws too (reported as ASSERT_AT_DEREFERENCE).
Test: TestJavaToKotlinStructure.optionalEmptyAndOfIntoTypeVariable (output compiles with kotlinc).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tom Tourwe <Tom.Tourwe@codelaser.io>
@bnaudts

bnaudts commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Thanks. Devel had already fixed this independently in another form (ba7a889, the optionalFactory rule), so the fix itself isn't taken. Your test passes without it on devel, and it is kept as a regression test in 00e31a8, with you as author. Closing this PR.

The whole batch (#146–#155) passes the Kotlin, C#, printer, front-end, inspection and bytecode suites on JDK 26 and JDK 27. It goes into devel with the next merge of the ws/maddi-bug branch.

🤖 Generated with Claude Code

@bnaudts bnaudts closed this Oct 19, 2026
bnaudts pushed a commit that referenced this pull request Oct 20, 2026
…tatic method reference returned as a Function

The tests of #148 and #149. Their fixes were already on devel in another form
(ba7a889 and the method-reference SAM constructor in assigned()); the tests pass without them and
stay as regression tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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