Not recognized dependency from "when-is" statement since kotlin 2.4.20-Beta2/RC
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start from the reproducing project (github.com/seregamorph/kotlin-arch-unit-issue, mvn clean test) and dump the compiled SampleFilter bytecode with javap -c at Kotlin 2.4.20 vs 2.4.10 to see how the is SampleData -> branch is now emitted. In ArchUnit, trace ClassFileImporter dependency resolution from the bytecode instruction used for type checks (the path behind getDirectDependenciesToSelf) and compare it with what the new bytecode contains. Done = the ArchUnit test in the repro reports SampleData as a dependency again, with existing ArchUnit tests still passing; note the linked KT-88509 has no confirmed root cause yet.
Written by the indexing model from the issue text.
Description
Please note, that this issue has related https://youtrack.jetbrains.com/issue/KT-88509/archunit-does-not-recognize-when-if-usage-since-2.4.20-Beta2 as the root cause is not clear so far.
We have arch-unit tests that inspect usages of classes (like exceptions, data classes, etc.) and fail in case if it locates classes which are not actually used. Since 2.4.20-Beta2 (and latest 2.4.20-RC) these tests fail for scenarios when the checked class is only used within when-is statement. Note: this is probably an issue of arch-unit, but I cannot say for sure. Also confusing here is that this behaviour changes within a minor update 2.4.10-2.4.20(now RC). I can only guess there, the when-is semantics is changed, which originally expected to match instaceof expression.
Reproducing snippet:
data class SampleData(private val id: Int)
object SampleFilter {
fun isSampleData(obj: Any): Boolean {
val result = when (obj) {
// please pay attention: we need this first check (otherwise the problem is not reproduced)
is String -> false
// then - check Kotlin type - expected that archunit "sees" it
is SampleData -> true
else -> false
}
return result
}
}
and arch-unit test
@Test
public void shouldHaveUsages() {
JavaClasses classes = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages("com.example");
JavaClass sampleData = classes.get(SampleData.class);
Set<Dependency> dependenciesToSelf = sampleData.getDirectDependenciesToSelf();
Set<String> originClassNames = dependenciesToSelf.stream()
.map(dependency -> dependency.getOriginClass().getName())
.filter(name -> !name.equals(SampleData.class.getName()))
.collect(Collectors.toSet());
assertFalse(
originClassNames.isEmpty(),
"Expected " + SampleData.class.getName() + " to have usages, but found none."
);
}
The library is com.tngtech.archunit:archunit:1.5.0
Reproducing project: https://github.com/seregamorph/kotlin-arch-unit-issue :
- run
mvn clean testto ensure it's reproduced - downgrade kotlin to
2.4.10and see there is no problem
- Dominant language
- Java
- Stars
- 3.9k
- Forks
- 350
- Avg merge
- 1d 20h
- Merged PRs (30d)
- 22
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from TNG/ArchUnit
-
breaking change
Difficulty 5/5 Over a week Newbie friendliness 18/100
TNG/ArchUnit#1756 · 1 comment ·
Maintainers usually reply within 1 day
-
breaking change
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Maintainers usually reply within 1 day
-
breaking change
Difficulty 4/5 3-5 days Newbie friendliness 22/100
Maintainers usually reply within 1 day
-
breaking change
Difficulty 3/5 Half a day Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
breaking change
Difficulty 5/5 Over a week Newbie friendliness 12/100
Maintainers usually reply within 1 day
Similar issues
-
pdfium-drift
Difficulty 1/5 Under an hour Newbie friendliness 88/100
Stirling-Tools/JPDFium#188 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
openremote/openremote#3487 · 1 comment ·
-
Type/Bug U2
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
wso2/product-is#28675 ·
Maintainers usually reply within 1 day
-
a:bug
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
erupts/erupt#460 · 1 comment ·
Maintainers usually reply within 1 day