Commit Graph

117 Commits

Author SHA1 Message Date
Pavel Kunyavskiy e6f4d6e6fa [Tests] Use stable order for ir/kotlinLike dumps
^KT-65406
2024-02-16 10:19:38 +00:00
Brian Norman 17a1871b83 [FIR] Make sure the primary constructor is first in class CFG
The primary constructor of a class needs to be the first subgraph of the
class control-flow graph. Based on the Kotlin specification, class
initialization order goes first primary constructor, in-place
declarations (properties and init blocks), and then secondary
constructors. If the class doesn't have a primary constructor, then it
is just skipped in the order.

Unfortunately, the class control-flow graph had in-place declarations
first and then all constructors. Instead, we should treat the primary
constructor as the first in-place declaration, and then continue with
the existing processing as secondary constructors. This will guarantee
that super constructor calls have the correct property initialization
information.

^KT-65093 Fixed
2024-01-23 23:16:00 +00:00
Kirill Rakhman ad2689ecbf [FIR] Prefer the non-smartcasted receiver in resolution if it's visible
Previously, we would ignore the candidate with the non-smartcasted
receiver if they have the same symbol.
Now we prefer them when they're visible or when the smart cast changes
the nullability.
2023-12-21 13:29:01 +00:00
Brian Norman b2041e0927 [FIR] Disable data flow from in-place lambdas
There are many complications with the current design of passing data
from within in-place lambdas to surrounding code. Solving these
complications will involve more time to investigation than is available
within the K2 release. So we are disabling passing type statement
information from lambdas for the time being until more time can be
devoted to a more complete solution.

^KT-60958 Fixed
^KT-63530 Fixed
2023-12-14 16:40:27 +00:00
Dmitriy Novozhilov 39063e59f6 [FIR2IR] Treat assignment on smartcasted this in classes using FIR instead of IR
KT-57105
2023-12-14 08:32:58 +00:00
Dmitriy Novozhilov f5028bfc27 [Test] Reformat test for KT-57105 and add an additional case to it 2023-12-14 08:32:58 +00:00
Brian Norman 0881910a1b [FIR] Rewind DFA after call arguments for correct receiver smartcasting
^KT-63709 Fixed
2023-12-08 14:32:22 +00:00
Stanislav Ruban b275aeb39d [tests] Add test data for KT-63705 2023-12-01 19:20:26 +00:00
Brian Norman e92fab65aa [FIR] Support including flow information when dumping CFG dot file 2023-11-16 18:04:26 +00:00
Mikhail Glukhikh 4b1368409d K2: add a black box test to confirm KT-55705 now works properly 2023-11-15 09:33:05 +00:00
Dmitriy Novozhilov 8578a0bf6a [FIR2IR] Properly insert casts for smartcasts in argument position
Previously cast inserter didn't consider expected type for arguments
  of function calls

^KT-63257 Fixed
2023-11-10 07:50:01 +00:00
Dmitriy Novozhilov d08567c2c7 [FIR2IR] Approximate extension receiver type after type argument substitution
Frontend may leak captured types into type arguments, so they should be
  approximated before using them in fir2ir cast inserter to determine
 a specific type from smartcast

^KT-62863 Fixed
2023-11-07 12:37:19 +00:00
Dmitrii Gridin ec3cfdefcb [LL FIR] add missing file node to control flow graph
The file

^KT-62834 Fixed
2023-10-24 10:30:55 +00:00
Dmitrii Gridin 2eb761adae [LL FIR] generate resolution tests over codegen/box testData
Such testData can contain contract violations and other resolution
problems, so we should check them as well

^KT-62776
^KT-62832
^KT-62834
^KT-62836
2023-10-24 10:30:55 +00:00
Alexander Udalov 019836e9c7 IR: use origin DEFINED for value parameters of fake overrides
Fake overrides created by K1, as well as fir2ir in K2, use DEFINED. So
this gets rid of a lot of differences in IR text dumps between the K2
normal mode and K2 with IR fake overrides, and will ultimately help in
implementing and testing KT-61360.

The change in AddContinuationLowering is needed to fix tests like
kt47549. When creating anonymous classes for SAM implementations, we're
reusing the same fake override building machinery. And since we can't
extract any meaningful information from the fake override parameter's
origin anymore, we have to get the parameter from the original symbol.
2023-09-15 12:52:57 +00:00
Alexander Korepanov 6bb939c6cb [FIR JS] Support JS_NAME_CLASH and JS_FAKE_NAME_CLASH diagnostics
^KT-59425 Fixed
^KT-59370 Fixed
2023-09-08 09:55:25 +00:00
Pavel Kunyavskiy 91e96e9f31 [Fir2Ir] Introduce testing with IrFakeOverrideBuilder
^KT-61514
2023-08-31 13:12:15 +00:00
Brian Norman b55fda0c55 [FIR] Create CFG for files to track top-level property initialization
In order to properly analyze top-level property initialization, a
control-flow graph must be created for FirFiles. This change adds the
foundation for the file CFG and updates body resolve to create the CFG.
Checking the CFG for proper initialization is separated into a following
change to ease code review.

