Commit Graph

262 Commits

Author SHA1 Message Date
Nikita Bobko 1aeefe6c33 [FIR] Don't traverse expect hierarchy to check DefaultArgumentsInExpectActualizedByFakeOverride
^KT-63860 Fixed
Review: https://jetbrains.team/p/kt/reviews/13334/timeline

The previous code was nonsense (I wrote it). It doesn't make sense to
subtract actualOverriddenDeclarations from expectOverriddenDeclarations.
Default parameters are mentioned on the expect side. So default params
in expect/actual supertypes won't be subtracted from
expectOverriddenDeclarations (but should be)
2023-12-05 13:30:14 +00:00
Nikita Bobko 48fdeeb42d [FIR, IR] Cleanups around allRecursivelyOverriddenDeclarationsIncludingSelf
- Pass containingClass (helps to avoid calculations of the
  containingClass in allRecursivelyOverriddenDeclarationsIncludingSelf
  implementation)
- Reuse allRecursivelyOverriddenDeclarationsIncludingSelf by
  DefaultArgumentsInExpectActualizedByFakeOverride &
  ActualFunctionWithDefaultParameters

Review: https://jetbrains.team/p/kt/reviews/13244
2023-12-04 13:08:23 +00:00
Mikhail Glukhikh 526bc1744a K2: implement BUILDER_INFERENCE_MULTI_LAMBDA_RESTRICTION
#KT-59390 Fixed
2023-12-01 10:54:29 +00:00
Kirill Rakhman 560c1cacf3 [FIR] Fix capturing of flexible types during resolution
Previously, because we didn't handle flexible types properly in
prepareCapturedType, projections inside flexible types would only be
captured during subtyping with captureStatus=FOR_SUBTYPING
which would lead to the constraint type being wrongly approximated
(see ConstraintInjector.TypeCheckerStateForConstraintInjector
.addNewIncorporatedConstraint).

Fixing the capturing produced two kinds of false positive diagnostics:

1. In ConstraintInjector.TypeCheckerStateForConstraintInjector
.addNewIncorporatedConstraint we would get two instances of cone types
that are structurally equal and containing the same captured type.
However, because we only skipped subtyping if the types were
referentially equal, we would get a contradiction here.
The fix was to use structural equality instead, which should be okay
as the captured type instances are the same.

2. Reified type variables were inferred to captured types because
flexible arrays with captured upper bounds
(Array<Foo>..Array<Captured(out Foo)>?) were not properly approximated.

#KT-62609 Fixed
2023-11-30 08:54:26 +00:00
Roman Efremov 29c82c668f [IR] Prevent duplicated reports of ACTUAL_ANNOTATIONS_NOT_MATCH_EXPECT
...on property accessors and enum entries.

^KT-62559
2023-11-28 23:17:23 +00:00
Nikita Bobko 7166adb179 [FIR, IR] Convert FirDefaultArgumentsInExpectActualizedByFakeOverrideChecker into ExpectActualCheckingCompatibility
FirDefaultArgumentsInExpectActualizedByFakeOverrideChecker is an adhoc
checker which can be converted to ExpectActualCheckingCompatibility to
reuse common expect-actual checking infrastructure.

^KT-62913 Fixed
Review: https://jetbrains.team/p/kt/reviews/13094/timeline

Tests that were broken by one of my previous commits are now fixed:
- actualFakeOverride_paramsAreCompatibleViaSharedMethodWithDefaultParams.kt
- inheritedJavaMembers.kt

DEFAULT_ARGUMENTS_IN_EXPECT_ACTUALIZED_BY_FAKE_OVERRIDE diagnostic
disappeared in delegation.fir.kt because only one
AbstractExpectActualChecker incompatibility can be reported at a time
(DEFAULT_ARGUMENTS_IN_EXPECT_ACTUALIZED_BY_FAKE_OVERRIDE is now reported
not by adhoc checker but by common AbstractExpectActualChecker). It
would be nice to report both of them, but it's a separate issue KT-62631

