Skip to content

Commit 2f6fb40

Browse files
committed
making LambdaArgument methods nullable
overridden `LambdaArgument.getArgumentExpression` removed because it is already nullable in parent #EA-117013
1 parent 664a25c commit 2f6fb40

File tree

18 files changed

+47
-45
lines changed

18 files changed

+47
-45
lines changed

compiler/frontend/src/org/jetbrains/kotlin/contracts/EffectsExtractingVisitor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class EffectsExtractingVisitor(
173173
valueArgumentsByIndex?.mapTo(arguments) {
174174
val valueArgument = (it as? ExpressionValueArgument)?.valueArgument ?: return null
175175
when (valueArgument) {
176-
is KtLambdaArgument -> ESLambda(valueArgument.getLambdaExpression())
176+
is KtLambdaArgument -> valueArgument.getLambdaExpression()?.let { ESLambda(it) } ?: return null
177177
else -> extractOrGetCached(valueArgument.getArgumentExpression() ?: return null)
178178
}
179179
} ?: return null

compiler/frontend/src/org/jetbrains/kotlin/psi/KtLambdaArgument.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import com.intellij.lang.ASTNode
2020

2121
class KtLambdaArgument(node: ASTNode) : KtValueArgument(node), LambdaArgument {
2222

23-
override fun getArgumentExpression() = super.getArgumentExpression()!!
24-
25-
override fun getLambdaExpression(): KtLambdaExpression = getArgumentExpression().unpackFunctionLiteral()!!
23+
override fun getLambdaExpression(): KtLambdaExpression? = getArgumentExpression()?.unpackFunctionLiteral()
2624
}
2725

2826
fun KtExpression.unpackFunctionLiteral(allowParentheses: Boolean = false): KtLambdaExpression? {

compiler/frontend/src/org/jetbrains/kotlin/psi/ValueArgument.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ interface ValueArgument {
3737
}
3838

3939
interface LambdaArgument : ValueArgument {
40-
fun getLambdaExpression(): KtLambdaExpression
41-
42-
override fun getArgumentExpression(): KtExpression
40+
fun getLambdaExpression(): KtLambdaExpression?
4341
}
4442

4543
interface ValueArgumentName {

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tasks/dynamicCalls.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl
3131
import org.jetbrains.kotlin.resolve.scopes.receivers.TransientReceiver
3232
import org.jetbrains.kotlin.storage.StorageManager
3333
import org.jetbrains.kotlin.storage.getValue
34-
import org.jetbrains.kotlin.types.KotlinType
35-
import org.jetbrains.kotlin.types.Variance
36-
import org.jetbrains.kotlin.types.createDynamicType
34+
import org.jetbrains.kotlin.types.*
3735
import org.jetbrains.kotlin.types.expressions.OperatorConventions
38-
import org.jetbrains.kotlin.types.isDynamic
3936
import org.jetbrains.kotlin.utils.Printer
4037
import java.util.*
4138

@@ -53,7 +50,8 @@ class DynamicCallableDescriptors(storageManager: StorageManager, builtIns: Kotli
5350
override fun getContributedFunctions(name: Name, location: LookupLocation): Collection<SimpleFunctionDescriptor> {
5451
if (isAugmentedAssignmentConvention(name)) return listOf()
5552
if (call.callType == Call.CallType.INVOKE
56-
&& call.valueArgumentList == null && call.functionLiteralArguments.isEmpty()) {
53+
&& call.valueArgumentList == null && call.functionLiteralArguments.isEmpty()
54+
) {
5755
// this means that we are looking for "imaginary" invokes,
5856
// e.g. in `+d` we are looking for property "plus" with member "invoke"
5957
return listOf()
@@ -217,7 +215,10 @@ class DynamicCallableDescriptors(storageManager: StorageManager, builtIns: Kotli
217215

218216
if (hasSpreadOperator) {
219217
for (funLiteralArg in call.functionLiteralArguments) {
220-
addParameter(funLiteralArg, getFunctionType(funLiteralArg.getLambdaExpression()), null)
218+
addParameter(
219+
funLiteralArg,
220+
funLiteralArg.getLambdaExpression()?.let { getFunctionType(it) } ?: TypeUtils.CANT_INFER_FUNCTION_PARAM_TYPE,
221+
null)
221222
}
222223

223224
break

compiler/frontend/src/org/jetbrains/kotlin/resolve/calls/tower/PSICallResolver.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,8 @@ class PSICallResolver(
462462
oldCall.valueArguments.last()
463463
} else {
464464
if (externalLambdaArguments.size > 2) {
465-
externalLambdaArguments.drop(1).forEach {
466-
context.trace.report(Errors.MANY_LAMBDA_EXPRESSION_ARGUMENTS.on(it.getLambdaExpression()))
465+
externalLambdaArguments.drop(1).mapNotNull { it.getLambdaExpression() }.forEach {
466+
context.trace.report(Errors.MANY_LAMBDA_EXPRESSION_ARGUMENTS.on(it))
467467
}
468468
}
469469

compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/generators/ErrorExpressionGenerator.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,13 @@ class ErrorExpressionGenerator(statementGenerator: StatementGenerator) : Stateme
5050
receiverExpression.genExpr()
5151
}
5252

53-
ktCall.valueArguments.forEach {
53+
(ktCall.valueArguments + ktCall.lambdaArguments).forEach {
5454
val ktArgument = it.getArgumentExpression()
5555
if (ktArgument != null) {
5656
irErrorCall.addArgument(ktArgument.genExpr())
5757
}
5858
}
5959

60-
ktCall.lambdaArguments.forEach {
61-
irErrorCall.addArgument(it.getArgumentExpression().genExpr())
62-
}
63-
6460
irErrorCall
6561
}
6662

idea/idea-analysis/src/org/jetbrains/kotlin/idea/references/KtInvokeFunctionReference.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class KtInvokeFunctionReference(expression: KtCallExpression) : KtSimpleReferenc
7676

7777
val functionLiteralArguments = expression.lambdaArguments
7878
for (functionLiteralArgument in functionLiteralArguments) {
79-
val functionLiteralExpression = functionLiteralArgument.getArgumentExpression().unpackFunctionLiteral() ?: continue
79+
val functionLiteralExpression = functionLiteralArgument.getLambdaExpression() ?: continue
8080
list.add(getRange(functionLiteralExpression.leftCurlyBrace))
8181
val rightCurlyBrace = functionLiteralExpression.rightCurlyBrace
8282
if (rightCurlyBrace != null) {

idea/idea-core/src/org/jetbrains/kotlin/idea/core/psiModificationUtils.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParenthese
4141
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
4242
import org.jetbrains.kotlin.types.KotlinType
4343
import org.jetbrains.kotlin.types.isError
44+
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
4445
import org.jetbrains.kotlin.utils.SmartList
4546

4647
@Suppress("UNCHECKED_CAST")
@@ -56,7 +57,10 @@ inline fun <reified T : PsiElement> PsiElement.replaced(newElement: T): T {
5657
fun <T : PsiElement> T.copied(): T = copy() as T
5758

5859
fun KtLambdaArgument.moveInsideParentheses(bindingContext: BindingContext): KtCallExpression {
59-
return moveInsideParenthesesAndReplaceWith(this.getArgumentExpression(), bindingContext)
60+
val ktExpression = this.getArgumentExpression()
61+
?: throw KotlinExceptionWithAttachments("no argument expression for $this")
62+
.withAttachment("lambdaExpression", this.text)
63+
return moveInsideParenthesesAndReplaceWith(ktExpression, bindingContext)
6064
}
6165

6266
fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(

idea/src/org/jetbrains/kotlin/idea/codeInliner/ReplacementPerformer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
2929
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
3030
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
3131
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression
32+
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
3233
import java.util.*
3334

3435
internal abstract class ReplacementPerformer<TElement : KtElement>(
@@ -210,7 +211,10 @@ internal class ExpressionReplacementPerformer(
210211

211212
val runExpression = psiFactory.createExpressionByPattern("run { $0 }", elementToBeReplaced) as KtCallExpression
212213
val runAfterReplacement = elementToBeReplaced.replaced(runExpression)
213-
val block = runAfterReplacement.lambdaArguments[0].getLambdaExpression().bodyExpression!!
214+
val ktLambdaArgument = runAfterReplacement.lambdaArguments[0]
215+
val block = ktLambdaArgument.getLambdaExpression()?.bodyExpression
216+
?: throw KotlinExceptionWithAttachments("cant get body expression for $ktLambdaArgument")
217+
.withAttachment("ktLambdaArgument", ktLambdaArgument.text)
214218
elementToBeReplaced = block.statements.single()
215219
return elementToBeReplaced
216220

idea/src/org/jetbrains/kotlin/idea/codeInliner/UsageReplacementStrategy.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private fun UsageReplacementStrategy.specialUsageProcessing(usage: KtSimpleNameE
164164
val grandParent = usageParent.parent
165165
val specifySignature = SpecifyExplicitLambdaSignatureIntention()
166166
for (lambdaArgument in lambdaArguments) {
167-
val lambdaExpression = lambdaArgument.getLambdaExpression()
167+
val lambdaExpression = lambdaArgument.getLambdaExpression() ?: continue
168168
val functionDescriptor =
169169
lambdaExpression.functionLiteral.resolveToDescriptorIfAny() as? FunctionDescriptor ?: continue
170170
if (functionDescriptor.valueParameters.isNotEmpty()) {

idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,9 @@ private static KtBlockExpression getDSLLambdaBlock(@NotNull PsiElement element,
217217
List<KtLambdaArgument> functionLiterals = callExpression.getLambdaArguments();
218218
if (functionLiterals.isEmpty()) return null;
219219

220-
return functionLiterals.get(0).getLambdaExpression().getBodyExpression();
220+
KtLambdaExpression lambdaExpression = functionLiterals.get(0).getLambdaExpression();
221+
if (lambdaExpression == null) return null;
222+
return lambdaExpression.getBodyExpression();
221223
}
222224

223225
@Nullable

idea/src/org/jetbrains/kotlin/idea/inspections/ScopeFunctionConversionInspection.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ private fun getCounterpart(expression: KtCallExpression): String? {
7272
val callee = expression.calleeExpression as? KtNameReferenceExpression ?: return null
7373
val calleeName = callee.getReferencedName()
7474
val counterpartName = counterpartNames[calleeName]
75-
val lambdaArgument = expression.lambdaArguments.singleOrNull()
76-
if (counterpartName != null && lambdaArgument != null) {
77-
if (lambdaArgument.getLambdaExpression().valueParameters.isNotEmpty()) {
75+
val lambdaExpression = expression.lambdaArguments.singleOrNull()?.getLambdaExpression()
76+
if (counterpartName != null && lambdaExpression != null) {
77+
if (lambdaExpression.valueParameters.isNotEmpty()) {
7878
return null
7979
}
8080
val bindingContext = callee.analyze(BodyResolveMode.PARTIAL)
@@ -151,7 +151,7 @@ abstract class ConvertScopeFunctionFix(private val counterpartName: String) : Lo
151151
val bindingContext = callExpression.analyze()
152152

153153
val lambda = callExpression.lambdaArguments.firstOrNull() ?: return
154-
val functionLiteral = lambda.getLambdaExpression().functionLiteral
154+
val functionLiteral = lambda.getLambdaExpression()?.functionLiteral ?: return
155155
val lambdaDescriptor = bindingContext[FUNCTION, functionLiteral] ?: return
156156

157157
val replacements = ReplacementCollection()
@@ -186,13 +186,13 @@ class ConvertScopeFunctionToParameter(counterpartName: String) : ConvertScopeFun
186186
) {
187187
val project = lambda.project
188188
val factory = KtPsiFactory(project)
189-
val functionLiteral = lambda.getLambdaExpression().functionLiteral
189+
val functionLiteral = lambda.getLambdaExpression()?.functionLiteral
190190
val lambdaExtensionReceiver = lambdaDescriptor.extensionReceiverParameter
191191
val lambdaDispatchReceiver = lambdaDescriptor.dispatchReceiverParameter
192192

193193
var parameterName = "it"
194194
val scopes = mutableSetOf<LexicalScope>()
195-
if (needUniqueNameForParameter(lambda, scopes)) {
195+
if (functionLiteral != null && needUniqueNameForParameter(lambda, scopes)) {
196196
val parameterType = lambdaExtensionReceiver?.type ?: lambdaDispatchReceiver?.type
197197
parameterName = findUniqueParameterName(parameterType, scopes)
198198
replacements.createParameter = {

idea/src/org/jetbrains/kotlin/idea/inspections/collections/SimplifyCallChainFix.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ class SimplifyCallChainFix(val newName: String) : LocalQuickFix {
4747
val lastArgumentPrefix = if (newName.startsWith("joinTo")) "transform = " else ""
4848
val arguments = secondCallExpression.valueArgumentList?.arguments.orEmpty().map { it.text } +
4949
firstCallExpression.valueArgumentList?.arguments.orEmpty().map { "$lastArgumentPrefix${it.text}" }
50-
val lambdaArgument = firstCallExpression.lambdaArguments.singleOrNull()
50+
val lambdaExpression = firstCallExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()
5151

5252
val argumentsText = arguments.ifNotEmpty { joinToString(prefix = "(", postfix = ")") } ?: ""
53-
val newQualifiedExpression = if (lambdaArgument != null) factory.createExpressionByPattern(
54-
"$0$1$2 $3 $4",
55-
receiverExpression ?: "",
56-
operationSign,
57-
newName,
58-
argumentsText,
59-
lambdaArgument.getLambdaExpression().text
53+
val newQualifiedExpression = if (lambdaExpression != null) factory.createExpressionByPattern(
54+
"$0$1$2 $3 $4",
55+
receiverExpression ?: "",
56+
operationSign,
57+
newName,
58+
argumentsText,
59+
lambdaExpression.text
6060
)
6161
else factory.createExpressionByPattern(
6262
"$0$1$2 $3",

idea/src/org/jetbrains/kotlin/idea/intentions/ConvertTryFinallyToUseCallIntention.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ConvertTryFinallyToUseCallIntention : SelfTargetingRangeIntention<KtTryExp
7676
else -> return
7777
}
7878
val lambda = call.lambdaArguments.firstOrNull() ?: return
79-
val lambdaParameter = lambda.getLambdaExpression().valueParameters.firstOrNull() ?: return
79+
val lambdaParameter = lambda.getLambdaExpression()?.valueParameters?.firstOrNull() ?: return
8080
editor?.selectionModel?.setSelection(lambdaParameter.startOffset, lambdaParameter.endOffset)
8181
}
8282

idea/src/org/jetbrains/kotlin/idea/intentions/MoveLambdaInsideParenthesesIntention.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
2222
import org.jetbrains.kotlin.idea.core.moveInsideParentheses
2323
import org.jetbrains.kotlin.psi.KtLambdaArgument
2424
import org.jetbrains.kotlin.psi.psiUtil.containsInside
25-
import org.jetbrains.kotlin.psi.unpackFunctionLiteral
2625

2726
class MoveLambdaInsideParenthesesIntention : SelfTargetingIntention<KtLambdaArgument>(
2827
KtLambdaArgument::class.java, "Move lambda argument into parentheses"
2928
), LowPriorityAction {
3029
override fun isApplicableTo(element: KtLambdaArgument, caretOffset: Int): Boolean {
31-
val body = element.getArgumentExpression().unpackFunctionLiteral()?.bodyExpression ?: return true
30+
val body = element.getLambdaExpression()?.bodyExpression ?: return true
3231
return !body.textRange.containsInside(caretOffset)
3332
}
3433

idea/src/org/jetbrains/kotlin/idea/intentions/ObjectLiteralToLambdaIntention.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
145145

146146
val callee = replaced.getCalleeExpressionIfAny()!! as KtNameReferenceExpression
147147
val callExpression = callee.parent as KtCallExpression
148-
val functionLiteral = callExpression.lambdaArguments.single().getLambdaExpression()
148+
val functionLiteral = callExpression.lambdaArguments.single().getLambdaExpression()!!
149149

150150
val returnLabel = callee.getReferencedNameAsName()
151151
returnSaver.restore(functionLiteral, returnLabel)

idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/ExtractableCodeDescriptor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ abstract class WrapInWithReplacement : Replacement {
101101
val call = (e as? KtSimpleNameExpression)?.getQualifiedElement() ?: return e
102102
val replacingExpression = KtPsiFactory(e).createExpressionByPattern("with($0) { $1 }", argumentText, call)
103103
val replace = call.replace(replacingExpression)
104-
return (replace as KtCallExpression).lambdaArguments.first().getLambdaExpression().bodyExpression!!.statements.first()
104+
return (replace as KtCallExpression).lambdaArguments.first().getLambdaExpression()!!.bodyExpression!!.statements.first()
105105
}
106106
}
107107

plugins/uast-kotlin/src/org/jetbrains/uast/kotlin/KotlinUastLanguagePlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ internal object KotlinConverter {
328328
}
329329

330330
is KtExpression -> KotlinConverter.convertExpression(element, givenParent, requiredType)
331-
is KtLambdaArgument -> KotlinConverter.convertExpression(element.getLambdaExpression(), givenParent, requiredType)
331+
is KtLambdaArgument -> element.getLambdaExpression()?.let { KotlinConverter.convertExpression(it, givenParent, requiredType) }
332332
is KtLightAnnotationForSourceEntry.LightExpressionValue<*> -> {
333333
val expression = element.originalExpression
334334
when (expression) {

0 commit comments

Comments
 (0)