551 Commits

Author SHA1 Message Date
Marco Pennekamp a9d7b0c595 [AA Standalone] Consider type aliases in direct inheritors search
A type alias may still be inherited from. For example:

```
sealed class MyClass

typealias T = MyClass

class Inheritor : T() // `Inheritor` is a direct inheritor of `MyClass`.
```

The index is a simplified version of the IDE's
`KotlinTypeAliasByExpansionShortNameIndex`, but it should be sufficient
for virtually all cases.

^KT-66013
2024-03-18 21:14:36 +00:00
Marco Pennekamp ea2bb32bc0 [PSI] Don't calculate ClassIds for local classes in call elements
- The fix uses relevant logic from  `getNonLocalContainingDeclaration`.
- The annotation entry case is covered by `KtCallElement` because
  `KtAnnotationEntry` is a subtype of it.
- KT-66038 is fixed by this because the static declaration provider only
  indexes non-local classes.

^KT-66038 fixed
2024-02-26 16:36:00 +00:00
Yan Zhulanow 1c1da6bced [Analysis API] Cause OOB when imports change in code fragments
In K1, code fragment analysis was completely invalidated on any PSI
change. Because imports are not a part of the PSI tree of
'KtCodeFragment's, a colon tremble happened on 'addImportsFromString()'.

In K2, changes inside code fragments are always considered in-body
modifications. So, even with the colon trembling, the 'FirFile',
together with its 'FirImport's was not recreated.

^KT-65600 Fixed
2024-02-26 11:28:56 +00:00
Yan Zhulanow 45c0fa8d23 [PSI] Refactor import handling in code fragments
Now there is a single place that performs import modification, as well
as reports events related to that modification.
2024-02-26 11:28:55 +00:00
Roman Golyshev 47aa6392bf KT-64808 [stubs] Materialize all synthethic declarations in Kotlin Stubs
Here is the reasoning behind that change:

Historically, all the declarations generated by compiler plugins
were marked with `SYNTHESIZED` member kind in Kotlin Metadata
by K1 compiler.

In K1 IDE, descriptors were deserialized directly from the Kotlin
Metadata, and they saw all the generated declarations from it.

In the stubs, however, such declarations were not materialized at all.
It caused no troubles, since K1 IDE relied on descriptors to get the
essential resolution information in completion and other subsystems.
So, the resolution of members from jars processed
by compiler plugins (e.g. `kotlinx-serialization-json`) was mostly fine.

In K2 IDE, however, we use stubs to "deserialize" FIR declarations from
them, to later create `KtSymbol`s upon that FIR.

If we see a library file which was processed by compiler plugins, we
build stubs for it based on the Kotlin Metadata, and then use stubs to
create FIR.
But if stubs do not contain information about the generated
declarations,
then the resulting FIR will also not contain such declarations.

In the end, the K2 IDE would also be blind to such declarations, since
there is no other way to retrieve them from the library jar.

By meterializing all the synthethic declarations in stubs (with some
minor exceptions for data classes), we would avoid such problems,
and the resolve of such declarations from compiler jars in
K2 IDE would become possible.

Important note: currently, the Kotlin Metadata format between K1 and K2
frontends is not 100% the same; most notably, in K2, the generated
declarations are marked as regular declarations,
not as `SYNTHESIZED` ones.

Hence, if you compile a jar with the current version of K2 frontend,
then all the generated declarations would have a regular `DECLARATION`
origin, and there would be no such issues as described above.

There are two notes here:

1. K2 IDE still has to support the jars compiled by the K1 compiler,
so it still makes sense to alter the stubs and make the generated
declarations visible.
2. The issue about the different stub formats has been reported
(see KT-64924), and might be resolved in the future.
So it is possible that K2 frontend's metadata will also start
marking the generated declarations as `SYNTHESIZED`.

^KT-64808 Fixed
2024-02-23 10:51:28 +00:00
Dmitrii Gridin 072d191306 [compiler] replace Enum values() with entries
To fix warnings. Also, use of `Enum.entries` may improve the performance

^KT-48872
2024-02-20 17:56:46 +00:00
Vladimir Dolzhenko 60b4ce2c36 Fix flakiness of createByPattern results
Fix the order of pointers and place logical operators first

