Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly disambiguate conflicting type names between the module itself and external modules. #111

Merged
merged 1 commit into from
Jan 14, 2024

Conversation

dnkoutso
Copy link
Collaborator

@dnkoutso dnkoutso commented Jan 9, 2024

If a type exists within a module that is being used by it and the same type exists in another module we do not properly disambiguate between them.

That is because we only keep track of referenceTypeNames that include a moduleName. This change collects the types used by the same module and properly disambiguates.

@dnkoutso dnkoutso requested a review from kdubb January 9, 2024 11:28
return importableTypes to referencedTypes.values.map { it.moduleName }.toSet()
}

companion object {
Copy link
Collaborator Author

@dnkoutso dnkoutso Jan 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kdubb I moved away from this style as it is a bit hard to carry additional information we need between the first pass and the second pass.

It was not being super helpful anyway and I did it initially to mirror KotlinPoet, but we are not KotlinPoet. I also thought by doing this we were avoiding writing the file twice but that is not true either because we use NullAppendable.


struct SomeType {

var yet_another_module_order: [yet_another_module.SortOrder]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails without my change. It outputs var yet_another_module_order: [SortOrder] which conflicts with the same module SortOrder that is added here.

@dnkoutso dnkoutso force-pushed the no_module_referenced_types branch 4 times, most recently from cab8f0e to 59ada1f Compare January 9, 2024 11:58
@dnkoutso dnkoutso changed the title Correclty disambiguate conflicting type names between the module itself and external modules. Correctly disambiguate conflicting type names between the module itself and external modules. Jan 9, 2024
return if (importedTypes[typeName.topLevelTypeName().simpleName]?.count() == 1 && importedTypes.values.flatMap { it }.any { it == topLevelTypeName }) {
return if (topLevelTypeName.moduleName.isNotEmpty() && sameModuleReferencedTypes.contains(topLevelTypeName.simpleName)) {
typeName.canonicalName
} else if (importedTypes[topLevelTypeName.simpleName]?.count() == 1 && importedTypes.values.flatMap { it }.any { it == topLevelTypeName }) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed from [typeName.topLevelTypeName().simpleName] to [topLevelTypeName.simpleName] did not need to calculate it again since its already stored above.

@@ -29,7 +29,8 @@ private val NO_MODULE = String()
internal class CodeWriter(
out: Appendable,
private val indent: String = DEFAULT_INDENT,
internal val importedTypes: Map<String, List<DeclaredTypeName>> = emptyMap(),
private val importedTypes: Map<String, List<DeclaredTypeName>> = emptyMap(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to be internal

@dnkoutso dnkoutso force-pushed the no_module_referenced_types branch 3 times, most recently from 23ecaa7 to afd998f Compare January 9, 2024 12:51
@@ -376,7 +379,9 @@ internal class CodeWriter(
*/
private fun resolveImport(typeName: DeclaredTypeName): String {
val topLevelTypeName = typeName.topLevelTypeName()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a val topLevelSimpleName = topLevelTypeName.simpleName since you need it a few times?

sameModuleReferencedTypes = sameModuleReferencedTypes
)
codeWriter.use {
emit(codeWriter, referencedModules = referencedModules)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is now wrapped in a .use so close gets called automatically.

@dnkoutso dnkoutso merged commit c965f3b into main Jan 14, 2024
1 check passed
@dnkoutso
Copy link
Collaborator Author

Tested Wire with this change and our internal monorepo and its ✅ ✅ ✅

@oldergod oldergod deleted the no_module_referenced_types branch January 15, 2024 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants