Commit Graph

96304 Commits

Author SHA1 Message Date
Dmitriy Novozhilov 1b42298025 [FIR] Implement IMPLICIT_NOTHING_*_TYPE diagnostics 2022-11-22 15:46:19 +00:00
Dmitriy Novozhilov 0e84bf2053 [FIR] Don't report ARGUMENT_TYPE_MISMATCH on error types 2022-11-22 15:46:19 +00:00
Dmitriy Novozhilov 32f6b71525 [FE] Add ability to configure PublicDeclaration type approximation configuration
This configuration allows to choose what to do with local and anonymous types
2022-11-22 15:46:18 +00:00
Dmitriy Novozhilov 82c39187f6 [FIR] Refactor: rename function 2022-11-22 15:46:18 +00:00
Dmitriy Novozhilov 07567d6748 [FIR] Always fix type variables with UNKNOWN direction 2022-11-22 15:46:18 +00:00
Dmitriy Novozhilov 1b27d60307 [FIR] Support @OnlyInputTypes annotation
^KT-54807 Fixed
2022-11-22 15:46:17 +00:00
pyos 5cc08fb314 FIR DFA: check for reassignments of LHS when handling ?..
`x?.y != null` does not imply that `x != null` if e.g. an argument to
`y` has reassigned `x` in the meantime.

The same is true for `x == y` and `functionWithContract(x, y)`, but
those are somewhat harder to implement since there is no easy way to
find the last node of a certain argument.

^KT-55096
2022-11-22 15:44:38 +00:00
pyos ae1048f8a9 FIR DFA: try to group mutations to persistent data structures
Thinking of monomorphizing `Flow` (= `PersistentFlow`) and instead
adding a completely separate `MutableFlow` version; then `joinFlow`
would produce a mutable one, data flow analysis would add some
statements, convert to a persistent `Flow` and proceed to the next
node.
2022-11-22 15:44:38 +00:00
pyos ea1caf0955 FIR DFA: do not generate nodes (and flows) for contracts on calls
The code was already duplicated between FirDataFlowAnalyzer and
FirReturnsImpliesAnalyzer, so might as well use the latter to slightly
speed up the former.
2022-11-22 15:44:37 +00:00
pyos 67a6785f63 FIR DFA: move eq/notEq null-to-type translation to LogicSystem
This makes the `returns() implies` checker slightly cleaner, and also
fixes the case that I've missed where in RHS of `x ?:` type of `x` was
not set to `Nothing?`.
2022-11-22 15:44:37 +00:00
pyos 564eca58dd FIR DFA: don't update receivers' types if flow is unchanged 2022-11-22 15:44:36 +00:00
pyos 49f8de50c3 FIR DFA: reduce the number of redundant statements created
If a variable does not yet exist, then it should only be created if it
is a real variable and the implication or statement we're about to add
is about its type or nullability (which is also about the type).
Otherwise, the statement/implication will have no effect because there
are currently no implications referencing that variable, and if it's
synthetic then there will never be because we won't visit it again.

Hopefully this reduces the performance impact of flow forks.
2022-11-22 15:44:36 +00:00
pyos ae31275f73 FIR DFA: unwrap transparent expressions in more places 2022-11-22 15:44:35 +00:00
pyos 2e9cccd809 FIR DFA: add a utility function for intersecting with original type 2022-11-22 15:44:35 +00:00
pyos 5b08c300f4 FIR DFA: don't assume != true/false => == false/true
This also fixes some returnsNotNull contracts because the old code added
an implication that `== true` => `!= null` then promptly removed any
statement that this could've affected if the argument was a synthetic
variable.

^KT-26612 tag fixed-in-k2
2022-11-22 15:44:34 +00:00
pyos 98f52f13ef FIR DFA: remove approved and impossible statements from flows 2022-11-22 15:44:34 +00:00
pyos 5796f0eb07 FIR DFA: clean up LogicSystem API
Receivers have nothing to do with it.
2022-11-22 15:44:33 +00:00
pyos 1506c493c8 FIR DFA: do not re-approve statements from LHS of and/or
If the right-hand side is evaluated at all, then in its flow those
statements were already approved. Re-approving them erases the effect of
reassignments.

^KT-28369 tag fixed-in-k2
2022-11-22 15:44:32 +00:00
pyos edaca59d83 FIR DFA: fork flow everywhere
In theory, forking persistent flows should be cheap because of object
reuse, so the proposal here is to start from scratch and prove
redundancy of forks on a case-by-case basis. Something something better
safe than sorry.

^KT-28333 tag fixed-in-k2
^KT-28489 tag fixed-in-k2
2022-11-22 15:44:32 +00:00
pyos 757921e63e FIR DFA: remove exactNotType
Literally not used for anything anymore. What a waste of CPU time.

...and safeCallBreakInsideDoWhile is broken again. Oh well.
2022-11-22 15:44:31 +00:00
pyos 6ee6d019ee FIR DFA: align return-implies checker closer to smartcasts 2022-11-22 15:44:31 +00:00
pyos 512deeb07b FIR DFA: avoid inverting type statements
By necessity, type statements underapproximate. Therefore, inverting
doesn't always produce an equal result. It's best to propagate negations
inwards to preserve precision as much as possible.
2022-11-22 15:44:30 +00:00
pyos 39f6b50836 FIR DFA: slightly simplify {and,or}ForTypeStatements
And use them less in boolean expression analysis. This uncovers a fun
new case that doesn't work:

    interface I { val x: String }
    class C(override val x: String) : I
    class D(override val x: String) : I

    fun foo(x: Any?) {
      if (x is D || (x as C).x != "") {
        println(x.x.length) // actually OK (x is D || x is C => x is I)
      }
    }
2022-11-22 15:44:30 +00:00
pyos ac0137f45f FIR DFA: do not remove statements about LHS of !!
Value equality statements about the result also propagate to left-hand
side, so these statements can still be useful.
2022-11-22 15:44:29 +00:00
pyos 1ff968eca2 FIR DFA: extract approveStatementsInsideFlow(variable notEq null)
I'm not sure how this fixes a test. Magic!
2022-11-22 15:44:29 +00:00
pyos 623dfdd5a3 FIR DFA: generate more correct implications on as/as?
^KT-54851 Fixed
2022-11-22 15:44:28 +00:00
pyos 6a2d74e211 FIR DFA: generate type implications on all null comparisons 2022-11-22 15:44:28 +00:00
pyos bc9b358c9f FIR DFA: bring into compliance with De Morgan's laws
I don't even know if this affects any real code, but it did uncover
some deeper issues, like the fact that `!is` did not add any statements
when `true`.
2022-11-22 15:44:27 +00:00
pyos 3436535e7a FIR DFA: deprioritize branches with no reassignments in flow unions
callBothLambdas({ x = "..." }, { x is Int })
  // the assignment always executes, so x is String | (String & Int);
  // the latter is always a subtype of the former so it can be ignored
2022-11-22 15:44:27 +00:00
pyos fd609416c6 Minor: FIR DFA: do not merge type statements for aliases
The result is the same as for whatever is being aliased
2022-11-22 15:44:26 +00:00
pyos 2ae06a8d46 FIR DFA: when removing a variable, move type statements about dependents
val c = C("...")
  val d = c
  if (c.x == null) return
  c.x.length // c.x has type String
  d.x.length // d.x -> c.x through d -> c
  c = C(null) // remove alias d -> c
  d.x.length // info from c.x moved to d.x

^KT-54824 Fixed
2022-11-22 15:44:26 +00:00
pyos 3cea2d7243 FIR DFA: assert that type statements are not attached to aliases 2022-11-22 15:44:25 +00:00
pyos ceb1607057 FIR DFA: when breaking aliasing, keep the rest of the group together
var a = ...
  var b = a
  var c = a
  a = ...
  // b and c still aliased
2022-11-22 15:44:25 +00:00
pyos 7c8e9ac316 FIR DFA: remove RealVariableAndType 2022-11-22 15:44:24 +00:00
pyos 33363ba1fc Add more variable aliasing tests 2022-11-22 15:44:23 +00:00
Vladimir Dolzhenko a8bef8ac81 Prevent unsafe publication of not full initialized instance
#KTIJ-22182
2022-11-22 15:24:38 +00:00
Egor Kulikov 067036c162 RawFirBuilder refactoring
Second step for KT-52615
Get rid of PsiHandlingMode
Get rid of source in FirLazyBlock
Refactor lazy creation

Merge-request: KT-MR-7753
Merged-by: Egor Kulikov <Egor.Kulikov@jetbrains.com>
2022-11-22 14:43:53 +00:00
Pavel Kunyavskiy 27949bccf6 [K/N] Simplify enumsSupport 2022-11-22 13:51:23 +00:00
Pavel Kunyavskiy bc8ccc496b [K/N] Fix per-file caches with new instance functions 2022-11-22 13:51:23 +00:00
Pavel Kunyavskiy 68f78f1e5f [K/N] Make instance a property not a field
This enables inlining machinery, and allows to avoid frame in simple
cases.
2022-11-22 13:51:23 +00:00
Pavel Kunyavskiy d3adfec2fb [K/N] Rework object and enum classes initialization
Now it works with common logic for all static scope not with custom one.
2022-11-22 13:51:22 +00:00
Pavel Kunyavskiy 7006eb938d [K/N] Apply initializers lowering to any declaration container 2022-11-22 13:51:22 +00:00
Pavel Kunyavskiy 9516094755 [K/N] Rename file initializers to static initializers 2022-11-22 13:51:21 +00:00
Pavel Kunyavskiy d09a18c88b [K/N] Get rid of outdated module initializers 2022-11-22 13:51:20 +00:00
Pavel Kunyavskiy cf80a86855 [K/N] Some strange fix to make wasm target happy 2022-11-22 13:51:20 +00:00
Anna Kozlova 1906a41d9a fix testdata after merge 2022-11-22 13:31:41 +01:00
Marco Pennekamp 93f560eb4d [Analysis API] Add call resolution tests for KTIJ-23373
- The Java functions aren't recognized as candidates during the test
  (`FULL_JDK` isn't helping), so I've replicated the tests with local
  extension functions and confirmed that they uncover the same
  exception.
2022-11-22 13:13:35 +01:00
Marco Pennekamp 60dbe73f60 [Analysis API] Create substitutor from type arguments defensively
- Call candidate collection sometimes provides candidate symbols to
  `createConeSubstitutorFromTypeArguments` with fewer type parameters
  than type arguments provided by `FirQualifiedAccess`, which lead to
  an NPE. Because call candidates collected for the purposes of the
  Analysis API are best-effort guesses, we can ignore the additional
  type arguments.

^KTIJ-23373 fixed
^KTIJ-21506 fixed
2022-11-22 13:13:35 +01:00
Pavel Kirpichenkov ad6582160b Add test for KT-55019 2022-11-22 10:56:25 +00:00
Nikolay Lunyak c5469d5fb5 [FIR] KT-53988: Report ILLEGAL_SELECTOR for constants as selectors
Merge-request: KT-MR-7756
Merged-by: Nikolay Lunyak <Nikolay.Lunyak@jetbrains.com>

^KT-53988 Fixed
2022-11-22 10:54:09 +00:00