@@ -45,9 +45,10 @@ import org.jetbrains.kotlin.fir.symbols.SymbolInternals
4545import org.jetbrains.kotlin.fir.symbols.impl.*
4646import org.jetbrains.kotlin.fir.types.*
4747import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef
48- import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
48+ import org.jetbrains.kotlin.fir.types.builder.buildTypeProjectionWithVariance
4949import org.jetbrains.kotlin.name.*
5050import org.jetbrains.kotlin.types.ConstantValueKind
51+ import org.jetbrains.kotlin.types.Variance
5152import org.jetbrains.kotlin.utils.keysToMap
5253import java.util.concurrent.ConcurrentHashMap
5354
@@ -202,11 +203,11 @@ class SuspendTransformFirTransformer(
202203 ): FirReceiverParameter ? {
203204 return typeRef.coneTypeOrNull
204205 ?.copyWithTypeParameters(originalTypeParameterCache)
205- ?.let { foundCopied ->
206+ ?.let { copied ->
206207 buildReceiverParameterCopy(this ) {
207208 symbol = FirReceiverParameterSymbol ()
208209 containingDeclarationSymbol = newContainingDeclarationSymbol
209- typeRef = typeRef.withReplacedConeType(foundCopied )
210+ typeRef = typeRef.withReplacedConeType(copied )
210211 }
211212 }
212213 }
@@ -245,6 +246,10 @@ class SuspendTransformFirTransformer(
245246 this .receiverParameter = it
246247 }
247248
249+ copyReturnType(originalTypeParameterCache)
250+ }
251+
252+ private fun FirSimpleFunctionBuilder.copyReturnType (originalTypeParameterCache : MutableList <CopiedTypeParameterPair >) {
248253 val coneTypeOrNull = returnTypeRef.coneTypeOrNull
249254 if (coneTypeOrNull != null ) {
250255 returnTypeRef = returnTypeRef
@@ -311,32 +316,30 @@ class SuspendTransformFirTransformer(
311316 thisContextParameters : List <FirValueParameter >,
312317 thisReceiverParameter : FirReceiverParameter ? ,
313318 thisValueParameters : List <FirValueParameter >,
319+ thisTypeParameters : List <FirTypeParameter >,
314320 bridgeFunSymbol : FirNamedFunctionSymbol ,
315321 newFunTarget : FirFunctionTarget ,
316322 originalTypeParameterCache : MutableList <CopiedTypeParameterPair >,
317323 functionReturnTypeRef : FirTypeRef ,
318324 ): FirBlock = buildBlock {
319325 this .source = originFunc.body?.source
320326
321- val originalFunReturnType = originFunSymbol.resolvedReturnTypeRef.coneType.copyConeTypeOrSelf(originalTypeParameterCache)
327+ val originalFunReturnType =
328+ originFunSymbol.resolvedReturnTypeRef.coneType.copyConeTypeOrSelf(originalTypeParameterCache)
322329
323330 // lambda: suspend () -> T
324331 val lambdaTarget = FirFunctionTarget (null , isLambda = true )
325332 val lambda = buildAnonymousFunction {
326333 this .resolvePhase = FirResolvePhase .BODY_RESOLVE
327- // this.resolvePhase = FirResolvePhase.RAW_FIR
328334 this .isLambda = true
329335 this .moduleData = originFunSymbol.moduleData
330336 // this.origin = FirDeclarationOrigin.Source
331337 // this.origin = FirDeclarationOrigin.Synthetic.FakeFunction
332338 this .origin = SuspendTransformK2V3Key .origin
333339 // lambda 的返回值类型,() -> T 的 T, 应当是原始函数真正的返回值
334- this .returnTypeRef = buildResolvedTypeRef {
335- coneType = originalFunReturnType
336- }
340+ this .returnTypeRef = originalFunReturnType.toResolvedTypeRef()
337341
338342 this .hasExplicitParameterList = false
339- // this.status = FirResolvedDeclarationStatusImpl.DEFAULT_STATUS_FOR_SUSPEND_FUNCTION_EXPRESSION
340343 this .status = this .status.copy(isSuspend = true )
341344 this .symbol = FirAnonymousFunctionSymbol ()
342345 this .body = buildSingleExpressionBlock(
@@ -347,6 +350,14 @@ class SuspendTransformFirTransformer(
347350 // this.coneTypeOrNull = originFunSymbol.resolvedReturnTypeRef.coneType
348351 this .coneTypeOrNull = originalFunReturnType
349352 this .source = originFunSymbol.source
353+ for (tp in thisTypeParameters) {
354+ val ta = buildTypeProjectionWithVariance {
355+ typeRef = tp.toConeType().toResolvedTypeRef()
356+ this .variance = Variance .INVARIANT
357+ }
358+ this .typeArguments.add(ta)
359+ }
360+
350361 this .calleeReference = buildResolvedNamedReference {
351362 this .source = originFunSymbol.source
352363 this .name = originFunSymbol.name
@@ -402,11 +413,10 @@ class SuspendTransformFirTransformer(
402413 }
403414 )
404415
405- this .typeRef = buildResolvedTypeRef {
406- this .coneType = StandardClassIds .SuspendFunctionN (0 )
407- // .createConeType(session, arrayOf(originFunSymbol.resolvedReturnType))
408- .createConeType(session, arrayOf(originalFunReturnType))
409- }
416+ this .typeRef = StandardClassIds .SuspendFunctionN (0 )
417+ .createConeType(session, arrayOf(originalFunReturnType))
418+ .toResolvedTypeRef()
419+
410420 }
411421 lambdaTarget.bind(lambda)
412422
@@ -584,6 +594,16 @@ class SuspendTransformFirTransformer(
584594 ),
585595 )
586596
597+ val returnTypeProjection = funData.markAnnotationTypeArgument
598+ if (returnTypeProjection != null ) {
599+ val returnTypeProjectionType = returnTypeProjection.toConeTypeProjection().type
600+ returnTypeRef = if (returnTypeProjectionType != null ) {
601+ returnTypeRef.withReplacedConeType(returnTypeProjectionType)
602+ } else {
603+ session.builtinTypes.nullableAnyType
604+ }
605+ }
606+
587607 // Copy the typeParameters.
588608 // Otherwise, in functions like the following, an error will occur
589609 // suspend fun <A> data(value: A): T = ...
@@ -604,17 +624,20 @@ class SuspendTransformFirTransformer(
604624 val thisReceiverParameter = this .receiverParameter
605625 val thisContextParameters = this .contextParameters
606626 val thisValueParameters = this .valueParameters
627+ val thisTypeParameters = this .typeParameters
607628
608629 annotations.clear()
609630 annotations.addAll(functionAnnotations)
610631
632+
611633 body = generateSyntheticFunctionBody(
612634 originFunc,
613635 originFunSymbol,
614636 owner,
615637 thisContextParameters,
616638 thisReceiverParameter,
617639 thisValueParameters,
640+ thisTypeParameters,
618641 realBridgeFunSymbol,
619642 newFunTarget,
620643 originalTypeParameterCache,
@@ -662,34 +685,33 @@ class SuspendTransformFirTransformer(
662685
663686 val pSymbol = FirPropertySymbol (callableId)
664687
665- // val pKey = SuspendTransformPluginKey(
666- // data = SuspendTransformUserDataFir(
667- // markerId = uniqueFunHash,
668- // originSymbol = original.symbol.asOriginSymbol(
669- // targetMarkerAnnotation,
670- // typeParameters = original.typeParameters,
671- // valueParameters = original.valueParameters,
672- // original.returnTypeRef.coneTypeOrNull?.classId,
673- // session
674- // ),
675- // asProperty = true,
676- // transformer = funData.transformer
677- // )
678- // )
679688 val pKey = SuspendTransformK2V3Key
680689
681- val originalReturnType = original.returnTypeRef
690+ val returnTypeProjection = funData.markAnnotationTypeArgument
682691
683- val copiedReturnType = originalReturnType.withReplacedConeType(
684- originalReturnType.coneTypeOrNull?.copyConeTypeOrSelf(originalTypeParameterCache)
685- )
692+ val originalReturnType: FirTypeRef = original.returnTypeRef
693+
694+ val copiedReturnType: FirResolvedTypeRef = if (returnTypeProjection != null ) {
695+ returnTypeProjection.toConeTypeProjection().type
696+ ?.copyConeTypeOrSelf(originalTypeParameterCache)
697+ ?.let (originalReturnType::withReplacedConeType)
698+ ? : session.builtinTypes.nullableAnyType
699+ } else {
700+ originalReturnType.withReplacedConeType(
701+ originalReturnType.coneTypeOrNull?.copyConeTypeOrSelf(originalTypeParameterCache)
702+ )
703+ }
704+
705+ // val copiedReturnType = originalReturnType.withReplacedConeType(
706+ // originalReturnType.coneTypeOrNull?.copyConeTypeOrSelf(originalTypeParameterCache)
707+ // )
686708
687709 // copy完了再resolve,这样里面包的type parameter就不会有问题了(如果有type parameter的话)
688710 val resolvedReturnType = resolveReturnType(
689711 funData,
690712 copiedReturnType,
691713 originalTypeParameterCache,
692- funData.markAnnotationTypeArgument
714+ returnTypeProjection
693715 )
694716
695717 val newFunTarget = FirFunctionTarget (null , isLambda = false )
@@ -768,6 +790,7 @@ class SuspendTransformFirTransformer(
768790 emptyList(),
769791 null ,
770792 thisValueParameters,
793+ emptyList(),
771794 funData.transformerFunctionSymbol(
772795 transformerFunctionSymbolMap,
773796 ::findTransformerFunctionSymbol
@@ -987,24 +1010,29 @@ class SuspendTransformFirTransformer(
9871010 ): FirTypeRef {
9881011 val transformer = funData.transformer
9891012
990- val returnTypeConeType: ConeKotlinType = returnTypeFromProjection?.toConeTypeProjection()?.let {
991- // TODO if is star projection, use Any or Nothing? and the nullable?
992- it.type
993- ?.copyConeTypeOrSelf(originalTypeParameterCache)
994- ? : session.builtinTypes.nullableAnyType.coneType
995- } ? : returnTypeRef.coneType
1013+ // val returnTypeConeType: ConeKotlinType = if (returnTypeFromProjection != null) {
1014+ // returnTypeFromProjection.toConeTypeProjection().type
1015+ // ?: session.builtinTypes.nullableAnyType.coneType
1016+ // } else {
1017+ // returnTypeRef.coneType
1018+ // }
1019+
1020+ // val returnTypeConeType: ConeKotlinType = returnTypeFromProjection?.toConeTypeProjection()?.let {
1021+ // // TODO if is star projection, use Any or Nothing? and the nullable?
1022+ // it.type
1023+ // ?.copyConeTypeOrSelf(originalTypeParameterCache)
1024+ // ?: session.builtinTypes.nullableAnyType.coneType
1025+ // } ?: returnTypeRef.coneType
9961026
997- val resultConeType: ConeKotlinType = resolveReturnConeType(transformer, returnTypeConeType )
1027+ val resultConeType: ConeKotlinType = resolveReturnConeType(transformer, returnTypeRef.coneType )
9981028
9991029 return if (resultConeType is ConeErrorType ) {
10001030 buildErrorTypeRef {
10011031 diagnostic = resultConeType.diagnostic
10021032 coneType = resultConeType
10031033 }
10041034 } else {
1005- buildResolvedTypeRef {
1006- coneType = resultConeType
1007- }
1035+ resultConeType.toResolvedTypeRef()
10081036 }
10091037 }
10101038
@@ -1073,9 +1101,7 @@ class SuspendTransformFirTransformer(
10731101 */
10741102 val copied: List <FirAnnotation > = notCompileAnnotationsCopied.map { a ->
10751103 buildAnnotation {
1076- annotationTypeRef = buildResolvedTypeRef {
1077- coneType = a.resolvedType
1078- }
1104+ annotationTypeRef = a.resolvedType.toResolvedTypeRef()
10791105
10801106 this .typeArguments.addAll(a.typeArguments)
10811107 this .argumentMapping = buildAnnotationArgumentMapping {
@@ -1099,9 +1125,7 @@ class SuspendTransformFirTransformer(
10991125
11001126 val includeAnnotation = buildAnnotation {
11011127 argumentMapping = buildAnnotationArgumentMapping()
1102- annotationTypeRef = buildResolvedTypeRef {
1103- coneType = classId.createConeType(session)
1104- }
1128+ annotationTypeRef = classId.createConeType(session).toResolvedTypeRef()
11051129 }
11061130 add(includeAnnotation)
11071131 }
@@ -1125,9 +1149,7 @@ class SuspendTransformFirTransformer(
11251149 mapping[Name .identifier(annotationMarkNamePropertyName)] = markNameArgument
11261150 }
11271151 val markNameAnnotationClassId = markNameProperty.annotation.toClassId()
1128- annotationTypeRef = buildResolvedTypeRef {
1129- coneType = markNameAnnotationClassId.createConeType(session)
1130- }
1152+ annotationTypeRef = markNameAnnotationClassId.createConeType(session).toResolvedTypeRef()
11311153 }
11321154
11331155 add(markNameAnnotation)
@@ -1152,9 +1174,7 @@ class SuspendTransformFirTransformer(
11521174 val classId = include.classId
11531175 val includeAnnotation = buildAnnotation {
11541176 argumentMapping = buildAnnotationArgumentMapping()
1155- annotationTypeRef = buildResolvedTypeRef {
1156- coneType = classId.createConeType(session)
1157- }
1177+ annotationTypeRef = classId.createConeType(session).toResolvedTypeRef()
11581178 }
11591179 add(includeAnnotation)
11601180 }
@@ -1175,9 +1195,7 @@ class SuspendTransformFirTransformer(
11751195
11761196 buildAnnotation {
11771197 argumentMapping = buildAnnotationArgumentMapping()
1178- annotationTypeRef = buildResolvedTypeRef {
1179- coneType = classId.createConeType(session)
1180- }
1198+ annotationTypeRef = classId.createConeType(session).toResolvedTypeRef()
11811199 }
11821200 }
11831201
0 commit comments