KT-56683
2023-08-31 12:50:52 +00:00
Dmitriy Novozhilov d4f55894b4 [FIR2IR] Properly calculate dispatch receiver type for fake overrides
If some function is not fake-override, then its type should be just
  default type of containing class
For fake overrides the default type calculated in the following way:
1. Find first overridden function, which is not fake override
2. Take its containing class
3. Find supertype of current containing class with type constructor of
   class from step 2

^KT-60252 Fixed
2023-08-25 21:06:15 +00:00
Mikhail Glukhikh 2ff075c961 FIR2IR: use ELVIS origin only on wrapping blocks #KT-60250 Fixed 2023-08-22 11:20:19 +00:00
Artem Kobzar 08bd0d6ce1 [K/JS] Generate tests for K2 + ES-classes compilation 2023-08-01 09:16:20 +00:00
Dmitriy Novozhilov fc57f48c8f [FIR] Union CFG edges from postponed lambdas only if outer call was analyzed independently
Before this change nodes unification was called if outer call was
  completed in the FULL mode, but it may happen even if this call is
  actually a part of some other outer call
2023-07-07 06:45:20 +00:00
Dmitrii Gridin 09ca335b7e [FIR] CFG: drop redundant edges
Edges from class to its functions and nested classes are redundant
and lead to extra resolution work in lazy resolve mode

^KT-59600 Fixed
2023-07-04 12:07:04 +00:00
Svyatoslav Kuzmich a3e2d2804c [Wasm] Update testData after adding K2 and new test infra support.
- Actualize muted K2 tests
- Actualize muted K1 tests with module systems because legacy Wasm test
  infra had no respect for "// MODULE: ..." test directives
2023-06-25 10:20:40 +02:00
Dmitriy Novozhilov 6409bf2fe8 [FIR] Store expressions of receivers inside candidates instead of ReceiverValue
This is needed because of mutable nature of receiver values: implicit
  receiver values can be modified because of smartcasts, but in candidate
  we need to store snapshot of receiver in the form it was at the beginning
  of the resolution

^KT-58823 Fixed
2023-06-09 22:51:31 +00:00
Dmitriy Novozhilov c55ddab1bf [Test] Reproduce KT-58823 2023-06-09 22:51:31 +00:00
Sergej Jaskiewicz f2031ae642 [IR] Don't print multifile/synthetic facade class names in irText tests
This only applies to JVM and fq-names in declaration references
in IR dumps.

This enables us to run more irText tests on platforms other than JVM
(see KT-58605).
2023-06-05 10:40:17 +00:00
Sergej Jaskiewicz fd76a34277 [IR] Don't print flags in declaration references in irText tests
This doesn't reduce the quality of tests, because the flags are still
printed for declarations themselves. We only omit them in references.

However, this makes the tests more compatible with non-JVM backends
(see KT-58605), because flags of referenced stdlib declarations may
differ among target platforms.
2023-05-30 17:26:30 +00:00
Dmitriy Novozhilov d7d7620db2 [FIR2IR] Correctly calculate signature for non-dynamic calls on dynamic receiver
If dispatch receiver is dynamic and call on it is not dynamic that means
  that this call was resolved to some specific declaration because of
  smartcast. So we should use original type of this declaration as
  dispatch receiver during calculation of IdSignature instead of Any
  (which came from dynamic type). Otherwise we will have two different
  signatures for the same member declaration

^KT-57682 Fixed
2023-04-03 12:20:01 +00:00
Kirill Rakhman 7a6a3a0b1d [FIR2IR] Fix generation of val initialization after smart-cast, again
#KT-57284
2023-03-17 13:29:48 +00:00
Kirill Rakhman 0e721c8fc2 [FIR2IR] Fix generation of val initialization after smart-cast
If neither setter nor backing field were found for an assignment call,
search overridden properties for backing fields.

^KT-57105 Fixed
2023-03-10 09:30:51 +00:00
pyos 9f17b5de97 FIR CFA: add edges according to constructor delegation 2023-01-26 13:12:11 +00:00
Artem Kobzar 71486a321c [K/JS] Add support of compilation with ES-classes 2023-01-17 18:14:17 +00:00
pyos e86b87fe0b Test: FIR CFA: fix the names of some nodes 2023-01-10 15:40:52 +02:00
pyos b548473544 FIR CFA: automatically compute node levels 2023-01-10 15:40:50 +02:00
pyos 17ee8f3a7b FIR CFA: put primary constructor before other class members 2023-01-10 15:40:50 +02:00
pyos c6e9afb788 FIR CFA: remove class initializer part nodes
Instead, attach subgraphs directly to the class enter node.
2023-01-10 15:40:50 +02:00
pyos 54f32a6fba Test: FIR CFA: index nodes in rendering order 2023-01-10 15:40:49 +02:00
pyos 4c6eff9174 FIR CFA: compute subgraph relationships automatically
No more `addSubGraph`. Also no more `owner` in graphs.

