Commit Graph

251 Commits

Author SHA1 Message Date
Nikita Bobko 373abbde5d Partially revert "[FE] Stop ignoring ABSTRACT_MEMBER_NOT_IMPLEMENTED for expect classes"
^KT-61039 Fixed
KT-59739 is now open for K1 (but fixed in K2)
Review: https://jetbrains.team/p/kt/reviews/11867/timeline

This partially reverts commit 4f3ecedbca.
Only K1 part is reverted.

Motivation for revert: KT-59739 cannot be properly fixed in K1 because
of the bug it causes - KT-61039

We just accepted that we will have one more "green in K1 -> red in K2"
case
2023-08-30 14:35:59 +00:00
Nikita Bobko 3722f4d7d6 ActualClassifierMustHasTheSameMembersAsNonFinalExpectClassifierChecker: reuse "expect-actual" matcher
In scope of: KT-22841
Review: https://jetbrains.team/p/kt/reviews/11867/timeline

Reduce complexity by reusing "expect-actual matcher" (namely
`AbstractExpectActualCompatibilityChecker.getCallablesCompatibility`)

The current solution has worse algorithmic complexity. Previously it was
O(n) in the best case, where `n` is a number of members. Now, it's
O(m^2), where `m` is number of members in one overload group. But we
prefer to have worse complexity but reuse expect-actual matcher, number
of elements in one overload group shall not be big on real world
examples.

The previous logic was non-trivial because it compared types with with
double comparison in `equals`.
2023-08-30 14:35:56 +00:00
Nikita Bobko 17089276a7 Add regression tests for modality changes
Motivation: the discovered false negative in test
changeModalityFromAbstractToOpenInOverride.kt

Unfortunately, it can't be fixed in K1, so we will just live with that.
Luckily, we report ABSTRACT_MEMBER_NOT_IMPLEMENTED additionally.

In K2, MODALITY_CHANGED_IN_NON_FINAL_EXPECT_CLASSIFIER_ACTUALIZATION
will be correctly reported as a compilation error

In scope of: KT-22841
Review: https://jetbrains.team/p/kt/reviews/11867/timeline
2023-08-30 14:35:55 +00:00
Ivan Kochurkin d9b5d37baf [K2, MPP] Forbid matching actual callable with dynamic return type
to expect callable with non-dynamic return type

^KT-59251 Fixed
2023-08-28 09:29:05 +00:00
Roman Efremov 6b217369a8 [FE1.0] Fix false reporting of non-matching annotations in IDE...
...when typealiased expect class used in class literal.

^KTIJ-26700 Fixed
2023-08-23 13:15:20 +00:00
Roman Efremov 77ab13400e [FE1.0] Fix false reporting of non-matching actual annotations in IDE
It was reported because in composite module analysis we have both expect
and actual annotation descriptors, which may differ.
This may lead to bugs in the checker of ACTUAL_ANNOTATIONS_NOT_MATCH_EXPECT
diagnostic.

`fqName` comparison can be safely removed, because we already check
types in `areCompatibleExpectActualTypes`.

Tests:
- typealiasedAnnotation.kt - for annotation use (so it has different
ClassId)
- typealiasedAnnotationAsArgument.kt - when typealiased annotation
used in another annotation as argument
- sourceRetentionAnnotationsWhenTypealias.kt - tests same as
compiler/testData/diagnostics/tests/multiplatform/annotationMatching/sourceRetentionAnnotationsWhenTypealias.kt

No special test added for opt-in, because it's prohibited to have
expect/actual opt-ins by EXPECT_ACTUAL_OPT_IN_ANNOTATION checker.

^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 f1ea6545eb [IR] Fix incorrect position of reporting with fake overrides inside actual
^KT-60668
^KT-60936
2023-08-22 12:14:42 +00:00
Roman Efremov 58a30d524c [IR] Fix positioning of actual annotations diagnostic when reported on value
`parentsWithSelf` returns empty sequence if receiver is not a
`IrDeclarationParent`.

^KT-60668
^KT-60936
2023-08-22 12:14:42 +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 673b4b4d52 [FE] Add OptIn annotation to whitelist of annotation checker
It's valid to have `@OptIn` only on `expect` declaration.

^KT-61373
2023-08-22 09:02:20 +00:00
Nikita Bobko 25c082f02b K1: Implement a checker that disallows to have different member scopes for expect open and its actual
^KT-22841 Fixed
Review: https://jetbrains.team/p/kt/reviews/11603/timeline

The commit also introduces `@AllowDifferentMembersInActual` annotation in
stdlib which allows to suppress the diagnostic
2023-08-21 19:51:08 +00:00
Kirill Rakhman d0cc86f52c [Tests] Update test data after changes to FIR diagnostic messages 2023-08-21 16:28:48 +00:00
Kirill Rakhman 5b9c35de2e [Tests] Update test data with error suppression warnings
#KT-61129 Fixed
2023-08-18 13:29:20 +00:00
Nikita Bobko 577d0db3f1 [FE] Skip delegation descriptors during default params matching
^KT-61163 Fixed
Review: https://jetbrains.team/p/kt/reviews/11599/timeline

They can be safely skiped because we check overridden descriptors
anyway.

IMO, it's a bug that delegated descriptors are copied with default
params. But it's much harder problem, and my IMO can easily be wrong for
some weird reason.
2023-08-15 16:34:23 +00:00
Ivan Kochurkin d4d30dcfcf [FIR] Introduce OPTIONAL_EXPECTATION_NOT_ON_EXPECTED diagnostic
^KT-60892 Fixed
2023-08-10 17:46:08 +00:00
Ivan Kochurkin 8e8de513ee [FIR] Introduce OPTIONAL_DECLARATION_USAGE_IN_NON_COMMON_SOURCE diagnostic
^KT-60892
2023-08-10 17:46:08 +00:00
Ivan Kochurkin b08df32cac [FIR] Introduce OPTIONAL_DECLARATION_OUTSIDE_OF_ANNOTATION_ENTRY diagnostic
^KT-60892
2023-08-10 17:46:08 +00:00
Ivan Kochurkin dc1f1fe979 Move optionalExpectationIncorrectUse from MPP integration to diagnostics tests 2023-08-10 17:46:08 +00:00
Nikita Bobko d4758014ec [FE] Type-safety refactoring: Make areCompatibleClassScopes to return Incompatible.WeakIncompatible
Review: https://jetbrains.team/p/kt/reviews/11039/timeline

For StrongIncompatible `actual` declaration is considered as overload
and error reports on expected declaration. For WeakIncompatible the
error is reported straight away

