Skip to content

Commit fb7534e

Browse files
sandwwraithSpace Team
authored andcommitted
[FIR] Add MustUse enhancements to known collection/iterable types
For most declarations, enhancements are automatically taken from Kotlin's supertypes, but functions like Collection.stream() are not declared in Kotlin sources. Also add a bunch of tests for Java types enhancements. #KT-74810 #KT-71195
1 parent 5797187 commit fb7534e

File tree

13 files changed

+211
-50
lines changed

13 files changed

+211
-50
lines changed

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLDiagnosticsFe10TestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLPartialDiagnosticsFe10TestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

analysis/low-level-api-fir/tests-gen/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLReversedDiagnosticsFe10TestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsWithLatestLanguageVersionTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/PhasedJvmDiagnosticLightTreeTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/PhasedJvmDiagnosticPsiTestGenerated.java

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/fir/fir-jvm/src/org/jetbrains/kotlin/fir/java/enhancement/SignatureEnhancement.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,15 @@ class FirSignatureEnhancement(
512512
): FirDeclarationStatus {
513513
val original = method.status
514514
if (original.returnValueStatus != ReturnValueStatus.Unspecified) return original
515-
val newRvStatus = overriddenMembers.firstNotNullOfOrNull { declaration ->
515+
predefinedEnhancementInfo?.returnValueStatus?.takeIf { it != ReturnValueStatus.Unspecified }?.let { newRvStatus ->
516+
return original.copy(returnValueStatus = newRvStatus)
517+
}
518+
overriddenMembers.firstNotNullOfOrNull { declaration ->
516519
declaration.status.returnValueStatus.takeIf { it != ReturnValueStatus.Unspecified }
520+
}?.let { newRvStatus ->
521+
return original.copy(returnValueStatus = newRvStatus)
517522
}
518-
if (newRvStatus == null) return original
519-
return original.copy(returnValueStatus = newRvStatus)
523+
return original
520524
}
521525

522526
private fun buildEnhancedValueParameter(
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN_PIPELINE_TILL: BACKEND
2+
// WITH_STDLIB
3+
// FULL_JDK
4+
5+
import java.util.Optional
6+
7+
fun list(m: MutableList<String>) {
8+
m.add("")
9+
<!RETURN_VALUE_NOT_USED!>m.isEmpty()<!>
10+
<!RETURN_VALUE_NOT_USED!>m.toString()<!>
11+
}
12+
13+
fun javaList(m: ArrayList<String>) {
14+
m.add("x")
15+
<!RETURN_VALUE_NOT_USED!>m.isEmpty()<!>
16+
}
17+
18+
fun javaSet(s: LinkedHashSet<String>) {
19+
s.add("x")
20+
<!RETURN_VALUE_NOT_USED!>s.isEmpty()<!>
21+
<!RETURN_VALUE_NOT_USED!>s.toString()<!>
22+
<!RETURN_VALUE_NOT_USED!>s.first()<!>
23+
}
24+
25+
fun map(s: MutableMap<String, String>) {
26+
<!RETURN_VALUE_NOT_USED!>s.get("")<!>
27+
<!RETURN_VALUE_NOT_USED!>s.getOrDefault("", "")<!>
28+
s.put("", "")
29+
s.putIfAbsent("", "")
30+
s.merge("", "") { a, b -> a + b }
31+
}
32+
33+
fun javaMap(s: LinkedHashMap<String, String>) {
34+
<!RETURN_VALUE_NOT_USED!>s.get("")<!>
35+
<!RETURN_VALUE_NOT_USED!>s.getOrDefault("", "")<!>
36+
s.put("", "")
37+
s.putIfAbsent("", "")
38+
s.merge("", "") { a, b -> a + b }
39+
}
40+
41+
fun iterable(i: Iterable<String>) {
42+
<!RETURN_VALUE_NOT_USED!>i.iterator()<!>
43+
<!RETURN_VALUE_NOT_USED!>i.spliterator()<!>
44+
}
45+
46+
fun collection(i: Collection<String>) {
47+
<!RETURN_VALUE_NOT_USED!>i.iterator()<!>
48+
<!RETURN_VALUE_NOT_USED!>i.stream()<!>
49+
<!RETURN_VALUE_NOT_USED!>i.parallelStream()<!>
50+
}
51+
52+
fun optional(s: String?) {
53+
<!RETURN_VALUE_NOT_USED!>Optional.of(s!!)<!>
54+
<!RETURN_VALUE_NOT_USED!>Optional.empty<String>()<!>
55+
<!RETURN_VALUE_NOT_USED!>Optional.ofNullable(s)<!>
56+
val o = Optional.ofNullable(s)
57+
<!RETURN_VALUE_NOT_USED!>o.get()<!>
58+
}
59+
60+
fun stringBuilder(sb: StringBuilder, ss: CharSequence) {
61+
sb.append("")
62+
sb.length // .length is not reported because of KT-80179
63+
<!RETURN_VALUE_NOT_USED!>sb.get(0)<!>
64+
<!RETURN_VALUE_NOT_USED!>ss.length<!>
65+
<!RETURN_VALUE_NOT_USED!>ss.get(0)<!>
66+
}
67+
68+
/* GENERATED_FIR_TAGS: functionDeclaration, inProjection, lambdaLiteral, nullableType, outProjection, samConversion,
69+
stringLiteral */
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN_PIPELINE_TILL: BACKEND
2+
// WITH_STDLIB
3+
// FULL_JDK
4+
5+
import java.util.Optional
6+
7+
fun list(m: MutableList<String>) {
8+
m.add("")
9+
m.isEmpty()
10+
m.toString()
11+
}
12+
13+
fun javaList(m: ArrayList<String>) {
14+
m.add("x")
15+
m.isEmpty()
16+
}
17+
18+
fun javaSet(s: LinkedHashSet<String>) {
19+
s.add("x")
20+
s.isEmpty()
21+
s.toString()
22+
s.first()
23+
}
24+
25+
fun map(s: MutableMap<String, String>) {
26+
s.get("")
27+
s.getOrDefault("", "")
28+
s.put("", "")
29+
s.putIfAbsent("", "")
30+
s.merge("", "") { a, b -> a + b }
31+
}
32+
33+
fun javaMap(s: LinkedHashMap<String, String>) {
34+
s.get("")
35+
s.getOrDefault("", "")
36+
s.put("", "")
37+
s.putIfAbsent("", "")
38+
s.merge("", "") { a, b -> a + b }
39+
}
40+
41+
fun iterable(i: Iterable<String>) {
42+
i.iterator()
43+
i.spliterator()
44+
}
45+
46+
fun collection(i: Collection<String>) {
47+
i.iterator()
48+
i.stream()
49+
i.parallelStream()
50+
}
51+
52+
fun optional(s: String?) {
53+
Optional.of(s!!)
54+
Optional.empty<String>()
55+
Optional.ofNullable(s)
56+
val o = Optional.ofNullable(s)
57+
o.get()
58+
}
59+
60+
fun stringBuilder(sb: StringBuilder, ss: CharSequence) {
61+
sb.append("")
62+
sb.length // .length is not reported because of KT-80179
63+
sb.get(0)
64+
ss.length
65+
ss.get(0)
66+
}
67+
68+
/* GENERATED_FIR_TAGS: functionDeclaration, inProjection, lambdaLiteral, nullableType, outProjection, samConversion,
69+
stringLiteral */

compiler/testData/diagnostics/tests/crv/exclusions.fir.kt

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,8 @@
44
@file:MustUseReturnValue
55

66
fun stringF(): String = ""
7-
fun nsf(): String? = "null"
87
fun unitF(): Unit = Unit
98

10-
fun coll(m: MutableList<String>) {
11-
m.add("")
12-
<!RETURN_VALUE_NOT_USED!>m.isEmpty()<!>
13-
<!RETURN_VALUE_NOT_USED!>m.toString()<!>
14-
}
15-
16-
fun nullable(m: MutableList<String>?) {
17-
m?.add("x")
18-
<!RETURN_VALUE_NOT_USED!>m?.isEmpty()<!>
19-
}
20-
21-
fun lhs(s: LinkedHashSet<String>) {
22-
s.add("x")
23-
<!RETURN_VALUE_NOT_USED!>s.isEmpty()<!>
24-
<!RETURN_VALUE_NOT_USED!>s.toString()<!>
25-
}
26-
27-
fun javaType(m: java.util.ArrayList<String>) {
28-
m.add("x")
29-
<!RETURN_VALUE_NOT_USED!>m.isEmpty()<!>
30-
}
31-
329
fun exlusionPropagation(cond: Boolean, m: MutableList<String>) {
3310
if (cond) m.add("x") else throw IllegalStateException()
3411
if (cond) <!RETURN_VALUE_NOT_USED!>stringF()<!> else throw IllegalStateException()

0 commit comments

Comments
 (0)