#KTIJ-28165 Fixed


Merge-request: KT-MR-14447
Merged-by: Vladimir Dolzhenko <Vladimir.Dolzhenko@jetbrains.com>
2024-02-19 12:16:09 +00:00
Anna Kozlova b4b1c7cd69 [PSI] avoid loading KtFile class during parsing of chameleons
^KTIJ-28801 fixed
2024-02-15 07:17:26 +00:00
Nikita Bobko 58e1c4c6ac [FIR] Kmp: Deprecate header/impl keywords
Review: https://jetbrains.team/p/kt/reviews/14224/timeline

Timeline:
**(1)** Kotlin 1.1.60 https://github.com/JetBrains/kotlin/commit/59efedf610a25b004edf3d19897ce4dfca28ddb2 `header` keyword is introduced (committed on Dec 13, 2016)
**(2)** Kotlin `header`/`impl` has been deprecated (warning) at least since 1.1.60 https://github.com/JetBrains/kotlin/commit/5d251062677e09d607f295189b7978d5833e448f (committed on Sep 15, 2017)
**(3)** 1.1.60 release https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-compiler/1.1.60/ Nov 13, 2017

It sounds stupid but it looks like `header` was introduced and
deprecated right in the same release. Though it may be possible that we
had a release between **(1)** and **(2)** that I couldn't find

Anyway the keywords have been deprecated since basically forever

The fix is not perfect

"The perfect fix" would be:
1. Introduce a separate `DEPRECATED_MODIFIER_ERROR` diagnostic
2. Introduce LanguageVersionSettings for the `DEPRECATED_MODIFIER_ERROR`
   diagnostic

But "the perfect fix" requires:
1. Adopting `ReplaceModifierFix` in the IDE to make it work with the new
   diagnostic. `ReplaceModifierFix` is only implemented in K1 IDE. So
   there are two ways:
   1. Create a KTIJ ticket to port the diagnostic to K2 IDE (it needs to
      be ported anyway), and **mention** in the ticket that
      `DEPRECATED_MODIFIER_ERROR` also needs to be supported in K2 IDE.
      It's the ticket that nobody will probably find and fix
   2. Go and port `ReplaceModifierFix` to K2 IDE myself. It's fairly
      simple task, but I've not worked in intellij for quite a while,
      and it will take me too much time to index and compile

Considering that DEPRECATED_MODIFIER diagnostic is used only to report
`header`/`impl`, and there are literally 0 known cases when people use
it. It's just easier to convert the diagnostic to error in K2

Related tests:
- compiler/testData/diagnostics/tests/multiplatform/deprecated/header.kt

^KT-59839 Fixed
2024-02-08 16:34:27 +00:00
Nikolay Lunyak 9688865953 [FIR] Check the presence of delegated constructor call via stubs
After `4b8b7aaa` external classes' secondary
constructors should not have a delegated call, so we
don't create a call if there's no explicit call.
But we need to check if there is.

The failing tests were muted in
`8fcf91d8`, and are now unmuted
back. Those are the ones in the
`testData/rawBuilder/declarations`
folder. These tests fail because they supply
PSI stubs for the secondary constructors,
so arbitrary operations (like reading .text of
the delegated constructor call) are not allowed
for them. This commit modifies secondary
constructor stubs to include the
required information.

^KT-65268 Fixed


Merge-request: KT-MR-13982
Merged-by: Nikolay Lunyak <Nikolay.Lunyak@jetbrains.com>
2024-02-02 08:15:54 +00:00
Anna Kozlova 69fe60f494 [psi] avoid usages of KtFile in kotlin psi
^KT-65223 fixed
2024-01-29 08:16:59 +00:00
Yan Zhulanow c8af3381d6 [Stubs] Fix stub inconsistency on unnamed setter parameters
If an unused setter parameter was replaced with an underscore ('_'),
it had a special name in the stub ('<anonymous parameter 0>'). The text
version of the stub inserted it as is, leading to an extra
'PsiErrorElement'.

