FIR: introduce delegate diagnostics

This commit adds diagnostics for the following

* DELEGATE_SPECIAL_FUNCTION_MISSING
* DELEGATE_SPECIAL_FUNCTION_AMBIGUITY
* DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE
This commit is contained in:
Tianyu Geng
2021-04-07 03:51:49 +03:00
committed by Mikhail Glukhikh
parent 454ae3b17a
commit b5caa658d6
56 changed files with 299 additions and 258 deletions
@@ -671,6 +671,19 @@ object DIAGNOSTICS_LIST : DiagnosticList() {
val NEXT_NONE_APPLICABLE by error<FirSourceElement, KtExpression> {
parameter<Collection<AbstractFirBasedSymbol<*>>>("candidates")
}
val DELEGATE_SPECIAL_FUNCTION_MISSING by error<FirSourceElement, KtExpression> {
parameter<String>("expectedFunctionSignature")
parameter<ConeKotlinType>("delegateType")
parameter<String>("description")
}
val DELEGATE_SPECIAL_FUNCTION_AMBIGUITY by error<FirSourceElement, KtExpression> {
parameter<String>("expectedFunctionSignature")
parameter<Collection<AbstractFirBasedSymbol<*>>>("candidates")
}
val DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE by error<FirSourceElement, KtExpression> {
parameter<String>("expectedFunctionSignature")
parameter<Collection<AbstractFirBasedSymbol<*>>>("candidates")
}
}
val TYPE_ALIAS by object : DiagnosticGroup("Type alias") {
@@ -393,6 +393,9 @@ object FirErrors {
val NEXT_MISSING by error0<FirSourceElement, KtExpression>()
val HAS_NEXT_FUNCTION_NONE_APPLICABLE by error1<FirSourceElement, KtExpression, Collection<AbstractFirBasedSymbol<*>>>()
val NEXT_NONE_APPLICABLE by error1<FirSourceElement, KtExpression, Collection<AbstractFirBasedSymbol<*>>>()
val DELEGATE_SPECIAL_FUNCTION_MISSING by error3<FirSourceElement, KtExpression, String, ConeKotlinType, String>()
val DELEGATE_SPECIAL_FUNCTION_AMBIGUITY by error2<FirSourceElement, KtExpression, String, Collection<AbstractFirBasedSymbol<*>>>()
val DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE by error2<FirSourceElement, KtExpression, String, Collection<AbstractFirBasedSymbol<*>>>()
// Type alias
val TOPLEVEL_TYPEALIASES_ONLY by error0<KtTypeAlias>()
@@ -0,0 +1,115 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.fir.analysis.checkers.declaration
import org.jetbrains.kotlin.fir.FirElement
import org.jetbrains.kotlin.fir.FirFakeSourceElementKind
import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.diagnostics.DiagnosticReporter
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors
import org.jetbrains.kotlin.fir.analysis.diagnostics.reportOn
import org.jetbrains.kotlin.fir.declarations.FirProperty
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.expressions.arguments
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeAmbiguityError
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeInapplicableCandidateError
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedNameError
import org.jetbrains.kotlin.fir.symbols.AbstractFirBasedSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinErrorType
import org.jetbrains.kotlin.fir.types.coneType
import org.jetbrains.kotlin.fir.types.render
import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid
import org.jetbrains.kotlin.resolve.calls.tower.CandidateApplicability
import org.jetbrains.kotlin.resolve.calls.tower.isSuccess
object FirDelegatedPropertyChecker : FirPropertyChecker() {
override fun check(declaration: FirProperty, context: CheckerContext, reporter: DiagnosticReporter) {
val delegate = declaration.delegate ?: return
val delegateType = delegate.typeRef.coneType
if (delegateType is ConeKotlinErrorType) {
val delegateSource = delegate.source
// Implicit recursion type is not reported since the type ref does not have a real source.
if (delegateSource != null && (delegateType.diagnostic as? ConeSimpleDiagnostic)?.kind == DiagnosticKind.RecursionInImplicitTypes) {
// skip reporting other issues in this case
reporter.reportOn(delegateSource, FirErrors.RECURSION_IN_IMPLICIT_TYPES, context)
}
return
}
class DelegatedPropertyAccessorVisitor(private val isGet: Boolean) : FirVisitorVoid() {
override fun visitElement(element: FirElement) = element.acceptChildren(this)
override fun visitFunctionCall(functionCall: FirFunctionCall) {
val errorNamedReference = functionCall.calleeReference as? FirErrorNamedReference ?: return
if (errorNamedReference.source?.kind != FirFakeSourceElementKind.DelegatedPropertyAccessor) return
val expectedFunctionSignature =
(if (isGet) "getValue" else "setValue") + "(${functionCall.arguments.joinToString(", ") { it.typeRef.coneType.render() }})"
val delegateDescription = if (isGet) "delegate" else "delegate for var (read-write property)"
fun reportInapplicableDiagnostics(
candidateApplicability: CandidateApplicability,
candidates: Collection<AbstractFirBasedSymbol<*>>
) {
if (candidateApplicability == CandidateApplicability.INAPPLICABLE_WRONG_RECEIVER) {
reporter.reportOn(
errorNamedReference.source,
FirErrors.DELEGATE_SPECIAL_FUNCTION_MISSING,
expectedFunctionSignature,
delegateType,
delegateDescription,
context
)
} else {
reporter.reportOn(
errorNamedReference.source,
FirErrors.DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE,
expectedFunctionSignature,
candidates,
context
)
}
}
when (val diagnostic = errorNamedReference.diagnostic) {
is ConeUnresolvedNameError -> {
reporter.reportOn(
errorNamedReference.source,
FirErrors.DELEGATE_SPECIAL_FUNCTION_MISSING,
expectedFunctionSignature,
delegateType,
delegateDescription,
context
)
}
is ConeAmbiguityError -> {
if (diagnostic.applicability.isSuccess) {
// Match is successful but there are too many matches! So we report DELEGATE_SPECIAL_FUNCTION_AMBIGUITY.
reporter.reportOn(
errorNamedReference.source,
FirErrors.DELEGATE_SPECIAL_FUNCTION_AMBIGUITY,
expectedFunctionSignature,
diagnostic.candidates.map { it.symbol },
context
)
} else {
reportInapplicableDiagnostics(diagnostic.applicability, diagnostic.candidates.map { it.symbol })
}
}
is ConeInapplicableCandidateError -> {
reportInapplicableDiagnostics(diagnostic.applicability, listOf(diagnostic.candidate.symbol))
}
}
}
}
declaration.getter?.body?.acceptChildren(DelegatedPropertyAccessorVisitor(true))
declaration.setter?.body?.acceptChildren(DelegatedPropertyAccessorVisitor(false))
}
}
@@ -16,6 +16,8 @@ import org.jetbrains.kotlin.fir.declarations.FirErrorFunction
import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic
import org.jetbrains.kotlin.fir.expressions.*
import org.jetbrains.kotlin.fir.references.FirErrorNamedReference
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeAmbiguityError
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeInapplicableCandidateError
import org.jetbrains.kotlin.fir.resolve.diagnostics.ConeUnresolvedNameError
import org.jetbrains.kotlin.fir.types.ConeClassErrorType
import org.jetbrains.kotlin.fir.types.FirErrorTypeRef
@@ -90,6 +92,14 @@ class ErrorNodeDiagnosticCollectorComponent(collector: AbstractDiagnosticCollect
if (source.elementType == KtNodeTypes.DESTRUCTURING_DECLARATION_ENTRY) {
return
}
// Will be handled by [FirDelegatedPropertyChecker]
if (source.kind == FirFakeSourceElementKind.DelegatedPropertyAccessor &&
(diagnostic is ConeUnresolvedNameError || diagnostic is ConeAmbiguityError || diagnostic is ConeInapplicableCandidateError)
) {
return
}
if (source.kind == FirFakeSourceElementKind.ImplicitConstructor || source.kind == FirFakeSourceElementKind.DesugaredForLoop) {
// See FirForLoopChecker
return
@@ -77,6 +77,9 @@ import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DATA_CLASS_VARARG
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DATA_CLASS_WITHOUT_PARAMETERS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DELEGATED_PROPERTY_INSIDE_INLINE_CLASS
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DELEGATED_PROPERTY_IN_INTERFACE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DELEGATE_SPECIAL_FUNCTION_AMBIGUITY
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DELEGATE_SPECIAL_FUNCTION_MISSING
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DELEGATION_IN_INTERFACE
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirErrors.DEPRECATED_MODIFIER_PAIR
@@ -846,13 +849,35 @@ class FirDefaultErrorMessages : DefaultErrorMessages.Extension {
// Conventions
map.put(NO_GET_METHOD, "No get method providing array access")
map.put(NO_SET_METHOD, "No set method providing array access")
map.put(
DELEGATE_SPECIAL_FUNCTION_MISSING,
"Type ''{1}'' has no method ''{0}'' and thus it cannot serve as a {2}",
TO_STRING,
RENDER_TYPE,
TO_STRING
)
map.put(
DELEGATE_SPECIAL_FUNCTION_AMBIGUITY,
"Overload resolution ambiguity on method ''{0}'': {1}",
TO_STRING,
SYMBOLS
)
map.put(
DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE,
"Property delegate must have a ''{0}'' method. None of the following functions is suitable: {1}",
TO_STRING,
SYMBOLS
)
// Type alias
map.put(TOPLEVEL_TYPEALIASES_ONLY, "Nested and local type aliases are not supported")
// Returns
map.put(RETURN_NOT_ALLOWED, "'return' is not allowed here")
map.put(RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY, "Returns are not allowed for functions with expression body. Use block body in '{...}'")
map.put(
RETURN_IN_FUNCTION_WITH_EXPRESSION_BODY,
"Returns are not allowed for functions with expression body. Use block body in '{...}'"
)
// Extended checkers group
map.put(REDUNDANT_VISIBILITY_MODIFIER, "Redundant visibility modifier")
@@ -50,7 +50,9 @@ object CommonDeclarationCheckers : DeclarationCheckers() {
FirConstPropertyChecker,
FirPropertyAccessorChecker,
FirPropertyTypeParametersChecker,
FirPropertyAccessorChecker,FirInitializerTypeMismatchChecker
FirPropertyAccessorChecker,
FirInitializerTypeMismatchChecker,
FirDelegatedPropertyChecker,
)
override val classCheckers: Set<FirClassChecker>
@@ -68,7 +68,7 @@ class CallAndReferenceGenerator(
// val `x$delegate` = y
// val x get() = `x$delegate`.getValue(this, ::x)
// The reference here (like the rest of the accessor) has DefaultAccessor source kind.
val isForDelegate = callableReferenceAccess.source?.kind == FirFakeSourceElementKind.DefaultAccessor
val isForDelegate = callableReferenceAccess.source?.kind == FirFakeSourceElementKind.DelegatedPropertyAccessor
val origin = if (isForDelegate) IrStatementOrigin.PROPERTY_REFERENCE_FOR_DELEGATE else null
return callableReferenceAccess.convertWithOffsets { startOffset, endOffset ->
when (symbol) {
@@ -671,7 +671,7 @@ class CallAndReferenceGenerator(
val typeParameters = fir.typeParameters.map { it.symbol.fir }
if (typeParameters.size != type.typeArguments.size) return null
val newTypeArguments = typeParameters.zip(type.typeArguments).map { (parameter, argument) ->
if (argument == ConeStarProjection){
if (argument == ConeStarProjection) {
parameter.bounds.first().coneType
} else {
argument
@@ -685,7 +685,9 @@ class CallAndReferenceGenerator(
// If the type of the argument is already an explicitly subtype of the type of the parameter, we don't need SAM conversion.
if (argument.typeRef !is FirResolvedTypeRef ||
AbstractTypeChecker.isSubtypeOf(
session.inferenceComponents.ctx.newBaseTypeCheckerContext(errorTypesEqualToAnything = false, stubTypesEqualToAnything = true),
session.inferenceComponents.ctx.newBaseTypeCheckerContext(
errorTypesEqualToAnything = false, stubTypesEqualToAnything = true
),
argument.typeRef.coneType,
parameter.returnTypeRef.coneType,
isFromNullabilityConstraint = true
@@ -37,7 +37,6 @@ import org.jetbrains.kotlin.fir.types.builder.*
import org.jetbrains.kotlin.fir.types.impl.*
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.types.ConstantValueKind
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.util.OperatorNameConventions
@@ -314,7 +313,7 @@ fun FirPropertyBuilder.generateAccessorsByDelegate(
else -> null
}
val isMember = ownerSymbol != null
val fakeSource = delegateBuilder.source?.fakeElement(FirFakeSourceElementKind.DefaultAccessor)
val fakeSource = delegateBuilder.source?.fakeElement(FirFakeSourceElementKind.DelegatedPropertyAccessor)
/*
* If we have delegation with provide delegate then we generate call like
@@ -458,6 +457,7 @@ fun FirPropertyBuilder.generateAccessorsByDelegate(
source = fakeSource
explicitReceiver = delegateAccess()
calleeReference = buildSimpleNamedReference {
source = fakeSource
name = SET_VALUE
}
argumentList = buildArgumentList {
@@ -37,6 +37,10 @@ sealed class FirFakeSourceElementKind : FirSourceElementKind() {
// they have a fake source which refers to property
object DefaultAccessor : FirFakeSourceElementKind()
// for delegated properties, getter & setter calls to the delegate
// they have a fake source which refers to the call that creates the delegate
object DelegatedPropertyAccessor : FirFakeSourceElementKind()
// for kt classes without implicit primary constructor one is generated
// with a fake source which refers to containing class
object ImplicitConstructor : FirFakeSourceElementKind()
@@ -33,8 +33,8 @@ operator fun CustomDelegate3.setValue(thisRef: Any?, prop: KProperty<*>, value:
class Example {
var a by <!INAPPLICABLE_CANDIDATE!>CustomDelegate<!>()
val aval by <!INAPPLICABLE_CANDIDATE!>CustomDelegate<!>()
var a by <!DELEGATE_SPECIAL_FUNCTION_MISSING, DELEGATE_SPECIAL_FUNCTION_MISSING!>CustomDelegate()<!>
val aval by <!DELEGATE_SPECIAL_FUNCTION_MISSING!>CustomDelegate()<!>
var b by OkDelegate()
var c by CustomDelegate2()
var d by CustomDelegate3()
@@ -29,9 +29,9 @@ private fun <T> lazy(init: () -> T): <!UNRESOLVED_REFERENCE!>kotlin.Lazy<T><!> {
}
object DefaultHttpClientWithBy : HttpClient by client {
val client by <!UNRESOLVED_REFERENCE!>lazy<!> { HttpClientImpl() }
val client by lazy { HttpClientImpl() }
}
object DefaultFqHttpClient : HttpClient by DefaultFqHttpClient.client {
val client = HttpClientImpl()
}
}
@@ -11,7 +11,7 @@ fun test() {
const val <T> a3 = 0
<!INAPPLICABLE_LATEINIT_MODIFIER!>lateinit<!> val <T> a4 = 0
val <T> a5 by Delegate<Int>()
val <T> a6 by <!ARGUMENT_TYPE_MISMATCH, ARGUMENT_TYPE_MISMATCH!>Delegate<<!UNRESOLVED_REFERENCE!>T<!>>()<!>
val <T> a6 by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>Delegate<<!UNRESOLVED_REFERENCE!>T<!>>()<!>
}
class Delegate<F> {
@@ -11,7 +11,7 @@ fun test() {
const val <T> a3 = 0
<!INAPPLICABLE_LATEINIT_MODIFIER!>lateinit<!> val <T> a4 = 0
val <T> a5 by Delegate<Int>()
val <T> a6 by <!ARGUMENT_TYPE_MISMATCH, ARGUMENT_TYPE_MISMATCH!>Delegate<<!UNRESOLVED_REFERENCE!>T<!>>()<!>
val <T> a6 by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>Delegate<<!UNRESOLVED_REFERENCE!>T<!>>()<!>
}
class Delegate<F> {
@@ -1,17 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
class A
class D {
val c: Int by <!UNRESOLVED_REFERENCE!>IncorrectThis<!><A>()
}
val cTopLevel: Int by <!UNRESOLVED_REFERENCE!>IncorrectThis<!><A>()
class IncorrectThis<T> {
fun <R> get(t: Any?, p: KProperty<*>): Int {
return 1
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
@@ -12,8 +12,8 @@ class A(outer: Outer) {
var b: String by foo(getMyProperty())
var r: String by foo(outer.getContainer().getMyProperty())
var e: String by + foo(getMyProperty())
var f: String by foo(getMyProperty()) - 1
var e: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>+ foo(getMyProperty())<!>
var f: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>foo(getMyProperty()) - 1<!>
}
fun <A, B> foo(a: Any?) = MyProperty<A, B>()
@@ -4,10 +4,10 @@ package foo
import kotlin.reflect.KProperty
open class A {
val B.w: Int by <!ARGUMENT_TYPE_MISMATCH!>MyProperty()<!>
val B.w: Int by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>MyProperty()<!>
}
val B.r: Int by <!ARGUMENT_TYPE_MISMATCH!>MyProperty()<!>
val B.r: Int by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>MyProperty()<!>
val A.e: Int by MyProperty()
@@ -25,4 +25,4 @@ class C<R>() {
}
var c1: Int by C()
var c2: Int by C<Number>()
var c2: Int by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>C<Number>()<!>
@@ -5,7 +5,7 @@ package test
import first.*
import second.*
val a12 by <!OVERLOAD_RESOLUTION_AMBIGUITY!>A<!>()
val a12 by <!DELEGATE_SPECIAL_FUNCTION_AMBIGUITY!>A()<!>
// FILE: first.kt
package first
@@ -4,8 +4,8 @@ package foo
import kotlin.reflect.KProperty
class A {
var a5: String by MyProperty1()
var b5: String by getMyProperty1()
var a5: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>MyProperty1()<!>
var b5: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>getMyProperty1()<!>
}
fun <A, B> getMyProperty1() = MyProperty1<A, B>()
@@ -24,8 +24,8 @@ class MyProperty1<T, R> {
// -----------------
class B {
var a5: String by MyProperty2()
var b5: String by getMyProperty2()
var a5: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>MyProperty2()<!>
var b5: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>getMyProperty2()<!>
}
fun <A, B> getMyProperty2() = MyProperty2<A, B>()
@@ -1,11 +0,0 @@
//KT-4640 "Trace is erased after resolution completion" exception
class ValueWrapper()
{
var backingValue: Int = 0
fun getValue() = backingValue
fun setValue(v: Int) { backingValue = v }
}
val foo by <!TOO_MANY_ARGUMENTS, TOO_MANY_ARGUMENTS!>ValueWrapper()<!>
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-4640 "Trace is erased after resolution completion" exception
class ValueWrapper()
@@ -1,3 +1,3 @@
val a: Int by <!UNRESOLVED_REFERENCE!>A<!>()
val a: Int by <!DELEGATE_SPECIAL_FUNCTION_MISSING!>A()<!>
class A
@@ -2,7 +2,7 @@
import kotlin.reflect.KProperty
var a: Int by A()
var a: Int by <!DELEGATE_SPECIAL_FUNCTION_MISSING!>A()<!>
class A {
operator fun getValue(t: Any?, p: KProperty<*>): Int {
@@ -1,9 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
object T1 {
operator fun Int.provideDelegate(host: T1, p: Any): Long = 2
operator fun Long.getValue(receiver: String, p: Any): Double = 1.0
val String.test1 by 1
val test2 by <!ARGUMENT_TYPE_MISMATCH!>1<!>
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
object T1 {
@@ -10,5 +10,5 @@ object T2 {
operator fun <T> Foo<T>.getValue(receiver: String, p: Any?): T = TODO()
val String.test1: String by delegate()
val test2: String by <!ARGUMENT_TYPE_MISMATCH!>delegate()<!>
val test2: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>delegate()<!>
}
@@ -13,7 +13,7 @@ fun wrong(arg: Wrong) {}
class Wrong
class Right {
val prop: () -> Unit by <!ARGUMENT_TYPE_MISMATCH, ARGUMENT_TYPE_MISMATCH!>::wrong<!>
val prop: () -> Unit by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>::wrong<!>
}
fun box(): String {
@@ -4,12 +4,12 @@
import kotlin.reflect.KProperty
val a by <!UNRESOLVED_REFERENCE!>a<!>
val a by <!RECURSION_IN_IMPLICIT_TYPES!>a<!>
val b by Delegate(b)
val c by <!UNRESOLVED_REFERENCE!>d<!>
val d by <!UNRESOLVED_REFERENCE!>c<!>
val c by d
val d by <!RECURSION_IN_IMPLICIT_TYPES!>c<!>
class Delegate(i: Int) {
operator fun getValue(t: Any?, p: KProperty<*>): Int {
@@ -1,19 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
import kotlin.reflect.KProperty
class D {
var c: Int by Delegate()
}
var cTopLevel: Int by Delegate()
class A
class Delegate {
operator fun getValue(t: Any?, p: KProperty<*>): Int {
return 1
}
operator fun setValue(t: A, p: KProperty<*>, i: Int) {}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
@@ -1,18 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
import kotlin.reflect.KProperty
class A {
var a: Int by <!ARGUMENT_TYPE_MISMATCH!>Delegate()<!>
}
var aTopLevel: Int by Delegate()
class Delegate {
operator fun getValue(t: Nothing?, p: KProperty<*>): Int {
return 1
}
operator fun setValue(t: Nothing?, p: KProperty<*>, a: Int) {
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
@@ -1,18 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
import kotlin.reflect.KProperty
class A {
var a: Int by <!ARGUMENT_TYPE_MISMATCH!>Delegate()<!>
}
var aTopLevel: Int by <!ARGUMENT_TYPE_MISMATCH!>Delegate()<!>
class Delegate {
fun getValue(t: Nothing, p: KProperty<*>): Int {
return 1
}
fun setValue(t: Nothing, p: KProperty<*>, a: Int) {
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
@@ -1,17 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
class A {
val c: Int by <!NONE_APPLICABLE!>Delegate<!>()
}
class Delegate {
fun getValue(t: Int, p: KProperty<*>): Int {
return 1
}
fun getValue(t: String, p: KProperty<*>): Int {
return 1
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
@@ -1,23 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
class A
class B {
val b: Int by <!ARGUMENT_TYPE_MISMATCH!>Delegate<A>()<!>
}
val bTopLevel: Int by <!ARGUMENT_TYPE_MISMATCH!>Delegate<A>()<!>
class C {
val c: Int by Delegate<C>()
}
val cTopLevel: Int by Delegate<Nothing?>()
class Delegate<T> {
operator fun getValue(t: T, p: KProperty<*>): Int {
return 1
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
@@ -1,17 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
import kotlin.reflect.KProperty
class A {
var a: Int by Delegate()
}
var aTopLevel: Int by Delegate()
class Delegate {
operator fun getValue(t: Any?, p: KProperty<*>): Int {
return 1
}
operator fun setValue(t: Any?, p: KProperty<*>, i: String) {}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
@@ -1,18 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
import kotlin.reflect.KProperty
class B {
val b: Int by <!ARGUMENT_TYPE_MISMATCH!>Delegate()<!>
}
val bTopLevel: Int by <!ARGUMENT_TYPE_MISMATCH!>Delegate()<!>
class A
class Delegate {
fun getValue(t: A, p: KProperty<*>): Int {
return 1
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
// !WITH_NEW_INFERENCE
@@ -1,15 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
class A {
val a: Int by Delegate(<!NO_VALUE_FOR_PARAMETER!>)<!>
}
val aTopLevel: Int by Delegate(<!NO_VALUE_FOR_PARAMETER!>)<!>
class Delegate {
fun getValue(t: Any?, p: KProperty<*>, a: Int): Int {
return a
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
@@ -1,17 +0,0 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
class A {
var a: Int by Delegate()
}
var aTopLevel: Int by Delegate()
class Delegate {
operator fun getValue(t: Any?, p: KProperty<*>): Int {
return 1
}
operator fun setValue(t: Any?, p: KProperty<*>, a: Int, c: Int) {}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: -UNUSED_PARAMETER
import kotlin.reflect.KProperty
@@ -1,23 +0,0 @@
// FILE: J.java
import org.jetbrains.annotations.*;
public class J {
public interface DP {
String getValue(Object a, Object b);
String setValue(Object a, Object b, Object c);
}
@NotNull
public static DP staticNN;
@Nullable
public static DP staticN;
public static DP staticJ;
}
// FILE: k.kt
var A by J.staticNN
var B by J<!UNSAFE_CALL!>.<!>staticN
var C by J.staticJ
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// FILE: J.java
import org.jetbrains.annotations.*;
@@ -8,7 +8,7 @@ class Delegate {
fun foo(): Int {
val prop: Int by Delegate()
val prop2: Int by <!UNRESOLVED_REFERENCE!>123<!>
val prop2: Int by <!DELEGATE_SPECIAL_FUNCTION_MISSING!>123<!>
val obj = object {
fun v(): Int {
@@ -17,7 +17,7 @@ fun case1() {
}
class B() {
val p: String by <!TOO_MANY_ARGUMENTS!>Delegate()<!> // DELEGATE_SPECIAL_FUNCTION_MISSING expected
val p: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>Delegate()<!> // DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE expected
}
class Delegate {
@@ -42,7 +42,7 @@ fun case2() {
}
class B() {
var p: String by <!TOO_MANY_ARGUMENTS!>Delegate()<!> // DELEGATE_SPECIAL_FUNCTION_MISSING expected
var p: String by <!DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE, DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE!>Delegate()<!> // DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE expected
}
class Delegate {
@@ -26,7 +26,7 @@ fun case1() {
}
class B() {
val p: String by Delegate() // DELEGATE_SPECIAL_FUNCTION_MISSING expected
val p: String by Delegate() // DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE expected
}
class Delegate {
@@ -51,7 +51,7 @@ fun case2() {
}
class B() {
var p: String by <!DELEGATE_SPECIAL_FUNCTION_MISSING!>Delegate()<!> // DELEGATE_SPECIAL_FUNCTION_MISSING expected
var p: String by <!DELEGATE_SPECIAL_FUNCTION_MISSING!>Delegate()<!> // DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE expected
}
class Delegate {
@@ -72,7 +72,7 @@ fun case_10(x: Boolean?) = when (val <T> x where T: suspend () -> Unit, T: Boole
// TESTCASE NUMBER: 11
fun case_11() {
val <T> x by lazy { 1 }
var <T> x by lazy { 1 }
var <T> x by <!DELEGATE_SPECIAL_FUNCTION_MISSING!>lazy { 1 }<!>
}
// TESTCASE NUMBER: 12
@@ -1862,6 +1862,35 @@ internal val KT_DIAGNOSTIC_CONVERTER = KtDiagnosticConverterBuilder.buildConvert
token,
)
}
add(FirErrors.DELEGATE_SPECIAL_FUNCTION_MISSING) { firDiagnostic ->
DelegateSpecialFunctionMissingImpl(
firDiagnostic.a,
firSymbolBuilder.typeBuilder.buildKtType(firDiagnostic.b),
firDiagnostic.c,
firDiagnostic as FirPsiDiagnostic<*>,
token,
)
}
add(FirErrors.DELEGATE_SPECIAL_FUNCTION_AMBIGUITY) { firDiagnostic ->
DelegateSpecialFunctionAmbiguityImpl(
firDiagnostic.a,
firDiagnostic.b.map { abstractFirBasedSymbol ->
firSymbolBuilder.buildSymbol(abstractFirBasedSymbol.fir as FirDeclaration)
},
firDiagnostic as FirPsiDiagnostic<*>,
token,
)
}
add(FirErrors.DELEGATE_SPECIAL_FUNCTION_NONE_APPLICABLE) { firDiagnostic ->
DelegateSpecialFunctionNoneApplicableImpl(
firDiagnostic.a,
firDiagnostic.b.map { abstractFirBasedSymbol ->
firSymbolBuilder.buildSymbol(abstractFirBasedSymbol.fir as FirDeclaration)
},
firDiagnostic as FirPsiDiagnostic<*>,
token,
)
}
add(FirErrors.TOPLEVEL_TYPEALIASES_ONLY) { firDiagnostic ->
ToplevelTypealiasesOnlyImpl(
firDiagnostic as FirPsiDiagnostic<*>,
@@ -1305,6 +1305,25 @@ sealed class KtFirDiagnostic<PSI: PsiElement> : KtDiagnosticWithPsi<PSI> {
abstract val candidates: List<KtSymbol>
}
abstract class DelegateSpecialFunctionMissing : KtFirDiagnostic<KtExpression>() {
override val diagnosticClass get() = DelegateSpecialFunctionMissing::class
abstract val expectedFunctionSignature: String
abstract val delegateType: KtType
abstract val description: String
}
abstract class DelegateSpecialFunctionAmbiguity : KtFirDiagnostic<KtExpression>() {
override val diagnosticClass get() = DelegateSpecialFunctionAmbiguity::class
abstract val expectedFunctionSignature: String
abstract val candidates: List<KtSymbol>
}
abstract class DelegateSpecialFunctionNoneApplicable : KtFirDiagnostic<KtExpression>() {
override val diagnosticClass get() = DelegateSpecialFunctionNoneApplicable::class
abstract val expectedFunctionSignature: String
abstract val candidates: List<KtSymbol>
}
abstract class ToplevelTypealiasesOnly : KtFirDiagnostic<KtTypeAlias>() {
override val diagnosticClass get() = ToplevelTypealiasesOnly::class
}
@@ -2116,6 +2116,34 @@ internal class NextNoneApplicableImpl(
override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic)
}
internal class DelegateSpecialFunctionMissingImpl(
override val expectedFunctionSignature: String,
override val delegateType: KtType,
override val description: String,
firDiagnostic: FirPsiDiagnostic<*>,
override val token: ValidityToken,
) : KtFirDiagnostic.DelegateSpecialFunctionMissing(), KtAbstractFirDiagnostic<KtExpression> {
override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic)
}
internal class DelegateSpecialFunctionAmbiguityImpl(
override val expectedFunctionSignature: String,
override val candidates: List<KtSymbol>,
firDiagnostic: FirPsiDiagnostic<*>,
override val token: ValidityToken,
) : KtFirDiagnostic.DelegateSpecialFunctionAmbiguity(), KtAbstractFirDiagnostic<KtExpression> {
override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic)
}
internal class DelegateSpecialFunctionNoneApplicableImpl(
override val expectedFunctionSignature: String,
override val candidates: List<KtSymbol>,
firDiagnostic: FirPsiDiagnostic<*>,
override val token: ValidityToken,
) : KtFirDiagnostic.DelegateSpecialFunctionNoneApplicable(), KtAbstractFirDiagnostic<KtExpression> {
override val firDiagnostic: FirPsiDiagnostic<*> by weakRef(firDiagnostic)
}
internal class ToplevelTypealiasesOnlyImpl(
firDiagnostic: FirPsiDiagnostic<*>,
override val token: ValidityToken,