From 061734d84b4f8b1c35ee8920f12183f45ad408d6 Mon Sep 17 00:00:00 2001 From: Dmitrii Gridin Date: Thu, 7 Dec 2023 18:03:28 +0100 Subject: [PATCH] [FIR] do not transform type annotations from function parameter during implicit type phase We shouldn't treat them as local and transform during the implicit type phase as they are visible from outside the function ^KT-64164 Fixed --- .../LLFirImplicitTypesLazyResolver.kt | 16 ++++- .../parameterWithAnnotations.after.txt | 6 +- .../parameterWithAnnotations.before.txt | 6 +- ...ameterWithAnnotationsBodyResolve.after.txt | 6 +- ...meterWithAnnotationsBodyResolve.before.txt | 6 +- .../parameterWithAnnotationsScript.after.txt | 7 ++- .../parameterWithAnnotationsScript.before.txt | 7 ++- ...WithAnnotationsScriptBodyResolve.after.txt | 7 ++- ...ithAnnotationsScriptBodyResolve.before.txt | 7 ++- .../delegateWithExplicitType.after.txt | 6 +- .../delegateWithExplicitType.before.txt | 6 +- .../delegateWithExplicitTypeScript.after.txt | 6 +- .../delegateWithExplicitTypeScript.before.txt | 6 +- ...egateWithExplicitTypeUnavailable.after.txt | 6 +- ...gateWithExplicitTypeUnavailable.before.txt | 6 +- ...ithExplicitTypeUnavailableScript.after.txt | 6 +- ...thExplicitTypeUnavailableScript.before.txt | 6 +- .../delegateWithImplicitType.after.txt | 6 +- .../delegateWithImplicitType.before.txt | 6 +- .../delegateWithImplicitTypeScript.after.txt | 6 +- .../delegateWithImplicitTypeScript.before.txt | 6 +- ...egateWithImplicitTypeUnavailable.after.txt | 6 +- ...gateWithImplicitTypeUnavailable.before.txt | 6 +- ...ithImplicitTypeUnavailableScript.after.txt | 6 +- ...thImplicitTypeUnavailableScript.before.txt | 6 +- .../errorType/errorType.after.txt | 4 +- .../errorType/errorType.before.txt | 4 +- .../errorType/errorTypeScript.after.txt | 4 +- .../errorType/errorTypeScript.before.txt | 4 +- .../errorType/nestedErrorReturnType.after.txt | 4 +- .../nestedErrorReturnType.before.txt | 4 +- .../nestedErrorReturnTypeScript.after.txt | 4 +- .../nestedErrorReturnTypeScript.before.txt | 4 +- .../function/explicitType.after.txt | 6 +- .../function/explicitType.before.txt | 6 +- .../function/explicitTypeScript.after.txt | 6 +- .../function/explicitTypeScript.before.txt | 6 +- .../function/localFunction.after.txt | 6 +- .../function/localFunction.before.txt | 6 +- .../parameterAsImplicitReturnType.after.txt | 12 ++-- .../parameterAsImplicitReturnType.before.txt | 40 ++++++------- ...ameterAsImplicitReturnType.lazyResolve.txt | 16 ++--- ...rAsImplicitReturnTypeBodyResolve.after.txt | 12 ++-- ...AsImplicitReturnTypeBodyResolve.before.txt | 12 ++-- ...licitReturnTypeBodyResolve.lazyResolve.txt | 16 ++--- ...rAsImplicitReturnTypePropagation.after.txt | 6 +- ...AsImplicitReturnTypePropagation.before.txt | 30 +++++----- ...licitReturnTypePropagation.lazyResolve.txt | 18 +++--- ...licitReturnTypePropagationScript.after.txt | 7 ++- ...icitReturnTypePropagationScript.before.txt | 31 +++++----- ...eturnTypePropagationScript.lazyResolve.txt | 19 +++--- ...ameterAsImplicitReturnTypeScript.after.txt | 13 ++-- ...meterAsImplicitReturnTypeScript.before.txt | 41 ++++++------- ...AsImplicitReturnTypeScript.lazyResolve.txt | 17 +++--- ...licitReturnTypeScriptBodyResolve.after.txt | 13 ++-- ...icitReturnTypeScriptBodyResolve.before.txt | 13 ++-- ...eturnTypeScriptBodyResolve.lazyResolve.txt | 17 +++--- .../typeParameterAnnotations.after.txt | 6 +- .../typeParameterAnnotations.before.txt | 6 +- ...ParameterAnnotationsInLocalClass.after.txt | 6 +- ...arameterAnnotationsInLocalClass.before.txt | 6 +- ...terAnnotationsInLocalClassScript.after.txt | 6 +- ...erAnnotationsInLocalClassScript.before.txt | 6 +- .../substitutionOverride/function.after.txt | 6 +- .../substitutionOverride/function.before.txt | 6 +- .../functionScript.after.txt | 6 +- .../functionScript.before.txt | 6 +- .../functionUnavailable.after.txt | 6 +- .../functionUnavailable.before.txt | 6 +- .../functionUnavailableScript.after.txt | 6 +- .../functionUnavailableScript.before.txt | 6 +- .../implicitFunction.after.txt | 6 +- .../implicitFunction.before.txt | 6 +- .../implicitFunctionScript.after.txt | 6 +- .../implicitFunctionScript.before.txt | 6 +- .../implicitFunctionUnavailable.after.txt | 6 +- .../implicitFunctionUnavailable.before.txt | 6 +- ...mplicitFunctionUnavailableScript.after.txt | 6 +- ...plicitFunctionUnavailableScript.before.txt | 6 +- ...rAndSetterWithExplicitReturnType.after.txt | 4 +- ...AndSetterWithExplicitReturnType.before.txt | 4 +- .../typeAlias/insideFunctionComplex.after.txt | 6 +- .../insideFunctionComplex.before.txt | 6 +- .../insideFunctionComplexScript.after.txt | 6 +- .../insideFunctionComplexScript.before.txt | 6 +- .../LightTreeRawFirDeclarationBuilder.kt | 17 ++++-- .../fir/lightTree/fir/ValueParameter.kt | 3 +- .../kotlin/fir/builder/PsiRawFirBuilder.kt | 37 ++++++------ .../annotation.annotationOwners.txt | 16 ++--- .../annotationOnField.annotationOwners.txt | 2 +- ...bleParenthesizedTypes.annotationOwners.txt | 4 +- ...sOnParenthesizedTypes.annotationOwners.txt | 4 +- ...ructorWithAnnotations.annotationOwners.txt | 6 +- ...egatesWithAnnotations.annotationOwners.txt | 2 +- ...nctionWithAnnotations.annotationOwners.txt | 8 +-- .../localAnnotations.annotationOwners.txt | 60 +++++++++---------- .../builder/AbstractRawFirBuilderTestCase.kt | 9 ++- 97 files changed, 469 insertions(+), 428 deletions(-) diff --git a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirImplicitTypesLazyResolver.kt b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirImplicitTypesLazyResolver.kt index de4d3e63f89..e44c13e0f1e 100644 --- a/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirImplicitTypesLazyResolver.kt +++ b/analysis/low-level-api-fir/src/org/jetbrains/kotlin/analysis/low/level/api/fir/transformers/LLFirImplicitTypesLazyResolver.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.fir.resolve.transformers.body.resolve.ImplicitBodyRe import org.jetbrains.kotlin.fir.scopes.callableCopySubstitutionForTypeUpdater import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirValueParameterSymbol import org.jetbrains.kotlin.fir.types.FirImplicitTypeRef import org.jetbrains.kotlin.fir.util.setMultimapOf import org.jetbrains.kotlin.fir.utils.exceptions.withFirSymbolEntry @@ -83,16 +84,25 @@ internal class LLImplicitBodyResolveComputationSession : ImplicitBodyResolveComp * @see postponedSymbols */ fun postponeForeignAnnotationResolution(symbol: FirBasedSymbol<*>) { + // We should unwrap local symbols to avoid recursion + val symbolToPostpone = when (symbol) { + is FirValueParameterSymbol -> symbol.containingFunctionSymbol + else -> symbol + } + // We cannot resolve them on demand, so we shouldn't postpone them - if (symbol.cannotResolveAnnotationsOnDemand()) { + if (symbolToPostpone.cannotResolveAnnotationsOnDemand()) { return } val currentSymbol = anchorForForeignAnnotations ?: errorWithAttachment("Unexpected state: the current symbol have to be here") { - withFirSymbolEntry("symbol to postpone", symbol) + withFirSymbolEntry("symbol to postpone", symbolToPostpone) } - postponedSymbols.put(currentSymbol, symbol) + // There is no sense to postpone itself as it will lead to recursion + if (currentSymbol == symbolToPostpone) return + + postponedSymbols.put(currentSymbol, symbolToPostpone) } /** diff --git a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.after.txt b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.after.txt index 029a687e844..56f58361a38 100644 --- a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.after.txt +++ b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.after.txt @@ -1,14 +1,14 @@ @R|Anno|(position = (String(parameter type ), R|/prop|)) kotlin/collections/List<@R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String>> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] FILE: [ResolvedTo(IMPORTS)] parameterWithAnnotations.kt @R|kotlin/annotation/Target|[Types](Q|kotlin/annotation/AnnotationTarget|.R|kotlin/annotation/AnnotationTarget.TYPE|) public final [ResolvedTo(STATUS)] annotation class Anno : R|kotlin/Annotation| { diff --git a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.before.txt b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.before.txt index 46b15a5ba7d..7887ff038ad 100644 --- a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.before.txt +++ b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotations.before.txt @@ -1,14 +1,14 @@ @R|Anno|((String(parameter type ), prop#)) kotlin/collections/List<@R|Anno|((String(nested parameter type ), prop#)) kotlin/collections/Collection<@R|Anno|((String(nested nested parameter type ), prop#)) kotlin/String>> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|((String(nested parameter type ), prop#)) kotlin/collections/Collection<@R|Anno|((String(nested nested parameter type ), prop#)) kotlin/String> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|((String(nested nested parameter type ), prop#)) kotlin/String context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] FILE: [ResolvedTo(IMPORTS)] parameterWithAnnotations.kt @R|kotlin/annotation/Target|[CompilerRequiredAnnotations](Q|kotlin/annotation/AnnotationTarget|.R|kotlin/annotation/AnnotationTarget.TYPE|) public? final? [ResolvedTo(COMPILER_REQUIRED_ANNOTATIONS)] annotation class Anno : R|kotlin/Annotation| { diff --git a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.after.txt b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.after.txt index ecceec50e62..f0857f6c02f 100644 --- a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.after.txt +++ b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.after.txt @@ -1,14 +1,14 @@ @R|Anno|(position = (String(parameter type ), R|/prop|)) kotlin/collections/List<@R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String>> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(anonymousFunction return type ), R|/prop|)) kotlin/Int context -> FirAnonymousFunctionSymbol special/anonymous diff --git a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.before.txt b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.before.txt index ecceec50e62..f0857f6c02f 100644 --- a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.before.txt +++ b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsBodyResolve.before.txt @@ -1,14 +1,14 @@ @R|Anno|(position = (String(parameter type ), R|/prop|)) kotlin/collections/List<@R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String>> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(anonymousFunction return type ), R|/prop|)) kotlin/Int context -> FirAnonymousFunctionSymbol special/anonymous diff --git a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsScript.after.txt b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsScript.after.txt index a8ceeca0e32..ac03363d5ab 100644 --- a/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsScript.after.txt +++ b/analysis/low-level-api-fir/testData/lazyResolveTypeAnnotations/constructor/parameterWithAnnotationsScript.after.txt @@ -1,14 +1,14 @@ @R|Anno|(position = (String(parameter type ), R|/prop|)) kotlin/collections/List<@R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String>> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested parameter type ), R|/prop|)) kotlin/collections/Collection<@R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String> context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] @R|Anno|(position = (String(nested nested parameter type ), R|/prop|)) kotlin/String context -> FirValueParameterSymbol /param from FirConstructorSymbol /A.A - anchor -> [FirConstructorSymbol /A.A] + anchor -> [FirValueParameterSymbol /param from FirConstructorSymbol /A.A] FILE: [ResolvedTo(IMPORTS)] parameterWithAnnotationsScript.kts context(