^KTIJ-28194 Fixed
2024-01-24 18:41:42 +00:00
Anna Kozlova 2ba02df361 [psi] allow creation of KotlinCommonParserDefinition
to avoid dependency on `java.psi` through `KtFile : PsiClassOwner, ...`

^KT-64883 fixed
2024-01-11 10:05:21 +00:00
Anna Kozlova 82d52ef6fc [psi] add parenthesis around a nullable functional type text
^KT-64862 fixed
2024-01-11 10:05:21 +00:00
Alexander.Likhachev 21b438f55d Replace the trove4j collections usages with the fastutil ones
The trove4j library is licensed under LGPL, and that causes some troubles while working with it. The fastutil library provides the same functionality in the context of our needs, and is licensed under the Apache license.
^KTI-1135 In Progress
2024-01-10 11:29:25 +00:00
Anna Kozlova ce0a41fc69 [psi] move frontend independent string manipulators to psi.main module
this way IDE can drop dependency to old frontend,
when it's actually redundant
2024-01-08 09:54:48 +00:00
Yan Zhulanow f0a21654f8 [PSI] Allow to create contextual 'KtPsiFactory' with eventSystemEnabled
The change allows creating trackable in-memory files, which are useful
for intention preview.
2024-01-05 16:04:14 +00:00
Marco Pennekamp bace053ea9 [AA decompiler] Store sealed modifiers in interface stubs
- If the `sealed` modifier is not kept in the interface's stub, a
  sealed interface will have an `abstract` modality instead, which
  causes issues with `when` exhaustiveness analysis in K2.
- This issue also affected K1, but it was less visible there because
  `when` exhaustiveness analysis in K1 uses deserialized descriptors.

^KT-62895
2024-01-05 15:55:29 +00:00
Dmitrii Gridin d88249bda7 [Analysis API decompiler] materialize delegate declarations in stubs
We should materialize delegated declarations to process callables
in scopes correctly. Standalone mode works the same way as it
deserialize directly into FIR.
Another solution is to rework proto and stub serializer/deserializer to
restore FirFields like `$$delegate_0` correctly to work with
`FirDelegatedMemberScope`

^KT-62896 Fixed
^KT-64584 Fixed
2023-12-28 08:48:08 +00:00
Anna Kozlova 35d2be25b2 [psi] extract KtCommonFile which doesn't depend on java psi
should not be used directly

^KT-64409 fixed
2023-12-19 10:39:11 +01:00
Anna Kozlova 687a23be47 [psi] remove KtEnumEntry#isEquivalentTo
This check is used in equivalence check which is symmetric, and
java refers to kotlin through light classes.
LC implementation already contains support for matching
 wrapped and origin kotlin members,
 both K1 and K2 LC implementations.
 Thus, this check is redundant.
 Removing it, we cleanup kotlin psi from java psi
2023-12-19 10:37:18 +01:00
Anna Kozlova f9fb8e3ddf [psi] remove PsiClassHolderFileStub<KtFile> from KotlinFileStubImpl
it's used in ClsFile only, and kotlin has its own decompiled hierarchy
2023-12-19 10:37:18 +01:00
Anna Kozlova 033f330703 [psi] remove unused super interface from java psi
given that the interface itself is not used externally,
the change is binary compatible

^KT-64320 fixed
2023-12-14 18:25:56 +00:00
Anna Kozlova cf57b44a1e [LC] avoid icon calculation in compiler
move calculation to the IDE icon provider

^KT-64282 fixed
2023-12-14 18:24:54 +00:00
Alexander.Likhachev a19bd2ed2e [Build] Migrate most of the build logic from Project.buildDir usage
It's going to be deprecated in Gradle 8.3

There's currently no way to pass a `org.gradle.api.provider.Provider` to the JavaExec.systemProperty or Test.systemProperty. There's a workaround using `org.gradle.process.CommandLineArgumentProvider`, but I intentionally don't rework these calls as Gradle is going to allow passing providers to configure system properties: https://github.com/gradle/gradle/issues/12247#issuecomment-1568427242
^KTI-1473 In Progress
2023-12-07 18:31:06 +00:00
Marco Pennekamp 39c38aa588 [PSI] KtQualifiedExpression: Replace AssertionError with errorWithAttachment
- There is no other usage of `AssertionError` in the
  `org.jetbrains.kotlin.psi` package. `errorWithAttachment` is more
  idiomatic, throws an `Exception` instead of an `Error`, and avoids
  leaking user code.