delegation2 test makes sure that
DEFAULT_ARGUMENTS_IN_EXPECT_ACTUALIZED_BY_FAKE_OVERRIDE is reported when
NO_ACTUAL_CLASS_MEMBER_FOR_EXPECTED_CLASS is fixed
2023-11-24 18:02:49 +00:00
Nikita Bobko 5b72c127bd [FIR, IR] Commonize isFakeOverride check for AbstractExpectActualAnnotationMatchChecker
This commit fixes fakeOverrides.fir.kt test that got broken in the
previous commit

Previously isFakeOverride was only checked on IR backend. Now this check
is moved to AbstractExpectActualAnnotationMatchChecker which is used by
both: frontend and backend. That's why frontend no longer reports false
positive ACTUAL_ANNOTATIONS_NOT_MATCH_EXPECT

Review: https://jetbrains.team/p/kt/reviews/13094/timeline
2023-11-24 18:02:48 +00:00
Nikita Bobko a1ce8ac175 [FIR] 1/2 match and check expect fake-overrides vs actuals
^KT-63550 Fixed
Review: https://jetbrains.team/p/kt/reviews/13094/timeline

Now it's required to create a new ScopeSession when searching for expect
members for actuals. If you keep reusing actualScopeSession then members
declared in platform may "slip into" the search results, resulting an
incorrect expect for actual (e.g. it happens in
supertypeIsExpectActual_covariantOverrideOfInjectedFromSuper_transitiveSubstitutionFakeOverride.fir.kt)

I suppose that it always has been a bug that we reused
actualScopeSession because we were mixing actualScopeSession and
expect FirSession (which is a bad idea), but we simply didn't have cases
where this bug could be observed. Now after we started matching
fake-overrides, we have such cases.

But creating a new ScopeSession every time is a suboptimal solution. We
need to design a scope caching KT-63773
2023-11-24 18:02:47 +00:00
Nikita Bobko 584c353a44 [FIR, IR] shouldCheckAbsenceOfDefaultParamsInActual: update comment
Fixing KT-61105 won't help.
Review: https://jetbrains.team/p/kt/reviews/13094/timeline
2023-11-24 18:02:45 +00:00
Nikita Bobko 7b8445f914 [FIR] 2/2 Support more wider actual member visibility, if the expect member is effectively final
^KT-61955 Fixed

Introduce LV vs don't introduce LV:

introduce LV:
- K1 IDE and K2 project: IDE accurately shows compiler errors
- K2 IDE and K1 project: There are cases when IDE shows red but in reality it's green

don't introduce LV:
- K1 IDE and K2 project: There are cases when IDE shows red but in reality it's green
- K2 IDE and K1 project: There are cases when IDE shows green but in reality it's red
2023-11-07 15:53:54 +00:00
Nikita Bobko e13ebe8474 [FIR, IR] 1/2 Refactoring: propogate LanguageVersionSettings to areCompatibleCallableVisibilities
This refactoring is needed for KT-61955
2023-11-07 15:53:54 +00:00
Nikita Bobko facdbfdc11 [FIR, IR] Cleanup: inline functions that have only one usage in matcher/checker
Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:26 +00:00
Nikita Bobko 92850dcc84 [FIR, IR] Cleanup: convert vector of actuals to scalar of a single actual in AbstractExpectActualChecker
Review: https://jetbrains.team/p/kt/reviews/12750/timeline

`AbstractExpectActualChecker` works on pairs obtain from
`AbstractExpectActualMatcher` and the last one always returns single
actual for single expect. That's why handling of multiple actuals
doesn't make sense
2023-11-06 14:41:25 +00:00
Nikita Bobko fd39795f41 [FIR, IR] Cleanup: deduplicate common code in AbstractExpectActualMatcher & AbstractExpectActualChecker
Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:25 +00:00
Nikita Bobko b331726c6e [FIR, IR] Cleanup: drop redundant enumConstructorsAreAlwaysCompatible
It's constantly true in FIR and IR (it was false in K1)

