Home > other >  <!WHATVER!> syntax in Kotlin? (Angle brackets wrapping exclamation points)
<!WHATVER!> syntax in Kotlin? (Angle brackets wrapping exclamation points)

Time:12-24

I saw this syntax I'm not familiar with in the Kotlin compiler test suite.

// !DIAGNOSTICS:  UNUSED_LAMBDA_EXPRESSION,  UNUSED_VARIABLE

fun unusedLiteral(){
    <!UNUSED_LAMBDA_EXPRESSION!>{ ->
        val <!UNUSED_VARIABLE!>i<!> = 1
    }<!>
}

What does <!UNUSED_LAMBDA_EXPRESSION!>...<!> mean?

Found in unusedLiteral.kt

The term UNUSED_LAMBDA_EXPRESSION is declared in Errors.kt to be:

DiagnosticFactory0<KtLambdaExpression> UNUSED_LAMBDA_EXPRESSION = DiagnosticFactory0.create(WARNING);

CodePudding user response:

This syntax is not valid Kotlin. It is only used in the test data files of Kotlin's test pipeline. That is, only the test runners recognises this syntax, not the Kotlin compiler. Specifically, the <!DIAGNOSTIC_NAME!>foo<!> syntax denotes a handler. Handlers do checks on things, or output information to a file. In this case, this syntax checks that there is indeed the specified diagnostic being emitted at that point in the file.

Also note that the // !DIAGNOSTICS comment at the top is not just a comment. It denotes a directive. Directives are like the options for running the test.

I highly recommend you read compiler/testData/diagnostics/ReadMe.md, which explains how diagnostic tests work specifically, and if you're really interested in this stuff, check out compiler/test-infrastructure/ReadMe.md too, which tells you all about how the whole test pipeline works in general.

  • Related