Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
37a702b9
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
37a702b9
编写于
2月 05, 2021
作者:
J
Jinseong Jeon
提交者:
Dmitriy Novozhilov
2月 08, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
FIR: coerce to Unit when a lambda has early returns
^KT-39075 Fixed
上级
83e32016
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
26 addition
and
11 deletion
+26
-11
compiler/fir/analysis-tests/testData/resolve/inference/coercionToUnitWithEarlyReturn.fir.txt
...a/resolve/inference/coercionToUnitWithEarlyReturn.fir.txt
+2
-2
compiler/fir/analysis-tests/testData/resolve/inference/coercionToUnitWithEarlyReturn.kt
...stData/resolve/inference/coercionToUnitWithEarlyReturn.kt
+1
-2
compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt
...formers/body/resolve/FirDeclarationsResolveTransformer.kt
+23
-7
未找到文件。
compiler/fir/analysis-tests/testData/resolve/inference/coercionToUnitWithEarlyReturn.fir.txt
浏览文件 @
37a702b9
...
...
@@ -11,7 +11,7 @@ FILE: coercionToUnitWithEarlyReturn.kt
public final fun foo(x: R|() -> kotlin/Unit|): R|kotlin/Unit| {
}
public final fun main(x: R|A?|): R|kotlin/Unit| {
lval lambda: R|() -> kotlin/Unit
?| = l@fun <anonymous>(): R|kotlin/Unit?
| {
lval lambda: R|() -> kotlin/Unit
| = l@fun <anonymous>(): R|kotlin/Unit
| {
when () {
==(R|<local>/x|?.{ $subj$.R|kotlin/Any.hashCode|() }, Int(0)) -> {
^@l Unit
...
...
@@ -21,5 +21,5 @@ FILE: coercionToUnitWithEarlyReturn.kt
^ R|<local>/x|?.{ $subj$.R|/A.unit|() }
}
<Inapplicable(INAPPLICABLE): /foo>#
(R|<local>/lambda|)
R|/foo|
(R|<local>/lambda|)
}
compiler/fir/analysis-tests/testData/resolve/inference/coercionToUnitWithEarlyReturn.kt
浏览文件 @
37a702b9
...
...
@@ -14,6 +14,5 @@ fun main(x: A?) {
x
?.
unit
()
}
// lambda has a type (() -> Unit?)
<!
INAPPLICABLE_CANDIDATE
{
LT
}!><!
INAPPLICABLE_CANDIDATE
{
PSI
}!>
foo
<!>(
lambda
)<!>
foo
(
lambda
)
}
compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/transformers/body/resolve/FirDeclarationsResolveTransformer.kt
浏览文件 @
37a702b9
...
...
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.fir.types.*
import
org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef
import
org.jetbrains.kotlin.fir.types.builder.buildImplicitTypeRef
import
org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import
org.jetbrains.kotlin.fir.types.impl.FirImplicitUnitTypeRef
import
org.jetbrains.kotlin.fir.visitors.*
import
org.jetbrains.kotlin.name.Name
import
org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult
...
...
@@ -780,13 +781,28 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
dataFlowAnalyzer
,
)
lambda
.
transformSingle
(
writer
,
expectedTypeRef
.
coneTypeSafe
<
ConeKotlinType
>()
?.
toExpectedType
())
val
returnTypes
=
dataFlowAnalyzer
.
returnExpressionsOfAnonymousFunction
(
lambda
)
.
mapNotNull
{
(
it
as
?
FirExpression
)
?.
resultType
?.
coneType
}
lambda
.
replaceReturnTypeRef
(
lambda
.
returnTypeRef
.
resolvedTypeFromPrototype
(
inferenceComponents
.
ctx
.
commonSuperTypeOrNull
(
returnTypes
)
?:
session
.
builtinTypes
.
unitType
.
type
)
)
val
returnStatements
=
dataFlowAnalyzer
.
returnExpressionsOfAnonymousFunction
(
lambda
)
val
returnExpressionsExceptLast
=
if
(
returnStatements
.
size
>
1
)
returnStatements
-
lambda
.
body
?.
statements
?.
lastOrNull
()
else
returnStatements
val
implicitReturns
=
returnExpressionsExceptLast
.
filter
{
(
it
as
?
FirExpression
)
?.
typeRef
is
FirImplicitUnitTypeRef
}
val
returnType
=
if
(
implicitReturns
.
isNotEmpty
())
{
// i.e., early return, e.g., l@{ ... return@l ... }
// Note that the last statement will be coerced to Unit if needed.
session
.
builtinTypes
.
unitType
.
type
}
else
{
// Otherwise, compute the common super type of all possible return expressions
inferenceComponents
.
ctx
.
commonSuperTypeOrNull
(
returnStatements
.
mapNotNull
{
(
it
as
?
FirExpression
)
?.
resultType
?.
coneType
}
)
?:
session
.
builtinTypes
.
unitType
.
type
}
lambda
.
replaceReturnTypeRef
(
lambda
.
returnTypeRef
.
resolvedTypeFromPrototype
(
returnType
))
lambda
.
replaceTypeRef
(
lambda
.
constructFunctionalTypeRef
(
isSuspend
=
expectedTypeRef
.
coneTypeSafe
<
ConeKotlinType
>()
?.
isSuspendFunctionType
(
session
)
==
true
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录