From 59f2ba98a66070513d48e70437ad4aa6092e167c Mon Sep 17 00:00:00 2001 From: Denis Zharkov Date: Wed, 24 May 2017 13:54:31 +0300 Subject: [PATCH] Avoid creating SimpleType instances for default type --- .../bugs/doWhileAssignment.values | 30 ++++----- .../testData/cfg-variables/bugs/kt9825.values | 18 +++--- compiler/testData/cfg/basic/Basic.values | 62 +++++++++---------- .../testData/cfg/conventions/equals.values | 14 ++--- .../testData/cfg/conventions/notEqual.values | 14 ++--- .../secondaryConstructors/withReturn.values | 20 +++--- compiler/testData/cfg/tailCalls/sum.values | 2 +- .../kotlin/types/KotlinTypeFactory.kt | 16 ++++- 8 files changed, 95 insertions(+), 81 deletions(-) diff --git a/compiler/testData/cfg-variables/bugs/doWhileAssignment.values b/compiler/testData/cfg-variables/bugs/doWhileAssignment.values index bf692ef878c..6d26029c9d5 100644 --- a/compiler/testData/cfg-variables/bugs/doWhileAssignment.values +++ b/compiler/testData/cfg-variables/bugs/doWhileAssignment.values @@ -6,14 +6,14 @@ fun test() { } while (s == "") } --------------------- -"" : String NEW: r("") -> +"" : String NEW: r("") -> s = "" !: * -{ val s: String s = "" } !: * COPY -s : OR{*, *} NEW: r(s) -> -"" : * NEW: r("") -> -s == "" : Boolean NEW: call(s == "", equals|, ) -> +{ val s: String s = "" } !: * COPY +s : * NEW: r(s) -> +"" : * NEW: r("") -> +s == "" : Boolean NEW: call(s == "", equals|, ) -> do { val s: String s = "" } while (s == "") !: * -{ do { val s: String s = "" } while (s == "") } !: * COPY +{ do { val s: String s = "" } while (s == "") } !: * COPY ===================== == test2 == fun test2() { @@ -24,15 +24,15 @@ fun test2() { } } --------------------- - : * NEW: magic[VALUE_CONSUMER](true|) -> -true : Boolean NEW: r(true) -> -"" : String NEW: r("") -> -s : OR{*, *} NEW: r(s) -> -"" : * NEW: r("") -> -s != "" : Boolean NEW: call(s != "", equals|, ) -> + : * NEW: magic[VALUE_CONSUMER](true|) -> +true : Boolean NEW: r(true) -> +"" : String NEW: r("") -> +s : * NEW: r(s) -> +"" : * NEW: r("") -> +s != "" : Boolean NEW: call(s != "", equals|, ) -> break !: * -if (s != "") break : * NEW: merge(if (s != "") break|!) -> -{ val s: String s = "" if (s != "") break } : * COPY +if (s != "") break : * NEW: merge(if (s != "") break|!) -> +{ val s: String s = "" if (s != "") break } : * COPY while (true) { val s: String s = "" if (s != "") break } !: * -{ while (true) { val s: String s = "" if (s != "") break } } !: * COPY +{ while (true) { val s: String s = "" if (s != "") break } } !: * COPY ===================== diff --git a/compiler/testData/cfg-variables/bugs/kt9825.values b/compiler/testData/cfg-variables/bugs/kt9825.values index 27553b2bd3f..245c09fa3a4 100644 --- a/compiler/testData/cfg-variables/bugs/kt9825.values +++ b/compiler/testData/cfg-variables/bugs/kt9825.values @@ -10,15 +10,15 @@ fun test5() { a.hashCode() } --------------------- -3 : Int NEW: r(3) -> +3 : Int NEW: r(3) -> a = 3 !: * -{ a = 3 } !: * COPY -5 : Int NEW: r(5) -> +{ a = 3 } !: * COPY +5 : Int NEW: r(5) -> a = 5 !: * -{ a = 5 } !: * COPY -try { a = 3 } finally { a = 5 } : * NEW: merge(try { a = 3 } finally { a = 5 }|!) -> -a : OR{{<: Any}, {<: Any}} NEW: r(a) -> -hashCode() : * NEW: call(hashCode(), hashCode|) -> -a.hashCode() : * COPY -{ var a: Int try { a = 3 } finally { a = 5 } a.hashCode() } : * COPY +{ a = 5 } !: * COPY +try { a = 3 } finally { a = 5 } : * NEW: merge(try { a = 3 } finally { a = 5 }|!) -> +a : {<: Any} NEW: r(a) -> +hashCode() : * NEW: call(hashCode(), hashCode|) -> +a.hashCode() : * COPY +{ var a: Int try { a = 3 } finally { a = 5 } a.hashCode() } : * COPY ===================== diff --git a/compiler/testData/cfg/basic/Basic.values b/compiler/testData/cfg/basic/Basic.values index 5205fcdc93b..809e058797d 100644 --- a/compiler/testData/cfg/basic/Basic.values +++ b/compiler/testData/cfg/basic/Basic.values @@ -17,47 +17,47 @@ fun f(a : Boolean) : Unit { } --------------------- - : Boolean NEW: magic[FAKE_INITIALIZER](a : Boolean) -> -1 : * NEW: r(1) -> -a : * NEW: r(a) -> -2 : {<: Number} NEW: r(2) -> -toLong() : * NEW: call(toLong(), toLong|) -> -2.toLong() : * COPY -a : Boolean NEW: r(a) -> -3 : Int NEW: r(3) -> -foo(a, 3) : * NEW: call(foo(a, 3), foo|, ) -> -genfun() : * NEW: call(genfun(), genfun) -> -{1} : {<: () -> Any} NEW: r({1}) -> -flfun {1} : * NEW: call(flfun {1}, flfun|) -> -3 : OR{{<: Any}, {<: Any}} NEW: r(3) -> -4 : * NEW: r(4) -> -equals(4) : * NEW: call(equals(4), equals|, ) -> -3.equals(4) : * COPY -3 : OR{{<: Any}, {<: Any}} NEW: r(3) -> -4 : * NEW: r(4) -> -3 equals 4 : * NEW: call(3 equals 4, equals|, ) -> -1 : Int NEW: r(1) -> -2 : Int NEW: r(2) -> -1 + 2 : * NEW: call(1 + 2, plus|, ) -> -a : Boolean NEW: r(a) -> -true : Boolean NEW: r(true) -> -a && true : * NEW: magic[AND](a && true|, ) -> -a : Boolean NEW: r(a) -> -false : Boolean NEW: r(false) -> -a || false : * NEW: magic[OR](a || false|, ) -> -{ 1 a 2.toLong() foo(a, 3) genfun() flfun {1} 3.equals(4) 3 equals 4 1 + 2 a && true a || false } : * COPY + : Boolean NEW: magic[FAKE_INITIALIZER](a : Boolean) -> +1 : * NEW: r(1) -> +a : * NEW: r(a) -> +2 : {<: Number} NEW: r(2) -> +toLong() : * NEW: call(toLong(), toLong|) -> +2.toLong() : * COPY +a : Boolean NEW: r(a) -> +3 : Int NEW: r(3) -> +foo(a, 3) : * NEW: call(foo(a, 3), foo|, ) -> +genfun() : * NEW: call(genfun(), genfun) -> +{1} : {<: () -> Any} NEW: r({1}) -> +flfun {1} : * NEW: call(flfun {1}, flfun|) -> +3 : {<: Any} NEW: r(3) -> +4 : * NEW: r(4) -> +equals(4) : * NEW: call(equals(4), equals|, ) -> +3.equals(4) : * COPY +3 : {<: Any} NEW: r(3) -> +4 : * NEW: r(4) -> +3 equals 4 : * NEW: call(3 equals 4, equals|, ) -> +1 : Int NEW: r(1) -> +2 : Int NEW: r(2) -> +1 + 2 : * NEW: call(1 + 2, plus|, ) -> +a : Boolean NEW: r(a) -> +true : Boolean NEW: r(true) -> +a && true : * NEW: magic[AND](a && true|, ) -> +a : Boolean NEW: r(a) -> +false : Boolean NEW: r(false) -> +a || false : * NEW: magic[OR](a || false|, ) -> +{ 1 a 2.toLong() foo(a, 3) genfun() flfun {1} 3.equals(4) 3 equals 4 1 + 2 a && true a || false } : * COPY ===================== == anonymous_0 == {1} --------------------- 1 : Int NEW: r(1) -> -1 : Int COPY +1 : Int COPY ===================== == foo == fun foo(a : Boolean, b : Int) : Unit {} --------------------- : Boolean NEW: magic[FAKE_INITIALIZER](a : Boolean) -> - : Int NEW: magic[FAKE_INITIALIZER](b : Int) -> + : Int NEW: magic[FAKE_INITIALIZER](b : Int) -> ===================== == genfun == fun genfun() : Unit {} diff --git a/compiler/testData/cfg/conventions/equals.values b/compiler/testData/cfg/conventions/equals.values index 3b22f76bb71..e98e32c1dd4 100644 --- a/compiler/testData/cfg/conventions/equals.values +++ b/compiler/testData/cfg/conventions/equals.values @@ -4,12 +4,12 @@ fun foo(a: Int, b: Int) { } } --------------------- - : Int NEW: magic[FAKE_INITIALIZER](a: Int) -> - : Int NEW: magic[FAKE_INITIALIZER](b: Int) -> -a : OR{*, *} NEW: r(a) -> -b : * NEW: r(b) -> -a == b : Boolean NEW: call(a == b, equals|, ) -> + : Int NEW: magic[FAKE_INITIALIZER](a: Int) -> + : Int NEW: magic[FAKE_INITIALIZER](b: Int) -> +a : * NEW: r(a) -> +b : * NEW: r(b) -> +a == b : Boolean NEW: call(a == b, equals|, ) -> { } !: * -if (a == b) { } : * NEW: merge(if (a == b) { }|!) -> -{ if (a == b) { } } : * COPY +if (a == b) { } : * NEW: merge(if (a == b) { }|!) -> +{ if (a == b) { } } : * COPY ===================== diff --git a/compiler/testData/cfg/conventions/notEqual.values b/compiler/testData/cfg/conventions/notEqual.values index 0fb6d1e532d..25f7fe044e2 100644 --- a/compiler/testData/cfg/conventions/notEqual.values +++ b/compiler/testData/cfg/conventions/notEqual.values @@ -3,12 +3,12 @@ fun neq(a: Int, b: Int) { if (a != b) {} } --------------------- - : Int NEW: magic[FAKE_INITIALIZER](a: Int) -> - : Int NEW: magic[FAKE_INITIALIZER](b: Int) -> -a : OR{*, *} NEW: r(a) -> -b : * NEW: r(b) -> -a != b : Boolean NEW: call(a != b, equals|, ) -> + : Int NEW: magic[FAKE_INITIALIZER](a: Int) -> + : Int NEW: magic[FAKE_INITIALIZER](b: Int) -> +a : * NEW: r(a) -> +b : * NEW: r(b) -> +a != b : Boolean NEW: call(a != b, equals|, ) -> {} !: * -if (a != b) {} : * NEW: merge(if (a != b) {}|!) -> -{ if (a != b) {} } : * COPY +if (a != b) {} : * NEW: merge(if (a != b) {}|!) -> +{ if (a != b) {} } : * COPY ===================== diff --git a/compiler/testData/cfg/secondaryConstructors/withReturn.values b/compiler/testData/cfg/secondaryConstructors/withReturn.values index 5415ec09de6..1bdfbccd6b5 100644 --- a/compiler/testData/cfg/secondaryConstructors/withReturn.values +++ b/compiler/testData/cfg/secondaryConstructors/withReturn.values @@ -20,16 +20,16 @@ constructor() { x = 1 } --------------------- - : * NEW: call(, ) -> -1 : OR{*, *} NEW: r(1) -> -1 : * NEW: r(1) -> -1 == 1 : Boolean NEW: call(1 == 1, equals|, ) -> + : * NEW: call(, ) -> +1 : * NEW: r(1) -> +1 : * NEW: r(1) -> +1 == 1 : Boolean NEW: call(1 == 1, equals|, ) -> return !: * -{ return } !: * COPY -null : * NEW: r(null) -> -null!! : * NEW: magic[NOT_NULL_ASSERTION](null!!|) -> -if (1 == 1) { return } else null!! : * NEW: merge(if (1 == 1) { return } else null!!|!, ) -> -1 : Int NEW: r(1) -> +{ return } !: * COPY +null : * NEW: r(null) -> +null!! : * NEW: magic[NOT_NULL_ASSERTION](null!!|) -> +if (1 == 1) { return } else null!! : * NEW: merge(if (1 == 1) { return } else null!!|!, ) -> +1 : Int NEW: r(1) -> x = 1 !: * -{ if (1 == 1) { return } else null!! x = 1 } !: * COPY +{ if (1 == 1) { return } else null!! x = 1 } !: * COPY ===================== diff --git a/compiler/testData/cfg/tailCalls/sum.values b/compiler/testData/cfg/tailCalls/sum.values index a56298af703..8144729e624 100644 --- a/compiler/testData/cfg/tailCalls/sum.values +++ b/compiler/testData/cfg/tailCalls/sum.values @@ -6,7 +6,7 @@ tailrec fun sum(x: Long, sum: Long): Long { --------------------- : Long NEW: magic[FAKE_INITIALIZER](x: Long) -> : Long NEW: magic[FAKE_INITIALIZER](sum: Long) -> -x : OR{*, *} NEW: r(x) -> +x : * NEW: r(x) -> 0 : {<: Number} NEW: r(0) -> toLong() : * NEW: call(toLong(), toLong|) -> 0.toLong() : * COPY diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt index c3e4eee2acf..6ea0ba6dedf 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/KotlinTypeFactory.kt @@ -34,13 +34,27 @@ object KotlinTypeFactory { } } + @JvmStatic + fun simpleType( + annotations: Annotations, + constructor: TypeConstructor, + arguments: List, + nullable: Boolean + ): SimpleType { + if (annotations.isEmpty() && arguments.isEmpty() && !nullable && constructor.declarationDescriptor != null) { + return constructor.declarationDescriptor!!.defaultType + } + + return SimpleTypeImpl(annotations, constructor, arguments, nullable, computeMemberScope(constructor, arguments)) + } + @JvmStatic fun simpleType( annotations: Annotations, constructor: TypeConstructor, arguments: List, nullable: Boolean, - memberScope: MemberScope = computeMemberScope(constructor, arguments) + memberScope: MemberScope ): SimpleType = SimpleTypeImpl(annotations, constructor, arguments, nullable, memberScope) @JvmStatic