Commit Graph

181 Commits

Author SHA1 Message Date
Paul Berry 20d9c6eb3a Flow analysis: implement "why not promoted" logic for boolean conditions
Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: Ia8f3a5ad2971dfb6baeefb0d2f9c6998af0d897e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192611
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2021-03-24 12:10:09 +00:00
Paul Berry eb37227666 Flow analysis: implement "why not promoted" logic for logical binary operators.
Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: If03750edf6bdf13fc2677e0c09f49f70d0da744e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192608
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2021-03-24 12:10:09 +00:00
Paul Berry 6c665c4607 Flow analysis: implement "why not promoted" logic for most binary operators.
`&&` and `||` will be addressed in a follow-up CL go through a
different code path in the analyzer.

Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: I20732ceceb113017a0b1e77134b1e28c4c924fbb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192607
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2021-03-24 12:10:09 +00:00
Paul Berry dd91769e90 Flow analysis: implement "why not promoted" functionality for arguments.
Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: Ib39ef04aadc2340c269878763e19a4a97001e2b5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/191989
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-03-22 17:31:54 +00:00
Paul Berry 00c188bb2e Flow analysis: Improve unit test failure output for _CheckPromoted.
The failure message now includes the stacktrace from when the
_CheckPromoted node was constructed; this makes it possible to quickly
find the line in the source code that encodes the expectation, which
is often much more useful than the stacktrace from the time the
failure was detected.

The stacktrace from the time the failure was detected still appears,
after the stacktrace from when the node was constructed.

Change-Id: If375e141a32b371e806399e42ff7cfaae9cf7873
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/192290
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-03-22 17:29:14 +00:00
Paul Berry 681d3e2a43 Allow the expensive parts of "why not promoted" computation to be deferred.
This is necessary because `whyNotPromoted` needs to be called right
after visiting a subexpression, but it's not always possible to tell
that the type of that subexpression will result in an error until more
code has been visited.

Change-Id: If44e2bb55612c1ba7996b523c3ec078d7b80c865
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/191601
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2021-03-18 19:52:13 +00:00
Paul Berry 217cd49aaf More "why not promoted" test cases for for-in loops
Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: Iea44cd482f21ef4406b28e264046025ce3f87695
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/191385
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-03-16 17:22:33 +00:00
Paul Berry 01ad9f1e8d Make flow analysis tests use a more sophisticated Type data structure.
Rather than represent types by strings in the flow analysis tests, we
have a "mini type" language that can represent function types, type
parameters, nullabilities, and promotions.  This should make it easier
to build more sophisticated tests for shared code.

Change-Id: I66d15ad3dc6a871958ed01654600aea749fa0e65
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/190340
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2021-03-10 14:53:58 +00:00
Paul Berry b661bf12c5 Front end: Additional "why not promoted" functionality.
This CL implements "why not promoted" functionality in the front end
for the following scenarios:
- null iterable in for-loop
- null iterable in yield* statement
- null iterable or map after spread (`...`) operator

Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: I471b8bf558341514207fad527dde009f1372182c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/188160
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-03-10 14:52:19 +00:00
Paul Berry 73f9d0cd26 Flow analysis: Remove unnecessary argument from tryFinallyStatement_end.
Flow analysis stopped using this argument (the AST node for the
finally block) some time ago, but I kept it around so I could assert
that clients didn't unnecessarily store assigned variables info for
it.  It's been long enough now that we can eliminate this code
entirely.

