Commit Graph

4848 Commits

Author SHA1 Message Date
Ilya Chernikov 6288bdbd3f K2 scripting, IDE: add missing extension registration
The commit "K2 scripting: treat default import similarly to K1"
introduced the new extension that provides default imports for scripts,
instead of the previous approach of explicit `FirImport` elements
creation. But registering it for the IDE case was forgotten. This
commit adds missing registration.

#KTIJ-28960 fixed
2024-02-29 12:35:18 +00:00
Alexander Udalov dfd9aabf8e JVM: update test data after enabling IR fake override builder
#KT-61514 Fixed
2024-02-29 10:14:31 +00:00
Mikhail Glukhikh 09db7dbd22 K2: add tests to confirm current behavior of KT-65679 examples 2024-02-29 10:02:06 +00:00
Jaebaek Seo a17ad6bc55 Fix K2 CodeGen annotation parameter resolution failure
When we have parameters of annotation like
```
// FILE: Anno.kt
package p3

@Target(AnnotationTarget.FUNCTION)
annotation class Anno(vararg val x: String)

// FILE: main.kt
import p3.Anno

@Anno("A", "B")
fun foo(): Int = 10
```
the K2 CodeGen causes an exception reporting the unresolved type
reference. A lazy resolution call fixes the issue.

In addition, FIR for the value parameter to IR conversion fails because
`Fir2IrLazyProperty` for the value parameter tries to resolve the
initializer before creating the `IrParameterSymbol`. When it checks the
`localStorage`, it reports an error for the missing `IrParameterSymbol`.
This commit adds `IrParameterSymbol` before resolving the initializer.

^KT-65099 Fixed
2024-02-29 06:44:05 +00:00
Marco Pennekamp beb40484dd [LL] Add LLFirSession invalidation tests
- This commit moves all session invalidation tests from the IDE which
  are non-cyclic to the Analysis API. Cyclic session invalidation tests
  cannot be moved because the Kotlin test infrastructure doesn't support
  cyclic module dependencies.
- In fact, these new tests cover more cases, because the IDE tests only
  covered global module state and module out-of-block modification
  events. Crucially, global source modification events are now
  supported, which require libraries to be left alone.
- Module out-of-block modification for libraries is technically
  undefined, but the test still checks this behavior. The current
  session invalidation implementation behaves the same for any kind of
  module-level modification event, so it is possible to publish a module
  OOBM event for libraries. If this changes in the future, we can
  disable those tests. But for now, it's good that these tests run,
  because they will alert us to (accidental) changes to this behavior.

^KT-56288 fixed
^KT-65265 fixed
2024-02-28 21:08:18 +00:00
Marco Pennekamp 56a08319ec [AA] Add toString implementation for KtLibraryModuleImpl
- This allows stable mentions of test module libraries in output test
  data.

^KT-56288
2024-02-28 21:08:18 +00:00
Marco Pennekamp 56fd4b562b [AA] Improve modification event test directives
- Rewrite `TestModule.publishModificationEventByDirective`: we can get
  the `KtModule` to publish an event for via the test module. The
  directive can now be made optional, which is necessary when we want to
  specify it in potentially multiple modules.
- Introduce the `WILDCARD_MODIFICATION_EVENT` directive, which allows
  specifying THAT some modification event should be raised for a module,
  but not WHICH one. This allows generating multiple tests which raise
  different modification events over the same test data.
- Add various convenience functions for publishing wildcard modification
  events. `publishWildcardModificationEventsByDirective` for the test
  module structure is quite opinionated, but takes a lot of work from
  test implementations.
- Because `ModificationEventDirectives` are part of the analysis test
  framework, we register them as a general Analysis API test directive.

^KT-56288
2024-02-28 21:08:17 +00:00
Marco Pennekamp 3044941201 [AA] Add TestModule.getKtModule convenience function
^KT-56288
2024-02-28 21:08:17 +00:00
Sonya Valchuk 4c8febf10d FIR DFA: slightly refactor smart cast state tracking
Instead of always looking up smart casts on `lastNode` and separately
tracking the receiver state, simply track which flow the smart casts
belong to right now. This tracked flow is auto-advanced when new
`lastNode`s are created, but can be manually rolled back for things like
KT-63709.
2024-02-28 18:36:08 +00:00
Dmitrii Gridin f246d1c037 [LL FIR] introduce NonLocalAnnotationVisitor
- Unified and optimized visitor instead of transformer with many
  redundant operations.
- `LLFirAnnotationArgumentsTargetResolver` now has the correct state
  keeper for nested declarations – e.g., property accessors or value
  parameters.
