Commit Graph

451 Commits

Author SHA1 Message Date
Brian Norman e3ddeff230 [FIR] Track local variable assigned types for smartcast stability
When a lambda captures a local variable without a contract, future
assignments can impact the smartcast stability for that variable within
the lambda. Because future assignments can appear in both resolved and
unresolved code, track each assignment as encountered. To determine if
a smartcast can be considered stable, check that all future assignments
are resolved and do not revert variable to a super type.

^KT-58191 Fixed
2024-02-14 17:44:53 +00:00
Evgeniy.Zhelenskiy 7eab4b672d [K1, K2, CLI] Mark whole position range of compiler diagnostics in CLI
#KT-64989
2024-02-08 12:59:31 +00:00
Brian Norman 1f904aef55 [FIR] Enhance equality smartcasting test case to include data classes
^KT-58169 Fixed
2024-02-07 22:26:16 +00:00
Nikita Bobko d69deb97fe K2: Put boolean DFA variables under a feature flag
^KT-62547 Fixed

I also tested the commit by disabling DfaBooleanVariables in all tests
and checking what tests will fail

I didn't find anything that worked in K1, but doesn't work in K2 with
DfaBooleanVariables disabled

Related tests:
- DiagnosticCompilerTestFirTestdataTestGenerated$Resolve$Smartcasts
- LLFirPreresolvedReversedDiagnosticCompilerFirTestDataTestGenerated$Resolve$Smartcasts
- PreFirIdeSpecTestGenerated$NotLinked$Dfa
- DiagnosticCompilerTestFE10TestdataTestGenerated$Tests$SmartCasts
- FirPsiDiagnosticTestGenerated$Resolve$Expresssions.testSyntheticSmartCast
2024-01-31 16:31:02 +00:00
Dmitry Savvinov f28863b983 [k2, dfa] Use transitive closure of dependsOn for stability checks 2024-01-31 09:53:45 +00:00
Dmitry Savvinov 337330f8c8 [Tests] Add a test on smartcasts on properties from dependsOn modules
Behaviour is undesired, will be fixed in the next commit
2024-01-31 09:53:45 +00:00
Dmitry Savvinov 74fa6fc12c [k2, dfa] Smartcasts are not allowed on FirJavaField from other module
Handle case with FirField properly. Previously the code was returning
STABLE_VALUE in that case.

Note that in fact the changed 'when' branch expects only FirJavaField,
as FirFieldImpl should be handled by FirBackingFieldSymbol-check at
the beginning of the method. However, it is impossible to refer to
FirJavaField directly due to module visibility

Enumerate all cases more carefully via exhaustive 'when',
it's more readable and will prevent potential issues in future in case
more subclasses are added.

^KT-58279 Fixed
2024-01-31 09:53:45 +00:00
Dmitry Savvinov 6661a66fc3 [Tests] Add tests on smartcasts on static Java fields
Current behavior for different modules is undesired, will be fixed in
the next commit

^KT-58279
2024-01-31 09:53:45 +00:00
Brian Norman 76b9ba05fd [FIR] Lambda reassigned arguments should not have contracts applied
When applying function contracts to arguments, make sure the argument
variable hasn't been reassigned within a lambda. Contracts can only be
applied to unchanged variables, otherwise outdated type statements could
be added to the variable.

^KT-63151 Fixed
2024-01-30 17:23:56 +00:00
Brian Norman a2c9e5b36a [FIR] Copy implications from previous data flows when there is only one
Postponed lambdas introduce a host of challenges in data-flow analysis.
While inheriting type statements was disabled while these challenges are
being considered, we must still copy type statements from non-postponed
lambda edges. It seems the same is true for implications.

Implications are copied from previous flows when there is only a single
previous flow. That is because it never seemed to be required based on
test results. However, a recent test case revealed that copying is
required when there are multiple previous flows, but only one flow is
from a non-postponed-lambda node.

Combining implications from multiple non-postponed-lambda nodes did not
have an impact on test results, so until such a test case can be
created, the overhead of calculating common implications from multiple
flows will be avoided.

^KT-63351 Fixed
2024-01-30 17:22:31 +00:00
Brian Norman 7f9774819c Revert "[FIR] Do not smartcast class delegation implemented properties"
This reverts commit bb6f466162.

Reverting the fix for KT-57417 as it causes failures in IntelliJ and
Space projects. May reintroduce fix after failures are investigated and
resolved.
2024-01-29 19:24:03 +00:00
Brian Norman bb6f466162 [FIR] Do not smartcast class delegation implemented properties
When a property is implemented via class delegation, it should be
considered unstable for smart-casting. This is because it is unknown
what kind of stability the underlying class delegate property has. It
could be a stable property, or it could be implemented via a custom
getter and unstable.

^KT-57417 Fixed
2024-01-29 14:20:11 +00:00
Brian Norman 8f9bb75e60 [FIR] Allow identity equality to be used in contract implications
^KT-63256 Fixed
2024-01-17 14:59:59 +00:00
Nikolay Lunyak dd85a81d45 [FIR] Add a minimized alternative for orOperatorWithConstant.kt
It was decided to leave this code as is, because supporting
it would probably require introducing Implication-statements,
but this is not a good-enough use-case for such work.
2024-01-09 10:47:14 +00:00
Brian Norman aae8cd2a7c [FIR] Local variable assignment must be propagated before loops
When performing lookahead for local variable assignments, make sure
assignments taking place within loops are being propagated before loops.
This makes sure smartcasts within non-inline declarations before the
loop are disallowed.

^KT-63867 Fixed
2024-01-04 14:20:21 +00:00
Kirill Rakhman 1018ff280e [FIR] Fully expand the bounds of type parameters for canBeNull check
This is required because a not-null bound `Foo` can resolve to a
`typealias Foo = Bar?` in which case we must return true.

#KT-64645 Fixed
2024-01-03 17:15:50 +00:00
Brian Norman 3346cc4b19 [FIR] Lambdas without contracts should be considered repeatable
^KT-63777 Fixed
2024-01-03 14:08:04 +00:00
Aleksandra.Arsenteva c6b32200df [Test] Add diagnostic tests for smartcast in K2
List of issues:
KT-28806, KT-7186, KT-22997, KT-1982, KT-22996, KT-54443, KT-37308, KT-37115, KT-4113, KT-25747, KT-24779
2023-12-18 13:08:33 +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
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
Nikolay Lunyak 425d5e808b [FIR] Link some tests with issues
Just in case.

^KT-59874
2023-11-14 15:19:33 +00:00
Brian Norman 6df97dd199 [FIR] Add test cases to prove resolution of backlog tickets
^KT-41728 Fixed
^KT-46586 Fixed
^KT-49696 Fixed
^KT-51160 Fixed
^KT-53308 Fixed
2023-11-09 19:28:26 +00:00
Brian Norman b309786353 [FIR] RHS assignment during equality comparison should invalidate DFA
If the right-hand side of an equality comparison contains an assignment
to the variable used in the left-hand side, then implications about the
equality comparison within data-flow analysis cannot be applied, as the
value of the variable will be different after the comparison.

^KT-55096 Fixed
2023-11-06 22:12:22 +00:00
Ivan Kochurkin d50c6f1b6d [FIR] Fix missing USELESS_CAST
`FirUselessTypeOperationCallChecker` always checks exact types matching for `as` operator

Simplify code of cast checker and utils

^KT-56629 Fixed
^KT-56615 Fixed
^KT-59820 Fixed
2023-10-24 20:59:56 +00:00
Brian Norman ccda8ae23d [FIR] Comparison to String implies smart cast to String
When the left-hand side of an equality comparison is known to be a
String, and the equality condition resolves to true, then the right-hand
side can be smart-cast to a String as well. This was working for String
expressions on the left-hand side but not for String constants.

