Skip to content

Commit

Permalink
Fix crash when type param has an annotation (#281)
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: #281

Reviewed By: strulovich

Differential Revision: D34167891

Pulled By: cgrushko

fbshipit-source-id: 16b32efd388f895fc69e445cc15f37287c42f905
  • Loading branch information
nreid260 authored and facebook-github-bot committed Feb 13, 2022
1 parent d4718f6 commit 059184b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ import org.jetbrains.kotlin.psi.KtWhileExpression
import org.jetbrains.kotlin.psi.psiUtil.children
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.psiUtil.startsWithComment
import org.jetbrains.kotlin.types.Variance

/** An AST visitor that builds a stream of {@link Op}s to format. */
class KotlinInputAstVisitor(
Expand Down Expand Up @@ -1936,21 +1935,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 @@ -1972,6 +1957,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
28 changes: 26 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 @@ -2518,10 +2518,34 @@ 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 059184b

Please sign in to comment.