is there a way in FIR to create a `ConeClassLikeTy...
# compiler
z
is there a way in FIR to create a
ConeClassLikeType
typed with a
FirFunctionTypeRef
? Essentially I'm trying to add a supertype like
Factory<() -> Unit>
in a supertype generation extension, but struggling to find an API for parameterizing an
FirClassLikeSymbol
with one since
FirFunctionTypeRef.coneType
doesn't work
basically trying to add an
is FirFunctionTypeRef
branch to this when statement and use it in the returned parameterized factory type at the end https://github.com/ZacSweers/metro/blob/main/compiler/src/main/kotlin/dev/zacsweers/metro/compiler/fir/generators/ProvidesFactoryFirGenerator.kt#L319
d
FirFunctionTypeRef
is an unresolved functional type, you should not rely on it. You need something like this:
Copy code
fun createFunctionNType(arguments: List<ConeTypeProjection>): ConeKotlinType {
    return FunctionTypeKind.Function
        .numberedClassId(arguments.size - 1)
        .toLookupTag()
        .constructClassType(
            arguments.toTypedArray(),
            isMarkedNullable = false
        )
}
There are also
KFunction
,
SuspendFunction
,
KSuspendFunction
in the
FunctionTypeKind
if you need
z
I see, where are the
arguments
coming from in this case?
d
From you
If you want just () -> Unit, then pass class like type with kotlin.Unit as lookup tag
j
I was playing around with trying to do something similar last week this is what I came up with:
Copy code
override fun generateTopLevelClassLikeDeclaration(classId: ClassId): FirClassLikeSymbol<*> {
        val functionSymbol: FirNamedFunctionSymbol = functionsToAlias().single {
            it.name == classId.shortClassName
        }

        return buildTypeAlias {
            moduleData = session.moduleData
            origin = Key.origin
            name = classId.shortClassName
            status = functionSymbol.resolvedStatus
            symbol = FirTypeAliasSymbol(classId)

            expandedTypeRef = functionSymbol.valueParameterSymbols.run {
                buildResolvedTypeRef {
                    coneType = ConeClassLikeTypeImpl(
                        lookupTag = StandardClassIds.FunctionN(size).toLookupTag(),
                        typeArguments = arrayOf(
                            *map { it.resolvedReturnType }.toTypedArray(),
                            functionSymbol.resolvedReturnType,
                        ),
                        isMarkedNullable = false,
                    )
                    annotations.addAll(functionSymbol.annotations)
                }
            }
        }.symbol
    }
z
Trying to figure out how to get those from the FirFunctionTypeRef I have at that point. Would I need to convert all the parameters + return type ref into ConeTypeProjections first and pass those on from there?
j
I got stuck at the point of trying to copy over the annotations from the source methods ie.
@Composable
to the generated typeliases
d
If you see the FirFunctionalTypeRef instead of FirResolvedTypeRef then you are on too early stage. There is a service for type resolution in supertype generation extension (which should be used carefully though), but there is nothing to do in class generation
z
hm, yes this is during supertype generation and I do have that resolver available. Would the types I pass into that be the parameter refs and return type ref of the functional type ref then?