^KT-57513 Fixed
2023-10-10 11:20:40 +00:00
Brian Norman 2e6dbbe020 [FIR] Completely unwrap original argument when computing type arguments
When computing the conversion type for a type operator call, the
argument needs to be fully unwrapped before getting the resolved type.
This avoids the situation when the argument is a when-subject and the
assumed original type is not correct. Before, only smart-casts were
unwrapped, and this change will also unwrap when-subjects (as well as a
few other FirExpressions).

^KT-62114 Fixed
2023-10-05 14:12:06 +00:00
Ilya Kirillov d98da87278 [FIR] Render dot-separated FQNs instead of slash-separated ones in diagnostics
^KT-62030 fixed
2023-09-22 10:48:31 +00:00
Brian Norman f51b7faa1b [FIR] Only track assignment nodes for uncaught exception edges in CFG
UncaughtExceptionPath edges are used to influence smart-casting within
catch and finally blocks. Previously these edges were added from every
node which could throw an exception. But only assignment nodes influence
smart-casts by resetting inference back to some less specific type.
Therefore, instead of tracking every possible node which could throw an
exception - even though almost every statement node can - only add edges
from assignment nodes to catch and finally blocks. This fixes many
missing exception cases and also reduces the total number of incoming
edges to catch and finally blocks.

#KT-56872 Fixed
2023-09-05 11:59:13 +00:00
Kirill Rakhman 10f7989af6 [FIR] Report UNRESOLVED_REFERENCE on the first unresolved qualifier
#KT-55471 Fixed
2023-09-04 08:05:07 +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
Kirill Rakhman 8d7c5b375e [FIR] Replace usages of FirExpression.typeRef with coneTypeOrNull
#KT-59855 Fixed
2023-08-24 07:54:57 +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
Brian Norman 0e2b3ce845 [FIR] Create alternate DFA flows through finally blocks
Entering a `finally` block can happen from many different places:
through an exception, a jump, or normal exit from the `try` block. When
in the `finally` block, all DFA flows must be merged to have correct
smart casting. However, after the `finally` block, if exiting normally
or because of a jump, the combined flow from within the `finally` block
should not be used, but rather an alternate flow which combines the
correct flows from before the `finally` block.