2023-11-16 19:50:51 +00:00
Marco Pennekamp 6668cc281d [LL] Avoid exceptions when analyzing inconsistent PSI in LLFirDeclarationModificationService
- The exceptions in KT-63130 occur because PSI tree change events may be
  published when parts of the PSI under modification are inconsistent.
  Such inconsistent elements are then analyzed by
  `LLFirDeclarationModificationService` to check if the modification
  occurs in a contract statement.
- The solution adds `*OrNull` functions to `KtQualifiedExpression` which
  return `null` instead of failing with an exception when the receiver
  or selector cannot be found.

^KT-63130 fixed
2023-11-16 19:50:51 +00:00
Egor Kulikov 408e2f109f [FIR] Correctly process slash in class name in ClassId
^KTIJ-27358 fixed


Merge-request: KT-MR-12673
Merged-by: Egor Kulikov <Egor.Kulikov@jetbrains.com>
2023-11-07 09:47:05 +00:00
Pavel Kirpichenkov 237f90d289 [stubs] Move KlibMetaFileType.STUB_VERSION to KotlinStubVersions
Track changes in K/N stubs directly instead of using the hack with
KlibMetaFileType.STUB_VERSION + KotlinStubVersions.BUILTIN_STUB_VERSION.
Changes in built-in stubs might or might not affect .knm stubs and
should be tracked independently.

The used offset constant for migration is the sum of the offsets from
KlibMetaFileType and KotlinStubVersions.BUILTIN_STUB_VERSION.

KTIJ-26761
KTIJ-26961
2023-11-02 10:28:37 +00:00
Pavel Kirpichenkov 30d45039fb [decompiler] Fix property and receiver annotations loading for stubs
Add annotation loading for property backing field, property delegate and
extension receiver to AnnotationLoaderForStubBuilderImpl. Use logic from
AnnotationAndConstantLoaderImpl.

AnnotationLoaderForStubBuilderImpl is used by KotlinMetadataDecompiler,
K2KlibMetadataDecompiler and KotlinJavaScriptMetaFileDecompiler. Stub
versions for built-ins (affects metadata and K/N decompilers) and JS
are bumped.

KTIJ-26761
KTIJ-26961
2023-11-02 10:28:37 +00:00
Dmitriy Novozhilov 704e2ef5c5 Suppress K2 specific warnings in the codebase
^KT-62472
2023-10-18 07:59:27 +00:00
Vladimir Dolzhenko d092e99ae8 Drop KtStubElementTypes#FILE
It has been marked as deprecated for a while and targeted to be deleted in 1.9.0

#KT-53781
2023-10-16 10:44:08 +00:00
Marco Pennekamp 9ac639e0d3 [PSI] Add forEachDescendantOfTypeInPreorder
- `forEachDescendantOfTypeInPreorder` is a preorder variant of
  `forEachDescendantOfType`, which operates in postorder.
2023-10-09 19:46:51 +00:00
Dmitriy Novozhilov 9e5ee3afa0 [FE] Add isLocal name to ClassId constructor calls where it needed 2023-09-21 12:40:44 +00:00
Dmitrii Gridin bd10ee10fe [psi] avoid usage of SAFE_IDENTIFIER_FOR_NO_NAME
This name leads to inconsistency between FqName from stub and
FqName from ClassId.
The first one is `no_name_in_PSI_3d19d79d_1ba9_4cd0_b7f5_b46aa3cd5d40`
but the second one is `<no name provided>`.
We can't just replace SAFE_IDENTIFIER_FOR_NO_NAME to NO_NAME_PROVIDED
due to many places with unsafe logic inside type mapping.