Also see: 8b9079d026

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:24 +00:00
Nikita Bobko 75a0442b61 [FIR] 2/2 Cleanup: drop no longer necessary FirActualCallableDeclarationChecker
It became possible to drop it after KT-62590. Now, on a frontend, the
return type check is part of a common AbstractExpectActualChecker logic

Change in nestedAnnotationClassViaActualTypealias.fir.kt aligns the
behaviour with K1. KT-61964

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:23 +00:00
Nikita Bobko a9583e4f9a [FIR, IR] 1/2 Align FirExpectActualMatchingContextImpl.areCompatibleExpectActualTypes with ExpectActualUtilsKt.areCompatibleExpectActualTypes
Those two functions are copy-paste of each other. They diverged since
the time they were copy-pasted. This commit makes
FirExpectActualMatchingContextImpl.areCompatibleExpectActualTypes
up-to-date.

I didn't update ExpectActualUtilsKt.areCompatibleExpectActualTypes
because I will drop it in the next commit.

Change in `dynamicTypesEqualToAnything` doesn't change any logic (yet.
This change in logic will take effect in the next commit), because:
1. `dynamicTypesEqualToAnything` is only changed in
AbstractExpectActualChecker.getCallablesCheckingIncompatibility. But
AbstractExpectActualChecker.getCallablesCheckingIncompatibility doesn't
check return types on frontend (it only check return types on backend).
2. `dynamicTypesEqualToAnything` is ignored on IR backend

I have no idea what is the difference between `createTypeCheckerState()`
and `actualSession.typeContext`, but it aligns these copy-pasted
versions and makes the tests behave like in K1 (at least
'typeUsageWithUnresolvedReference' and 'kt57320' are affected)

This commit is mainly a preparation for the next commit.

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:23 +00:00
Nikita Bobko dab69e38c4 [FIR, IR] AbstractExpectActualChecker: drop redundant code
- getCallablesStrongIncompatibility is unused (because it moved to the
  "matcher" KT-62590)
- `checkClassScopesCompatibility` parameter is always `true` or unused

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:22 +00:00
Nikita Bobko 24e74487d6 [FIR] Fix DEFAULT_ARGUMENTS_IN_EXPECT_ACTUALIZED_BY_FAKE_OVERRIDE tests
This commit fixes the following tests:
    FirOldFrontendMPPDiagnosticsWithPsiTestGenerated
        DefaultArguments
            MethodDefaultArgsViaActualFakeOverride
                testIntersectionOverride
                testSimple
                testDelegation
                testSubstitutionOverride
                testSimpleIncompatible

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:21 +00:00
Nikita Bobko 8722e142f6 [IR] 2/2 Propogate expect-actual checker incompatibilities to IR backend
Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:20 +00:00
Nikita Bobko eb0c8c00c3 [FIR, IR] 1/2 Minimize usages of ExpectActualCompatibility, MismatchOrIncompatible and MatchedOrCompatible
- Migrate ExpectActualCompatibility -> ExpectActualCheckingCompatibility
  where the "checker" is expect
- Migrate ExpectActualCompatibility -> ExpectActualMatchingCompatibility
  where the "matching" is expect

KT-62590 in progress. A lot of tests start to fail now. I will fix them
in next commits

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:19 +00:00
Nikita Bobko 664b9cc3ba [FIR, IR] ClassScopes incompatibility: split unfulfilled incompatibilities into "mismatchedMembers" and "incompatibleMembers"
KT-62590 is in progress

Unfortunatelly, ACTUAL_ANNOTATIONS_NOT_MATCH_EXPECT is now reported
twice in 'classScopeViaTypealiasIncompatible' test.

We had a duplicated diagnostic in other cases before my commits, anyway.
it will be fixed in
https://youtrack.jetbrains.com/issue/KT-62559/KMP-K2-prevent-reporting-ACTUALANNOTATIONSNOTMATCHEXPECT-twice-in-CLI#focus=Comments-27-8293687.0-0

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:19 +00:00
Nikita Bobko d0b34fe378 [FIR, IR] Refactor: restructure ExpectActualCompatibility class hierarchy
KT-62590 is in progress

