Commit Graph

764 Commits

Author SHA1 Message Date
Ilya Kirillov 0089517b25 [Analysis API FIR] optimize KtFirCompletionCandidateChecker.checkExtensionFitsCandidate
We do not need running IMPLICIT_TYPES_BODY_RESOLVE to check candidate applicability.

Additional resolve to IMPLICIT_TYPES_BODY_RESOLVE takes about 5% of completion time

^KTIJ-24640
2023-02-27 09:40:41 +00:00
Jiaxiang Chen 7ce2f64c18 AA: apply java type enhancement to declaredMemberScope.
* added getDeclaredMemberScope to JavaScopeProvider.
2023-02-24 19:57:10 +01:00
Jinseong Jeon 5455942859 AA: handle underscore as type arguments
^KTIJ-24742 Fixed
2023-02-23 13:02:34 +01:00
Pavel Mikhailovskii 7700484a16 [AA] Fix conversion of annotation values 2023-02-22 13:55:50 +00:00
Ilya Kirillov 803c3b88ac [Analysis API] fix exception on symbol restore when symbol cannot be seen from the use-site module
^KT-56763 fixed
2023-02-20 11:01:32 +00:00
Dmitriy Novozhilov e70e85d51a [FIR] Move unwrapSmartcastExpression util into :compiler:fir:tree module 2023-02-17 12:29:17 +00:00
Ilya Chernikov 27db8ce1bb FIR LT: extract syntax diagnostic info from error element
#KT-56701 fixed
2023-02-16 10:21:04 +00:00
Roman Efremov d120d83d60 Fix navigation in IDE to synthetic methods in compiled Kotlin
For some synthetic methods from compiled Kotlin classes, it is
not possible to find the PsiElement, when navigating to declaration
in the IDE.

For other methods it works like this:
1. There is a SourceElement with a psi inside
2. Or for K1, the element is searched in the decompiled text
   (see ByDescriptorIndexer),
3. Or for K2, the element is searched by Stub taken from metadata
   (see FirDeserializedDeclarationSourceProvider)

However, these approaches do not apply to synthetic methods which have
no SourceElement and are not written into decompiled text and metadata.

These are methods values, valueOf, entries from enum and
copy, equals, hashCode, toString from data classes.

Therefore, it was decided to handle their cases separately
at the resolve stage.

Tests (in idea repository):
- org.jetbrains.kotlin.idea.resolve.ReferenceResolveWithLibTestGenerated#testDataClassSyntheticMethods
- org.jetbrains.kotlin.idea.resolve.ReferenceResolveWithLibTestGenerated#testEnumSyntheticMethods
- org.jetbrains.kotlin.idea.fir.resolve.FirReferenceResolveWithLibTestGenerated#testDataClassSyntheticMethods
- org.jetbrains.kotlin.idea.fir.resolve.FirReferenceResolveWithLibTestGenerated#testEnumSyntheticMethods

^KTIJ-24413 Fixed
2023-02-15 19:56:08 +00:00
Justin Paupore fa517180b7 [AA-FIR] Add support for constant evaluation of string templates.
Support FirStringConcatenationCall in FirCompileTimeConstantEvaluator.
This allows string templates ("foo${bar}") to be evaluated as constants,
assuming the interpolated expressions are themselves constant.

In addition, fixes some handling bugs with KtConstantEvaluationMode,
where some expressions that are not valid in a `const val` declaration
were being supported for `CONSTANT_EXPRESSION_EVALUATION`, including
non-static final Java fields in FIR, and composite expressions of
non-const properties in FE1.0.
2023-02-15 16:45:22 +01:00
Denis.Zharkov e46ef0bbec Fix FIR bootstrap caused by KT-55281
In this branch it's necessary, because we started reporting diagnostics
that have been ignored before
2023-02-15 08:13:51 +00:00
Roman Golyshev b23aabf6e0 [Analysis API] KTIJ-24610 Ignore qualifiers with fake source in KtFirImportOptimizer
Such qualifiers can appear for extensions imported from objects

^KTIJ-24610 Fixed
2023-02-14 16:08:40 +00:00
Kirill Rakhman 314784f435 [FIR] Introduce FirDeclarationStatus.hasStableParameterNames
This flag is true by default but is set to false for

- Java methods and constructors
- interface delegation methods that delegate to Java

The NAMED_ARGUMENTS_NOT_ALLOWED logic is mostly refactored to use the
new flag though some custom logic remains for determining the correct
message and to work around a corner case with fake overrides.

The flag is (de)serialized from/to metadata. For backward compatibility
with K1, delegated methods to Java types are deserialized as stable.