- `FirAllLazyAnnotationCalculatorVisitor` in tests now almost the same
  as in the production as now it uses the same visitor without recursive
  visiting

^KT-63606 Fixed
^KT-64000 Fixed
^KT-64579 Fixed
2024-02-28 17:10:33 +00:00
Dmitrii Gridin 410dbcb4cf [LL FIR] LLFirTypeLazyResolver: add missed type checks for context receivers 2024-02-28 17:10:33 +00:00
Dmitrii Gridin 7048184acf [LL FIR] LLFirAnnotationArgumentsLazyResolver: add missed checks for context receivers 2024-02-28 17:10:33 +00:00
Dmitrii Gridin fe4ead751f [LL FIR] LLFirTargetResolver: do not resolve file annotation container before each declaration
We don't have to resolve `FirFileAnnotationsContainer` before each
declaration.
This became possible after KT-65345 as now `FirDesignation` has `FirFile`
in its path so, `PersistenceContextCollector#collectContext` resolves
it as well as we don't have problems with unresolved annotations
during diagnostics processing

^KT-65876
2024-02-28 17:10:32 +00:00
Dmitrii Gridin dc898fdf5c [LL FIR] LLFirTargetResolver: add missed checkThatResolvedAtLeastToPreviousPhase check for jumping phase 2024-02-28 17:10:32 +00:00
Igor Yakovlev a5ef668e3c [Wasm] Boolean boxed instances are the same
Fixed #KT-65411
2024-02-28 15:23:21 +00:00
Dmitriy Novozhilov 6cfe81de50 [FIR] Properly reports about conflicts in MPP setup
Checker for conflicting declarations will now check for the following
  scenarios too
- two expect declarations in different modules
- actual and non-expect declarations in different modules

^KT-63826 Fixed
2024-02-28 13:45:29 +00:00
Dmitriy Novozhilov 5c632bc78e [FIR2IR] Don't create f/o symbols on smartcasted Nothing in dispatch receiver
^KT-63525 Fixed
2024-02-28 08:28:14 +00:00
Dmitriy Novozhilov 5b01396a11 [FIR] Carefully process constructors of inner and nested classes in the tower
If there is an expression receiver, we should process constructors only
  of inner classes. Constructors of nested classes can be called only
  on classifier

^KT-65333 Fixed
2024-02-28 08:08:32 +00:00
Dmitriy Novozhilov 67557ce731 [Test] Reproduce KT-65333 2024-02-28 08:08:31 +00:00
Marco Pennekamp 708ed81eb2 [Test] Avoid importing unused @Nested annotations in generated tests
- Unused `Nested` imports frequently cause unused import warnings in the
  IDE, which are especially annoying in after-commit warning/error
  analysis.
2024-02-27 20:30:06 +00:00
Anna Kozlova e650ba9855 [AA] return expect declaration for "implicit" actual
If actual declaration is broken (missing explicit `actual` keyword),
expect declaration still makes sense.
This way, we allow refactorings on broken code

^KT-65191 fixed
2024-02-27 18:49:02 +00:00
Marco Pennekamp aabb7aaa18 [LL] CleanableSoftValueCache: Make read/write action requirements bold
- This should improve the read/write action requirement visibility in
  the documentation.

^KT-61222
2024-02-27 16:45:52 +00:00
Marco Pennekamp 2b40ef6b05 [LL] CleanableSoftValueCache: Avoid cleanup on already removed values
- The cache guarantees deterministic cleanup on removal, so if the
  reference has already been removed from the cache, there is no need to
  clean it up again.
- Still, I don't want to guarantee that `SoftValueCleaner` is only
  invoked once (see its documentation), as soft reference semantics
  cannot be tested properly in our current test infrastructure.

^KT-61222
2024-02-27 16:45:52 +00:00
Marco Pennekamp f7a0e1f7c1 [LL] CleanableSoftValueCacheLincheckTest: Check cleanup state of removed values
- Now it's checked that cleanup behaves as expected in a concurrent
  setting for `put` and `remove`. Note that Lincheck never checks that a
  value is actually cleaned up. Instead, it compares the result with the
  single-threaded execution of a scenario. Hence, the non-concurrent
  `CleanableSoftValueCacheTest` is crucial, as it separately ensures
  correct deterministic cleanup in a single-threaded environment.
- We cannot test that operations returning a new value (such as `get`)
  always return values which haven't been cleaned up yet, because the
  value might be cleaned up by another thread between the return point
  of the operation and cleanup checking.