This commit is pure refactoring.
- No tests changed their behaviour
- Semantics isn't changed

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:18 +00:00
Nikita Bobko bbbc775181 [FIR, IR] Drop code that don't belong to AbstractExpectActualMatcher
- The Matcher must only do the "matching" (aka "strong compatibility")
- Drop all "checking" related (aka "weak compatibility" related) logic
- `checkClassScopesCompatibility` parameter belongs to the
  "expect-actual checker" not to the "expect-actual matcher". Drop it

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:18 +00:00
Nikita Bobko 1c6e529146 [FIR, IR] Rough split of AbstractExpectActualCompatibilityChecker into matcher & checker
This commit:
1. renames AbstractExpectActualCompatibilityChecker to AbstractExpectActualChecker
2. `cp compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/AbstractExpectActualChecker.kt compiler/resolution.common/src/org/jetbrains/kotlin/resolve/calls/mpp/AbstractExpectActualMatcher.kt`
   + rename the object in AbstractExpectActualMatcher.kt
3. KDocs

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:17 +00:00
Nikita Bobko 4ef8595a4b [IR] Don't check class scopes compatibility in collectActualCallablesMatchingToSpecificExpect
This is a minor optimization.
This commit is a step forward for KT-62590

Conceptually, `checkClassScopesCompatibility` belongs to the
"expect-actual checker". It doesn't belong to the "expect-actual
matcher".

- `checkClassScopesCompatibility` should be set to `true` when you want
  to do the "checking"
- `checkClassScopesCompatibility` should be set to `false` when you want
  to do the "matching"

`collectActualCallablesMatchingToSpecificExpect` only needs the
"matching"

No tests changed their behaviour

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:17 +00:00
Nikita Bobko 3b47610817 [FIR] Refactoring: "Move in" parentSubstitutor creation to getCallablesCompatibility
Motivation: Make getCallablesCompatibility API slightly nicer. The
function is going to be called from more places after KT-62590 is fixed.
This commit makes fixing KT-62590 easier

Review: https://jetbrains.team/p/kt/reviews/12750/timeline
2023-11-06 14:41:16 +00:00
Nikita Bobko bc5180656d [FIR] ReturnType expect-actual incompatibility: convert from strong to weak
It should have been WeakIncompatible from the beginning because it's not
possible to overload by return type in Kotlin

This commit is a step forward to fix KT-62591

Unfortunately, the test cannot demonstrate the problem because of
another bug in K2 KT-59887

^KT-62752 Fixed
Review: https://jetbrains.team/p/kt/reviews/12750/timeline

Motivation:

It makes expect-actual matching-checking model more consistent.

expect-actual "matching" is run before FirResolvePhase.BODY_RESOLVE. You
can't know return types, until you run BODY_RESOLVE. That's why the
return type can't be checked during expect-actual matching. But it's
cursed: you have something that have to match by, but, at the same time,
you can't do it.

expect-actual "checking" is run after FirResolvePhase.BODY_RESOLVE.
That's why if we convert ReturnType incompatibility to WeakIncompatible
(which should have been called CheckingIncompatible), then expect-actual
matching model becomes consistent.

We will also be able to get rid of unnecessary
FirActualCallableDeclarationChecker. Because it won't be necessary.
Return types will be checked by common logic of expect-actual "checker"
2023-11-06 14:41:16 +00:00
Dmitriy Novozhilov 9366847e96 [FIR2IR] Properly approximate intersection types during fir2ir conversion
^KT-62544 Fixed
2023-10-18 13:02:11 +00:00
Mikhail Glukhikh 9f6abfc166 K2/MPP: treat Array<Some> & Array<out Some> as similar types in annotations
#KT-59561 Fixed
Related to KT-61100
2023-10-17 06:49:38 +00:00
Roman Efremov 0fd700de21 [FIR] Fix case with lazy resolve in expect-actual annotation checker
Unresolved annotation arguments were treated as absent arguments,
which lead to false-positive reports.
Add assert and test for that and fix.