```
try {
    str as String // Potential cast exception
} finally {
    str.length // Shouldn`t be resolved
}
str.length // Should be resolved
```

When building DFA flows, track the start of possible alternate flows,
and continue building them until they end. Both of these situations are
now marked on CFGNodes via interfaces.

When building the default DFA flow, and the source node is the end node
of alternate flows, attempt to use the alternate flow with the same edge
label instead of the default flow of the source node.

#KT-56888 Fixed
2023-07-27 13:05:58 +00:00
Brian Norman 1acdb3c143 [FIR] finally CFG exit node jump edges should match enter node edges
Jump edge targets are maintained globally within the CFG builder and are
added indiscriminately to `finally` exit nodes. This means that a
`finally` exit node could have a jump edge added which doesn't match how
the `finally` block was entered. Make sure edges are only added to the
exit node if they also exist on the enter node.

#KT-60723 Fixed
2023-07-27 13:05:58 +00:00
Kirill Rakhman 6318da0f02 [FIR] Remove or replace whole expressions in FIR diagnostic messages
FIR expressions rendered by FirRenderer don't look very nice in error
messages anyway, and additionally, they can become arbitrarily large,
so we shouldn't use them in messages.

#KT-59449 Fixed
2023-06-22 08:02:07 +00:00
Brian Norman 48484368c7 [Test] Support RENDER_DIAGNOSTICS_FULL_TEXT directive for FIR tests 2023-06-06 15:42:21 +00:00
Dmitriy Novozhilov 40b8b682f9 [FIR] Don't save DFA implications for unstable local vars
^KT-57502 Fixed
2023-05-10 14:19:25 +00:00
Kirill Rakhman b2fa104081 [FIR] Keep all failed resolution candidates and fully resolve them
Previously, when a candidate was found with an applicability that is
better than the current best applicability, all previous candidates were
thrown away. Now we keep them, unless the new applicability is
successful. If no successful candidates are found, we fully resolve all
the unsuccessful ones and select the ones with the least bad
applicability. This improves diagnostics for unresolved calls.

#KT-57844 Fixed
2023-05-10 11:48:58 +00:00
Dmitriy Novozhilov 1936658e40 [FIR] Consider stability of receiver for DFA variables
^KT-57425 Fixed
2023-05-10 07:50:31 +00:00
Dmitrii Gridin 72def186a3 [LL FIR] rework transformers, so transformers resolve only a specific set of declarations
The change is needed for the parallel resolution (^KT-55750), so we can resolve the declaration
under a lock that is specific to this declaration.
Previously, if LL FIR was resolving some FirClass, LL FIR  resolved all its children too, and it had no control over what parts of the FIR tree were modified.
The same applied to the designation path, sometimes the classes on the designation path
might be unexpectedly (and without lock) modified.

This commit introduces LLFirResolveTarget, which specifies which exact declarations should be resolved during the lazy resolution of the declaration.
All elements outside the declarations specified for resolve in LLFirResolveTarget, should not be modified.

The logic of lazy transformers is the following:
- Go to target declaration collecting all scopes from the file and containing classes
- Resolve only declarations that are specified by the LLFirResolveTarget, performing the resolve under a separate lock for each declaration

^KT-56543
^KT-57619 Fixed
2023-04-19 20:12:38 +00:00
Dmitriy Novozhilov f8ef478647 [FIR] Assume smartacsts on member properties from friend modules as stable
^KT-57893 Fixed
2023-04-12 08:59:03 +00:00
Dmitrii Gridin 9a4a3d1f49 [LL FIR] introduce test with reversed resolve order
^KT-56543

Merge-request: KT-MR-9299
Merged-by: Dmitrii Gridin <dmitry.gridin@jetbrains.com>
2023-03-22 17:34:07 +00:00
Mikhail Glukhikh e8232a7572 FE: add one more test related to KT-56511 with the relevant feature ON 2023-03-17 14:57:41 +00:00
Mikhail Glukhikh 41c868445c K1: introduce smartcast deprecation for a public API property in invisible class
#KT-56511 Fixed
Related to KT-57290
2023-03-17 14:57:40 +00:00
Mikhail Glukhikh 0b31e5ad63 K2: reproduce & add test for KT-56511 2023-03-17 14:57:40 +00:00
Denis.Zharkov a4c5e1bc87 K2: Unwrap fake overrides before using them as DFA identifiers
It's necessary because even for stable a.b.c.d we can't guarantee that
this reference will always point to the same symbol because
different capture type instantiations generate different scopes
with different resulting symbol instances.
2023-02-15 08:13:53 +00:00
Denis.Zharkov 2b3f34cc52 K2: Fix false-positive type equality for different captured types
This is mostly a revert of 2f61a2f56f
There, we erroneously assumed that we may take captured types as equal
if they are based on the same-typed projections.

Each instance of capturing defines its own captured type,
that should not be equal to any other type captured in other place.

Initial motivation was brought by FP Ultimate, where a piece of code
from the new test was found that started working differently after
recent changes.

The most obvious consequence is the change in addAllProjection.fir.kt:
one cannot use an instance as an argument when expected type
is captured type based on the same instance.

Otherwise, it would lead to CCE if we allowed to put arbitrary charsequences
to the list that initially was a MutableList<String>

All other test data changes (but addAllProjection.fir.kt and differentCapturedTypes.kt)
are irrelevant and will be fixed in the subsequent commits
2023-02-15 08:13:52 +00:00
Denis.Zharkov 9fa0f51a61 K2: Fix false-negative RETURN_TYPE_MISMATCH
^KT-53987 Fixed
^KT-55932 Fixed
2023-02-15 08:13:42 +00:00