Skip to content

[java] align parameter annotations with the parameters they belong to - #2352

Open
VladimirMakaev wants to merge 2 commits into
meta:mainfrom
VladimirMakaev:cut-kotlin-capture
Open

VladimirMakaev wants to merge 2 commits into
meta:mainfrom
VladimirMakaev:cut-kotlin-capture

Conversation

@VladimirMakaev

@VladimirMakaev VladimirMakaev commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

A method's parameter annotations are indexed against its source parameter list, which leaves out the synthetic parameters the descriptor begins with: the outer instance of an inner class, and the name and ordinal of an enum constructor. Reading them in order put each annotation on the parameter before the one it was written for, so a nullable parameter inherited its neighbour's non-null requirement. The synthetic constructor that supplies default arguments passes null for a defaulted nullable parameter, and that null then met a requirement that was never written.

class Outer(val tag: String) {
  // reported: null passed where non-null was expected, on the constructor the compiler
  // generates for the defaults
  inner class Event(val id: String? = null, val origin: Origin = Origin.UNKNOWN)
}

Annotations are now matched to the end of the parameter list, which is where the two lists agree. A method whose counts already agree is unaffected.

The report only appears when Infer is given a library's compiled classes instead of running the compiler under it, so the second commit adds a Kotlin case that compiles its sources and then captures the classes through --generated-classes. Its fixture is the inner class above. Without the first commit it reports the impossible null, and its two controls, a null that really does reach a parameter declared non-null through the ordinary and the default-arguments constructor, report nothing. With it, the fixture is silent and both controls report.

Test plan

With Kotlin 2.4.21 and JDK 21: the new jar-capture case passes, and fails as described without the first commit. The Kotlin resources and Java pulse suites pass. The Kotlin pulse suite differs from its expectations only in two lambda names in TAINT_ERROR traces ($lambda$1 vs $lambda$0); the parent commit shows the same difference with this compiler.

@meta-cla meta-cla Bot added the CLA Signed label Dec 2, 2026
@VladimirMakaev
VladimirMakaev marked this pull request as ready for review December 2, 2026 11:25
A method's parameter annotations are indexed against its source parameter list, which leaves out
the synthetic parameters the descriptor begins with: the outer instance of an inner class, and
the name and ordinal of an enum constructor. Reading them in order put each annotation on the
parameter before the one it was written for, so a nullable parameter inherited its neighbour's
non-null requirement. The synthetic constructor that supplies default arguments passes null for
a defaulted nullable parameter, and that null then met a requirement that was never written.

    class Outer(val tag: String) {
      // reported: null passed where non-null was expected, on the constructor the compiler
      // generates for the defaults
      inner class Event(val id: String? = null, val origin: Origin = Origin.UNKNOWN)
    }

Annotations are now matched to the end of the parameter list, which is where the two lists agree.
A method whose counts already agree is unaffected.

## Test plan
kotlin/pulse/DefaultArguments.kt is a silent guard in source capture. In compiled-output
capture, the false null argument disappears and both non-null controls report. Java Pulse
and Kotlin resources are unchanged; Kotlin Pulse retains its two existing lambda-naming
trace differences.
…g the compiler

A build system that hands Infer a library's compiled classes gives the analysis a different
program from one that runs the compiler under Infer: the compiler's own helpers stay outside the
capture. Several Kotlin shapes only report in that mode, so the fixtures for them are silent in
the other Kotlin tests and there is nowhere to pin them. This adds a case that compiles its
sources and then captures the classes through --generated-classes.

Its first fixture is the inner class whose default-arguments constructor passes null for a
defaulted nullable parameter. Without the parameter-annotation alignment that fixture reports a
null argument that cannot happen, and the two controls beside it -- a null that really does
reach a parameter declared non-null, through the ordinary and the default-arguments constructor
-- report nothing at all. With it, the first is silent and both controls report.

## Test plan
The new case, and the Kotlin and Java suites.
@VladimirMakaev VladimirMakaev changed the title [tests] a Kotlin case that captures compiled output instead of running the compiler [java] align parameter annotations with the parameters they belong to Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant