FIR declared member scope: remove constructors from callable index

This removes constructor resolve ambiguity in some cases.
Now constructors are resolved only via classifiers
This commit is contained in:
Mikhail Glukhikh
2019-11-19 17:48:33 +03:00
parent bd71d1dc3f
commit d39a36ee25
8 changed files with 86 additions and 33 deletions
@@ -46,7 +46,7 @@ class FirTowerResolver(
// } // }
towerDataConsumer.consume( towerDataConsumer.consume(
TOWER_LEVEL, TOWER_LEVEL,
MemberScopeTowerLevel(session, implicitReceiverValue, scopeSession = components.scopeSession), MemberScopeTowerLevel(session, components, implicitReceiverValue, scopeSession = components.scopeSession),
group++ group++
) )
@@ -57,7 +57,7 @@ class FirTowerResolver(
// } // }
towerDataConsumer.consume( towerDataConsumer.consume(
TOWER_LEVEL, TOWER_LEVEL,
MemberScopeTowerLevel(session, implicitReceiverValue, implicitReceiverValue, components.scopeSession), MemberScopeTowerLevel(session, components, implicitReceiverValue, implicitReceiverValue, components.scopeSession),
group++ group++
) )
@@ -127,7 +127,8 @@ class FirTowerResolver(
towerDataConsumer.consume( towerDataConsumer.consume(
TOWER_LEVEL, TOWER_LEVEL,
MemberScopeTowerLevel( MemberScopeTowerLevel(
session, scopeSession = components.scopeSession, session, components,
scopeSession = components.scopeSession,
dispatchReceiver = implicitDispatchReceiverValue, dispatchReceiver = implicitDispatchReceiverValue,
implicitExtensionReceiver = implicitReceiverValue implicitExtensionReceiver = implicitReceiverValue
), ),
@@ -171,13 +171,14 @@ class ExplicitReceiverTowerDataConsumer<T : AbstractFirBasedSymbol<*>>(
if (skipGroup(group, resultCollector)) return ProcessorAction.NEXT if (skipGroup(group, resultCollector)) return ProcessorAction.NEXT
return when (kind) { return when (kind) {
TowerDataKind.EMPTY -> TowerDataKind.EMPTY ->
MemberScopeTowerLevel(session, explicitReceiver, scopeSession = candidateFactory.bodyResolveComponents.scopeSession) MemberScopeTowerLevel(
.processElementsByName( session, resultCollector.components, explicitReceiver, scopeSession = candidateFactory.bodyResolveComponents.scopeSession
token, ).processElementsByName(
name, token,
explicitReceiver = null, name,
processor = EmptyKindTowerProcessor(group) explicitReceiver = null,
) processor = EmptyKindTowerProcessor(group)
)
TowerDataKind.TOWER_LEVEL -> { TowerDataKind.TOWER_LEVEL -> {
if (token == TowerScopeLevel.Token.Objects) return ProcessorAction.NEXT if (token == TowerScopeLevel.Token.Objects) return ProcessorAction.NEXT
towerScopeLevel.processElementsByName( towerScopeLevel.processElementsByName(
@@ -86,6 +86,7 @@ abstract class SessionBasedTowerLevel(val session: FirSession) : TowerScopeLevel
// or given implicit or explicit receiver, otherwise // or given implicit or explicit receiver, otherwise
class MemberScopeTowerLevel( class MemberScopeTowerLevel(
session: FirSession, session: FirSession,
val bodyResolveComponents: BodyResolveComponents,
val dispatchReceiver: ReceiverValue, val dispatchReceiver: ReceiverValue,
val implicitExtensionReceiver: ImplicitReceiverValue<*>? = null, val implicitExtensionReceiver: ImplicitReceiverValue<*>? = null,
val scopeSession: ScopeSession val scopeSession: ScopeSession
@@ -128,7 +129,7 @@ class MemberScopeTowerLevel(
this.processPropertiesByName(name, symbol.cast()) this.processPropertiesByName(name, symbol.cast())
} }
TowerScopeLevel.Token.Functions -> processMembers(processor, explicitExtensionReceiver) { symbol -> TowerScopeLevel.Token.Functions -> processMembers(processor, explicitExtensionReceiver) { symbol ->
this.processFunctionsByName(name, symbol.cast()) this.processFunctionsAndConstructorsByName(name, session, bodyResolveComponents, symbol.cast())
} }
TowerScopeLevel.Token.Objects -> processMembers(processor, explicitExtensionReceiver) { symbol -> TowerScopeLevel.Token.Objects -> processMembers(processor, explicitExtensionReceiver) { symbol ->
this.processClassifiersByName(name, symbol.cast()) this.processClassifiersByName(name, symbol.cast())
@@ -37,13 +37,6 @@ class FirClassDeclaredMemberScope(klass: FirClass<*>, useLazyNestedClassifierSco
} }
result.getOrPut(name) { mutableListOf() } += declaration.symbol result.getOrPut(name) { mutableListOf() } += declaration.symbol
} }
is FirRegularClass -> {
for (nestedDeclaration in declaration.declarations) {
if (nestedDeclaration is FirConstructor) {
result.getOrPut(declaration.name) { mutableListOf() } += nestedDeclaration.symbol
}
}
}
} }
} }
result result
@@ -2,8 +2,28 @@
* We see same constructor of `B` in two scopes * We see same constructor of `B` in two scopes
*/ */
class A() { open class A() {
class B() : A() { class B() : A() {
fun copy() = <!AMBIGUITY!>B<!>() fun copy() = B()
}
open class C() {
fun copy() = C()
}
}
class D : A.C() {
fun foo() {
val a = A()
val ac = A.C()
val c = <!UNRESOLVED_REFERENCE!>C<!>() // shouldn't resolve
}
}
class E : A() {
fun foo() {
val a = A()
val c = C()
} }
} }
@@ -1,5 +1,5 @@
FILE: nestedClassContructor.kt FILE: nestedClassContructor.kt
public final class A : R|kotlin/Any| { public open class A : R|kotlin/Any| {
public constructor(): R|A| { public constructor(): R|A| {
super<R|kotlin/Any|>() super<R|kotlin/Any|>()
} }
@@ -9,10 +9,44 @@ FILE: nestedClassContructor.kt
super<R|A|>() super<R|A|>()
} }
public final fun copy(): <ERROR TYPE REF: Ambiguity: B, [/A.B.B, /A.B.B]> { public final fun copy(): R|A.B| {
^copy <Ambiguity: B, [/A.B.B, /A.B.B]>#() ^copy R|/A.B.B|()
}
}
public open class C : R|kotlin/Any| {
public constructor(): R|A.C| {
super<R|kotlin/Any|>()
}
public final fun copy(): R|A.C| {
^copy R|/A.C.C|()
} }
} }
} }
public final class D : R|A.C| {
public constructor(): R|D| {
super<R|A.C|>()
}
public final fun foo(): R|kotlin/Unit| {
lval a: R|A| = R|/A.A|()
lval ac: R|A.C| = Q|A|.R|/A.C.C|()
lval c: <ERROR TYPE REF: Unresolved name: C> = <Unresolved name: C>#()
}
}
public final class E : R|A| {
public constructor(): R|E| {
super<R|A|>()
}
public final fun foo(): R|kotlin/Unit| {
lval a: R|A| = R|/A.A|()
lval c: R|A.C| = R|/A.C.C|()
}
}
@@ -4,10 +4,11 @@ FILE fqName:<root> fileName:/constructorWithOwnTypeParametersCall.kt
RETURN type=kotlin.Nothing from='public final fun testKotlin (): <root>.K1.K2<kotlin.String> declared in <root>' RETURN type=kotlin.Nothing from='public final fun testKotlin (): <root>.K1.K2<kotlin.String> declared in <root>'
CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.K1.K2' type=<root>.K1.K2<kotlin.String> origin=null CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in <root>.K1.K2' type=<root>.K1.K2<kotlin.String> origin=null
<class: T2>: <none> <class: T2>: <none>
FUN name:testJava visibility:public modality:FINAL <> () returnType:IrErrorType FUN name:testJava visibility:public modality:FINAL <> () returnType:<root>.J1.J2<kotlin.Double>
BLOCK_BODY BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun testJava (): IrErrorType declared in <root>' RETURN type=kotlin.Nothing from='public final fun testJava (): <root>.J1.J2<kotlin.Double> declared in <root>'
ERROR_CALL 'Unresolved reference: <Unresolved name: J2>#' type=IrErrorType CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.J1.J2' type=<root>.J1.J2<kotlin.Double> origin=null
<class: X2>: <none>
CLASS CLASS name:K1 modality:FINAL visibility:public superTypes:[kotlin.Any] CLASS CLASS name:K1 modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.K1 $this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.K1
TYPE_PARAMETER name:T1 index:0 variance: superTypes:[] TYPE_PARAMETER name:T1 index:0 variance: superTypes:[]
@@ -10,14 +10,16 @@ FILE fqName:<root> fileName:/javaConstructorWithTypeParameters.kt
CONSTRUCTOR_CALL 'public constructor <init> (x1: X1 of <uninitialized parent>?) declared in <root>.J1' type=<root>.J1<kotlin.Int> origin=null CONSTRUCTOR_CALL 'public constructor <init> (x1: X1 of <uninitialized parent>?) declared in <root>.J1' type=<root>.J1<kotlin.Int> origin=null
<class: T1>: <none> <class: T1>: <none>
x1: CONST Int type=kotlin.Int value=1 x1: CONST Int type=kotlin.Int value=1
FUN name:test3 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:IrErrorType FUN name:test3 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:<root>.J1.J2<kotlin.Int>
VALUE_PARAMETER name:j1 index:0 type:<root>.J1<kotlin.Any> VALUE_PARAMETER name:j1 index:0 type:<root>.J1<kotlin.Any>
BLOCK_BODY BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test3 (j1: <root>.J1<kotlin.Any>): IrErrorType declared in <root>' RETURN type=kotlin.Nothing from='public final fun test3 (j1: <root>.J1<kotlin.Any>): <root>.J1.J2<kotlin.Int> declared in <root>'
ERROR_CALL 'Unresolved reference: <Unresolved name: J2>#' type=IrErrorType CONSTRUCTOR_CALL 'public constructor <init> () declared in <root>.J1.J2' type=<root>.J1.J2<kotlin.Int> origin=null
FUN name:test4 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:IrErrorType <class: T2>: <none>
FUN name:test4 visibility:public modality:FINAL <> (j1:<root>.J1<kotlin.Any>) returnType:<root>.J1.J2<kotlin.Int>
VALUE_PARAMETER name:j1 index:0 type:<root>.J1<kotlin.Any> VALUE_PARAMETER name:j1 index:0 type:<root>.J1<kotlin.Any>
BLOCK_BODY BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun test4 (j1: <root>.J1<kotlin.Any>): IrErrorType declared in <root>' RETURN type=kotlin.Nothing from='public final fun test4 (j1: <root>.J1<kotlin.Any>): <root>.J1.J2<kotlin.Int> declared in <root>'
ERROR_CALL 'Unresolved reference: <Unresolved name: J2>#' type=IrErrorType CONSTRUCTOR_CALL 'public constructor <init> (x2: X2 of <uninitialized parent>?) declared in <root>.J1.J2' type=<root>.J1.J2<kotlin.Int> origin=null
CONST Int type=kotlin.Int value=1 <class: T2>: <none>
x2: CONST Int type=kotlin.Int value=1