^KT-40480 Fixed
2023-02-13 12:09:39 +00:00
Marco Pennekamp fb43e53ca3 [LL FIR] KTIJ-24574 Fix containing declaration finder for init blocks
- KTIJ-24574 occurred because a local destructuring declaration was
  erroneously returned as the non-local containing declaration of an
  element by `getNonLocalContainingOrThisDeclaration`. This occurred in
  `init` blocks.

KTIJ-24574 fixed
2023-02-13 10:38:26 +00:00
Ilya Kirillov 1f834aa99c [Analysis API FIR] avoid redundant resolve in renderer
Before we resolved all declarations to body resolve.
It was rarely needed and badly affects performance.
If the resolution will still be needed, it will be lazily performed by KtSymbol
2023-02-13 09:53:33 +00:00
Dmitriy Novozhilov a9248569a6 [FIR] Fix reporting of UNINITIALIZED_ENUM_ENTRY in init blocks
^KT-41126 Fixed
2023-02-10 12:30:12 +00:00
Jinseong Jeon 88b07f5287 SLC: keep annotations on type when converting to PsiType
^KT-55815 Fixed
2023-02-09 19:53:45 +01:00
Roman Golyshev ac8d5a0ea8 [Analysis API] KTIJ-24527 Properly handle typealiased functional types
Use expanded ConeTypes to get correct parameters and return types

Also, fix the order of rendering modifiers in `KtFunctionalTypeRenderer`

^KTIJ-24527 Fixed
2023-02-09 12:51:23 +00:00
Ting-Yuan Huang 42b08d411b [FIR] Pass container when creating FunctionType
so that it can be resolved when type annotations are demanded.
2023-02-08 19:43:24 +00:00
Dmitrii Gridin 8f34b6599a [AA FIR] BuilderCache: replace regular map with concurrent to avoid exceptions
Example:
```stacktrace
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 32 out of bounds for length 17
  at com.intellij.util.containers.RefHashMap$MyMap.rehash(RefHashMap.java:85)
  at it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap.insert(Object2ObjectOpenHashMap.java:251)
  at it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap.put(Object2ObjectOpenHashMap.java:259)
  at com.intellij.util.containers.RefHashMap.putKey(RefHashMap.java:160)
  at com.intellij.util.containers.RefKeyRefValueHashMap.put(RefKeyRefValueHashMap.java:55)
  at org.jetbrains.kotlin.analysis.api.fir.KtSymbolByFirBuilder$FunctionLikeSymbolBuilder.buildFunctionSymbol(KtSymbolByFirBuilder.kt:727)
  at org.jetbrains.kotlin.analysis.api.fir.scopes.FirScopeUtilsKt$getCallableSymbols$1$1$1$1.invoke(firScopeUtils.kt:25)
  at org.jetbrains.kotlin.analysis.api.fir.scopes.FirScopeUtilsKt$getCallableSymbols$1$1$1$1.invoke(firScopeUtils.kt:24)
  at org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScopeImpl.processFunctionsByName(FirClassDeclaredMemberScope.kt:103)
  ```

^KTIJ-22749
2023-02-08 20:12:12 +01:00
Jaebaek Seo a09d0aa1cf Handle SHORTEN_IF_ALEADY_IMPORTED case of KtFirReferenceShortener
For the following example, when we run the reference shortener, it
drops `a.b.c` qualifier, because it matches "FOURTH".
```
package a.b.c

fun <T, E, D> foo(a: T, b: E, c: D) = a.hashCode() + b.hashCode() + c.hashCode() // FIRST
fun <E> E.foo() = hashCode() // SECOND

object Receiver {
    fun <T, E, D> foo(a: T, b: E, c: D) = a.hashCode() + b.hashCode() + c.hashCode() // THIRD
    fun foo(a: Int, b: Boolean, c: String) = a.hashCode() + b.hashCode() + c.hashCode() // FOURTH
    fun test(): Int {
        fun foo(a: Int, b: Boolean, c: Int) = a + b.hashCode() + c // FIFTH
        return <expr>a.b.c.foo(1, false, "bar")</expr>
    }
}
```

As shown in the above example, when SHORTEN_IF_ALEADY_IMPORTED option is
given from a user, the reference shortener has to check whether it can
drop the qualifier without changing the referenced symbol and if it is
possible to do that without adding a new import directive, it deletes
the qualifier.

It needs two steps:
 1. Collect all candidate symbols matching the signature e.g., function
    arguments / type arguments
 2. Determine whether the referenced symbol has the highest reference
    priority when we drops the qualifier depending on scopes