^KT-40526 Obsolete
^KT-40582 Obsolete
2023-01-10 15:40:48 +02:00
pyos cb8cb1f610 FIR CFA: attach accessors and nested classes as class subgraphs 2023-01-10 15:40:48 +02:00
pyos 5d4fb3ead8 FIR CFA: add control flow between default parameter values
function enter -> default 1 -> default 2 -> rest of function
                   \----------^ \----------^

This probably has no effect (in non-stupid code, at least), but it makes
graph construction more architecturally correct (now value parameters'
subgraphs get attached to a node).
2023-01-10 15:40:48 +02:00
pyos 74758278d7 FIR CFA: attach method graphs to class exit node
Even for non-local classes. This ensures that the CFG edges in this case
will only go to a subgraph, not to an unrelated graph.
2023-01-10 15:40:48 +02:00
pyos aadea0e26f FIR CFA: properly visit subgraphs in checkers
Interpretation: a graph A is a subgraph of B if information available at
nodes of A depends on the paths taken in B. For example, local classes
are subgraphs of a graph in which they are declared, and members of
those classes are subgraphs of the local class itself - because these
members can reference captured values.

Consequences:

 * if graph G is a subgraph of node N, then G is a subgraph of N's
   owner;
 * `ControlFlowAnalysisDiagnosticComponent` will only visit root graphs;
 * `graph.traverse` will ignore subgraph boundaries, as if all subgraphs
   are inlined into one huge root graph;
 * if a control flow checker needs information from a declaration to
   which a graph is attached, it must look at subgraphs explicitly.

For example, consider the `callsInPlace` checker. When a function
has a `callsInPlace` contract and a local declaration, the checker must
visit that local declaration to ensure it does not capture the allegedly
called-in-place argument - hence `graph.traverse` will look at the
nodes. However, the local declaration can also be a function with its
own `callsInPlace` contracts, so the checker should also run for it in
isolation. If that sounds quadratic, that's because unfortunately it is.
2023-01-10 15:40:48 +02:00
pyos faf0129a5d Test: FIR CFA: sort edges by style & target node id 2023-01-10 15:40:46 +02:00
Steven Schäfer 6af616d3c3 FIR: make declarations marked with 'override' implicitly open
#KT-52236 Fixed
2022-12-14 21:46:41 +00:00
pyos c4c05f5248 FIR CFG: remove ordering from control flow through in-place lambdas
Old graph:

  arg -> lambda enter -> ... -> lambda exit -> lambda enter -> ... ->
   -> lambda exit -> call

New graph:

  arg -+-> lambda enter -> ... -> lambda exit -+-> call
       \-> lambda enter -> ... -> lambda exit -/
2022-12-08 10:19:31 +00:00
pyos a9be27e330 FIR CFG: add union nodes
Quick quiz:

 Q: In a CFG, what does `a -> b -> c -> d` mean?
 A: `a`, then `b`, then `c`, then `d`.

 Q: In a CFG, what does `a -> b -> d; a -> c -> d` mean?
 A: `a`, then `b` or `c`, then `d`.

 Q: So how do you encode "a, then (b, then c) or (c, then b), then d`?
 A: You can't.

Problem is, you need to, because that's what `a; run2({ b }, { c }); d`
does when `run2` has a contract that it calls both its lambda arguments
in-place: `shuffle(listOf(block1, block2)).forEach { it() }` is a
perfectly valid implementation for it, as little sense as that makes.

So that's what union nodes solve. When a node implements
`UnionNodeMarker`, its inputs are interpreted as "all visited in some
order" instead of the normal "one of the inputs is visited".

Currently this is used for data flow. It *should* also be used for
control flow, but it isn't. But it should be. But that's not so easy.

BTW, `try` exit is NOT a union node; although lambdas in one branch can
be completed according to types' of lambdas in another, data does not
flow between the branches anyway (since we don't know how much of the
`try` executed before jumping into `catch`, and `catch`es are mutually
exclusive) so a `try` expression is more like `when` than a function
call with called-in-place-exactly-once arguments. The fact that
`exitTryExpression` used `processUnionOfArguments` in a weird way
should've hinted at that, but now we know for certain.
2022-12-08 10:19:29 +00:00
pyos 16b8811697 FIR DFA: take all statements from ?. if result is non-null.
I.e. a?.f(b as T) != null => b is T.

This also allows to remove the copyAllInformationFrom hack by moving the
edge directly in the control flow graph.
2022-12-08 10:19:28 +00:00
Ilya Chernikov 78ca733c38 FIR JS: add K2 variants of all other JS tests
except tests that are not possible to add without some modifications in
the test infra. See todos on the commented-out test declarations
2022-11-12 16:28:24 +01:00
Ilya Chernikov 5b3816cce5 Test infra: refactor IGNORE_BACKEND directive
treat it as a general one, introduce *_K1 and *_K2 variants for
more specific ignoring
2022-11-12 16:28:23 +01:00