^KT-62136
2024-02-27 16:45:52 +00:00
Marco Pennekamp d4b0dfee0a [LL] Update Lincheck to version 2.24
- This new version fixes problems with concurrent hash maps in model
  checking tests and allows us to remove the correctness guarantee for
  them.

^KT-62136
2024-02-27 16:45:52 +00:00
Marco Pennekamp 5207940f0b [LL] CleanableSoftValueCache: Add non-concurrent tests for value cleanup
- The non-concurrent `CleanableSoftValueCacheTest` ensures that
  deterministic cleanup behaves correctly. A Lincheck test can only
  discover differences between the single-threaded and concurrent
  executions of a test scenario, so it cannot find correctness issues
  with deterministic cleanup on its own.

^KT-61222
2024-02-27 16:45:52 +00:00
Marco Pennekamp 53e545aa3f [LL] CleanableSoftValueCache: Don't perform cleanup when putting the same value
- If we have some value `X` in the cache, and we put `X` a second time
  into the same location, `X` shouldn't be cleaned up because it wasn't
  actually removed from the cache.
- With this feature, it's necessary to implement `put` in terms of
  `backingMap.compute`, because we need to compare the old and the new
  value atomically, and based on that, decide whether to create a new
  soft reference.

^KT-61222
2024-02-27 16:45:52 +00:00
Marco Pennekamp 80a2382cf6 [LL] Add Lincheck tests for CleanableSoftValueCache
- Lincheck tests the linearizability of a concurrent data structure,
  which helps us verify that `CleanableSoftValueCache` works in
  concurrent scenarios.

^KT-62136 fixed
2024-02-27 16:45:52 +00:00
Marco Pennekamp b2cd29726b [LL] Rewrite CleanableSoftValueCache in terms of ConcurrentHashMap.compute
- The previous implementation of `putIfAbsent` made two calls to the
  `backingMap`: `putIfAbsent` and `replace`. This breaks atomicity at
  least in theory. Implementing the major compute operations in terms of
  `backingMap.compute` allows us to restrict the critical section to
  this single atomic `ConcurrentHashMap` operation, which is easier to
  reason about.
- Using `backingMap.compute` also improves the guarantees we can make in
  respect to `computeIfAbsent`'s computation function `f`. We can now
  guarantee that the function is called exactly once iff the `key` is
  absent, because it is only ever invoked inside `backingMap.compute`,
  which makes this guarantee itself.
- Remove `putIfAbsent`, which isn't currently used by
  `LLFirSessionCache`. It can easily be implemented using
  `computeIfAbsent` in the future.

^KT-61222
2024-02-27 16:45:52 +00:00
Marco Pennekamp 5819f4eaa2 [LL FIR] Test resolve extension disposal after modification events
- These tests simply ensure that a resolve extension is disposed after
  any kind of modification event is raised. It's not meant to test
  complex scenarios, but rather to detect when resolve extension
  disposal isn't invoked *at all*.
- I tried implementing a similar test for resolve extension disposal
  after soft reference garbage collection, but the only way to force the
  GC to collect soft references is to allocate memory until an
  out-of-memory error occurs. That is bad for the test infrastructure,
  because it might allocate A LOT of memory (depending on the max heap),
  which is problematic for running tests locally. Also, our Kotlin tests
  stop on an OOM error altogether (so additional configuration would be
  required) and the heap is dumped into a 20GB file (on my machine),
  which is again problematic for local test runs.

^KT-61222
2024-02-27 16:45:52 +00:00
Marco Pennekamp 040b7472a9 [AA] Add MODIFICATION_EVENT test directive
- This allows tests to specify which modification event they want to
  publish.
- The modification event won't be published automatically. Tests need to
  invoke `publishModificationEventByDirective` at the right time.

^KT-56288
2024-02-27 16:45:52 +00:00
Marco Pennekamp 7a5a1833e7 [AA] Implement disposable KtResolveExtensions
- This commit adds the `Disposable` interface to `KtResolveExtension`.
  Resolve extensions are disposed after their associated session has
  been invalidated, or reclaimed by the GC.
- Example use case: Opening a message bus connection with the lifetime
  of the resolve extension.

^KT-61222 fixed
2024-02-27 16:45:52 +00:00
Marco Pennekamp efb56ed30a [LL FIR] Implement disposal support for LLFirSessions
- This commit adds a `Disposable` to `LLFirSession` which lives as long
  as the session. For example, session components can now open a message
  bus connection that is automatically disposed after the session has
  been invalidated or reclaimed.
- Because `LLFirSession`s should still benefit from soft reference-based
  automatic reclamation by the GC, we have to use a soft value map with
  cleanup in `LLFirSessionCache`. It ensures that the child disposables
  are properly disposed even when the session is collected as a soft
  value.
