diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt index 338e76a2cf3..4b41267498f 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/transformations/InsertImplicitCasts.kt @@ -21,10 +21,7 @@ import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrStatement -import org.jetbrains.kotlin.ir.declarations.IrField -import org.jetbrains.kotlin.ir.declarations.IrFunction -import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer -import org.jetbrains.kotlin.ir.declarations.IrVariable +import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol @@ -153,6 +150,11 @@ class InsertImplicitCasts(private val builtIns: KotlinBuiltIns, private val symb } } + override fun visitClass(declaration: IrClass): IrStatement = + runInTypeParameterScope(declaration) { + super.visitClass(declaration) + } + override fun visitWhen(expression: IrWhen): IrExpression = expression.transformPostfix { for (irBranch in branches) { diff --git a/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.kt b/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.kt index 44407653b51..5e9a09ea788 100644 --- a/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.kt +++ b/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.kt @@ -4,4 +4,13 @@ inline fun Any.test1(): T? = interface Foo inline val Foo.asT: T? - get() = if (this is T) this else null \ No newline at end of file + get() = if (this is T) this else null + +class Bar { + fun test(arg: Any) { + arg as T + useT(arg) + } + + fun useT(t: T) {} +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.txt b/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.txt index f74d4f37f83..cf7f1bea7c2 100644 --- a/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.txt +++ b/compiler/testData/ir/irText/expressions/implicitCastToTypeParameter.txt @@ -56,3 +56,45 @@ FILE fqName: fileName:/implicitCastToTypeParameter.kt BRANCH if: CONST Boolean type=kotlin.Boolean value=true then: CONST Null type=kotlin.Nothing? value=null + CLASS CLASS name:Bar modality:FINAL visibility:public flags: + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:Bar flags: + superClasses: + CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Any modality:OPEN visibility:public flags: + TYPE_PARAMETER name:T index:0 variance: upperBounds:[kotlin.Any?] + superClassifiers: + CLASS IR_EXTERNAL_DECLARATION_STUB CLASS name:Any modality:OPEN visibility:public flags: + CONSTRUCTOR visibility:public <> () returnType:Bar flags: + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'constructor Any()' + INSTANCE_INITIALIZER_CALL classDescriptor='Bar' + FUN name:test visibility:public modality:FINAL <> ($this:Bar, arg:kotlin.Any) returnType:Unit flags: + $this: VALUE_PARAMETER name: type:Bar flags: + VALUE_PARAMETER name:arg index:0 type:kotlin.Any flags: + BLOCK_BODY + TYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit + typeOperand: CLASS IR_EXTERNAL_DECLARATION_STUB OBJECT name:Unit modality:FINAL visibility:public flags: + TYPE_OP type=T origin=CAST typeOperand=T + typeOperand: TYPE_PARAMETER name:T index:0 variance: upperBounds:[kotlin.Any?] + GET_VAR 'value-parameter arg: Any' type=kotlin.Any origin=null + CALL 'useT(T): Unit' type=kotlin.Unit origin=null + $this: GET_VAR 'this@Bar: Bar' type=Bar origin=null + t: TYPE_OP type=T origin=IMPLICIT_CAST typeOperand=T + typeOperand: TYPE_PARAMETER name:T index:0 variance: upperBounds:[kotlin.Any?] + GET_VAR 'value-parameter arg: Any' type=kotlin.Any origin=null + FUN name:useT visibility:public modality:FINAL <> ($this:Bar, t:T) returnType:Unit flags: + $this: VALUE_PARAMETER name: type:Bar flags: + VALUE_PARAMETER name:t index:0 type:T flags: + BLOCK_BODY + FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:Boolean flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:Boolean flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + VALUE_PARAMETER name:other index:0 type:kotlin.Any? flags: + FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:Int flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:Int flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: + FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:String flags: + overridden: + FUN IR_EXTERNAL_DECLARATION_STUB name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:String flags: + $this: VALUE_PARAMETER name: type:kotlin.Any flags: