290 Commits

Author SHA1 Message Date
Kirill Rakhman 672512d19d [FE] Skip CST computation if list of types has exactly one entry
Otherwise, we can get in a situation where the single item is flexible,
and we replace its attributes with the attribute of the lower bound,
which messes up `EnhancedTypeForWarningAttribute`.

#KT-65193 Fixed
2024-03-18 15:52:50 +00:00
Kirill Rakhman 85a1d67d19 [FIR] Fix giant type produced by CST
#KT-65704 Fixed
2024-03-18 08:41:26 +00:00
Nikita Bobko a8275c99a3 [FIR, IR] Check for strict subtypes during actualization
^KT-65775 Fixed
Review: https://jetbrains.team/p/kt/reviews/14906/timeline
2024-03-14 10:53:43 +00:00
Mikhail Glukhikh d04625666a K2: don't approximate captured types in ResultTypeResolver in certain cases
To be more precises, ResultTypeResolver for K2 now searches for
similar LOWER/UPPER constraints pair based on the same flexible type,
like LOWER(CapturedType&Any..CapturedType?) and UPPER(CapturedType!).
If such a pair is found, the CapturedType is not approximated.
This is done to avoid a big difference between this case and
completely same constraints, like LOWER(CapturedType!) and
UPPER(CapturedType!). In this case we squash them to EQUAL(CapturedType!)
even before ResultTypeResolver, and captured types which is got from
an EQUAL constraint are not approximated even before this commit.

This commit fixes back a case from KT-50134 and a problem with intellij.vcs.git

#KT-65596 Fixed
2024-03-11 13:38:05 +00:00
Ivan Kochurkin 2d4f4b9bb5 [FIR] Disallow operators not on functions
Introduce `NOT_FUNCTION_AS_OPERATOR` and use it instead of `PROPERTY_AS_OPERATOR`

^KT-65881 Fixed

Merge-request: KT-MR-14547
2024-03-08 10:07:23 +00:00
Nikita Bobko 4c602585b0 [FIR, IR] Cleanup: drop unused allowClassActualizationWithWiderVisibility & allowTransitiveSupertypesActualization
These flags were used when ExpectActualMatchingContext was reused in K1.
2024-02-25 18:18:55 +00:00
Kirill Rakhman ef29879740 [FIR] Remove hack for @OnlyInputTypes
#KT-65704
2024-02-12 08:47:10 +00:00
Kirill Rakhman b90598823e [FIR] Improve approximation of captured types
- Handle flexible types in recursion check
- Handle intersected supertypes separately
- Make check when not to approximate captured types in type argument
  position more fine-grained.
  Only apply it to case when the captured type is replaced by a star
  projection.
  All other cases are handled by recursive calls to
  approximateCapturedType

#KT-65377 Fixed
2024-02-12 08:47:10 +00:00
Denis.Zharkov b86c0d990e Minor. Reformat PCLA related part at ResultTypeResolver 2024-02-09 19:47:35 +00:00
Mikhail Glukhikh 195d3604ae Add 'K2Only' OptIn for 'usesOuterCs' 2024-02-09 11:38:34 +00:00
Mikhail Glukhikh f3523edb61 Minor cleanup of approximateCapturedType comments/code 2024-02-08 10:49:55 +00:00
Mikhail Glukhikh 44dcae2090 Rename: type -> capturedType 2024-02-08 10:49:55 +00:00
Mikhail Glukhikh 94bcf6d87f K2: handle recursive types properly in approximation
This commit is intended to deal with inconsistency in K1/K2
star projection handling.
K1 star projection includes a 'type' property.
This type from a star projection can be used for relevant
functions / properties return types,
and already includes some approximation for recursive generics.
In contrast, K2 star projection is an object,
and return types of relevant functions / properties are
represented as captured types.
To prevent recursion in them in recursive generic case,
this commit includes additional replacement of their type arguments.
See more details in added comments.

#KT-65057 Fixed
2024-02-08 10:49:55 +00:00
Kirill Rakhman 2f4860886a [FE] Add todos
#KT-65228
2024-01-25 08:27:47 +00:00
Kirill Rakhman 45f5672b50 [FE] Reuse result of approximate calls in approximation of captured type
#KTIJ-28549 Fixed
2024-01-25 08:27:47 +00:00
Kirill Rakhman 582dd1d3c0 [FIR] Only don't approximate nested captured arguments if they have recursive supertypes
This fixes a compiler crash
IllegalStateException: Captured type for incorporation shouldn't escape
from incorporation

The crash occurs when a captured type with status FOR_INCORPORATION
is two layers deep inside a captured type with status FROM_EXPRESSION.
We first check if approximation is required for the most outer captured
type in AbstractTypeApproximator.approximateCapturedType.
Then we encounter the second captured type with status FROM_EXPRESSION
in AbstractTypeApproximator.approximateParametrizedType.
At this point, we stop checking and miss the third captured type with
status FOR_INCORPORATION.

Unfortunately, we can't check recursively if nested captured types
need to be approximated because of types with recursive super types
(the original reason why the extra check was introduced).
That's why we restrict the second check to types with recursive
super types, effectively restoring the previous behavior for all other
types.

#KT-65050 Fixed
2024-01-18 09:22:15 +00:00
Kirill Rakhman 9d91eb2510 [FIR] Fix inference based on recursive upper bound
#KT-59012 Fixed
2024-01-17 08:20:06 +00:00
Kirill Rakhman 251827c9aa [FIR] Don't approximate captured types
This fixes some type argument mismatch errors caused by a captured type
being approximated and then captured again.
Some places need to be adapted to work with captured types that
previously only worked with approximated types.