- The session cannot be the `Disposable` itself because a valid
  disposable reference is needed to call `Disposer.dispose`. When a soft
  reference is added to a reference queue (such as the reference queue
  employed by the soft value map), its referent is already cleared.
  Hence, it's not possible to watch a softly referenced `Disposable`
  with a reference queue and also pass it to `Disposer.dispose`. This is
  why the `LLFirSessionCleaner` contains a strong reference to the
  `disposable`, because that will not keep the `LLFirSession` from being
  reclaimed, while keeping the `disposable` alive slightly longer than
  the session.

^KT-61222
2024-02-27 16:45:52 +00:00
Marco Pennekamp 35856ab58f [LL FIR] Implement CleanableSoftValueCache
- This cache implementation is useful for lifecycle management of cached
  values when we also want to allow the GC to reclaim those values which
  are unused. It ensures that the cleanup is (almost) definitely invoked
  at some point after a value has been reclaimed by the GC, and also
  invokes the same cleanup operation when the value is removed from the
  cache conventionally. This unifies the approach to cleaning up such
  elements.
  - It would be possible to have the cache ONLY perform cleanup for
    references reclaimed by the GC, but this would put the onus on the
    consumer to perform the cleanup when an element is removed from the
    cache conventionally. That comes with its own pitfalls, such as
    requiring the user to handle `clear` correctly, or not forgetting
    to perform cleanup with the old value returned by `put`.
- "Almost definitely" because the reference queue won't be processed if
  the cache becomes unused at some point. However, this is not an issue
  for the current intended use case (session invalidation and disposal)
  and can be solved by clearing the cache before it becomes entirely
  unused.
- The cache requires read and write actions for certain operations to
  simplify the implementation of `clear`.

^KT-61222
2024-02-27 16:45:51 +00:00
Artem Kobzar 3429cbd321 [K/JS] Support companion objects in external and exported declarations 2024-02-27 16:30:13 +00:00
Alexander Udalov 5cda3fba12 IR: produce new fake override for each static member
This is an addition to d4278250e6. Apparently we still need to produce
a new fake override for each inherited static member, because otherwise
we would try to determine the most specific return type, maximum
visibility, etc, all of which makes no sense for static members.

 #KT-66152 Fixed
2024-02-27 16:05:52 +00:00
Roman Efremov 23ae617ea0 Fix inaccurate report of DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM
Add separate diagnostic for the case when it is reported on qualifiers.

^KT-64488
2024-02-27 11:22:35 +00:00
Roman Efremov 02b5fed389 Fix inaccurate report of DEPRECATED_ACCESS_TO_ENTRY_PROPERTY_FROM_ENUM
It can be reported not only from within the enum, but also from
top-level functions. Add separate diagnostic for such cases.

^KT-64488
2024-02-27 11:22:35 +00:00
Roman Efremov 34d87465ac [Test] Add test for Enum.entries shadowing when property is imported
This is similar to KT-64488 but without context receivers.
Warning message "access ... from within the enum" is also incorrect
here.

^KT-64488
2024-02-27 11:22:35 +00:00
Mikhail Glukhikh 678816f9e8 K1: introduce BUILDER_INFERENCE_STUB_PARAMETER_TYPE to prevent compiler crashes
This diagnostic is reported in rare situations when
StubTypeForBuilderInference is kept as a parameter type
of for loop or lambda. Before this commit, we had in K1
"Could not load module <error module>" from IrLinker instead.

Related to: KT-52757, KT-53109, KT-63841, KT-64066
#KT-53478 Fixed
2024-02-27 10:25:13 +00:00
Dmitriy Novozhilov 234cc5be2b [Test] Reproduce KT-65576 2024-02-27 09:34:02 +00:00
Marco Pennekamp b5eccd63b1 [AA] Fix compilation facility "multi binary module" test module structure
- The module structure for these tests was set up backwards: We should
  keep the test module kind of the main module flexible (as it is
  configured by test configurators), but keep the module kinds of the
  binary libraries fixed.

^KT-65960
^KT-64994
2024-02-26 21:57:23 +00:00
Marco Pennekamp 4ea3de1760 [AA] Refactoring: Rename KtModuleFactory to KtTestModuleFactory
^KT-65960
2024-02-26 21:57:23 +00:00
Marco Pennekamp 3f04604187 [AA] Tests: Improve handling of dependencyPaths
- The name wasn't specific enough.
- Remove default `emptyList()` argument from `CompiledLibraryProvider`
  so that passing `dependencyBinaryRoots` isn't missed. (My changes
  didn't include it in `KtLibraryBinaryModuleFactoryBase` yet, but it
  was actually missed from `KtLibrarySourceModuleFactory` even in the
  original commit.)

