FIR checker: skip error named reference if receiver is unresolved

Currently, FIR reports errors caused by previous resolution failure. For
example with unresolved `a` and `b` in code `a.b`, both `a` and `b` are
highlighted. FE1.0 only highlights `a` since it's the root cause. This
change applies this heuristics when reporting FirDiagnostics.
This commit is contained in:
Tianyu Geng
2021-03-25 12:20:18 -07:00
committed by Dmitriy Novozhilov
parent d6907222cd
commit fb14b03824
73 changed files with 209 additions and 228 deletions
@@ -15,13 +15,13 @@ class CallBasedInExpressionGenerator(
operatorReference: KtSimpleNameExpression
) : InExpressionGenerator {
private val resolvedCall = operatorReference.<!UNRESOLVED_REFERENCE!>getResolvedCallWithAssert<!>(codegen.<!UNRESOLVED_REFERENCE!>bindingContext<!>)
private val isInverted = operatorReference.<!UNRESOLVED_REFERENCE!>getReferencedNameElementType<!>() == <!UNRESOLVED_REFERENCE!>KtTokens<!>.<!UNRESOLVED_REFERENCE!>NOT_IN<!>
private val isInverted = operatorReference.<!UNRESOLVED_REFERENCE!>getReferencedNameElementType<!>() == <!UNRESOLVED_REFERENCE!>KtTokens<!>.NOT_IN
override fun generate(argument: StackValue): BranchedValue =
gen(argument).let { if (isInverted) <!UNRESOLVED_REFERENCE!>Invert<!>(it) else it }
private fun gen(argument: StackValue): BranchedValue =
object : <!INAPPLICABLE_CANDIDATE{LT}!><!INAPPLICABLE_CANDIDATE{PSI}!>BranchedValue<!>(argument, null, argument.<!UNRESOLVED_REFERENCE!>type<!>, <!UNRESOLVED_REFERENCE!>Opcodes<!>.<!UNRESOLVED_REFERENCE!>IFEQ<!>)<!> {
object : <!INAPPLICABLE_CANDIDATE{LT}!><!INAPPLICABLE_CANDIDATE{PSI}!>BranchedValue<!>(argument, null, argument.<!UNRESOLVED_REFERENCE!>type<!>, <!UNRESOLVED_REFERENCE!>Opcodes<!>.IFEQ)<!> {
override fun putSelector(type: Type, kotlinType: KotlinType?, v: InstructionAdapter) {
invokeFunction(v)
<!UNRESOLVED_REFERENCE!>coerceTo<!>(type, kotlinType, v)
@@ -29,7 +29,7 @@ class CallBasedInExpressionGenerator(
override fun condJump(jumpLabel: Label, v: InstructionAdapter, jumpIfFalse: Boolean) {
invokeFunction(v)
v.<!UNRESOLVED_REFERENCE!>visitJumpInsn<!>(if (jumpIfFalse) <!UNRESOLVED_REFERENCE!>Opcodes<!>.<!UNRESOLVED_REFERENCE!>IFEQ<!> else <!UNRESOLVED_REFERENCE!>Opcodes<!>.<!UNRESOLVED_REFERENCE!>IFNE<!>, jumpLabel)
v.<!UNRESOLVED_REFERENCE!>visitJumpInsn<!>(if (jumpIfFalse) <!UNRESOLVED_REFERENCE!>Opcodes<!>.IFEQ else <!UNRESOLVED_REFERENCE!>Opcodes<!>.IFNE, jumpLabel)
}
private fun invokeFunction(v: InstructionAdapter) {
@@ -18,4 +18,4 @@ fun test() {
B.foo()
}
val bb = <!UNRESOLVED_REFERENCE!>B<!>.<!UNRESOLVED_REFERENCE!>foo<!>()
val bb = <!UNRESOLVED_REFERENCE!>B<!>.foo()
@@ -6,5 +6,5 @@ object A {
object B
val err = B.<!UNRESOLVED_REFERENCE!>A<!>.<!UNRESOLVED_REFERENCE!>B<!>
val err = B.<!UNRESOLVED_REFERENCE!>A<!>.B
val correct = A.B.A
@@ -23,7 +23,7 @@ fun main() {
x <!UNRESOLVED_REFERENCE!>><!> 1
}
JavaUsage.<!INAPPLICABLE_CANDIDATE!>foo<!>({ <!UNRESOLVED_REFERENCE!>it<!> <!UNRESOLVED_REFERENCE!>><!> 1 })
JavaUsage.<!INAPPLICABLE_CANDIDATE!>foo<!>({ <!UNRESOLVED_REFERENCE!>it<!> > 1 })
val x = { x: Int -> x > 1 }
@@ -79,7 +79,7 @@ fun test_5(a: A, in1: In1<A>, in2: In1<in A>, in3: In1<out A>) {
fun test_6(a: A, out1: Out1<A>, out2: Out1<in A>, out3: Out1<out A>) {
out1.value().foo()
out2.<!UNRESOLVED_REFERENCE!>value<!>().<!UNRESOLVED_REFERENCE!>foo<!>()
out2.<!UNRESOLVED_REFERENCE!>value<!>().foo()
out3.value().foo()
}
@@ -34,17 +34,28 @@ class ErrorNodeDiagnosticCollectorComponent(collector: AbstractDiagnosticCollect
override fun visitErrorNamedReference(errorNamedReference: FirErrorNamedReference, data: CheckerContext) {
val source = errorNamedReference.source ?: return
val qualifiedAccessSource = data.qualifiedAccesses.lastOrNull()?.takeIf {
val qualifiedAccess = data.qualifiedAccesses.lastOrNull()?.takeIf {
// Use the source of the enclosing FirQualifiedAccessExpression if it is exactly the call to the erroneous callee.
it is FirQualifiedAccessExpression && it.calleeReference == errorNamedReference
}?.source
}
// Don't report duplicated unresolved reference on annotation entry (already reported on its type)
if (source.elementType == KtNodeTypes.ANNOTATION_ENTRY && errorNamedReference.diagnostic is ConeUnresolvedNameError) return
// Already reported in FirConventionFunctionCallChecker
if (source.kind == FirFakeSourceElementKind.ArrayAccessNameReference &&
errorNamedReference.diagnostic is ConeUnresolvedNameError
) return
reportFirDiagnostic(errorNamedReference.diagnostic, source, reporter, data, qualifiedAccessSource)
// If the receiver cannot be resolved, we skip reporting any further problems for this call.
if (qualifiedAccess?.dispatchReceiver.hasUnresolvedNameError() || qualifiedAccess?.extensionReceiver.hasUnresolvedNameError() || qualifiedAccess?.explicitReceiver.hasUnresolvedNameError()) return
reportFirDiagnostic(errorNamedReference.diagnostic, source, reporter, data, qualifiedAccess?.source)
}
private fun FirExpression?.hasUnresolvedNameError(): Boolean {
return when ((this?.typeRef as? FirErrorTypeRef)?.diagnostic) {
is ConeUnresolvedNameError -> true
else -> false
}
}
override fun visitErrorExpression(errorExpression: FirErrorExpression, data: CheckerContext) {