FIR: fix type approximation by visibility

This commit is contained in:
Mikhail Glukhikh
2021-02-15 19:15:43 +03:00
parent 74bdb2398e
commit 357a7907a3
18 changed files with 90 additions and 92 deletions
@@ -1,5 +1,5 @@
FILE: innerClassInAnonymousObject.kt
public final val x: R|<anonymous>| = object : R|kotlin/Any| {
public final val x: R|kotlin/Any| = object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
@@ -16,4 +16,4 @@ FILE: innerClassInAnonymousObject.kt
}
public get(): R|<anonymous>|
public get(): R|kotlin/Any|
@@ -8,7 +8,7 @@ FILE: annotationArgumentKClassLiteralTypeError.kt
public get(): R|kotlin/Array<kotlin/reflect/KClass<*>>|
}
public final val <reified T> R|T|.test: R|<anonymous><T>|
public final val <reified T> R|T|.test: R|kotlin/Any|
public get(): R|<anonymous><T>| {
^ @R|Ann|(<implicitArrayOf>(<getClass>(R|T|), <getClass>(Q|kotlin/Array|))) object : R|kotlin/Any| {
private constructor(): R|<anonymous><T>| {
@@ -90,14 +90,14 @@ FILE: conflictingOverloads.kt
}
public final val Companion: R|<anonymous>| = object : R|kotlin/Any| {
public final val Companion: R|kotlin/Any| = object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
}
public get(): R|<anonymous>|
public get(): R|kotlin/Any|
}
public final fun R|B|.foo(): R|kotlin/Unit| {
@@ -21,7 +21,7 @@ FILE: localEntitytNotAllowed.kt
public abstract interface X : R|kotlin/Any| {
}
public final val a: R|<anonymous>| = object : R|kotlin/Any| {
public final val a: R|kotlin/Any| = object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
@@ -48,7 +48,7 @@ FILE: localEntitytNotAllowed.kt
}
public get(): R|<anonymous>|
public get(): R|kotlin/Any|
public final fun b(): R|kotlin/Unit| {
local final object E : R|kotlin/Any| {
@@ -20,7 +20,7 @@ FILE: privateObjectLiteral.kt
public final val y: R|kotlin/Int| = this@R|/C|.R|/C.x|.R|/<anonymous>.foo|()
public get(): R|kotlin/Int|
internal final val z: R|<anonymous>| = object : R|kotlin/Any| {
internal final val z: R|kotlin/Any| = object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
@@ -31,9 +31,9 @@ FILE: privateObjectLiteral.kt
}
internal get(): R|<anonymous>|
internal get(): R|kotlin/Any|
public final val w: R|kotlin/Int| = this@R|/C|.R|/C.z|.R|/<anonymous>.foo|()
public get(): R|kotlin/Int|
public final val w: R|ERROR CLASS: Unresolved name: foo| = this@R|/C|.R|/C.z|.<Unresolved name: foo>#()
public get(): R|ERROR CLASS: Unresolved name: foo|
}
@@ -9,5 +9,5 @@ class C {
fun foo() = 13
}
val w = z.foo() // ERROR!
val w = z.<!UNRESOLVED_REFERENCE{LT}!><!UNRESOLVED_REFERENCE{PSI}!>foo<!>()<!> // ERROR!
}
@@ -22,7 +22,7 @@ FILE: RedundantVisibilityModifierChecker.kt
super<R|kotlin/Any|>()
}
internal final val z: R|<anonymous>| = object : R|kotlin/Any| {
internal final val z: R|kotlin/Any| = object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
@@ -33,7 +33,7 @@ FILE: RedundantVisibilityModifierChecker.kt
}
internal get(): R|<anonymous>|
internal get(): R|kotlin/Any|
}
public final class Foo2<T1, T2 : R|T1|> : R|kotlin/Any| {
@@ -1,7 +1,7 @@
FILE: problems.kt
public final val sb: R|java/lang/StringBuilder| = R|java/lang/StringBuilder.StringBuilder|()
public get(): R|java/lang/StringBuilder|
public final val o: R|<anonymous>| = object : R|kotlin/Any| {
public final val o: R|kotlin/Any| = object : R|kotlin/Any| {
private constructor(): R|<anonymous>| {
super<R|kotlin/Any|>()
}
@@ -15,7 +15,7 @@ FILE: problems.kt
}
public get(): R|<anonymous>|
public get(): R|kotlin/Any|
public final fun test(): R|kotlin/Unit| {
local final class Local : R|kotlin/Any| {
public constructor(): R|Local| {
@@ -6,6 +6,8 @@
package org.jetbrains.kotlin.fir.resolve.transformers.body.resolve
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.*
import org.jetbrains.kotlin.fir.declarations.builder.buildValueParameter
@@ -45,6 +47,17 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
private var containingClass: FirRegularClass? = null
private val statusResolver: FirStatusResolver = FirStatusResolver(session, scopeSession)
private fun FirDeclaration.visibilityForApproximation(): Visibility {
if (this !is FirMemberDeclaration) return Visibilities.Local
val container = context.containers.getOrNull(context.containers.size - 2)
val containerVisibility =
if (container == null) Visibilities.Public
else (container as? FirRegularClass)?.visibility ?: Visibilities.Local
if (containerVisibility == Visibilities.Local || visibility == Visibilities.Local) return Visibilities.Local
if (containerVisibility == Visibilities.Private) return Visibilities.Private
return visibility
}
private inline fun <T> withFirArrayOfCallTransformer(block: () -> T): T {
transformer.expressionsTransformer.enableArrayOfCallTransformation = true
return try {
@@ -560,7 +573,9 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
transformer,
withExpectedType(
returnExpression.resultType.approximatedIfNeededOrSelf(
inferenceComponents.approximator, simpleFunction?.visibility, simpleFunction?.isInline == true
inferenceComponents.approximator,
simpleFunction?.visibilityForApproximation(),
simpleFunction?.isInline == true
)
)
)
@@ -990,67 +1005,50 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
private fun storeVariableReturnType(variable: FirVariable<*>) {
val initializer = variable.initializer
if (variable.returnTypeRef is FirImplicitTypeRef) {
when {
val resultType = when {
initializer != null -> {
val unwrappedInitializer = (initializer as? FirExpressionWithSmartcast)?.originalExpression ?: initializer
val expectedType = when (val resultType = unwrappedInitializer.resultType) {
is FirImplicitTypeRef -> buildErrorTypeRef {
diagnostic = ConeSimpleDiagnostic("No result type for initializer", DiagnosticKind.InferenceError)
}
else -> {
buildResolvedTypeRef {
type = resultType.coneType
annotations.addAll(resultType.annotations)
resultType.source?.fakeElement(FirFakeSourceElementKind.PropertyFromParameter)?.let {
source = it
}
unwrappedInitializer.resultType
}
variable.getter != null && variable.getter !is FirDefaultPropertyAccessor -> variable.getter?.returnTypeRef
else -> null
}
if (resultType != null) {
val expectedType = when (resultType) {
is FirImplicitTypeRef -> buildErrorTypeRef {
diagnostic = ConeSimpleDiagnostic("No result type for initializer", DiagnosticKind.InferenceError)
}
else -> {
buildResolvedTypeRef {
type = resultType.coneType
annotations.addAll(resultType.annotations)
resultType.source?.fakeElement(FirFakeSourceElementKind.PropertyFromParameter)?.let {
source = it
}
}
}
variable.transformReturnTypeRef(
transformer,
withExpectedType(
expectedType.approximatedIfNeededOrSelf(inferenceComponents.approximator, (variable as? FirProperty)?.visibility)
}
variable.transformReturnTypeRef(
transformer,
withExpectedType(
expectedType.approximatedIfNeededOrSelf(
inferenceComponents.approximator,
variable.visibilityForApproximation()
)
)
}
variable.getter != null && variable.getter !is FirDefaultPropertyAccessor -> {
val expectedType = when (val resultType = variable.getter?.returnTypeRef) {
is FirImplicitTypeRef -> buildErrorTypeRef {
diagnostic = ConeSimpleDiagnostic("No result type for getter", DiagnosticKind.InferenceError)
}
else -> {
resultType?.let {
buildResolvedTypeRef {
type = resultType.coneType
annotations.addAll(resultType.annotations)
resultType.source?.fakeElement(FirFakeSourceElementKind.PropertyFromParameter)?.let {
source = it
}
}
}
}
}
variable.transformReturnTypeRef(
transformer,
withExpectedType(
expectedType?.approximatedIfNeededOrSelf(inferenceComponents.approximator, (variable as? FirProperty)?.visibility)
)
)
} else {
variable.transformReturnTypeRef(
transformer,
withExpectedType(
buildErrorTypeRef {
diagnostic = ConeSimpleDiagnostic(
"Cannot infer variable type without initializer / getter / delegate",
DiagnosticKind.InferenceError,
)
},
)
}
else -> {
variable.transformReturnTypeRef(
transformer,
withExpectedType(
buildErrorTypeRef {
diagnostic = ConeSimpleDiagnostic(
"Cannot infer variable type without initializer / getter / delegate",
DiagnosticKind.InferenceError,
)
},
)
)
}
)
}
if (variable.getter?.returnTypeRef is FirImplicitTypeRef) {
variable.getter?.transformReturnTypeRef(transformer, withExpectedType(variable.returnTypeRef))
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.fir.types
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.declarations.classId
import org.jetbrains.kotlin.fir.declarations.FirAnonymousObject
import org.jetbrains.kotlin.fir.diagnostics.ConeSimpleDiagnostic
import org.jetbrains.kotlin.fir.resolve.fullyExpandedType
import org.jetbrains.kotlin.fir.resolve.substitution.substitutorByMap
@@ -309,7 +309,8 @@ private fun FirTypeRef.hideLocalTypeIfNeeded(
?.type as? ConeClassLikeType)
?.lookupTag as? ConeClassLookupTagWithFixedSymbol)
?.symbol?.fir
if (firClass?.classId?.isLocal != true) {
if (firClass !is FirAnonymousObject) {
// NB: local classes are acceptable here, but reported by EXPOSED_* checkers as errors
return this
}
if (firClass.superTypeRefs.size > 1) {
@@ -318,7 +319,7 @@ private fun FirTypeRef.hideLocalTypeIfNeeded(
}
}
val superType = firClass.superTypeRefs.single()
if (superType is FirResolvedTypeRef && !superType.isAny) {
if (superType is FirResolvedTypeRef) {
return superType
}
}
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_FIR: JVM_IR
// DONT_TARGET_EXACT_BACKEND: WASM
// WASM_MUTE_REASON: PROPERTY_REFERENCES
// WITH_RUNTIME
@@ -20,11 +20,11 @@ private fun foo4(f: () -> Int) = object {
}
fun test1(b: Boolean) {
var x = <!DEBUG_INFO_EXPRESSION_TYPE("<anonymous>")!>foo1 { 1 }<!>
var x = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>foo1 { 1 }<!>
if (b) {
x = <!DEBUG_INFO_EXPRESSION_TYPE("<anonymous>")!>foo1 { 2 }<!>
x = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>foo1 { 2 }<!>
}
x.bar()
x.<!UNRESOLVED_REFERENCE!>bar<!>()
}
fun test2(b: Boolean) {
@@ -20,11 +20,11 @@ private fun foo4(f: () -> Int) = object {
}
fun test1(b: Boolean) {
var x = <!DEBUG_INFO_EXPRESSION_TYPE("<anonymous>")!>foo1 { 1 }<!>
var x = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>foo1 { 1 }<!>
if (b) {
x = <!DEBUG_INFO_EXPRESSION_TYPE("<anonymous>")!>foo1 { 2 }<!>
x = <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>foo1 { 2 }<!>
}
x.bar()
x.<!UNRESOLVED_REFERENCE!>bar<!>()
}
fun test2(b: Boolean) {
@@ -3,7 +3,7 @@ interface IFoo {
}
val test1: <no name provided>
val test1: Any
field = { // BLOCK
local class <no name provided> {
private constructor() /* primary */ {
@@ -17,7 +17,7 @@ FILE fqName:<root> fileName:/objectLiteralExpressions.kt
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
PROPERTY name:test1 visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test1 type:<root>.test1.<no name provided> visibility:private [final,static]
FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.Any visibility:private [final,static]
EXPRESSION_BODY
BLOCK type=<root>.test1.<no name provided> origin=OBJECT_LITERAL
CLASS CLASS name:<no name provided> modality:FINAL visibility:local superTypes:[kotlin.Any]
@@ -40,11 +40,11 @@ FILE fqName:<root> fileName:/objectLiteralExpressions.kt
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.test1.<no name provided>' type=<root>.test1.<no name provided> origin=OBJECT_LITERAL
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test1> visibility:public modality:FINAL <> () returnType:<root>.test1.<no name provided>
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test1> visibility:public modality:FINAL <> () returnType:kotlin.Any
correspondingProperty: PROPERTY name:test1 visibility:public modality:FINAL [val]
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-test1> (): <root>.test1.<no name provided> declared in <root>'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test1 type:<root>.test1.<no name provided> visibility:private [final,static]' type=<root>.test1.<no name provided> origin=null
RETURN type=kotlin.Nothing from='public final fun <get-test1> (): kotlin.Any declared in <root>'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:test1 type:kotlin.Any visibility:private [final,static]' type=kotlin.Any origin=null
PROPERTY name:test2 visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:test2 type:<root>.IFoo visibility:private [final,static]
EXPRESSION_BODY
@@ -53,7 +53,7 @@ object Z {
get
val anObject: <no name provided>
val anObject: Any
field = { // BLOCK
local class <no name provided> {
private constructor() /* primary */ {
@@ -108,7 +108,7 @@ FILE fqName:<root> fileName:/objectReference.kt
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:aLambda type:kotlin.Function0<kotlin.Unit> visibility:private [final]' type=kotlin.Function0<kotlin.Unit> origin=null
receiver: GET_VAR '<this>: <root>.Z declared in <root>.Z.<get-aLambda>' type=<root>.Z origin=null
PROPERTY name:anObject visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:anObject type:<root>.Z.anObject.<no name provided> visibility:private [final]
FIELD PROPERTY_BACKING_FIELD name:anObject type:kotlin.Any visibility:private [final]
EXPRESSION_BODY
BLOCK type=<root>.Z.anObject.<no name provided> origin=OBJECT_LITERAL
CLASS CLASS name:<no name provided> modality:FINAL visibility:local superTypes:[kotlin.Any]
@@ -156,12 +156,12 @@ FILE fqName:<root> fileName:/objectReference.kt
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
CONSTRUCTOR_CALL 'private constructor <init> () [primary] declared in <root>.Z.anObject.<no name provided>' type=<root>.Z.anObject.<no name provided> origin=OBJECT_LITERAL
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-anObject> visibility:public modality:FINAL <> ($this:<root>.Z) returnType:<root>.Z.anObject.<no name provided>
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-anObject> visibility:public modality:FINAL <> ($this:<root>.Z) returnType:kotlin.Any
correspondingProperty: PROPERTY name:anObject visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.Z
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-anObject> (): <root>.Z.anObject.<no name provided> declared in <root>.Z'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:anObject type:<root>.Z.anObject.<no name provided> visibility:private [final]' type=<root>.Z.anObject.<no name provided> origin=null
RETURN type=kotlin.Nothing from='public final fun <get-anObject> (): kotlin.Any declared in <root>.Z'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:anObject type:kotlin.Any visibility:private [final]' type=kotlin.Any origin=null
receiver: GET_VAR '<this>: <root>.Z declared in <root>.Z.<get-anObject>' type=<root>.Z origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
@@ -63,8 +63,8 @@ fun case_6(x: EmptyClass) {
// TESTCASE NUMBER: 7
fun case_7() {
if (anonymousTypeProperty == null || <!DEBUG_INFO_EXPRESSION_TYPE("<anonymous> & <anonymous>")!>anonymousTypeProperty<!> == null) {
<!DEBUG_INFO_EXPRESSION_TYPE("<anonymous>")!>anonymousTypeProperty<!>
if (anonymousTypeProperty == null || <!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any & kotlin.Any")!>anonymousTypeProperty<!> == null) {
<!DEBUG_INFO_EXPRESSION_TYPE("kotlin.Any")!>anonymousTypeProperty<!>
}
}