^KTIJ-26848 Fixed
2023-09-05 12:58:50 +00:00
Dmitrii Gridin ae02d78aff [PSI] ClassIdCalculator: cleanup code
^KTIJ-26848
^KTIJ-26896
2023-09-05 12:58:50 +00:00
Dmitrii Gridin 0701da6c59 [stub] simplify ClassId creation
We can create nested ClassId just from the parent stub instead of calculation by tree.
This commit simplified the complexity of ClassId building to liner.
Example:
```kotlin
package one
class A {
  class B {
    class C {
      class D
    }
  }
}
```
Previously, we had to create ClassId by traverse parents for each class.
Now we will visit each class only once.
It should improve performance and memory consumption during indexing.

^KTIJ-26848
2023-09-05 12:58:50 +00:00
Ilya Kirillov 9ea344fe76 [decompiler] fix ambiguities to builtin members resolve in K2 IDE
If a user has multiple kotlin-stdlib libraries in a project, all those stdlib libraries will contain builtins
which will result in resolution ambiguities.

To prevent such kind of ambiguities inside LLFirDependenciesSymbolProvider,
callables are grouped by facade class name.
Only matching callables from the first facade are used.

Facade is a Kotlin/JVM-term so right now it works only for JVM targets.
Builtins are also used in JVM, so the current solution is to have a Facade name also for builtins.

^KTIJ-26760
2023-08-30 14:45:14 +00:00
Bogdan Mukvich 7b00323b89 [Build] Update guava
Fix some reports from "Show Vulnerable Dependencies"

^KTI-1342
2023-08-25 14:10:37 +00:00
Dmitrii Gridin 3d52ba3bca [psi] KtFile: add hasImportAlias with caching
We have hotspots in UAST due to aliases, so we can reduce the time
significantly by caching `hasImportAlias` property

^KTIJ-26688
2023-08-21 13:57:10 +00:00
Alexey Merkulov 73b2ccff76 [Stubs] Refactoring: reduce member visibility in KotlinStubOrigin 2023-08-17 12:35:19 +00:00
Alexey Merkulov 60d5de56cb [Stubs] Fix writing/reading multi file facades
The fixed error leaded to the invalid resolve of methods during
evaluation, e.g. kotlinx.coroutines#runBlocking should be called from
facade BuildersKt instead of BuildersKt__BuildersKt
2023-08-17 12:35:19 +00:00
Anna Kozlova 788fa5d545 [psi] skip absent parameter names in function type presentation
KTIJ-26566
2023-08-09 16:29:37 +00:00
Ilya Chernikov 1d88c307ea K2 Scripting: fix locality of script declarations
the script declarations are considered public, at least because they
can be reused from another script then used with the `importedScripts`
configuration property. It also improves the compatibility with K1
scripting.
2023-07-26 08:49:27 +00:00
Ilya Kirillov 24db4e62f5 Migrate some FIR/Analysis API KotlinExceptionWithAttachment usages to buildErrorWithAttachment API 2023-07-18 11:49:21 +00:00
Ilya Kirillov 52c065b08a Extract a base class from KotlinExceptionWithAttachments 2023-07-18 11:49:20 +00:00
Yan Zhulanow 6156609617 [PSI] Increment modification stamp on adding imports to 'KtCodeFragment'
LL API incremental analysis depends on modification stamps for files and
declarations. Adding an import directive would modify a modification
stamp of an ordinary KtFile. The same should apply to a KtCodeFragment.
2023-07-05 16:55:38 +00:00
Yan Zhulanow df28bd1d79 [Stubs] Pass proper container source to stub-based FIR declarations
Backend, which is used by the expression evaluator, relies on the class
name in 'FacadeClassSource'. To pass the correct JVM class name,
stubs for top-level functions and properties got the 'origin'.
2023-07-04 11:17:39 +00:00
Egor Kulikov 19fb6e7458 [FIR] Don't resolve parameters in function types in invalid code
^KTIJ-25653 fixed

Merge-request: KT-MR-10831
Merged-by: Egor Kulikov <Egor.Kulikov@jetbrains.com>
2023-07-03 16:30:14 +00:00
Anna Kozlova 11c4fcda5f [psi][AA] don't decompile code when stubs have enough information
^ KTIJ-25979
+ do not assert when there are no stubs (text is already computed)
2023-06-29 10:09:20 +00:00