Commit Graph

234 Commits

Author SHA1 Message Date
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
Roman Efremov 2f00ed3ed7 [FE] Refactor: reorganize methods in AbstractExpectActualAnnotationMatchChecker
Separate callable and class checks to prepare for checking
class scopes. Extract common checks to separate methods.

Also, it is found that in IR checker annotations on type parameters also checked,
because they stored in `matchedExpectToActual`. But it is expected that
only classes and callables are checked. This started to fail because of
added input parameter type checks inside `areAnnotationsCompatible`.
That's why `expectSymbol is IrTypeParameterSymbol` early-return is added.

^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 52f0f75175 [FE] Cleanup: rename areXyzCompatible -> getXyzCompatibility
Review: https://jetbrains.team/p/kt/reviews/11039/timeline

Motivation:
- Functions with prefix "are" must return Boolean. And
  AbstractExpectActualCompatibilityChecker even already contains some
  functions with prefix "are" that return Boolean (e.g.
  `areCompatibleCallableVisibilities`,
  `areCompatibleSupertypesOneByOne`, etc)
- Unification with functions that are prefixed with "are" and return
  Boolean
2023-08-10 15:46:48 +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 98ec13b51a [FE] Type-safety refactoring: extract main logic of areCompatibleCallables into two functions
Review: https://jetbrains.team/p/kt/reviews/11039/timeline

Extract main logic of `areCompatibleCallables` into two functions:
`areStrongIncompatibleCallables` and `areWeakIncompatibleCallables`.

The main point is that `areStrongIncompatibleCallables` &
`areWeakIncompatibleCallables` have very specific return types.

This commit doesn't change any logic. The commit makes the API more
type-safe ensuring that bugs like in previous commit (KT-60902) won't
happen again
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 fd5e97ff23 [FE] Refactoring: Move check for "enumConstructorsAreAlwaysCompatible" higher than check for callable kind
Review: https://jetbrains.team/p/kt/reviews/11039/timeline

This is a preparation refactoring for the following KT-60902 fix and
type-safety refactoring.

This commit doesn't have observable side effects (and can be called pure refactoring) because the following conditions:
1. `expectDeclaration is ConstructorSymbolMarker && actualDeclaration is ConstructorSymbolMarker`
2. `expectDeclaration is FunctionSymbolMarker != actualDeclaration is FunctionSymbolMarker`

can't be both `true` at the same time
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
Denis.Zharkov 1eb0c606da Add clarification comments to some shouldKeepTypeVariableBasedType impls
^KT-61090 Related
2023-08-10 12:54:09 +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 05cead2ab5 Fix implementation doesn't match name of property
Currently, property `hasSourceAnnotationsErased` returns the opposite
to what is stated in name. Invert it both in implementation and on
call site.

^KT-58551
2023-08-03 17:07:08 +00:00
Roman Efremov 991a016405 Remove obsolete TODO about repeatable annotations
^KT-60670
2023-08-03 17:07:08 +00:00
Roman Efremov 11ccad7e40 [FE, IR] Add expect actual annotations incompatibility info into diagnostic
This is needed for more beautiful reporting and easier
implementation of quick fix in IDE.

^KT-58551
2023-08-02 13:12:31 +00:00
Roman Efremov a64bac0b8c [FE] Refactor: differentiate two types of problems in annotation checker
This is needed for more beautiful reporting and easier
implementation of quick fix in IDE.

^KT-58551
2023-08-02 13:12:31 +00:00
Kirill Rakhman 951b299268 [FIR] Rewrite redeclaration checks
#KT-60124 Fixed
#KT-60573 Fixed
2023-07-27 08:06:20 +00: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 66824435b5 [FE] Add TODOs for changes for next PR within scope of this issue
^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 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
Roman Efremov a79282cec1 [FE] Prohibit actual typealias to certain compiler annotations
^KT-58554
2023-07-13 16:12:59 +00:00
Dmitriy Novozhilov 10054c584c [FIR] Attach YT tickets to meaningful TODOs in :fir:resolve module 2023-07-05 08:25:43 +00:00
Mikhail Glukhikh fa6b1e24f9 FE: perform IOT approximation after resolution & inference
#KT-54917 Fixed
2023-06-22 12:29:11 +00:00
Dmitriy Novozhilov 1b24b95cde [FIR] Check for subtyping during actualization of supertypes of expect class
^KT-59356 Fixed
2023-06-22 07:05:31 +00:00
Dmitriy Novozhilov d972b78627 [FIR] Allow actualization of expect classes by classes with wider visibility
^KT-59355 Fixed
2023-06-19 11:09:32 +00:00
Mikhail Glukhikh 35b475f9f7 K2: Fix processing inference lower bound NullableType <: T & Any
This commit is a follow-up to 0d070f8ba9.
Here we remove accidentally returning "Not a sub-type" for a constraint
like TypeVariable <: DNN type

#KT-59241 Fixed
2023-06-12 22:36:02 +00:00
Dmitriy Novozhilov 8b9079d026 [Expect/Actual] Add ability to skip matching of constructors of expect enums
In terms of MPP there are no such thing as `expect constructor` for enums,
  but they are physically exist in FIR and IR, so we need a switch which
  skips matching for them

FE 1.0 implementation did not touch to avoid any hidden changes
2023-06-10 07:33:29 +00:00