^KT-65960
^KT-64994
2024-02-26 21:57:23 +00:00
Marco Pennekamp 5663521a36 [AA] Tests: Remove topological ordering from KtTestModule creation
- The test infrastructure already requires test modules to be properly
  ordered regarding dependencies, so a module A which is a dependency
  for a module B must always be ordered before B. Hence, it makes no
  sense to also order modules topologically.
- Reordering modules may cause the resulting `KtTestModule`s to be
  out-of-order with the test modules, because the topological order
  might be different from the test module order, even if the dependency
  relationship already holds in the original order.
  `KtTestModuleProjectStructure` requires the same ordering as test
  modules to ensure the same order during iteration of `KtTestModule`s.

^KT-65960
^KT-64994
2024-02-26 21:57:23 +00:00
Marco Pennekamp c7ccfcc785 [AA] Fix sealed inheritors provider for binary libraries in Standalone tests
- Since binary libraries don't carry decompiled files anymore, the
  static declaration provider doesn't index binary libraries in the
  Standalone mode, where we don't use stub-based deserialization. This
  led `LLSealedInheritorsProviderFactoryForTests` to fail in Standalone
  mode for sealed classes from binary libraries, as this provider is now
  populated from the declaration provider's index (see the previous
  commit). Luckily, class-based deserialization sets
  `sealedInheritorsAttr`, so we can simply use this attribute.

^KT-65960
2024-02-26 21:57:23 +00:00
Marco Pennekamp e62038f5f3 [AA] Tests: Rewrite sealed class inheritors collection to use the declaration provider
- Now that binary libraries are decompiled to stubs instead of PSI
  files, we cannot collect sealed inheritors from `KtFile`s anymore.
  Since all `KtFile`s and binary library stubs are both indexed by the
  declaration provider, we can collect inheritors from its index
  instead.
- Invalidating all sessions at the end of `prepareSealedClassInheritors`
  fixes some improper resolve phases in lazy resolution test data. While
  the previous implementation requested an uncached resolve session, it
  didn't account for sessions of dependencies still being cached.

^KT-65960
2024-02-26 21:57:23 +00:00
Marco Pennekamp 47afd37596 [AA] IDE mode tests: Build and index stubs for binary libraries
- Instead of indexing binary library declarations from decompiled PSI,
  the static declaration provider now builds and indexes stubs. As noted
  in KT-65960, this brings IDE mode tests much more in line with
  decompiled stubs indexing in the IDE. It should allow us to catch
  issues with the stub-based deserialized symbol provider outside of IDE
  tests.
- In the Standalone mode, we can skip stub-indexing completely, as we
  provide FIR symbols via class-based deserialization. This also extends
  to shared binary roots.

^KT-65960 fixed
2024-02-26 21:57:23 +00:00
Marco Pennekamp 78ef58bef4 [AA] Tests: Remove decompiled files from LibraryBinary and add LibraryBinaryDecompiled
- `LibraryBinary` should not contain any decompiled files, as we want
  FIR symbols in tests to be provided from indexed stubs or class files,
  but definitely not from decompiled PSI. This brings `LibraryBinary`
  much closer to the behavior of binary libraries in the IDE.
- Some tests may still require access to a decompiled file, for example
  when trying to test `getOrBuildFir` for some `KtElement` coming from a
  library. This commit introduces `LibraryBinaryDecompiled`, which
  does contain decompiled files.
- We don't really need `LibraryBinary` as a main test module kind
  anymore, since tests generally want to access some main `KtFile`.
  Hence, test configurators for `LibraryBinary` have been turned into
  configurators for `LibraryBinaryDecompiled`.
- An alternative would be decompiling files on demand, but this is not
  currently feasible because the Standalone API doesn't reconcile stubs
  with decompiled PSI, like the IDE does automatically. (For the same
  declaration, the stub and the PSI will have a different identity.) As
  long as there is no support for this, we'll have to rely on a separate
  test module kind.

^KT-65960
2024-02-26 21:57:23 +00:00
Marco Pennekamp 7baaa38b8a [AA] Add KtTestModule.moduleKind
- `TestModule.explicitTestModuleKind` requires a directive to be present
  to get the `TestModuleKind`. But we still want to find out the test
  module kind for test modules without a directive. Hence, we have to
  add this property to `KtTestModule` during its construction.

^KT-65960
2024-02-26 21:57:23 +00:00