Skip to content

Commit 9688126

Browse files
committed
pass correct name to the configuration
I've also changed resolver to pass this name to the type mapper. It is a bit hacky because kotlin passes down a special name which we need to revert back to the original
1 parent 4c84dff commit 9688126

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/processing/impl/ResolverImpl.kt

+25-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import org.jetbrains.kotlin.load.java.structure.impl.JavaTypeImpl
5757
import org.jetbrains.kotlin.load.java.structure.impl.JavaTypeParameterImpl
5858
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
5959
import org.jetbrains.kotlin.name.FqName
60+
import org.jetbrains.kotlin.name.Name
6061
import org.jetbrains.kotlin.psi.*
6162
import org.jetbrains.kotlin.resolve.*
6263
import org.jetbrains.kotlin.resolve.calls.inference.components.NewTypeSubstitutor
@@ -99,7 +100,7 @@ class ResolverImpl(
99100

100101
private val typeMapper = KotlinTypeMapper(
101102
BindingContext.EMPTY, ClassBuilderMode.LIGHT_CLASSES,
102-
JvmProtoBufUtil.DEFAULT_MODULE_NAME,
103+
module.name.getNonSpecialIdentifier(),
103104
KotlinTypeMapper.LANGUAGE_VERSION_SETTINGS_DEFAULT// TODO use proper LanguageVersionSettings
104105
)
105106

@@ -629,3 +630,26 @@ private fun TypeSubstitutor.toNewSubstitutor() = composeWith(
629630
private fun KotlinType.createTypeSubstitutor(): NewTypeSubstitutor {
630631
return SubstitutionUtils.buildDeepSubstitutor(this).toNewSubstitutor()
631632
}
633+
634+
/**
635+
* Extracts the identifier from a module Name.
636+
*
637+
* One caveat here is that kotlin passes a special name into the plugin which cannot be used as an identifier.
638+
* On the other hand, to construct the correct TypeMapper, we need a non-special name.
639+
* This function extracts the non-special name from a given name if it is special.
640+
*
641+
* @see: https://github.com/JetBrains/kotlin/blob/master/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/TopDownAnalyzerFacadeForJVM.kt#L305
642+
*/
643+
private fun Name.getNonSpecialIdentifier() :String {
644+
// the analyzer might pass down a special name which will break type mapper name computations.
645+
// If it is a special name, we turn it back to an id
646+
if (!isSpecial || asString().isBlank()) {
647+
return asString()
648+
}
649+
// special names starts with a `<` and usually end with `>`
650+
return if (asString().last() == '>') {
651+
asString().substring(1, asString().length - 1)
652+
} else {
653+
asString().substring(1)
654+
}
655+
}

compiler-plugin/src/test/kotlin/com/google/devtools/ksp/test/AbstractKotlinKSPTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ abstract class AbstractKotlinKSPTest : KotlinBaseTest<AbstractKotlinKSPTest.KspT
106106
listOf(module.javaSrcDir),
107107
emptyList()
108108
)
109+
configuration.put(CommonConfigurationKeys.MODULE_NAME, module.name)
109110

110111
val environment = KotlinCoreEnvironment.createForTests(
111112
testRootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES

0 commit comments

Comments
 (0)