Fix issue #2358 Make Map.keySet() handle multiple argments to @KeyFor
Summary
Enhance @KeyFor propagation for Map.keySet() by modifying addComputedTypeAnnotations in KeyForAnnotatedTypeFactory.
Problem
The element type of Set returned by Map.keySet() only carried @KeyFor for the current variable (e.g., "sharedCounts1") and did not inherit @KeyFor values from the Mapโs key type argument (e.g., "sharedBooks").
Map<@KeyFor({"sharedBooks"}) String, Integer> sharedBooks = new HashMap<>();
Map<@KeyFor({"sharedBooks"}) String, Integer> sharedCounts1 = new HashMap<>();
Set<@KeyFor({"sharedBooks", "sharedCounts1"}) String> otherChars1 = sharedCounts1.keySet();
Solution
Override addComputedTypeAnnotations in KeyForAnnotatedTypeFactory. When the tree is a Map.keySet() call, read @KeyFor values from the receiver Mapโs key type argument and from the current Set element type, merge the names, and update the keySet() return typeโs element with the combined @KeyFor.
Reference
https://github.com/typetools/checker-framework/issues/2358
The changes modify KeyFor annotation handling in the Checker Framework's nullness checker. The main file is updated to merge KeyFor annotations from a Map's key type into the return type of keySet() invocations. A new method override handles type argument extraction and annotation merging through a private helper method. Additionally, new imports are introduced to support this logic. A test file is updated by removing a skip annotation and adding error diagnostic markers to specific test methods, enabling test execution with expected error annotations.๐ Walkthrough
Walkthrough
Pre-merge checks and finishing touches
โ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | โ ๏ธ Warning | Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
โจ Finishing touches
- [ ] ๐ Generate docstrings
๐งช Generate unit tests (beta)
- [ ] Create PR with unit tests
- [ ] Post copyable unit tests in a comment
๐ Recent review details
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
๐ฅ Commits
Reviewing files that changed from the base of the PR and between d14f924f7641b7baf2ff9e9059dd1637693eec38 and c86cce77f4af23c4ee5f19492315c59b46580795.
โ Files ignored due to path filters (1)
checker/tests/nullness/KeyForMultiple.classis excluded by!**/*.class
๐ Files selected for processing (2)
checker/src/main/java/org/checkerframework/checker/nullness/KeyForAnnotatedTypeFactory.java(4 hunks)checker/tests/nullness/KeyForMultiple.java(2 hunks)
๐งฐ Additional context used
๐ง Learnings (2)
๐ Common learnings
Learnt from: mernst
Repo: typetools/checker-framework PR: 7354
File: annotation-file-utilities/tests/LocalMultipleManyMethodsShifted.java:14-14
Timestamp: 2025-11-02T02:18:00.514Z
Learning: In the Checker Framework repository, when doing refactoring PRs (such as preferring isEmpty() over size() comparisons), test files should not be changed. Tests should remain stable to preserve their exact patterns for annotation processing and bytecode verification purposes.
๐ Learning: 2025-10-22T20:41:01.112Z
Learnt from: kelloggm
Repo: typetools/checker-framework PR: 7166
File: checker/src/main/java/org/checkerframework/checker/collectionownership/CollectionOwnershipAnnotatedTypeFactory.java:128-136
Timestamp: 2025-10-22T20:41:01.112Z
Learning: In checker/src/main/java/org/checkerframework/checker/collectionownership/CollectionOwnershipAnnotatedTypeFactory.java, prefer instance fields over static state for analysis caches (e.g., maps keyed by Tree/Block). There is at most one ATF per running checker, so static mutable fields are discouraged.
Applied to files:
checker/src/main/java/org/checkerframework/checker/nullness/KeyForAnnotatedTypeFactory.java
๐งฌ Code graph analysis (1)
checker/src/main/java/org/checkerframework/checker/nullness/KeyForAnnotatedTypeFactory.java (3)
framework/src/main/java/org/checkerframework/framework/type/AnnotatedTypeMirror.java (2)
AnnotatedTypeMirror(64-2780)AnnotatedDeclaredType(948-1237)javacutil/src/main/java/org/checkerframework/javacutil/TreeUtils.java (1)
TreeUtils(116-2967)javacutil/src/main/java/org/checkerframework/javacutil/AnnotationUtils.java (1)
AnnotationUtils(51-1512)
โฐ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
- GitHub Check: typetools.checker-framework (typecheck_part2_jdk25)
- GitHub Check: typetools.checker-framework (misc_jdk25)
- GitHub Check: typetools.checker-framework (typecheck_part1_jdk25)
- GitHub Check: typetools.checker-framework (nonjunit_jdk25)
- GitHub Check: typetools.checker-framework (junit_jdk25)
- GitHub Check: typetools.checker-framework (inference_part1_jdk25)
- GitHub Check: typetools.checker-framework (inference_part2_jdk25)
๐ Additional comments (2)
checker/src/main/java/org/checkerframework/checker/nullness/KeyForAnnotatedTypeFactory.java (1)
252-326: Great integration ofkeySetqualifier merging.Hooking into
addComputedTypeAnnotationskeeps the propagation localized, and the helper cleanly unions receiver and return@KeyForsets without disturbing other qualifiers. Nicely done.checker/tests/nullness/KeyForMultiple.java (1)
15-33: Thanks for tightening the regression test.The added diagnostics confirm the new propagation path and guard against future regressions.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.
Hi, @mernst @smillst, I would appreciate it if you could take a look when you have a chance, thanks in advance!