/
TypeResolver.kt
943 lines (809 loc) · 44.4 KB
/
TypeResolver.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve
import com.intellij.util.SmartList
import org.jetbrains.kotlin.builtins.PlatformToKotlinClassMap
import org.jetbrains.kotlin.builtins.createFunctionType
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.annotations.composeAnnotations
import org.jetbrains.kotlin.descriptors.impl.VariableDescriptorImpl
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.codeFragmentUtil.suppressDiagnosticsInDebugMode
import org.jetbrains.kotlin.psi.debugText.getDebugText
import org.jetbrains.kotlin.psi.psiUtil.checkReservedYield
import org.jetbrains.kotlin.psi.psiUtil.getNextSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.psi.psiUtil.hasSuspendModifier
import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
import org.jetbrains.kotlin.resolve.PossiblyBareType.bare
import org.jetbrains.kotlin.resolve.PossiblyBareType.type
import org.jetbrains.kotlin.resolve.bindingContextUtil.recordScope
import org.jetbrains.kotlin.resolve.calls.checkers.checkCoroutinesFeature
import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallableDescriptors
import org.jetbrains.kotlin.resolve.descriptorUtil.findImplicitOuterClassArguments
import org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
import org.jetbrains.kotlin.resolve.scopes.LexicalScopeKind
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.resolve.scopes.utils.findFirstFromMeAndParent
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
import org.jetbrains.kotlin.resolve.source.getPsi
import org.jetbrains.kotlin.resolve.source.toSourceElement
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.Variance.*
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliasParameters
import org.jetbrains.kotlin.types.typeUtil.containsTypeAliases
import org.jetbrains.kotlin.types.typeUtil.isArrayOfNothing
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import kotlin.math.min
class TypeResolver(
private val annotationResolver: AnnotationResolver,
private val qualifiedExpressionResolver: QualifiedExpressionResolver,
private val moduleDescriptor: ModuleDescriptor,
private val typeTransformerForTests: TypeTransformerForTests,
private val dynamicTypesSettings: DynamicTypesSettings,
private val dynamicCallableDescriptors: DynamicCallableDescriptors,
private val identifierChecker: IdentifierChecker,
private val platformToKotlinClassMap: PlatformToKotlinClassMap,
private val languageVersionSettings: LanguageVersionSettings
) {
private val isNonParenthesizedAnnotationsOnFunctionalTypesEnabled =
languageVersionSettings.getFeatureSupport(LanguageFeature.NonParenthesizedAnnotationsOnFunctionalTypes) == LanguageFeature.State.ENABLED
open class TypeTransformerForTests {
open fun transformType(kotlinType: KotlinType): KotlinType? = null
}
fun resolveType(scope: LexicalScope, typeReference: KtTypeReference, trace: BindingTrace, checkBounds: Boolean): KotlinType {
// bare types are not allowed
return resolveType(
TypeResolutionContext(scope, trace, checkBounds, false, typeReference.suppressDiagnosticsInDebugMode(), false),
typeReference
)
}
fun resolveAbbreviatedType(scope: LexicalScope, typeReference: KtTypeReference, trace: BindingTrace): SimpleType {
val resolvedType = resolveType(
TypeResolutionContext(scope, trace, true, false, typeReference.suppressDiagnosticsInDebugMode(), true),
typeReference
).unwrap()
return when (resolvedType) {
is DynamicType -> {
trace.report(Errors.TYPEALIAS_SHOULD_EXPAND_TO_CLASS.on(typeReference, resolvedType))
ErrorUtils.createErrorType("dynamic type in wrong context")
}
is SimpleType -> resolvedType
else -> error("Unexpected type: $resolvedType")
}
}
fun resolveExpandedTypeForTypeAlias(typeAliasDescriptor: TypeAliasDescriptor): SimpleType {
val typeAliasExpansion = TypeAliasExpansion.createWithFormalArguments(typeAliasDescriptor)
val expandedType = TypeAliasExpander.NON_REPORTING.expandWithoutAbbreviation(typeAliasExpansion, Annotations.EMPTY)
return expandedType
}
private fun resolveType(c: TypeResolutionContext, typeReference: KtTypeReference): KotlinType {
assert(!c.allowBareTypes) { "Use resolvePossiblyBareType() when bare types are allowed" }
return resolvePossiblyBareType(c, typeReference).actualType
}
fun resolvePossiblyBareType(c: TypeResolutionContext, typeReference: KtTypeReference): PossiblyBareType {
val cachedType = c.trace.bindingContext.get(BindingContext.TYPE, typeReference)
if (cachedType != null) return type(cachedType)
val resolvedTypeSlice = if (c.abbreviated) BindingContext.ABBREVIATED_TYPE else BindingContext.TYPE
val annotations = resolveTypeAnnotations(c, typeReference)
val type = resolveTypeElement(c, annotations, typeReference.modifierList, typeReference.typeElement)
c.trace.recordScope(c.scope, typeReference)
if (!type.isBare) {
for (argument in type.actualType.arguments) {
forceResolveTypeContents(argument.type)
}
c.trace.record(resolvedTypeSlice, typeReference, type.actualType)
}
return type
}
internal fun KtElementImplStub<*>.getAllModifierLists(): Array<out KtDeclarationModifierList> =
getStubOrPsiChildren(KtStubElementTypes.MODIFIER_LIST, KtStubElementTypes.MODIFIER_LIST.arrayFactory)
// TODO: remove this method and its usages in 1.4
private fun checkNonParenthesizedAnnotationsOnFunctionalType(
typeElement: KtFunctionType,
annotationEntries: List<KtAnnotationEntry>,
trace: BindingTrace
) {
val lastAnnotationEntry = annotationEntries.lastOrNull()
val isAnnotationsGroupedUsingBrackets =
lastAnnotationEntry?.getNextSiblingIgnoringWhitespaceAndComments()?.node?.elementType == KtTokens.RBRACKET
val hasAnnotationParentheses = lastAnnotationEntry?.valueArgumentList != null
val isFunctionalTypeStartingWithParentheses = typeElement.firstChild is KtParameterList
val hasSuspendModifierBeforeParentheses =
typeElement.getPrevSiblingIgnoringWhitespaceAndComments().run { this is KtDeclarationModifierList && hasSuspendModifier() }
if (lastAnnotationEntry != null &&
isFunctionalTypeStartingWithParentheses &&
!hasAnnotationParentheses &&
!isAnnotationsGroupedUsingBrackets &&
!hasSuspendModifierBeforeParentheses
) {
trace.report(Errors.NON_PARENTHESIZED_ANNOTATIONS_ON_FUNCTIONAL_TYPES.on(lastAnnotationEntry))
}
}
private fun resolveTypeAnnotations(c: TypeResolutionContext, modifierListsOwner: KtElementImplStub<*>): Annotations {
val modifierLists = modifierListsOwner.getAllModifierLists()
var result = Annotations.EMPTY
var isSplitModifierList = false
if (!isNonParenthesizedAnnotationsOnFunctionalTypesEnabled) {
val targetType = when (modifierListsOwner) {
is KtNullableType -> modifierListsOwner.innerType
is KtTypeReference -> modifierListsOwner.typeElement
else -> null
}
val annotationEntries = when (modifierListsOwner) {
is KtNullableType -> modifierListsOwner.modifierList?.annotationEntries
is KtTypeReference -> modifierListsOwner.annotationEntries
else -> null
}
// `targetType.stub == null` means that we don't apply this check for files that are built with stubs (that aren't opened in IDE and not in compile time)
if (targetType is KtFunctionType && targetType.stub == null && annotationEntries != null) {
checkNonParenthesizedAnnotationsOnFunctionalType(targetType, annotationEntries, c.trace)
}
}
for (modifierList in modifierLists) {
if (isSplitModifierList) {
c.trace.report(MODIFIER_LIST_NOT_ALLOWED.on(modifierList))
}
val annotations = annotationResolver.resolveAnnotationsWithoutArguments(c.scope, modifierList.annotationEntries, c.trace)
result = composeAnnotations(result, annotations)
isSplitModifierList = true
}
return result
}
/**
* This function is light version of ForceResolveUtil.forceResolveAllContents
* We can't use ForceResolveUtil.forceResolveAllContents here because it runs ForceResolveUtil.forceResolveAllContents(getConstructor()),
* which is unsafe for some cyclic cases. For Example:
* class A: List<A.B> {
* class B
* }
* Here when we resolve class B, we should resolve supertype for A and we shouldn't start resolve for class B,
* otherwise it would be a cycle.
* Now there is no cycle here because member scope for A is very clever and can get lazy descriptor for class B without resolving it.
*
* todo: find another way after release
*/
private fun forceResolveTypeContents(type: KotlinType) {
type.annotations // force read type annotations
if (type.isFlexible()) {
forceResolveTypeContents(type.asFlexibleType().lowerBound)
forceResolveTypeContents(type.asFlexibleType().upperBound)
} else {
type.constructor // force read type constructor
for (projection in type.arguments) {
if (!projection.isStarProjection) {
forceResolveTypeContents(projection.type)
}
}
}
}
private fun resolveTypeElement(
c: TypeResolutionContext,
annotations: Annotations,
outerModifierList: KtModifierList?,
typeElement: KtTypeElement?
): PossiblyBareType {
var result: PossiblyBareType? = null
val hasSuspendModifier = outerModifierList?.hasModifier(KtTokens.SUSPEND_KEYWORD) ?: false
val suspendModifier by lazy { outerModifierList?.getModifier(KtTokens.SUSPEND_KEYWORD) }
if (hasSuspendModifier && !typeElement.canHaveFunctionTypeModifiers()) {
c.trace.report(Errors.WRONG_MODIFIER_TARGET.on(suspendModifier!!, KtTokens.SUSPEND_KEYWORD, "non-functional type"))
} else if (hasSuspendModifier) {
checkCoroutinesFeature(languageVersionSettings, c.trace, suspendModifier!!)
}
typeElement?.accept(object : KtVisitorVoid() {
override fun visitUserType(type: KtUserType) {
val qualifierResolutionResult = resolveDescriptorForType(c.scope, type, c.trace, c.isDebuggerContext)
val classifier = qualifierResolutionResult.classifierDescriptor
if (classifier == null) {
val arguments = resolveTypeProjections(
c, ErrorUtils.createErrorType("No type").constructor, qualifierResolutionResult.allProjections
)
result = type(ErrorUtils.createUnresolvedType(type.getDebugText(), arguments))
return
}
val referenceExpression = type.referenceExpression ?: return
checkReservedYield(referenceExpression, c.trace)
c.trace.record(BindingContext.REFERENCE_TARGET, referenceExpression, classifier)
result = resolveTypeForClassifier(c, classifier, qualifierResolutionResult, type, annotations)
}
override fun visitNullableType(nullableType: KtNullableType) {
val innerModifierList = nullableType.modifierList
if (innerModifierList != null && outerModifierList != null) {
c.trace.report(MODIFIER_LIST_NOT_ALLOWED.on(innerModifierList))
}
val innerAnnotations = composeAnnotations(annotations, resolveTypeAnnotations(c, nullableType))
val innerType = nullableType.innerType
val baseType = resolveTypeElement(c, innerAnnotations, outerModifierList ?: innerModifierList, innerType)
if (baseType.isNullable || innerType is KtNullableType || innerType is KtDynamicType) {
c.trace.report(REDUNDANT_NULLABLE.on(nullableType))
}
result = baseType.makeNullable()
}
override fun visitFunctionType(type: KtFunctionType) {
val receiverTypeRef = type.receiverTypeReference
val receiverType = if (receiverTypeRef == null) null else resolveType(c.noBareTypes(), receiverTypeRef)
val parameterDescriptors = resolveParametersOfFunctionType(type.parameters)
checkParametersOfFunctionType(parameterDescriptors)
val returnTypeRef = type.returnTypeReference
val returnType = if (returnTypeRef != null) resolveType(c.noBareTypes(), returnTypeRef)
else moduleDescriptor.builtIns.unitType
result = type(
createFunctionType(
moduleDescriptor.builtIns, annotations, receiverType,
parameterDescriptors.map { it.type },
parameterDescriptors.map { it.name },
returnType,
suspendFunction = hasSuspendModifier
)
)
}
private fun checkParametersOfFunctionType(parameterDescriptors: List<VariableDescriptor>) {
val parametersByName = parameterDescriptors.filter { !it.name.isSpecial }.groupBy { it.name }
for (parametersGroup in parametersByName.values) {
if (parametersGroup.size < 2) continue
for (parameter in parametersGroup) {
val ktParameter = parameter.source.getPsi()?.safeAs<KtParameter>() ?: continue
c.trace.report(DUPLICATE_PARAMETER_NAME_IN_FUNCTION_TYPE.on(ktParameter))
}
}
}
private fun resolveParametersOfFunctionType(parameters: List<KtParameter>): List<VariableDescriptor> {
class ParameterOfFunctionTypeDescriptor(
containingDeclaration: DeclarationDescriptor,
annotations: Annotations,
name: Name,
type: KotlinType,
source: SourceElement
) : VariableDescriptorImpl(containingDeclaration, annotations, name, type, source) {
override fun getVisibility() = Visibilities.LOCAL
override fun substitute(substitutor: TypeSubstitutor): VariableDescriptor? {
throw UnsupportedOperationException("Should not be called for descriptor of type ${this::class.java}")
}
override fun isVar() = false
override fun isLateInit() = false
override fun getCompileTimeInitializer() = null
override fun <R : Any?, D : Any?> accept(visitor: DeclarationDescriptorVisitor<R, D>, data: D): R {
return visitor.visitVariableDescriptor(this, data)
}
}
parameters.forEach {
identifierChecker.checkDeclaration(it, c.trace)
checkParameterInFunctionType(it)
}
return parameters.map { parameter ->
val parameterType = resolveType(c.noBareTypes(), parameter.typeReference!!)
val descriptor = ParameterOfFunctionTypeDescriptor(
c.scope.ownerDescriptor,
annotationResolver.resolveAnnotationsWithoutArguments(c.scope, parameter.modifierList, c.trace),
parameter.nameAsSafeName,
parameterType,
parameter.toSourceElement()
)
c.trace.record(BindingContext.VALUE_PARAMETER, parameter, descriptor)
descriptor
}
}
override fun visitDynamicType(type: KtDynamicType) {
result = type(dynamicCallableDescriptors.dynamicType.replaceAnnotations(annotations))
if (!dynamicTypesSettings.dynamicTypesAllowed) {
c.trace.report(UNSUPPORTED.on(type, "Dynamic types are not supported in this context"))
}
}
override fun visitSelfType(type: KtSelfType) {
c.trace.report(UNSUPPORTED.on(type, "Self-types are not supported"))
}
override fun visitKtElement(element: KtElement) {
c.trace.report(UNSUPPORTED.on(element, "Self-types are not supported yet"))
}
private fun checkParameterInFunctionType(param: KtParameter) {
if (param.hasDefaultValue()) {
c.trace.report(Errors.UNSUPPORTED.on(param.defaultValue!!, "default value of parameter in function type"))
}
if (param.name != null) {
for (annotationEntry in param.annotationEntries) {
c.trace.report(Errors.UNSUPPORTED.on(annotationEntry, "annotation on parameter in function type"))
}
}
val modifierList = param.modifierList
if (modifierList != null) {
KtTokens.MODIFIER_KEYWORDS_ARRAY
.mapNotNull { modifierList.getModifier(it) }
.forEach {
c.trace.report(Errors.UNSUPPORTED.on(it, "modifier on parameter in function type"))
}
}
param.valOrVarKeyword?.let {
c.trace.report(Errors.UNSUPPORTED.on(it, "val or val on parameter in function type"))
}
}
})
return result ?: type(ErrorUtils.createErrorType(typeElement?.getDebugText() ?: "No type element"))
}
private fun KtTypeElement?.canHaveFunctionTypeModifiers(): Boolean =
this is KtFunctionType
private fun resolveTypeForTypeParameter(
c: TypeResolutionContext, annotations: Annotations,
typeParameter: TypeParameterDescriptor,
referenceExpression: KtSimpleNameExpression,
typeArgumentList: KtTypeArgumentList?
): KotlinType {
val scopeForTypeParameter = getScopeForTypeParameter(c, typeParameter)
if (typeArgumentList != null) {
resolveTypeProjections(c, ErrorUtils.createErrorType("No type").constructor, typeArgumentList.arguments)
c.trace.report(TYPE_ARGUMENTS_NOT_ALLOWED.on(typeArgumentList, "for type parameters"))
}
val containing = typeParameter.containingDeclaration
if (containing is ClassDescriptor) {
DescriptorResolver.checkHasOuterClassInstance(c.scope, c.trace, referenceExpression, containing)
}
return if (scopeForTypeParameter is ErrorUtils.ErrorScope)
ErrorUtils.createErrorType("?")
else
KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(
annotations,
typeParameter.typeConstructor,
listOf(),
false,
scopeForTypeParameter
)
}
private fun getScopeForTypeParameter(c: TypeResolutionContext, typeParameterDescriptor: TypeParameterDescriptor): MemberScope {
return when {
c.checkBounds -> TypeIntersector.getUpperBoundsAsType(typeParameterDescriptor).memberScope
else -> LazyScopeAdapter(LockBasedStorageManager.NO_LOCKS.createLazyValue {
TypeIntersector.getUpperBoundsAsType(typeParameterDescriptor).memberScope
})
}
}
fun resolveTypeForClassifier(
c: TypeResolutionContext,
descriptor: ClassifierDescriptor,
qualifierResolutionResult: QualifiedExpressionResolver.TypeQualifierResolutionResult,
element: KtElement,
annotations: Annotations
): PossiblyBareType {
val qualifierParts = qualifierResolutionResult.qualifierParts
return when (descriptor) {
is TypeParameterDescriptor -> {
assert(qualifierParts.size == 1) {
"Type parameter can be resolved only by it's short name, but '${element.text}' is contradiction " +
"with ${qualifierParts.size} qualifier parts"
}
val qualifierPart = qualifierParts.single()
type(resolveTypeForTypeParameter(c, annotations, descriptor, qualifierPart.expression, qualifierPart.typeArguments))
}
is ClassDescriptor -> resolveTypeForClass(c, annotations, descriptor, element, qualifierResolutionResult)
is TypeAliasDescriptor -> resolveTypeForTypeAlias(c, annotations, descriptor, element, qualifierResolutionResult)
else -> error("Unexpected classifier type: ${descriptor::class.java}")
}
}
private fun resolveTypeForClass(
c: TypeResolutionContext, annotations: Annotations,
classDescriptor: ClassDescriptor, element: KtElement,
qualifierResolutionResult: QualifiedExpressionResolver.TypeQualifierResolutionResult
): PossiblyBareType {
val typeConstructor = classDescriptor.typeConstructor
val projectionFromAllQualifierParts = qualifierResolutionResult.allProjections
val parameters = typeConstructor.parameters
if (c.allowBareTypes && projectionFromAllQualifierParts.isEmpty() && isPossibleToSpecifyTypeArgumentsFor(classDescriptor)) {
// See docs for PossiblyBareType
return PossiblyBareType.bare(typeConstructor, false)
}
if (ErrorUtils.isError(classDescriptor)) {
return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor)
}
val (collectedArgumentAsTypeProjections, argumentsForOuterClass) =
collectArgumentsForClassifierTypeConstructor(c, classDescriptor, qualifierResolutionResult.qualifierParts)
?: return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor)
assert(collectedArgumentAsTypeProjections.size <= parameters.size) {
"Collected arguments count should be not greater then parameters count," +
" but ${collectedArgumentAsTypeProjections.size} instead of ${parameters.size} found in ${element.text}"
}
val argumentsFromUserType = resolveTypeProjections(c, typeConstructor, collectedArgumentAsTypeProjections)
val arguments = buildFinalArgumentList(argumentsFromUserType, argumentsForOuterClass, parameters)
assert(arguments.size == parameters.size) {
"Collected arguments count should be equal to parameters count," +
" but ${collectedArgumentAsTypeProjections.size} instead of ${parameters.size} found in ${element.text}"
}
val resultingType = KotlinTypeFactory.simpleNotNullType(annotations, classDescriptor, arguments)
// We create flexible types by convention here
// This is not intended to be used in normal users' environments, only for tests and debugger etc
typeTransformerForTests.transformType(resultingType)?.let { return type(it) }
if (shouldCheckBounds(c, resultingType)) {
val substitutor = TypeSubstitutor.create(resultingType)
for (i in parameters.indices) {
val parameter = parameters[i]
val argument = arguments[i].type
val typeReference = collectedArgumentAsTypeProjections.getOrNull(i)?.typeReference
if (typeReference != null) {
DescriptorResolver.checkBounds(typeReference, argument, parameter, substitutor, c.trace)
}
}
}
if (resultingType.isArrayOfNothing()) {
c.trace.report(UNSUPPORTED.on(element, "Array<Nothing> is illegal"))
}
return type(resultingType)
}
private fun buildFinalArgumentList(
argumentsFromUserType: List<TypeProjection>,
argumentsForOuterClass: List<TypeProjection>?,
parameters: List<TypeParameterDescriptor>
): List<TypeProjection> {
return argumentsFromUserType +
(argumentsForOuterClass ?: appendDefaultArgumentsForLocalClassifier(argumentsFromUserType.size, parameters))
}
private fun shouldCheckBounds(c: TypeResolutionContext, inType: KotlinType): Boolean {
if (!c.checkBounds) return false
if (inType.containsTypeAliasParameters()) return false
if (c.abbreviated && inType.containsTypeAliases()) return false
return true
}
private fun resolveTypeForTypeAlias(
c: TypeResolutionContext,
annotations: Annotations,
descriptor: TypeAliasDescriptor,
type: KtElement,
qualifierResolutionResult: QualifiedExpressionResolver.TypeQualifierResolutionResult
): PossiblyBareType {
val typeConstructor = descriptor.typeConstructor
val projectionFromAllQualifierParts = qualifierResolutionResult.allProjections
if (ErrorUtils.isError(descriptor)) {
return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor)
}
if (!languageVersionSettings.supportsFeature(LanguageFeature.TypeAliases)) {
c.trace.report(UNSUPPORTED_FEATURE.on(type, LanguageFeature.TypeAliases to languageVersionSettings))
return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor)
}
val parameters = typeConstructor.parameters
if (c.allowBareTypes && projectionFromAllQualifierParts.isEmpty() && isPossibleToSpecifyTypeArgumentsFor(descriptor)) {
val classDescriptor = descriptor.classDescriptor
if (classDescriptor != null && canBeUsedAsBareType(descriptor)) {
return bare(descriptor.classDescriptor!!.typeConstructor, TypeUtils.isNullableType(descriptor.expandedType))
}
}
val typeAliasQualifierPart =
qualifierResolutionResult.qualifierParts.lastOrNull()
?: return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor)
val (argumentElementsFromUserType, argumentsForOuterClass) =
collectArgumentsForClassifierTypeConstructor(c, descriptor, qualifierResolutionResult.qualifierParts)
?: return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor)
val argumentsFromUserType = resolveTypeProjections(c, typeConstructor, argumentElementsFromUserType)
val arguments = buildFinalArgumentList(argumentsFromUserType, argumentsForOuterClass, parameters)
val reportStrategy = TracingTypeAliasExpansionReportStrategy(
c.trace,
type, typeAliasQualifierPart.typeArguments ?: typeAliasQualifierPart.expression,
descriptor, descriptor.declaredTypeParameters,
argumentElementsFromUserType // TODO arguments from inner scope
)
if (parameters.size != arguments.size) {
reportStrategy.wrongNumberOfTypeArguments(descriptor, parameters.size)
return createErrorTypeForTypeConstructor(c, projectionFromAllQualifierParts, typeConstructor)
}
return if (c.abbreviated) {
val abbreviatedType = KotlinTypeFactory.simpleType(annotations, descriptor.typeConstructor, arguments, false)
type(abbreviatedType)
} else {
val typeAliasExpansion = TypeAliasExpansion.create(null, descriptor, arguments)
val expandedType = TypeAliasExpander(reportStrategy, c.checkBounds).expand(typeAliasExpansion, annotations)
type(expandedType)
}
}
/**
* Type alias can be used as bare type (after is/as, e.g., 'x is List')
* iff all type arguments of the corresponding expanded type are either star projections
* or type parameters of the given type alias in invariant projection,
* and each of the type parameters is mentioned no more than once.
*
* E.g.:
* ```
* typealias HashMap<K, V> = java.util.HashMap<K, V> // can be used as bare type
* typealias MyList<T, X> = List<X> // can be used as bare type
* typealias StarMap<T> = Map<T, *> // can be used as bare type
* typealias MyMap<T> = Map<T, T> // CAN NOT be used as bare type: type parameter 'T' is used twice
* typealias StringMap<T> = Map<String, T> // CAN NOT be used as bare type: type argument 'String' is not a type parameter
* ```
*/
private fun canBeUsedAsBareType(descriptor: TypeAliasDescriptor): Boolean {
val expandedType = descriptor.expandedType
if (expandedType.isError) return false
val classDescriptor = descriptor.classDescriptor ?: return false
if (!isPossibleToSpecifyTypeArgumentsFor(classDescriptor)) return false
val usedTypeParameters = linkedSetOf<TypeParameterDescriptor>()
for (argument in expandedType.arguments) {
if (argument.isStarProjection) continue
if (argument.projectionKind != INVARIANT) return false
val argumentTypeDescriptor = argument.type.constructor.declarationDescriptor as? TypeParameterDescriptor ?: return false
if (argumentTypeDescriptor.containingDeclaration != descriptor) return false
if (usedTypeParameters.contains(argumentTypeDescriptor)) return false
usedTypeParameters.add(argumentTypeDescriptor)
}
return true
}
private class TracingTypeAliasExpansionReportStrategy(
val trace: BindingTrace,
val type: KtElement?,
val typeArgumentsOrTypeName: KtElement?,
val typeAliasDescriptor: TypeAliasDescriptor,
typeParameters: List<TypeParameterDescriptor>,
typeArguments: List<KtTypeProjection>
) : TypeAliasExpansionReportStrategy {
private val mappedArguments = typeParameters.zip(typeArguments).toMap()
override fun wrongNumberOfTypeArguments(typeAlias: TypeAliasDescriptor, numberOfParameters: Int) {
if (typeArgumentsOrTypeName != null) {
trace.report(WRONG_NUMBER_OF_TYPE_ARGUMENTS.on(typeArgumentsOrTypeName, numberOfParameters, typeAliasDescriptor))
}
}
override fun conflictingProjection(
typeAlias: TypeAliasDescriptor,
typeParameter: TypeParameterDescriptor?,
substitutedArgument: KotlinType
) {
val argumentElement = typeParameter?.let { mappedArguments[it] }
if (argumentElement != null) {
trace.report(CONFLICTING_PROJECTION.on(argumentElement, typeParameter))
} else if (type != null) {
trace.report(CONFLICTING_PROJECTION_IN_TYPEALIAS_EXPANSION.on(type, typeAliasDescriptor.underlyingType))
}
}
override fun recursiveTypeAlias(typeAlias: TypeAliasDescriptor) {
if (type != null) {
trace.report(RECURSIVE_TYPEALIAS_EXPANSION.on(type, typeAlias))
}
}
override fun boundsViolationInSubstitution(
bound: KotlinType,
unsubstitutedArgument: KotlinType,
argument: KotlinType,
typeParameter: TypeParameterDescriptor
) {
val descriptorForUnsubstitutedArgument = unsubstitutedArgument.constructor.declarationDescriptor
val argumentElement = mappedArguments[descriptorForUnsubstitutedArgument]
val argumentTypeReferenceElement = argumentElement?.typeReference
if (argumentTypeReferenceElement != null) {
trace.report(UPPER_BOUND_VIOLATED.on(argumentTypeReferenceElement, bound, argument))
} else if (type != null) {
trace.report(UPPER_BOUND_VIOLATED_IN_TYPEALIAS_EXPANSION.on(type, bound, argument, typeParameter))
}
}
override fun repeatedAnnotation(annotation: AnnotationDescriptor) {
val annotationEntry = (annotation.source as? KotlinSourceElement)?.psi as? KtAnnotationEntry ?: return
trace.report(REPEATED_ANNOTATION.on(annotationEntry))
}
}
private fun createErrorTypeForTypeConstructor(
c: TypeResolutionContext,
arguments: List<KtTypeProjection>,
typeConstructor: TypeConstructor
): PossiblyBareType =
type(
ErrorUtils.createErrorTypeWithArguments(
typeConstructor.declarationDescriptor?.name?.asString() ?: typeConstructor.toString(),
resolveTypeProjectionsWithErrorConstructor(c, arguments)
)
)
// Returns true in case when at least one argument for this class could be specified
// It could be always equal to 'typeConstructor.parameters.isNotEmpty()' unless local classes could captured type parameters
// from enclosing functions. In such cases you can not specify any argument:
// fun <E> foo(x: Any?) {
// class C
// if (x is C) { // 'C' should not be treated as bare type here
// ...
// }
// }
//
// It's needed to determine whether this particular type could be bare
private fun isPossibleToSpecifyTypeArgumentsFor(classifierDescriptor: ClassifierDescriptorWithTypeParameters): Boolean {
// First parameter relates to the innermost declaration
// If it's declared in function there
val firstTypeParameter = classifierDescriptor.typeConstructor.parameters.firstOrNull() ?: return false
return firstTypeParameter.original.containingDeclaration is ClassifierDescriptorWithTypeParameters
}
/**
* @return yet unresolved KtTypeProjection arguments and already resolved ones relevant to an outer class
* @return null if error was reported
*
* If second component is null then rest of the arguments should be appended using default types of relevant parameters
*/
private fun collectArgumentsForClassifierTypeConstructor(
c: TypeResolutionContext,
classifierDescriptor: ClassifierDescriptorWithTypeParameters,
qualifierParts: List<QualifiedExpressionResolver.ExpressionQualifierPart>
): Pair<List<KtTypeProjection>, List<TypeProjection>?>? {
val classifierDescriptorChain = classifierDescriptor.classifierDescriptorsFromInnerToOuter()
val reversedQualifierParts = qualifierParts.asReversed()
var wasStatic = false
val result = SmartList<KtTypeProjection>()
val classifierChainLastIndex = min(classifierDescriptorChain.size, reversedQualifierParts.size) - 1
for (index in 0..classifierChainLastIndex) {
val qualifierPart = reversedQualifierParts[index]
val currentArguments = qualifierPart.typeArguments?.arguments.orEmpty()
val declaredTypeParameters = classifierDescriptorChain[index].declaredTypeParameters
val currentParameters = if (wasStatic) emptyList() else declaredTypeParameters
if (wasStatic && currentArguments.isNotEmpty() && declaredTypeParameters.isNotEmpty()) {
c.trace.report(TYPE_ARGUMENTS_FOR_OUTER_CLASS_WHEN_NESTED_REFERENCED.on(qualifierPart.typeArguments!!))
return null
}
if (currentArguments.size != currentParameters.size) {
c.trace.report(
WRONG_NUMBER_OF_TYPE_ARGUMENTS.on(
qualifierPart.typeArguments ?: qualifierPart.expression,
currentParameters.size, classifierDescriptorChain[index]
)
)
return null
}
result.addAll(currentArguments)
wasStatic = wasStatic || !classifierDescriptorChain[index].isInner
}
val nonClassQualifierParts =
reversedQualifierParts.subList(
min(classifierChainLastIndex + 1, reversedQualifierParts.size),
reversedQualifierParts.size
)
for ((_, _, typeArguments) in nonClassQualifierParts) {
if (typeArguments != null) {
c.trace.report(TYPE_ARGUMENTS_NOT_ALLOWED.on(typeArguments, "here"))
return null
}
}
val parameters = classifierDescriptor.typeConstructor.parameters
if (result.size < parameters.size) {
val nextParameterOwner =
parameters[result.size].original.containingDeclaration as? ClassDescriptor
// If next parameter is captured from the enclosing function, default arguments must be used
// (see appendDefaultArgumentsForLocalClassifier)
?: return Pair(result, null)
val restArguments = c.scope.findImplicitOuterClassArguments(nextParameterOwner)
val restParameters = parameters.subList(result.size, parameters.size)
val typeArgumentsCanBeSpecifiedCount =
classifierDescriptor.classifierDescriptorsFromInnerToOuter().sumBy { it.declaredTypeParameters.size }
if (restArguments == null && typeArgumentsCanBeSpecifiedCount > result.size) {
c.trace.report(
OUTER_CLASS_ARGUMENTS_REQUIRED.on(qualifierParts.first().expression, nextParameterOwner)
)
return null
} else if (restArguments == null) {
assert(typeArgumentsCanBeSpecifiedCount == result.size) {
"Number of type arguments that can be specified ($typeArgumentsCanBeSpecifiedCount) " +
"should be equal to actual arguments number ${result.size}, (classifier: $classifierDescriptor)"
}
return Pair(result, null)
} else {
assert(restParameters.size == restArguments.size) {
"Number of type of restParameters should be equal to ${restParameters.size}, " +
"but ${restArguments.size} were found for $classifierDescriptor/$nextParameterOwner"
}
return Pair(result, restArguments)
}
}
return Pair(result, null)
}
private fun ClassifierDescriptor?.classifierDescriptorsFromInnerToOuter(): List<ClassifierDescriptorWithTypeParameters> =
generateSequence(
{ this as? ClassifierDescriptorWithTypeParameters },
{ it.containingDeclaration as? ClassifierDescriptorWithTypeParameters }
).toList()
private fun resolveTypeProjectionsWithErrorConstructor(
c: TypeResolutionContext,
argumentElements: List<KtTypeProjection>,
message: String = "Error type for resolving type projections"
) = resolveTypeProjections(c, ErrorUtils.createErrorTypeConstructor(message), argumentElements)
/**
* For cases like:
* fun <E> foo() {
* class Local<F>
* val x: Local<Int> <-- resolve this type
* }
*
* type constructor for `Local` captures type parameter E from containing outer function
*/
private fun appendDefaultArgumentsForLocalClassifier(
fromIndex: Int,
constructorParameters: List<TypeParameterDescriptor>
) = constructorParameters.subList(fromIndex, constructorParameters.size).map {
TypeProjectionImpl(it.original.defaultType)
}
fun resolveTypeProjections(
c: TypeResolutionContext,
constructor: TypeConstructor,
argumentElements: List<KtTypeProjection>
): List<TypeProjection> {
return argumentElements.mapIndexed { i, argumentElement ->
val projectionKind = argumentElement.projectionKind
ModifierCheckerCore.check(argumentElement, c.trace, null, languageVersionSettings)
if (projectionKind == KtProjectionKind.STAR) {
val parameters = constructor.parameters
if (parameters.size > i) {
val parameterDescriptor = parameters[i]
TypeUtils.makeStarProjection(parameterDescriptor)
} else {
TypeProjectionImpl(OUT_VARIANCE, ErrorUtils.createErrorType("*"))
}
} else {
val type = resolveType(c.noBareTypes(), argumentElement.typeReference!!)
val kind = resolveProjectionKind(projectionKind)
if (constructor.parameters.size > i) {
val parameterDescriptor = constructor.parameters[i]
if (kind != INVARIANT && parameterDescriptor.variance != INVARIANT) {
if (kind == parameterDescriptor.variance) {
c.trace.report(REDUNDANT_PROJECTION.on(argumentElement, constructor.declarationDescriptor!!))
} else {
c.trace.report(CONFLICTING_PROJECTION.on(argumentElement, constructor.declarationDescriptor!!))
}
}
}
TypeProjectionImpl(kind, type)
}
}
}
private fun LexicalScope.findImplicitOuterClassArguments(
outerClass: ClassDescriptor
): List<TypeProjection>? {
val enclosingClass = findFirstFromMeAndParent { scope ->
if (scope is LexicalScope && scope.kind == LexicalScopeKind.CLASS_MEMBER_SCOPE)
scope.ownerDescriptor as ClassDescriptor
else
null
} ?: return null
return findImplicitOuterClassArguments(enclosingClass, outerClass)
}
fun resolveClass(
scope: LexicalScope, userType: KtUserType, trace: BindingTrace, isDebuggerContext: Boolean
): ClassifierDescriptor? = resolveDescriptorForType(scope, userType, trace, isDebuggerContext).classifierDescriptor
fun resolveDescriptorForType(
scope: LexicalScope, userType: KtUserType, trace: BindingTrace, isDebuggerContext: Boolean
): QualifiedExpressionResolver.TypeQualifierResolutionResult {
if (userType.qualifier != null) { // we must resolve all type references in arguments of qualifier type
for (typeArgument in userType.qualifier!!.typeArguments) {
typeArgument.typeReference?.let {
// in qualified expression, type argument can have bounds only in incorrect code
forceResolveTypeContents(resolveType(scope, it, trace, false))
}
}
}
return qualifiedExpressionResolver.resolveDescriptorForType(userType, scope, trace, isDebuggerContext).apply {
if (classifierDescriptor != null) {
PlatformClassesMappedToKotlinChecker.reportPlatformClassMappedToKotlin(
platformToKotlinClassMap, trace, userType, classifierDescriptor
)
}
}
}
companion object {
@JvmStatic
fun resolveProjectionKind(projectionKind: KtProjectionKind): Variance {
return when (projectionKind) {
KtProjectionKind.IN -> IN_VARIANCE
KtProjectionKind.OUT -> OUT_VARIANCE
KtProjectionKind.NONE -> INVARIANT
else -> // NOTE: Star projections must be handled before this method is called
throw IllegalStateException("Illegal projection kind:" + projectionKind)
}
}
}
}