Skip to content

Commit

Permalink
Fix crash when type param has an annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
nreid260 committed Feb 11, 2022
1 parent 3b53c80 commit ed686ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1939,21 +1939,7 @@ class KotlinInputAstVisitor(

override fun visitTypeParameter(parameter: KtTypeParameter) {
builder.sync(parameter)
if (parameter.hasModifier(KtTokens.REIFIED_KEYWORD)) {
builder.token("reified")
builder.space()
}
when (parameter.variance) {
Variance.INVARIANT -> {}
Variance.IN_VARIANCE -> {
builder.token("in")
builder.space()
}
Variance.OUT_VARIANCE -> {
builder.token("out")
builder.space()
}
}
visit(parameter.modifierList)
builder.token(parameter.nameIdentifier?.text ?: "")
val extendsBound = parameter.extendsBound
if (extendsBound != null) {
Expand All @@ -1975,6 +1961,7 @@ class KotlinInputAstVisitor(
/** Example `T : Foo` */
override fun visitTypeConstraint(constraint: KtTypeConstraint) {
builder.sync(constraint)
// TODO(nreid260): What about annotations on the type reference? `where @A T : Int`
visit(constraint.subjectTypeParameterName)
builder.space()
builder.token(":")
Expand Down
29 changes: 27 additions & 2 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2501,10 +2501,35 @@ class FormatterTest {
deduceMaxWidth = true)

@Test
fun `annotations on functions types parameters`() =
fun `annotations in literal function types`() =
assertFormatted(
"""
|val callback: (List<@JvmSuppressWildcards String>) -> Unit = foo
|val callback: (@Anno List<@JvmSuppressWildcards String>) -> Unit = foo
|""".trimMargin())

@Test
fun `annotations on type parameters`() =
assertFormatted(
"""
|class Foo<@Anno out @Anno T, @Anno in @Anno U> {
| inline fun <@Anno reified @Anno X, @Anno reified @Anno Y> bar() {}
|}
|""".trimMargin())

@Test
fun `annotations on type constraints`() =
assertFormatted(
"""
|class Foo<T : @Anno Kip, U> where U : @Anno Kip, U : @Anno Qux {
| fun <T : @Anno Kip, U> bar() where U : @Anno Kip, U : @Anno Qux {}
|}
|""".trimMargin())

@Test
fun `annotations on type arguments`() =
assertFormatted(
"""
|fun foo(x: Foo<in @Anno Int>) {}
|""".trimMargin())

@Test
Expand Down

0 comments on commit ed686ce

Please sign in to comment.