This commit uses `AllCandidatesResolver(shorteningContext.analysisSession.useSiteSession).
getAllCandidates( .. fake FIR call/property-access ..)` for step1.
For step2, we use a heuristic based on scopes of candidates. If a
candidate symbol is under the same scope with the target expression, it
has a `FirLocalScope` which has the high priority. So when we have a
candidate under a `FirLocalScope` and the actual referenced symbol is
different from the candidate, we must avoid dropping its qualifier
because the shortening will change its semantics i.e., reference.

The order of scopes depending on their scope types is:
 1. FirLocalScope
 2. FirClassUseSiteMemberScope / FirNestedClassifierScope
 3. FirExplicitSimpleImportingScope
 4. FirPackageMemberScope
 5. others

Note that for "others" the above rule can be wrong. Please update it if
you find other scopes that have a priority higher than the specified
scopes.

One of non-trivial parts is the priority among multiple
FirClassUseSiteMemberScope and FirNestedClassifierScope. They are
basically scopes for class declarations. We decide their priorities
based on the distance of class declaration from the target expression.

Note that we take a strict approach to reject all false positive. For
example, when we are not sure, we don't shorten it to avoid changing its
semantics.

TODO: One corner case is handling receivers. We have to update
```
private fun shortenIfAlreadyImported(
    firQualifiedAccess: FirQualifiedAccess,
    calledSymbol: FirCallableSymbol<*>,
    expressionInScope: KtExpression,
): Boolean
```

The current implementation cannot handle the following example:
```
package foo
class Foo {
    fun test() {
        // It references FIRST. Removing `foo` lets it reference SECOND.
        <caret>foo.myRun {
            42
        }
    }
}
inline fun <R> myRun(block: () -> R): R = block()         // FIRST
inline fun <T, R> T.myRun(block: T.() -> R): R = block()  // SECOND
```

Tests related to TODO:
 - analysis/analysis-api/testData/components/referenceShortener/referenceShortener/receiver2.kt
 - analysis/analysis-api/testData/components/referenceShortener/referenceShortener/receiver3.kt
2023-02-08 18:39:12 +00:00
Jaebaek Seo 860dee2cb1 Fix reference resolver bug for companion object whose name is the same as class
FirReferenceResolveHelper internally checks whether the referenced class
id matches the qualifed access or not. If they do not match, it reports
an error. When the companion object has the same name as the class,
resolving a qualified expression access to a member of the companion
object causes an error because of the mismatch e.g.,

```
package my.sample

class Test {
    fun a() {
        my.sample.<caret>Test.say()
    }

    companion object Test {
        fun say() {}
    }
}
```

This commit fixes the issue.

TODO: When the companion object has a name difference from class, it
does not report an error but the resolution result is wrong in FIR. See
KT-56167.

---

Commentary from rebaser: the issue mentioned in this code is
fixed in 71a368e06e, so the actual
fix is omitted, and only test data is preserved
2023-02-08 18:39:11 +00:00
Ilya Kirillov 69e41e19e7 [Analysis API] fix possible leaks of FirDeclarations from cache keys in KtSymbolByFirBuilder
If some FirDeclarations is inaccessible then there is no reason to store KtSymbol for it

^KTIJ-22749
2023-02-07 17:57:38 +00:00
Ilya Kirillov f5e5a134e7 [Analysis API] remove caches from KtSymbolByFirBuilder with very low hit rates
^KTIJ-22749
2023-02-07 17:57:38 +00:00
Ilya Kirillov a587913225 [Analysis API] avoid having separate versions of KtFirScopeProvider for each thread
Now KtFirScopeProvider do not contain caches, so it can be reused between the threads. The only thread-local thing it had, was a ScopeSession. Now it's not stored in the KtFirScopeProvider
2023-02-07 17:57:37 +00:00
Ilya Kirillov 489ffc5acb [Analysis API] remove caches from KtFirScopeProvider
The hit-rate for this cache seems to be very law and it causes additional memory consumption

^KTIJ-22749
2023-02-07 17:57:37 +00:00
Dmitriy Novozhilov f479a56f5e [FIR] Fix reporting of CANNOT_INFER_PARAMETER_TYPE on lambda parameters
^KT-56138 Fixed
2023-02-06 08:09:57 +00:00
Dmitrii Gridin 379044f2c8 [AA] KtAnnotationApplication: make index nullable
^KT-56046
2023-02-03 19:49:08 +00:00
Dmitrii Gridin 38575cdbc1 [FIR] FirBasedSymbol: iterate annotations by index instead of iterator to avoid possible ConcurrentModificationException
^KT-56046
2023-02-03 19:49:05 +00:00
Dmitrii Gridin 93232a23df [AA] introduce AnnotationUseSiteTargetFilter to simplify API
^KT-56046
2023-02-03 19:49:01 +00:00
Dmitrii Gridin 6e25b45f7d [AA] introduce KtAnnotationApplication
^KT-56046
2023-02-03 19:49:01 +00:00
Dmitrii Gridin 3a7602a38f [AA] KtAnnotationsList: introduce 'annotationOverviews'
it provides a solution for several problems with redundant resolve.
It is a more powerful tool than 'annotationClassIds'