Before the refactoring `areCompatibleClassScopes` returned just
`Incompatible`. It is bad because StrongIncompatible isn't possible for
classes (classes can't be overloaded). Now all class incompatibilities
are weak.

The commit has a minor impact on observable behavior (cases where we
reported the compilation problems are still reported but on another
elements):
- We no longer report type parameter class incompatibilities on expect
  declaration, we report them only on actuals (it happened because all
  WeakIncompatible are reported only on actuals)
- In a sense, Java implicit actualization was the only way to "overload"
  classes (it would be a redeclaration compilation problem, so it
  doesn't count as a valid "overload"). And since type parameters
  incompatibility was StrongIncompatible for classes, we counted them as
  "overloads" and didn't report incompatibility problems on Kotlin
  class. Now we do report. (see
  implicitJavaActualization_multipleActuals)
2023-08-10 15:46:48 +00:00
Nikita Bobko eac4b81b11 [FE] Fix TypeParameterUpperBounds incompatibility priority
^KT-60902 Fixed
Review: https://jetbrains.team/p/kt/reviews/11039/timeline

We should prioritize to return STRONG incompatibilities over WEAK
incompatibilities. But this invariant broke in `areCompatibleCallables`,
because `areCompatibleTypeParameters` returns incompatibilities of both
types, and `areCompatibleTypeParameters` is called in WEAK
incompatibilities section.

The fix is to split `areCompatibleTypeParameters` into two functions:
`areStrongIncompatibleTypeParameters` and
`areWeakIncompatibleTypeParameters`. And call each of this function in
appropriate `areCompatibleCallables` sections.
2023-08-10 15:46:47 +00:00
Nikita Bobko d39755b578 [FE] Convert specific diagnostic for actual function with default arguments into a common incompatibility
^KT-59665 Fixed
Review: https://jetbrains.team/p/kt/reviews/11039/timeline

It's better to have this logic in common place
(AbstractExpectActualCompatibilityChecker) to avoid missing compilation
errors in the future

This commit fixes:
1. Missing compilation error for actual function with default arguments
   for 'actual typealias' KT-59665
2. Missing compilation error for actual function with default arguments
   for actual fake-override KT-59665

Alternative solution for KT-59665 is to create a special checker.

"incompatibility" vs "special checker":

Arguments for common incompatibility:
- What if we had a rule that expect and actual default params must
  match? If so then it certainly would be an incompatibility.
- Technically, we do the matching of expect and actual params (because
  we allow default params in common ancestors of expect and actual
  declarations).
- It's hard to check that the actual definition doesn't use default
  params because `ExpectedActualResolver.findActualForExpected` filters
  out fake-overrides and doesn't return them. It's not clear logic for
  me, that I'm afraid to touch.
  implicitActualFakeOverride_AbstractMap.kt test breaks if you drop this
  weird logic
- WEAK incompatibilities can be considered as "checkers". So it doesn't
  matter how it's implemented, as a "incompatibility" or a "checker"

Arguments against common incompatibility:
- Although we match expect and actual declarations to allow default
  params in common ancestors of expect and actual declarations, it's
  still can be considered that we check that the actual declaration
  doesn't have default params. And it doesn't feel right that we check
  correctness of the actual declaration in expect-actual matcher.
- ~~It may change the rules of expect actual matching~~ (It's not true,
  because ActualFunctionWithDefaultParameters is declared as WEAK
  incompatibility)
2023-08-10 15:46:46 +00:00
Nikita Bobko ab8913dee8 [FE] Cover "implicit actualization"/"actual fake-overrides" KT-58544 with regression tests
Review: https://jetbrains.team/p/kt/reviews/11039/timeline
2023-08-10 15:46:45 +00:00
marat.akhin ae4fab8483 [KMP] Allow matching expect ctorless final classes to objects
both directly and via typealias.

This is a possible fix to KT-59747
2023-08-08 23:18:35 +00:00
Roman Efremov 6943d03883 [FE, IR] Make expect actual annotations diagnostic message more friendly
Print which annotation exactly has a problem instead of
printing whole declarations with all annotations.

^KT-58551
2023-08-02 13:12:31 +00:00
Roman Efremov 31f21d8593 [IR] Fix IR expect-actual annotations checker in case when annotation is actual typealias
^KT-60830 Fixed
2023-08-01 14:10:04 +00:00
Roman Efremov 5ed3f308fa Add tests for repeatable annotations on expect-actual declarations
Add tests showing current behavior.

^KT-60670
2023-08-01 14:04:03 +00:00
Roman Efremov 3a3b30aef4 Fix mistake in name of test
^KT-58551
2023-08-01 07:08:24 +00:00
marat.akhin 34b271b13d [KMP] Implement ACTUAL_TYPE_ALIAS_TO_NOTHING/TO_NULLABLE_TYPE errors
^KT-60650 Done
2023-07-27 09:13:37 +00:00
Nikita Bobko f4ba5aaf9a [FE 1.0] Prohibit implicit Java actualization in K1
^KT-58545 Fixed
Review: https://jetbrains.team/p/kt/reviews/10561

It's not yet supported in K2 KT-59213

Related tests:
- ApiTest.testStdlib
- RuntimePublicAPITest.kotlinStdlibRuntimeMerged
- KotlinProjectViewTestGenerated.test_Arrays (in Kotlin plugin)
2023-07-25 22:30:09 +02:00
Kirill Rakhman 53ff4584d4 [FIR] Implement DEFAULT_VALUE_NOT_ALLOWED_IN_OVERRIDE diagnostic
#KT-59409 Fixed
2023-07-25 11:25:43 +00:00
Roman Efremov cedfdbdc66 Fix test became invalid after KT-59739
^KT-58551
2023-07-24 14:25:26 +02:00
Roman Efremov 6611a55a60 [FE] Relax rules of matching @Target annotation on expect and actual
Allow `expect` targets to be subset of `actual`.

^KT-58551
2023-07-24 09:48:49 +00:00
Roman Efremov ad84c83ee9 [FE] Skip more special annotations in KMP annotation matching
Many errors are reported in stdlib with these annotations
(SinceKotlin, Deprecated, so on).
But having them only on expect is a valid case. E.g. SinceKotlin added
if some old platform-specific API becomes commonized.

^KT-58551
2023-07-24 09:48:49 +00:00
Roman Efremov 1dcdcee452 [FE] Add test for intrinsic const evaluation in KMP annotation matching
^KT-58551
2023-07-24 09:48:49 +00:00
Roman Efremov 234f453173 [FE, IR] Don't check SOURCE annotations if actual declaration has no source
^KT-58551
2023-07-24 09:48:49 +00:00
Roman Efremov 454756a2b5 [IR] Don't check annotations on expect/actual fake overrides
^KT-58551
2023-07-24 09:48:48 +00:00
Roman Efremov 2980179bd7 [IR] Implement IR checker for expect actual annotations matching
^KT-58551
2023-07-24 09:48:48 +00:00
Roman Efremov b6cae1adcc [FE] Allow @OptionalExpectation to be present only on expect
^KT-58551
2023-07-24 09:48:48 +00:00
Roman Efremov 1a4ab9bb4b [FE] Implement FE logic of expect actual annotations matching
This implementation only checks annotations set on expect/actual
declarations and requires further refinement (e.g. checking of other
annotation targets, class scopes within typealiases).

^KT-58551
2023-07-24 09:48:48 +00:00
Nikita Bobko 4f3ecedbca [FE] Stop ignoring ABSTRACT_MEMBER_NOT_IMPLEMENTED for expect classes
^KT-59739 Fixed
Review: https://jetbrains.team/p/kt/reviews/11038/timeline
2023-07-24 09:15:11 +00:00
Ivan Kochurkin 8c39b2f71d [FIR] Report missing NO_ELSE_IN_WHEN for when statement with expect enums and sealed classes
^KT-59404 Fixed
2023-07-20 11:04:17 +00:00
Kirill Rakhman ff6b3350ae [FIR] Resolve array literal with non-primitive-array expected type as arrayOf call
This lets us properly complete the call which fixes some issues with
false-positive type mismatches.
This change doesn't apply to array literals in annotation calls yet
because they are resolved as context-dependent.
This will be adapted in a following commit.

#KT-59581
2023-07-19 13:34:58 +00:00
Ivan Kochurkin 5f1e2764c3 [FIR] Fix some missing EXPECTED_DECLARATION_WITH_BODY 2023-07-19 10:14:08 +00:00
Ivan Kochurkin 8f4a580c17 [FIR] Report EXPECTED_CLASS_CONSTRUCTOR_DELEGATION_CALL
^KT-59069 Fixed
2023-07-19 10:14:08 +00:00
Kirill Rakhman 883102c4c6 [FIR] Always run expect actual matching for actual annotation/inline class primary constructor
Previously, we didn't run it when the primary constructor itself didn't
have the actual modifier even though the modifier is optional.

#KT-60120 Fixed
2023-07-17 12:06:37 +00:00
Roman Efremov a79282cec1 [FE] Prohibit actual typealias to certain compiler annotations
^KT-58554
2023-07-13 16:12:59 +00:00
Ivan Kochurkin f4a648aa3e [K2, MPP] Fix NO_ACTUAL_FOR_EXPECT reporting for all modules including the platform ones
^KT-58483 Fixed
2023-07-13 16:01:26 +00:00
Roman Efremov 4a598afc36 [FE] Prohibit expect or actual opt-in annotations
^KT-58554
2023-07-13 08:15:42 +00:00