mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-24 00:21:28 +00:00
FIR: adjust type of integer operator call as property initializer
This commit is contained in:
committed by
Dmitriy Novozhilov
parent
ddd26de139
commit
30b91b128a
@@ -54,7 +54,24 @@ class IntegerLiteralTypeApproximationTransformer(
|
||||
}
|
||||
|
||||
override fun transformFunctionCall(functionCall: FirFunctionCall, data: ConeKotlinType?): CompositeTransformResult<FirStatement> {
|
||||
val operator = functionCall.toResolvedCallableSymbol()?.fir as? FirIntegerOperator ?: return functionCall.compose()
|
||||
val operator = functionCall.toResolvedCallableSymbol()?.fir as? FirIntegerOperator
|
||||
if (operator == null) {
|
||||
if (functionCall is FirIntegerOperatorCall) {
|
||||
// functionCall _was_ a named call whose candidate symbol was an integer operator, but has been transformed to an integer
|
||||
// operator call (by [IntegerOperatorsTypeUpdater]). So, technically, this _was_ a call that this transformer was looking
|
||||
// for, i.e., a call with ILT, but in a resolved form already. Here we just adapt to the expected type if any.
|
||||
//
|
||||
// Note that such inequality can happen to the resolved call, since the call completer doesn't complete the call with the
|
||||
// given, expected type: see [FirCallCompleter#completeCall]. One reason _not_ to propagate the expected type to the call
|
||||
// completing transformation is to handle integer overflow naturally. E.g., if a property with Long, a bigger type, is
|
||||
// intentionally set with an integer operator that overflows, knowing the expected type will hide the overflow. Rather, we
|
||||
// have a second chance here to sort of wrap such overflowed integer with type conversion, like `n.toLong()`.
|
||||
data?.let {
|
||||
functionCall.resultType = functionCall.resultType.resolvedTypeFromPrototype(it)
|
||||
}
|
||||
}
|
||||
return functionCall.compose()
|
||||
}
|
||||
functionCall.transformChildren(this, data)
|
||||
val argumentType = functionCall.arguments.firstOrNull()?.resultType?.coneTypeUnsafe<ConeClassLikeType>()
|
||||
val receiverClassId = functionCall.dispatchReceiver.typeRef.coneTypeUnsafe<ConeClassLikeType>().lookupTag.classId
|
||||
|
||||
@@ -133,7 +133,7 @@ open class FirDeclarationsResolveTransformer(transformer: FirBodyResolveTransfor
|
||||
withLocalScopeCleanup {
|
||||
addLocalScope(context.getPrimaryConstructorParametersScope())
|
||||
property.transformChildrenWithoutAccessors(returnTypeRef)
|
||||
property.transformInitializer(integerLiteralTypeApproximator, null)
|
||||
property.transformInitializer(integerLiteralTypeApproximator, property.returnTypeRef.coneTypeSafe())
|
||||
}
|
||||
if (property.initializer != null) {
|
||||
storeVariableReturnType(property)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// TARGET_BACKEND: JVM
|
||||
|
||||
// WITH_RUNTIME
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// TARGET_BACKEND: JVM
|
||||
|
||||
// WITH_RUNTIME
|
||||
|
||||
@@ -29,7 +29,7 @@ FILE fqName:<root> fileName:/primitivesImplicitConversions.kt
|
||||
PROPERTY name:test4 visibility:public modality:FINAL [val]
|
||||
FIELD PROPERTY_BACKING_FIELD name:test4 type:kotlin.Long visibility:private [final,static]
|
||||
EXPRESSION_BODY
|
||||
CALL 'public final fun unaryMinus (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null
|
||||
CALL 'public final fun unaryMinus (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Long origin=null
|
||||
$this: CONST Int type=kotlin.Int value=42
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test4> visibility:public modality:FINAL <> () returnType:kotlin.Long
|
||||
correspondingProperty: PROPERTY name:test4 visibility:public modality:FINAL [val]
|
||||
@@ -39,7 +39,7 @@ FILE fqName:<root> fileName:/primitivesImplicitConversions.kt
|
||||
PROPERTY name:test5 visibility:public modality:FINAL [val]
|
||||
FIELD PROPERTY_BACKING_FIELD name:test5 type:kotlin.Short visibility:private [final,static]
|
||||
EXPRESSION_BODY
|
||||
CALL 'public final fun unaryMinus (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null
|
||||
CALL 'public final fun unaryMinus (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Short origin=null
|
||||
$this: CONST Int type=kotlin.Int value=42
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test5> visibility:public modality:FINAL <> () returnType:kotlin.Short
|
||||
correspondingProperty: PROPERTY name:test5 visibility:public modality:FINAL [val]
|
||||
@@ -49,7 +49,7 @@ FILE fqName:<root> fileName:/primitivesImplicitConversions.kt
|
||||
PROPERTY name:test6 visibility:public modality:FINAL [val]
|
||||
FIELD PROPERTY_BACKING_FIELD name:test6 type:kotlin.Byte visibility:private [final,static]
|
||||
EXPRESSION_BODY
|
||||
CALL 'public final fun unaryMinus (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null
|
||||
CALL 'public final fun unaryMinus (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Byte origin=null
|
||||
$this: CONST Int type=kotlin.Int value=42
|
||||
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-test6> visibility:public modality:FINAL <> () returnType:kotlin.Byte
|
||||
correspondingProperty: PROPERTY name:test6 visibility:public modality:FINAL [val]
|
||||
|
||||
Reference in New Issue
Block a user