MR: KT-MR-12245

^KT-60671 Fixed
2023-10-16 10:48:08 +00:00
Roman Efremov 423f4ca5f0 [FE, IR] Check compatibility of annotations set on type usages in expect and actual declarations
This includes checking of annotatins set on:

- value parameter types
- type parameter bound types
- extension functions receiver types
- function return types
- class super types

Fix in `defaultParams_inheritanceByDelegation_positive.kt`
is needed because of problem in resolution of implicit return types
(KT-62064), which leads to crash in annotation checker, because it
expects resolved return type.

MR: KT-MR-12245

^KT-60671 Fixed
2023-10-16 10:48:08 +00:00
Roman Efremov ab2c129466 [FE, IR] Refactor: extract compatibility check of two annotation lists to separate method
This is needed for subsequent checks of annotations
set on types, while currently method accepts only
declarations.

MR: KT-MR-12245

^KT-60671
2023-10-16 10:48:08 +00:00
Simon Ogorodnik 024e94a479 [K2] Document KMP implementation in the K2 Compiler
Add comprehensive documentation explaining the implementation and
functions of the Kotlin multiplatform (KMP) support in the K2 Compiler.
2023-10-12 12:27:54 +00:00
Nikita Bobko 91a337074e Refactoring: Improve createExpectActualTypeParameterSubstitutor API
Review: https://jetbrains.team/p/kt/reviews/12279/files

Motivation: make sure that cases like KT-62027 won't happen again
Review: https://jetbrains.team/p/kt/reviews/12279/files

Now it's responsibility of the
`createExpectActualTypeParameterSubstitutor` calller to think about the
case when parameters size isn't equal. You must not be able to create a
substitutor if type parameters sizes are not equal

Improvement in `createExpectActualTypeParameterSubstitutor` API also
improves
`AbstractExpectActualCompatibilityChecker.getCallablesCompatibility`
API.

Because suppose that you accidentally created a redundant wrapping
substitutor => you need to handle the case of not equal type parameters
size on the call site => you start thinking why you should do that on
the call site? It must be a responsibility of
`getCallablesCompatibility` => you realize that you created a redundant
wrapping substitutor
2023-10-10 09:02:07 +00:00
Mikhail Glukhikh 4da7453a7b Add comment to TypeSystemCommonSuperTypesContext.checkRecursion
Related to KT-38544, KT-60581
2023-10-05 12:17:17 +00:00
Mikhail Glukhikh aea7b8f00a Common supertypes: unwrap captured type from DNN type during recursion control
This commit handles subtle situation when K1 represents flexible type
arguments as just T..T?, but K2 does it as T&Any..T?.
This can provoke a type like Captured(*)&Any..Captured(*)?,
and before this commit we couldn't find recursion inside Captured(*)&Any.
This could lead to explosions inside type system and inference errors

#KT-60581 Fixed
2023-10-05 12:17:17 +00:00
Roman Efremov ad4d1a5ba8 [IR] Support ACTUAL_ANNOTATION_CONFLICTING_DEFAULT_ARGUMENT_VALUE in K2
It's implemented as IR checker because in K2 constant expressions are
evaluated on backend. FIR diagnostic removed because isn't needed.

"annotationViaActualTypeAlias" test has no `// FIR_IDENTICAL` because
diagnostic reported on entire typealias declaration instead of its name.
This is because in IR+LightTree we have only offsets, so can't navigate
to typealias name element.

^KT-59940 Fixed
2023-09-20 15:40:37 +00:00
Roman Efremov e79851910e [FE, IR] Check annotations compatibility on expect and actual getters and setters
^KT-60671
2023-09-15 14:41:38 +00:00
Roman Efremov 8fb2935ef6 [FE, IR] Check annotations compatibility on expect and actual type parameters
^KT-60671
2023-09-15 14:41:37 +00:00
Roman Efremov 420dceb7d8 [FE, IR] Store PsiElement for mismatched annotation's actual target
There is IDE quick fix which suggests to copy mismatched annotation
from `expect` to `actual` (see KTIJ-26633). It needs to find
`actual` PsiElement where to add annotation. Before previous commit, it
was easy - just get source of `Incompatibility.actualSymbol`.
After previous commit, the problem might be in value parameter, while
`actualSymbol` would contain function symbol. This is solved by adding
new field `Incompatibility.actualAnnotationTargetElement`.

