FIR: implement resolve of unqualified enum references in when

See KT-16768
This commit is contained in:
Mikhail Glukhikh
2022-02-17 14:13:14 +03:00
committed by teamcity
parent f35b4b7515
commit 981f8b1871
28 changed files with 820 additions and 34 deletions
@@ -0,0 +1,12 @@
FILE: test.kt
public final fun foo(javaEnum: R|JavaEnum|): R|kotlin/Int| {
^foo when (R|<local>/javaEnum|) {
==($subj$, R|/JavaEnum.FIRST|) -> {
Int(1)
}
==($subj$, R|/JavaEnum.SECOND|) -> {
Int(2)
}
}
}
@@ -0,0 +1,13 @@
// FILE: JavaEnum.java
public enum JavaEnum {
FIRST,
SECOND;
}
// FILE: test.kt
fun foo(javaEnum: JavaEnum) = when (javaEnum) {
FIRST -> 1
SECOND -> 2
}
@@ -0,0 +1,12 @@
FILE: test.kt
public final fun foo(javaEnum: R|JavaEnum|): R|kotlin/Unit| {
^foo when (R|<local>/javaEnum|) {
==($subj$, <Unresolved name: first>#()) -> {
Int(1)
}
==($subj$, <Unresolved name: second>#) -> {
Int(2)
}
}
}
@@ -0,0 +1,19 @@
// FILE: JavaEnum.java
public enum JavaEnum {
FIRST,
SECOND;
public static JavaEnum first() {
return FIRST;
}
public static JavaEnum second = SECOND;
}
// FILE: test.kt
fun foo(javaEnum: JavaEnum) = <!NO_ELSE_IN_WHEN!>when<!> (javaEnum) {
<!UNRESOLVED_REFERENCE!>first<!>() -> 1
<!UNRESOLVED_REFERENCE!>second<!> -> 2
}
@@ -0,0 +1,71 @@
FILE: nested.kt
public final enum class Outer : R|kotlin/Enum<Outer>| {
private constructor(): R|Outer| {
super<R|kotlin/Enum<Outer>|>()
}
public final static enum entry FIRST: R|Outer|
public final static enum entry SECOND: R|Outer|
public final static fun values(): R|kotlin/Array<Outer>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|Outer| {
}
}
public final enum class Inner : R|kotlin/Enum<Inner>| {
private constructor(): R|Inner| {
super<R|kotlin/Enum<Inner>|>()
}
public final static enum entry SECOND: R|Inner|
public final static enum entry THIRD: R|Inner|
public final static fun values(): R|kotlin/Array<Inner>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|Inner| {
}
}
public final fun foo(o: R|Outer|, i: R|Inner|): R|kotlin/Int| {
^foo when (R|<local>/o|) {
==($subj$, R|/Outer.FIRST|) -> {
Int(1)
}
==($subj$, R|/Outer.SECOND|) -> {
when (R|<local>/i|) {
==($subj$, R|/Inner.SECOND|) -> {
Int(2)
}
==($subj$, R|/Inner.THIRD|) -> {
Int(3)
}
}
}
}
}
public final fun bar(o: R|Outer|, i: R|Inner|): R|kotlin/Int| {
^bar when (R|<local>/o|) {
==($subj$, R|/Outer.FIRST|) -> {
Int(1)
}
==($subj$, R|/Outer.SECOND|) -> {
local final fun baz(): R|kotlin/Int| {
^baz when (R|<local>/i|) {
==($subj$, R|/Inner.SECOND|) -> {
Int(2)
}
==($subj$, R|/Inner.THIRD|) -> {
Int(3)
}
}
}
R|<local>/baz|()
}
}
}
@@ -0,0 +1,32 @@
enum class Outer {
FIRST, SECOND;
}
enum class Inner {
SECOND, THIRD;
}
fun foo(o: Outer, i: Inner): Int {
return when (o) {
FIRST -> 1
SECOND -> when (i) {
SECOND -> 2
THIRD -> 3
}
}
}
fun bar(o: Outer, i: Inner): Int {
return when (o) {
FIRST -> 1
SECOND -> {
fun baz(): Int {
return when (i) {
SECOND -> 2
THIRD -> 3
}
}
baz()
}
}
}
@@ -0,0 +1,26 @@
FILE: notInsideBranches.kt
public final enum class Some : R|kotlin/Enum<Some>| {
private constructor(): R|Some| {
super<R|kotlin/Enum<Some>|>()
}
public final static enum entry FIRST: R|Some|
public final static enum entry SECOND: R|Some|
public final static fun values(): R|kotlin/Array<Some>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|Some| {
}
}
public final fun foo(s: R|Some|): <ERROR TYPE REF: Cannot infer argument for type parameter K> {
^foo when (R|<local>/s|) {
==($subj$, R|/Some.FIRST|) -> {
<Unresolved name: SECOND>#
}
==($subj$, R|/Some.SECOND|) -> {
<Unresolved name: FIRST>#
}
}
}
@@ -0,0 +1,9 @@
enum class Some {
FIRST,
SECOND;
}
fun foo(s: Some) = when (s) {
FIRST -> <!UNRESOLVED_REFERENCE!>SECOND<!>
SECOND -> <!UNRESOLVED_REFERENCE!>FIRST<!>
}
@@ -0,0 +1,60 @@
FILE: first.kt
package first
public final enum class First : R|kotlin/Enum<first/First>| {
private constructor(): R|first/First| {
super<R|kotlin/Enum<first/First>|>()
}
public final static enum entry ONE: R|first/First|
public final static enum entry TWO: R|first/First|
public final static fun values(): R|kotlin/Array<first/First>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|first/First| {
}
}
public final val THREE: R|first/First| = Q|first/First|.R|first/First.ONE|
public get(): R|first/First|
FILE: second.kt
package second
public final enum class Second : R|kotlin/Enum<second/Second>| {
private constructor(): R|second/Second| {
super<R|kotlin/Enum<second/Second>|>()
}
public final static enum entry THREE: R|second/Second|
public final static enum entry FOUR: R|second/Second|
public final static fun values(): R|kotlin/Array<second/Second>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|second/Second| {
}
}
public final val ONE: R|second/Second| = Q|second/Second|.R|second/Second.THREE|
public get(): R|second/Second|
public final fun foo(f: R|first/First|): R|kotlin/Unit| {
^foo when (R|<local>/f|) {
==($subj$, R|second/ONE|) -> {
Int(1)
}
==($subj$, R|first/First.TWO|) -> {
Int(2)
}
}
}
public final fun bar(s: R|second/Second|): R|kotlin/Unit| {
^bar when (R|<local>/s|) {
==($subj$, R|first/THREE|) -> {
Int(3)
}
==($subj$, R|second/Second.FOUR|) -> {
Int(4)
}
}
}
@@ -0,0 +1,33 @@
// FILE: first.kt
package first
enum class First {
ONE, TWO;
}
val THREE = First.ONE
// FILE: second.kt
package second
import first.First
import first.THREE
enum class Second {
THREE, FOUR;
}
val ONE = Second.THREE
fun foo(f: First) = <!NO_ELSE_IN_WHEN!>when<!> (f) {
<!INCOMPATIBLE_TYPES!>ONE<!> -> 1
TWO -> 2
}
fun bar(s: Second) = <!NO_ELSE_IN_WHEN!>when<!> (s) {
<!INCOMPATIBLE_TYPES!>THREE<!> -> 3
FOUR -> 4
}
@@ -0,0 +1,27 @@
FILE: typeAlias.kt
public final enum class Some : R|kotlin/Enum<Some>| {
private constructor(): R|Some| {
super<R|kotlin/Enum<Some>|>()
}
public final static enum entry FIRST: R|Some|
public final static enum entry SECOND: R|Some|
public final static fun values(): R|kotlin/Array<Some>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|Some| {
}
}
public final typealias Other = R|Some|
public final fun foo(o: R|Other|): R|kotlin/Int| {
^foo when (R|<local>/o|) {
==($subj$, R|/Some.FIRST|) -> {
Int(1)
}
==($subj$, R|/Some.SECOND|) -> {
Int(2)
}
}
}
@@ -0,0 +1,11 @@
enum class Some {
FIRST,
SECOND;
}
typealias Other = Some
fun foo(o: Other) = when (o) {
FIRST -> 1
SECOND -> 2
}
@@ -0,0 +1,77 @@
FILE: unqualifiedEnum.kt
package test
public final enum class Sample : R|kotlin/Enum<test/Sample>| {
private constructor(): R|test/Sample| {
super<R|kotlin/Enum<test/Sample>|>()
}
public final static enum entry FIRST: R|test/Sample|
public final static enum entry SECOND: R|test/Sample|
public final static enum entry THIRD: R|test/Sample|
public final static fun values(): R|kotlin/Array<test/Sample>| {
}
public final static fun valueOf(value: R|kotlin/String|): R|test/Sample| {
}
}
public final fun trivial(s: R|test/Sample|): R|kotlin/Int| {
^trivial when (R|<local>/s|) {
==($subj$, R|test/Sample.FIRST|) -> {
Int(1)
}
==($subj$, R|test/Sample.SECOND|) -> {
Int(2)
}
==($subj$, R|test/Sample.THIRD|) -> {
Int(3)
}
}
}
public final fun shouldNotWork(s: R|test/Sample|): R|kotlin/Int| {
^shouldNotWork when () {
==(R|<local>/s|, <Unresolved name: FIRST>#) -> {
Int(1)
}
==(R|<local>/s|, <Unresolved name: SECOND>#) -> {
Int(2)
}
==(R|<local>/s|, <Unresolved name: THIRD>#) -> {
Int(3)
}
else -> {
Int(0)
}
}
}
public final class Container : R|kotlin/Any| {
public constructor(): R|test/Container| {
super<R|kotlin/Any|>()
}
public final val SECOND: R|test/Sample| = Q|test/Sample|.R|test/Sample.SECOND|
public get(): R|test/Sample|
public final fun priority(s: R|test/Sample|): R|kotlin/Int| {
lval FIRST: R|test/Sample| = Q|test/Sample|.R|test/Sample.THIRD|
^priority when (R|<local>/s|) {
==($subj$, R|<local>/FIRST|) -> {
Int(3)
}
==($subj$, this@R|test/Container|.R|test/Container.SECOND|) -> {
Int(2)
}
==($subj$, Q|test/Sample|.R|test/Sample.FIRST|) -> {
Int(1)
}
else -> {
Int(0)
}
}
}
}
@@ -0,0 +1,37 @@
package test
enum class Sample {
FIRST, SECOND, THIRD;
}
fun trivial(s: Sample): Int {
return when (s) {
FIRST -> 1
SECOND -> 2
THIRD -> 3
}
}
fun shouldNotWork(s: Sample): Int {
return when {
s == <!UNRESOLVED_REFERENCE!>FIRST<!> -> 1
s == <!UNRESOLVED_REFERENCE!>SECOND<!> -> 2
s == <!UNRESOLVED_REFERENCE!>THIRD<!> -> 3
else -> 0
}
}
class Container {
val SECOND = test.Sample.SECOND
fun priority(s: Sample): Int {
val FIRST = test.Sample.THIRD
return when (s) {
FIRST -> 3
SECOND -> 2
test.Sample.FIRST -> 1
else -> 0
}
}
}