#KT-62959 Fixed
2024-01-17 08:20:05 +00:00
Denis.Zharkov 276f5b26d8 K2: Implement partially constrained lambda analysis (PCLA)
It's expected to partially mimic the behavior of what
previously was called builder inference, but with more clear contracts
(documentation is in progress, though)

See a lot of fixed issues in the later commits with test data,
especially [red-to-green]

^KT-59791 In Progress
2024-01-10 14:56:30 +00:00
Nikita Bobko 013b5e3780 [IR] Fix missing AMBIGUOUS_ACTUALS
^KT-59938 Fixed
Review: https://jetbrains.team/p/kt/reviews/13760

After this patch:

    > The /compiler/testData/diagnostics/tests/multiplatform/topLevelFun/conflictingImplDeclarations.kt test:

    Fixed

    > The /compiler/testData/diagnostics/tests/multiplatform/headerClass/expectDeclarationWithWeakIncompatibilities.kt test:

    Declarations are in the same module. `PACKAGE_OR_CLASSIFIER_REDECLARATION` is reported anyway

    > The /compiler/testData/diagnostics/tests/multiplatform/java/varPropertyAgainstJavaGetterAndNonFinalField.kt test:

    Expect-actual matcher doesn't match fields in K2 https://youtrack.jetbrains.com/issue/KT-63667 => green code

    > The /compiler/testData/diagnostics/tests/multiplatform/java/propertyAgainstJavaPrivateFieldAndPublicMethod.kt test:

    Expect-actual matcher doesn't match fields in K2 https://youtrack.jetbrains.com/issue/KT-63667 => green code

    > The /compiler/testData/diagnostics/tests/multiplatform/java/implicitJavaActualization_multipleActuals.kt test:

    K2 doesn't have implicit Java actualization. And PACKAGE_OR_CLASSIFIER_REDECLARATION is reported anyway

    > The /compiler/testData/diagnostics/tests/multiplatform/java/propertyAgainstJavaPublicFieldAndPublicGetter.kt test:

    Expect-actual matcher doesn't match fields in K2 https://youtrack.jetbrains.com/issue/KT-63667 => green code

    > The /compiler/testData/diagnostics/tests/multiplatform/actualClassifierMustHasTheSameMembersAsNonFinalExpectClassifierChecker/injectContextReceiverOverload.kt test:

    Context receivers are not supported in expect-actual matcher. https://youtrack.jetbrains.com/issue/KT-61447
    And K2 reports another error right now anyway
2024-01-10 08:08:42 +00:00
Denis.Zharkov c163f86ecb Minor. Reformat some inference files 2024-01-09 18:03:05 +00:00
Denis.Zharkov cb5070e335 NI: Add TODO for removing ALL_CONSTRAINTS_TRIVIAL_OR_NON_PROPER
^KT-63032 Related
2024-01-08 11:26:31 +00:00
Denis.Zharkov 90fcf086bb NI: Clean things around hasOnlyTrivialProperConstraint
- Get rid of unused val and its initialization
- Use a bit clearer naming
- Simplify the definition of `allConstraintsTrivialOrNonProper`
2024-01-08 11:26:30 +00:00
Kirill Rakhman 49ae1b8d01 [FIR2IR] Consolidate type approximation
This commit gets rid of the redundant typeApproximatorConfiguration
in Fir2IrTypeConverter and uses the type approximator for captured
types instead of the manual approximation used before.

This fixes the nullability of approximated captured types, which fixes
a runtime error in WASM.

This also brings K2 IR closer to K1 IR in one test.

#KT-64261 Fixed
2023-12-21 15:56:49 +00:00
Roman Efremov 30aad31ece [FIR, IR] Prohibit actualization of expect Kotlin property to Java field
The problem from KT-63624 was that during matching phase we must choose
only one candidate, but in Java we can have two successfully matched
properties: 1) from field and 2) from method, which overrides Kotlin
property.
See test `propertyAgainstJavaPrivateFieldAndPublicMethod.kt`.
As a result, we choose field candidate, throw away method candidate, and
then fail during visibility check.

Instead of inventing special rule of prioritizing field over method
it was decided to prohibit actualization to Java field at all because:

1. It doesn't seem that Java fields actualization was implemented in K1
on purpose
2. People usually don't use public Java fields, and use instead
private field + getter, especially when compatibility is important, so
it shouldn't be a breaking change

Besides that, such solution simplifies code and is consistent with
the current logic of matcher, which doesn't expect that two members
can be matched successfully. Also, it fixes KT-63624 and KT-63667.

^KT-63624 Fixed
^KT-63667 Fixed
2023-12-20 13:37:43 +00:00
Kirill Rakhman 3bfcf3090c [FE] Use indexed loop to prevent CME in incorporation
#KT-60225 Fixed
2023-12-15 08:47:09 +00:00
Mikhail Glukhikh 06ce57ea56 K2: report MISSING_DEPENDENCY_CLASS for lambda parameters if needed
#KT-62525 Fixed
2023-12-05 18:16:52 +00:00
Nikita Bobko 2a7403a979 [FIR, IR] Run DefaultArgumentsInExpectActualizedByFakeOverride not only in FIR but in IR as well
Review: https://jetbrains.team/p/kt/reviews/13334/timeline

Thanks to the previous commit, it's now possible to run
DefaultArgumentsInExpectActualizedByFakeOverride on both: frontend (FIR)
and backend (IR).

We aim to perform a thorough examination of checks involving
fake-overrides on both FIR and IR, given their distinct implementations
for handling fake-overrides.

The commit decreases scope of influence of hacky
`shouldCheckDefaultParams` flag.
2023-12-05 13:30:14 +00:00
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