`SourceElementMarker` introduced, because it's needed to be used in
abstract checker. Existing `DeclarationSymbolMarker` doesn't fit
because in next PR for this issue annotations set on types will be reported,
and types are not declarations.

^KT-60671
2023-09-15 14:41:37 +00:00
Roman Efremov 5c95f69aef [FE, IR] Check annotations compatibility on expect and actual value parameters
Comment was added to make it clear why whole declarations reported
in diagnostic instead of value parameter symbols (same will be done
with other targets in subsequent commits).

^KT-60671
2023-09-15 14:41:37 +00:00
Denis.Zharkov a02cb16fb2 K2: Rewrite delegate inference
The main idea is getting rid of stub types and using just type variables
See more detailed description at docs/fir/delegated_property_inference.md

The problem with stub types is that they need really special treatment
in many places, and on the other hand, there are no clear contracts on
how they should work (that regularly leads to bugs like KT-59529)

^KT-61060 Fixed
^KT-61075 Fixed
^KT-61077 Fixed
^KT-59529 Fixed
^KT-61633 Related
^KT-61618 Related
^KT-61740 Related
^KT-59107 Related
^KT-61747 Related
^KT-61077 Related
^KT-61781 Related
2023-09-08 07:11:48 +00:00
Denis.Zharkov f4004fb06a Add kdoc for ConstraintSystemCompletionMode.PARTIAL 2023-09-08 07:11:48 +00:00
Roman Efremov 5e49b472f8 [FE, IR] Allow @ImplicitlyActualizedByJvmDeclaration to be set only on expect
@ImplicitlyActualizedByJvmDeclaration is the only one
OptionalExpectation annotation which works correctly when set only on
`expect`. All other (like @JvmName, @JsName) - not, so warning for them
must be reported.

^KT-61725 Fixed
2023-09-06 14:27:07 +00:00
Roman Efremov 3124cbcbad [FE, IR] Refactor: rename areAnnotationArgumentsEqual parameters
Rename to "expect" and "actual" annotation.
This will be needed in next commit to make it clear that
only expect annotation value needs special handling.

^KTIJ-26700
2023-08-23 13:15:20 +00:00
Roman Efremov def4388c19 [FE, IR] Check annotations on expect and actual enum entries
^KT-60668
^KT-60936
2023-08-22 12:14:43 +00:00
Roman Efremov b57940a59b [FE1.0] Optimize search of expect class member in annotation checker
In the following scenario, when we search corresponding expect member
for actual `A.B`, we can skip checking compatibility of `B` scope.
```
class A {
  class B {
    fun foo() {}
  }
}
actual typealias AImpl = A
```

This is because:

1. Annotation checker runs no matter if found expect class is compatible
or not.
2. Class always has at most one corresponding `expect` class (unlike for
functions, which may have several overrides), so we are sure that we
found the right member.

^KT-60668
^KT-60936
2023-08-22 12:14:43 +00:00
Roman Efremov ab1c0e3c48 [FE] Check annotations on actual class members including fake overrides
^KT-60668
^KT-60936
2023-08-22 12:14:42 +00:00
Roman Efremov 8aa3ccd342 [FIR] Store matched actual class members inside FIR attribute
Currently, there is only attribute `ExpectForActualAttributeKey`
where mapping is stored only for source declarations with `actual`
modifier. But we need mapping of all class members, including classes
which were actualized via `actual typealias` or fake override members.
This data will be needed for the annotation checker in subsequent
commits.

^KT-60668
^KT-60936
2023-08-22 12:14:42 +00:00