Change-Id: I8c64e2b4fc5b154f441ec2d057637f3dc9ced277
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/190060
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-03-10 13:14:08 +00:00
Paul Berry 8be535350d Flow analysis: improve handling of types in "why not promoted" logic.
This CL plumbs the types of `this` and property get expression from
the CFE and analyzer into flow analysis, so that flow analysis will be
able to create more accurate "why not promoted" information for those
expression types.  This made it possible to eliminate a clumsy aspect
of the previous implementation, namely that we would consider a
promotion attempt like `if (x.y == null) return;` as an attempt to
promote the type of `x.y` to `Object`; now we compute the type the
user is actually trying to promote to, so we will be able to generate
more accurate "why not promoted" messages.

Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: I67f9fc59e72103194a1ea6b1c4dfeae8aeb194a2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/187064
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-03-03 23:16:55 +00:00
Paul Berry 085529d58d Flow analysis: hardcode allowLocalBooleanVarsToPromote flag.
This flag was being kept around in case the corresponding feature
(https://github.com/dart-lang/language/issues/1274) wound up having
bugs and needed to be switched off.  It's now had sufficient bake time
that we no longer need the flag.

Removing the flag allows us to remove a bunch of flow analysis code
that's no longer needed.

Bug: https://github.com/dart-lang/language/issues/1274
Change-Id: Ib91fa09a560bb0ebc6bff280ffc37d6037816ef9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/187981
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-02-26 18:28:57 +00:00
Paul Berry 265f6fca2d Flow analysis: Fix "why not promoted" functionality for implicit this
Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: Ief6014c34aef329e5f408916f5d8ffe14a62effd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/187940
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-02-26 18:14:47 +00:00
Paul Berry 4e67a67d06 Flow analysis: fix constness in unit tests.
The Node class hierarchy shouldn't be const constructible, because
flow analysis assumes it can distinguish nodes by identity, and we
don't want const canonicalization to prevent this.

Change-Id: I60f70a0ece77c8efbeaa001cbddb21d1dc8b4d6e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/187980
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-02-26 17:55:57 +00:00
Paul Berry 588fadcd49 Flow analysis: expand analyzer support for "why not promoted" messages.
This CL adds support for the following scenarios to the analyzer:

- Attempt to use a non-promoted nullable expression as the iterable of
  a for-in loop

- Attempt to use a non-promoted nullable expression as the argument of
  a `yield *` statement

- Attempt to implicitly invoke `.call` on a non-promoted nullable
  expression

- Attempt to use a non-promoted nullable expression as the argument of
  a spread operator (`...`) that is not null-aware

Some of these cases are already handled by the CFE.  Others will be
addressed in a follow-up CL.

Change-Id: I3cf31b1496e1bd92fdd3f8192f04c98dff15077c
Bug: https://github.com/dart-lang/sdk/issues/44898
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/186320
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-02-23 17:51:16 +00:00
Paul Berry 1d191a1374 Implement more "why not promoted" functionality in the front end.
This CL ensures that we produce appropriate "why not promoted" context
messages in all the scenarios where the CFE reports the error code
`NullableExpressionCallError`.

Analyzer support for these scenarios will be in a follow-up CL.

Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: Id1407b28a97fc917189e885f995a4efc0bf0c250
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/185920
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-02-22 14:41:09 +00:00
Johnni Winther 02f0f53ddf [cfe] Don't block nnbd top merge on typedef types
The MergeVisitor was trying to merge FunctionType.typedefType in order
to merge function types, thus preventing nnbd top merge of two
compatible types that were just introduced through different typedefs
or function type syntax.

Change-Id: Icea75598168c86ed33314db22ebeec3e666c3675
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184785
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
2021-02-16 11:30:21 +00:00
Paul Berry c2eb847697 Add "why not promoted" support for method invocations to CFE.
This required moving the logic for computing the context messages from
the inference visitor to the type inferrer.

This includes support for extension method invocations and invocation
of `.call` on a function type.

Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: Icda160f69cc815953c43edc92be910c5f49f9401
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184842
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2021-02-13 15:39:17 +00:00
Paul Berry 957c0a1c9d Improve "why not promoted" info for field/property reference.
When reporting that a null check is needed due to a lack of type
promotion, and the thing that was not promoted was a reference to a
field or property, the CFE and analyzer now report "why not promoted"
using a context message that points to the definition of the field or
getter.  (Previously the CFE reported a context message with no
location, and the analyzer didn't report "why not promoted").

Bug: https://github.com/dart-lang/sdk/issues/44898
Change-Id: If1841727b8b60dd87c43f239e6a06b98f363801f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/184522
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-02-12 22:43:55 +00:00
Paul Berry a42244f73b Flow analysis: begin tracking non-promotion reasons.
This CL implements the core flow analysis infrastructure for tracking
reasons why an expression was not promoted.  It supports the following
reasons:

- Expression was a property access
- Expression has been written to since it was promoted

I expect to add support for other non-promotion reasons in the future,
for example:

- `this` cannot be promoted
- Expression has been write captured
- Expression was a reference to a static field or top level variable

These non-promotion reasons are plumbed through to the CFE and
analyzer for the purpose of making errors easier for the user to
understand.  For example, given the following code:

  class C {
    int? i;
    f() {
      if (i == null) return;
      print(i.isEven);
    }
  }

The front end now prints:

  ../../tmp/test.dart:5:13: Error: Property 'isEven' cannot be accessed on 'int?' because it is potentially null.
  Try accessing using ?. instead.
      print(i.isEven);
              ^^^^^^
  Context: 'i' refers to a property so it could not be promoted.

Much work still needs to be done to round out this feature, for example:

- Currently the analyzer only shows the new "why not promoted"
  messages when the "--verbose" flag is specified; this means the
  feature is unlikely to be noticed by users.

- Currently the analyzer doesn't show a "why not promoted" message
  when the non-promotion reason is that the expression is a property
  access.

- We need one or more web pages explaining non-promotion reasons in
  more detail so that the error messages can contain pointers to them.

- The analyzer and front end currently only show non-promotion reasons
  for expressions of the form `x.y` where `x` fails to be promoted to
  non-nullable.  There are many other scenarios that should be
  handled.

Change-Id: I0a12df74d0fc6274dfb3cb555abea81a75884231
Bug: https://github.com/dart-lang/sdk/issues/38773
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181741
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2021-02-02 16:15:34 +00:00
Dmitry Stefantsov c1a401c5da [cfe] Implement weak mode constant canonicalization update
Closes #44641.
Closes #44246.

Bug: https://github.com/dart-lang/sdk/issues/44641
Bug: https://github.com/dart-lang/sdk/issues/44246
Change-Id: I78cb0514a57ab3adafb3639b09ea8930791da030
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179340
Commit-Queue: Dmitry Stefantsov <dmitryas@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2021-01-22 12:06:49 +00:00
Paul Berry 4ee861b7d0 Flow analysis: Track additional assigned variables information.
We now track the following additional information:

- All local variable reads and read captures (previously we only
  tracked writes)

- For any conditional expression (`?:`) or if-test, the variables
  written in the "then" branch

- For any logical and expression (`&&`), the variables written in the
  right hand side of the expression

Tracking this information will allow us to implement legacy type
promotion as part of _fe_analyzer_shared, using the same API as flow
anaylsis, which will in turn allow a lot of front end and analyzer
code to be removed and simplified.

Change-Id: I01aaf64cefb989a4450c8e6d3a8373c1ca2b6dec
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179980
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2021-01-21 19:48:22 +00:00
Paul Berry 75aea30724 Add an implementation of legacy type promotion to _fe_analyzer_shared.
The new implementation uses the same API as flow analysis.  This
should allow us to significantly simplify the CFE and analyzer, by
dropping their implementations of legacy type promotion in favor of
the shared implementation.

This CL just introduces the new implementation and unit tests for it;
it does not integrate it with the analyzer or CFE.  I will follow up
with a CL that does the integration.

Change-Id: Ie07b3b39604d6a022ad42f3ae6b648a317c8af28
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179560
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2021-01-16 17:07:23 +00:00
Paul Berry f452e763e0 Add a class to represent references tracked by flow analysis.
The only references that are promotable are references to variables,
however in the near future I'll be adding logic to flor analysis to
keep track of attempts to promote fields; this will require tracking
references to property gets as well as references to `this`, so we
need to start building up data structures to track those references.

Bug: https://github.com/dart-lang/sdk/issues/38773
Change-Id: I0e3fd44f580bb85db3e8f405675b66aa4069e455
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/179280
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2021-01-15 17:55:02 +00:00
Paul Berry 10917ffd55 Don't promote this inside extension methods.
Bug: https://github.com/dart-lang/sdk/issues/44652
Change-Id: I05e5acfbff1d498fa3ad513d956510909ec7d24f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/178920
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2021-01-13 16:48:08 +00:00
Paul Berry 6f40248275 Simplify usage of addFactor and addSubtype.
These test harness methods now take strings rather than Type objects,
which should make the tests easier to read (and less work to write).

Change-Id: I160bb7a7a61116e18736d414d5a549971d851c02
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/178883
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2021-01-12 21:47:04 +00:00
Johnni Winther 63373b9e9c [cfe,analyzer] Pass message arguments directly to AnalysisError
This allows for supporting customized message for scanner/parser
errors.

Change-Id: Ib9470c3d8231de69611bc672570ae8902e9c0e6e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/177940
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2021-01-11 16:45:26 +00:00
Paul Berry 88cd4739b8 Flow analysis: switch on inference based on local boolean variables.
The implementation was completed in previous CLs, but hidden behind a
flag.  This CL switches on the feature and adds integration tests.

Bug: https://github.com/dart-lang/language/issues/1274
Change-Id: I0e2d7ac96d3a6d68d33d49f1eab5c341c9167f6f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175500
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2021-01-08 18:57:02 +00:00
Paul Berry 474edbe27f Flow analysis: do not treat null vars as equivalent to null.
When analyzing code like this with the
`allowLocalBooleanVarsToPromote` flag enabled, don't treat a variable
containing `null` as equivalent to a literal `null`.  For example:

    f(int? x) {
      var y = null;
      if (x != y) {
        print(x + 1); // ERROR: x must be null checked.
      }
    }

The rationale is that flow analysis doesn't promote variables that are
known to be `null` to the `Null` type, so it seems like it would be
inconsistent if we used the fact that they're `null` to promote other
variables.

Bug: https://github.com/dart-lang/language/issues/1274
Change-Id: Icfa14104936a26732353921e3a29a840c53d4d52
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176560
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-12-17 19:31:34 +00:00
Paul Berry 4510f1dc98 Flow analysis: add the ability to promote based on local boolean vars.
This CL adds functionality to FlowAnalysis.variableRead so that if the
truth value of the variable is known to be correlated with other flow
analysis state, that state can be restored.  This allows promotion
based on boolean variables in addition to boolean expressions, e.g.:

    int? x = ...;
    var xIsNotNull = x != null;
    if (xIsNotNull) {
      print(x + 1); // Ok; x is known to be non-null.
    }

This functionality is not enabled yet; it is hidden behind the flag
`allowLocalBooleanVarsToPromoteByDefault`.

Bug: https://github.com/dart-lang/language/issues/1274
Change-Id: I2a0183b69d285193db7acb36cc6af5c34e165c2c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176500
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2020-12-17 15:16:04 +00:00
Paul Berry 5c19c9f5e4 Flow analysis: store ExpressionInfo in SSA nodes.
This CL modifies flow analysis API so that when a variable is written
or initialized, if the written expression has non-trivial flow
analysis information, it is captured in the SsaNode associated with
the variable.

The stored information is not yet used; in a follow-up CL, I will add
the ability to retrieve it on a read and use it for promotions.

Bug: https://github.com/dart-lang/language/issues/1274
Change-Id: I1e2590205d4a0c59f4400a119f3d6b380a11414c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176460
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2020-12-17 15:16:04 +00:00
Paul Berry c64842e582 Flow analysis: Make arguments to VariableModel constructor named.
This makes the call sites a lot easier to read.

Change-Id: I26a538e6dcc3971a6604565f18929949bb9ca740
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176383
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2020-12-17 15:16:04 +00:00
Paul Berry afc9a4eeff Flow analysis: implement the new attachFinally algorithm.
When https://github.com/dart-lang/language/issues/1274 (Infer
non-nullability from local boolean variables) is fixed, this algorithm
will replace the `restrict` algorithm is currently used at the bottom
of a try/finally statement to combine the flow models from the `try`
and `finally` blocks.  The new algorithm has very similar behavior to
the old one, however since it is based on SSA nodes it (a) is able to
make slightly more promotions than the old one, and (b) will be able
to properly update SSA nodes so that local boolean variables assigned
inside a `try` block will be able to used for promotion after the
`finally` block.

The new functionality is hidden behind a flag for now, so there's no
customer-visible change yet.

Bug: https://github.com/dart-lang/language/issues/1274
Change-Id: I1d37f981688f58da1e5c6c7eee48f2319c997cef
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174960
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2020-12-17 15:16:04 +00:00
Paul Berry 1eb147d8af Flow analysis: add infrastructure for tracking SSA nodes.
This is the first step in a sequence of CLs to support type promotion
based on local boolean variables
(https://github.com/dart-lang/language/issues/1274).  To do this, we
will need a way to reliably tell whether a variable's value has
changed from one point in program execution to another, and to
associate additional information with a particular value of a
variable.  We'll accomplish both of these tasks by associating each
variable with a pointer to an "SSA node".  This pointer is updated to
point to a fresh node whenever the variable is written to, or two
different possible values come together at a control flow join.

Note that when a variable is write captured, it becomes impossible to
track when/if its value might change Previously we tracked this using
a `writeCaptured` boolean; now we track it by setting the SSA node
pointer to `null`.

This CL just lays the groundwork infrastructure and unit tests it;
there is no user-visible change.

Bug: https://github.com/dart-lang/language/issues/1274
Change-Id: Id729390655c9371cba264816b418f6c0463e1758
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176180
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-12-15 19:22:01 +00:00
Paul Berry c171f92beb Flow analysis: check that implicit downcasts don't promote.
Bug: https://github.com/dart-lang/language/issues/1362
Change-Id: I4d7e9a15a8f138e53b6f620d32a9f90e6ceed818
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175902
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2020-12-14 19:09:28 +00:00
Paul Berry 3ee471e490 Flow analysis: additional tests for promotion based on initializers
Change-Id: I19b16646097c4638ce44c5ceb3c700edcaa7a23a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175980
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-12-14 06:50:08 +00:00
Konstantin Shcheglov 3d67e6f998 Migrate _fe_analyzer_shared to null safety.
Some notes.

1. `Token.next` is `Token?` because there are null(s) for comment tokens.
  But I think that it is never `null` in code, because there is at
  least EOF token. So, lots of `!` null checks.

2. `Link.tail` is never null when `isNotEmpty`, but the type system
  cannot express it. So, some number of `!` null checks.

3. I keep assert(s) for now, and use comment
  `// ignore: unnecessary_null_comparison`.

4. I disabled `can_get_rid_of_nnbd_issue_error`, I think it fails
  because of changes in `_fe_analyzer_shared` language version.

Smoke test in google3 looks green.
https://test.corp.google.com/ui#id=OCL:346825148:BASE:346841611:1607632317129:6e87bf7

I have not tried yet full TAP, will do in the evening.

I have not tried yet Golem, or looked at benchmarks.

Change-Id: I651301e5d3a851dd77d73af960dac779cb0fc991
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175620
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2020-12-12 03:22:29 +00:00
Johnni Winther 8103c90dbf [cfe] Use combined member signature in override inference in legacy code
+ emit only one message per method given that it now always has the
same root cause, namely that the combined member signature does not
exist.

Closes #31797

Change-Id: Id89a5557c5fdda1db087c20cbe92847dd08216a3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175305
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2020-12-09 17:33:20 +00:00
Johnni Winther c7519a3f01 [cfe] Pass static target to inferInvocation
This is used to detect calls to identical which need special treatment
for flow analysis.

Change-Id: I5109ccc94cdffd8cd46843adfbe4df6b83b92122
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175240
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2020-12-07 13:21:57 +00:00
Paul Berry 2448fc70b7 Flow analysis: Unit test parenthesized expressions properly.
Previously, the unit tests for flow analysis re-used the logic from
the _WrappedExpression class, which uses
FlowAnalysis.forwardExpression rather than
FlowAnalysis.parenthesizedExpression.  As a result,
FlowAnalysis.parenthesizedExpression wasn't being unit tested.

Change-Id: I1905808fd3a7788cb15d28c82d773369c061da59
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175164
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-12-05 01:40:32 +00:00
Paul Berry 25c73c0e56 Flow analysis: better unit testing of captured variables.
Previously, we didn't have a unit test to verify that variable
captures in nested contexts are propagated to enclosing contexts.

Change-Id: I579bce6d07f61119f44259f1fbf1da7f0800adde
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175102
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2020-12-04 22:39:43 +00:00
Paul Berry 6a097edc48 Flow analysis: allow identical to promote values to non-nullable.
This change causes `identical(x, y)` to be treated similarly to `x ==
y` for flow analysis.  For example:

    int? x = ...;
    if (!identical(x, null)) {
      print(x + 1); // Ok, x is known not to be null.
    }

Fixes https://github.com/dart-lang/language/issues/1226.

Bug: https://github.com/dart-lang/language/issues/1226
Change-Id: If2939d3d4dd0dc9d7aaf39984045e9ec3e10ce7d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174760
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-12-04 14:40:15 +00:00
Paul Berry 4234769982 Flow analysis: remove joinUnassigned and markNotUnassigned methods.
They were not being used.

Change-Id: I9f82b02e6e06327de9d7da7092e649ab08a5e658
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174763
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-12-02 20:53:58 +00:00
Paul Berry 1779cc86d2 Rework flow analysis unit tests based on a mini-AST representation.
In preparation for some flow anaylsis work I intend to do this month,
I want to make the flow analysis unit tests easier to understand and
maintain.  This CL creates an AST representation that models just
enough of the Dart language to be able to do flow analysis testing, so
that when reading or writing a flow analysis unit test, instead of
having to remember the correct sequence of calls to FlowAnalysis to
model a given construct, you can just create a mini-AST representation
of the given construct and pass it to the flow analysis test harness.

The mini-AST model for expressions contains methods that can be used
to build larger expressions out of smaller ones, so a lot of code can
be modeled compactly.  For example, to create a mini-AST model of the
statement `x = y && z == null;`, call
`x.write(y.read.and(z.read.eq(nullLiteral()))).stmt`.

Change-Id: I11e3882078fdc5797176019398db48011232cf35
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174560
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2020-12-01 22:41:56 +00:00
Johnni Winther d75f7005a0 [cfe] Stop shorting in ??
Closes #44276

Change-Id: I60f398c413f3b40215e9049ee3292eaf7f700d7c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/173722
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2020-11-24 16:01:38 +00:00
Paul Berry b8a243ea48 Remove unnecessary bool return types from flow analysis methods.
The `bool` return types were added to `FlowAnalysis` methods
`nullAwareAccess_rightBegin`, `ifNullExpression_rightBegin`,
`equalityOp_end`, and `isExpression_end` in September as part of an
effort to fix mixed-mode unsoundness loopholes by having the front end
throw exceptions if "impossible" conditions arose.  We later decided
that we would prefer to allow these conditions to arise without
generating exceptions, because that makes it easier for the user to
write defensive mixed-mode code.  So the booleans stopped being used.
This CL removes them entirely.

Change-Id: Ia0ff91a22fe79c2df1dea89bbb286c84a3603821
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170125
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2020-11-03 12:42:26 +00:00
Joshua Litt 043bebcd02 [dart2js] Fixes to dart2js unit tests to support nnbd by default.
Change-Id: I440dddd34a62bfe8c0f3fd563888acb680c2b031
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168141
Commit-Queue: Joshua Litt <joshualitt@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
2020-10-20 18:40:44 +00:00
Johnni Winther 137c7b6c84 [cfe] Explicitly run front_end unittest without non-nullable
This prepares front_end unittests for turning on non-nullable by default.

Change-Id: I9588a2d026ab9f5b1dcf08a140d25bd5dca3d5bd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/168346
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2020-10-20 13:13:33 +00:00
Paul Berry 7a1e92a670 Ensure that assert statements promote properly in unreachable code.
Fixes #40009.

Bug: https://github.com/dart-lang/sdk/issues/40009
Change-Id: I96561a72b6ba8dd8b8b95a4d9827d195941ea038
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166760
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2020-10-08 21:42:14 +00:00
Paul Berry 18644c338b Ensure that while statements promote properly in unreachable code.
Bug: https://github.com/dart-lang/sdk/issues/40009
Change-Id: I492b465b08a0f00549e8e69390cead35af211e0b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/166700
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2020-10-08 21:42:14 +00:00