-
Notifications
You must be signed in to change notification settings - Fork 292
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
Aliased import ignored for nullable variants of that type #2068
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems fine? What did it do before, insert the regular import or create another automatic alias?
Yes, It creates another automatic alias and becomes 2 member type imports (one for Object and another one for nullable Object) |
@JakeWharton Can you help me to merge the PR, please? |
I'd like to take a look as well and will merge today/tomorrow if no feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The proposed fix does not work that well if I change ClassName(packageName, "PersonId")
in the test to be a nested class (ClassName(packageName, "Person", "Id")
):
expected:
package org.example
import org.example.Person.Id as PID
public fun pid(): PID? = PID()
but was:
package org.example
import org.example.Person as ExamplePID
import org.example.Person.Id as PID
public fun pid(): ExamplePID.Id? = ExamplePID.Id()
Produced code is correct, but it's super awkward and includes and doesn't actually use the import alias we explicitly passed in.
I think a better fix would be to ensure the type we're adding to importableTypes
is always non-nullable. Interestingly, className.topLevelClassName()
always returns a non-nullable ClassName
(probably a separate bug), but for className
we need to remove nullability manually:
if (simpleName !in importableMembers) {
// Maintain the inner class name if the alias exists.
val newImportTypes = if (alias == null) {
topLevelClassName
} else {
- className
+ className.copy(nullable = false) as ClassName
}
importableTypes[simpleName] = importableTypes.getValue(simpleName) + newImportTypes
}
I'll add it as a suggestion along with the tweaked test.
kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt
Outdated
Show resolved
Hide resolved
Co-authored-by: Jake Wharton <[email protected]> Co-authored-by: Egor Andreevich <[email protected]>
@Egorand: Thank you very much for the good suggestion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks for addressing the feedback!
@RaoPrashanth please run |
docs/changelog.md
has been updated if applicable.be added to the changelog.
Closes #2020. Closes #2021.