^KT-56046
2023-02-03 19:48:57 +00:00
Dmitrii Gridin d13ad454da [AA FIR] fix nested type annotations lazy resolve
^KT-56046
2023-02-03 19:48:56 +00:00
Dmitrii Gridin 2c7fb49a80 [AA FIR] annotationClassIds should return fully-expanded classIds
^KT-56046
2023-02-03 19:48:53 +00:00
Dmitrii Gridin 4b955944ff [AA] KtAnnotationApplication: add 'index' parameter
it is required for lazy annotations resolve

^KT-56046
2023-02-03 19:48:47 +00:00
Dmitrii Gridin 886c5b7412 [AA FIR] avoid 'is resolved' check for annotation calls without arguments
^KT-56046
2023-02-03 19:48:44 +00:00
Dmitrii Gridin 77609c2223 [AA FIR] avoid redundant resolve by annotations in IDE mode
^KT-56046
2023-02-03 19:48:43 +00:00
Ilya Kirillov 3772d60ca6 [Analysis API] optimize RAM consumption for some KtFirSymbol's
Remove psi caching for local declarations as it's fast to get the psi for those.

Local declarations cannot be from the library, so we do not need to search for deserialized psi for those.

^KTIJ-22749
2023-02-03 14:29:54 +00:00
Ilya Kirillov 3d3ab5d379 [Analysis API] optimize RAM consumption in KtFirSymbol
- remove extra field which can be retrieved via KtFirAnalysisSession

^KTIJ-22749
2023-02-03 14:29:54 +00:00
Ilya Kirillov a9b4f77196 [Analysis API] optimize RAM consumption in KtLifetimeOwner's
- remove `token` field

^KTIJ-22749
2023-02-03 14:29:53 +00:00
Ilya Kirillov e0c23dae47 [Analysis API] optimize RAM consumption in KtTypes
- remove cache allocation for cases when the value is fast to compute

^KTIJ-22749
2023-02-03 14:29:53 +00:00
Ilya Kirillov 2e5d284472 [Analysis API] optimize RAM consumption in ValidityAwareCachedValue
- make it inline class
- remove non-required `token` field

^KTIJ-22749
2023-02-03 14:29:52 +00:00
Marco Pennekamp 5790a32436 [AA] Remove duplicate withValidityAssertion calls 2023-02-03 10:24:05 +00:00
Ilya Kirillov 8b286577c7 [Analysis API] do not throw exception when KtCall is improperly resolved
Log the exception instead. This way the analysis of the file will not be stuck after finding an improperly resolved call
2023-02-03 07:06:07 +00:00
Dmitriy Novozhilov 89c42e20c9 [FIR] Consistently use _function_ instead of _functional_ in names of classes and functions 2023-02-02 08:24:52 +00:00
Dmitriy Novozhilov b48751ec9c [FIR] Add ability to extract functional type kind from annotation on functional literal type 2023-02-02 08:24:51 +00:00
Dmitriy Novozhilov d1b797ed97 [FIR] Generify resolution and inference of arguments of functional types 2023-02-02 08:24:50 +00:00
Dmitriy Novozhilov 67aa80562d [FE] Completely replace FunctionClassKind with FunctionalTypeKind
FunctionalTypeKind can be used in FE 1.0 too, so there is no need to
  keep both classes. Also, removal of FunctionClassKind simplifies work
  with FunctionalTypeKind in common code, like Analysis Api
2023-02-02 08:24:50 +00:00
Dmitriy Novozhilov c86495dcae [FIR] Introduce ConeFunctionalTypeKind as a replacement of FunctionClassKind
This is needed to provide an ability to extend different kinds of
  functional types

Also, cleanup and rename utilities related to functional types to avoid
  possible confusions
2023-02-02 08:24:49 +00:00
Dmitriy Novozhilov c98c8d3682 [FIR] Introduce utility searching for classifier symbol by ClassId
There were places which created lookupTag from classId just to call
  `lookupTag.toSymbol(session)`
2023-02-02 08:24:48 +00:00
Dmitriy Novozhilov 125b773205 [FIR] Introduce utility for creating ConeClassLikeLookupTagImpl from ClassId
`classId.toLookupTag()` looks much cleaner than `ConeClassLikeLookupTagImpl(classId)`
2023-02-02 08:24:48 +00:00