Commit Graph

792 Commits

Author SHA1 Message Date
Paul Berry bd2d261bc6 Shared type analysis: add support for when clauses and fix label support.
Support for `when` clauses requires flow analysis integration, so that
`when` clauses can promote variables, e.g.:

    f(int x, String? y) {
      switch (x) {
        case 0 when y != null:
          // y is known to be non-null here
      }
    }

Support for labels in switch statements had a small flaw: we weren't
reporting an error in the case where a label shared a case body with a
pattern that tried to bind a variable, e.g.:

    f(int x) {
      switch (x) {

        L: // Error: does not mind the variable `y`
        case var y:
          ...
      }
    }
Change-Id: I0b2bb4721a6b3a8f7898df682b24b75ddb6e44ae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256605
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-08-29 03:27:53 +00:00
Paul Berry ac86c134e9 Prototype shared type analysis for switches and variable patterns.
This change introduces the TypeAnalyzer methods
analyzeConstOrLiteralPattern, analyzeExpression,
analyzeInitializedVariableDeclaration, analyzeSwitchExpression,
analyzeSwitchStatement, analyzeUninitializedVariableDeclaration, and
analyzeVariablePattern.  These are sufficient to analyze legacy switch
statements and legacy variable declarations, as well as switch
statements and switch expressions involving either constants or
variable patterns.

Although the code is not used in the analyzer or front end yet, it is
unit tested in isolation, and it's integrated into the existing flow
analysis unit tests.

A few minor tweaks had to be made to flow analysis to support this new
functionality.  There should be no visible effect to existing analyzer
or front end behavior.

Change-Id: Ie8ec31ca92d5f2f7a7f6f6a20ca1baba3c6b28f9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256604
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-29 03:03:53 +00:00
Johnni Winther 468beb8cfd [cfe] Add MergedScope
This changes how scopes are computed for libraries and classes
(work on extensions is still pending). The change supports the new
shared scope need for augmentations, in which a declaration on the
origin or any of the augmentations is directly accessible in the
origin and all augmentations.

Change-Id: Ifb76f49bf80fcad2d92a0400b9be623406afb40d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256262
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2022-08-26 12:12:11 +00:00
Paul Berry 7fb9540f09 Initial infrasturucte for sharing type analysis logic.
This change introduces the mixin TypeAnalyzer, which is intended to be
mixed into analyzer and front end classes to provide shared logic for
type analysis of Dart code.  This work is currently experimental, and
not hooked up to the analyzer or front end, but the eventual hope is
that it can replace the logic that's currently duplicated between the
analyzer's ResolverVisitor and the front end's InferenceVisitorImpl.
A secondary goal of introducing this code is to allow some of the
static consequences of the patterns proposal to be explored now, even
before parser support is finished.

To avoid introducing additional code duplication while the project is
in this experimental phase, I'm going to attempt to restrict myself as
much as possible to prototyping functionality that is not yet
implemented in the analyzer or front end.  This initial CL is an
exception; it introduces the method `analyzeIntLiteral`, which
duplicates logic that already exists in both the front end and
analyzer that analyzes integer literals.  I'm doing this because it's
just complex enough to serve as a validation of the basic approach,
and because integer literals will be handy in writing test cases for
the expanded switch functionality in the patterns proposal, which I
plan to work on next.

Although the code is not used in the analyzer or front end yet, it is
unit tested in isolation, and it's integrated into the existing flow
analysis unit tests.

Change-Id: I07c7cd709eec9e8492669f2dc8db57fb7c10798f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255081
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-25 18:21:05 +00:00
Jens Johansen 4970534cab [parser] Allow ? to be parsed a part of the type after is/as right before end bracket
Before this was ok:

```
  var x = [a as int?, a];
```

but this was not:

```
  var x = [a, a as int?];
```

Nor was this:

```
list[i as int?];
```

Fixes https://github.com/dart-lang/sdk/issues/49678

Change-Id: I8a169e15b189965ad19ad489e8a0a6f02c09b34b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256300
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2022-08-25 13:47:22 +00:00
Jens Johansen 7aeea765d8 [parser] Parse record type return types for functions taking type parameters
Fixes https://github.com/dart-lang/sdk/issues/49794

Change-Id: Ie80497bcfcda42cfb082240346ed547586742598
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256203
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2022-08-25 06:10:03 +00:00
Lasse R.H. Nielsen 8a883fa54d Change : to = for default values in pkg.
Leaves some in parser test:
 pkg/front_end/parser_testcases/error_recovery/keyword_named_formal_parameter_prime.dart

TEST=Refactoring, covered by existing tests.

Change-Id: I7a83ef95df3cbd283878b3685b5c747bd89a1b16
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256125
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2022-08-24 15:57:16 +00:00
Johnni Winther dbac2a4df3 [cfe] Add InternalRecordLiteral
This adds support for handling record literals in the BodyBuilder,
passing and handling it in the inference visitor. The correct
AST node is still not produced.

Change-Id: Ifc1e22f3ed30ddde7058ff666117e02d43a4c6a8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256210
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2022-08-24 15:29:39 +00:00
Jens Johansen bba453b798 [parser] Record type type arguments
Fixes https://github.com/dart-lang/sdk/issues/49769

Change-Id: Icffa2dcfae95d950c00e72aa01121b6d6ebd147d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/256065
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2022-08-24 08:25:38 +00:00
Jens Johansen cf8f47df4f [parser] Support RecordType for setter/getters and static methods
Fixes https://github.com/dart-lang/sdk/issues/49709

Change-Id: I710e687ce7e1066eef8439f541f2e5fe2771db36
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255988
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2022-08-24 07:42:51 +00:00
Johnni Winther e0aafecd3c [cfe] Add RecordTypeBuilder
This prepares for the building of the RecordType nodes for record
types.

Change-Id: If055dd31ac55c4314553ccce50b803723a9e09ae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255993
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2022-08-24 05:02:20 +00:00
Paul Berry 61e714b2fc Create ExpressionTypeAnalysisResult interface and use in tests.
This is a preparatory step towards sharing type inference logic
between the front end and analyzer.  The ExpressionTypeAnalysisResult
interface will be returned from the shared `analyze` methods for
expressions, as a container for all the information needed by both the
client (front end or analyzer) and by the code that analyzes the
containing expression or statement.

For now, the only implementation of the interface is
SimpleTypeAnalysisResult, which represents the result of analyzing a
simple expression with no null shorting.  In future CLs I plan to add
more types, recording information such as:

- For an integer literal, whether it was implicitly converted to
  `double`.

- For a binary operator or a compound assignment, the resolved binary
  operator.

- Information necessary to coordinate null shorting.

And so on.  There is a placeholder method `resolveShorting` that will
encapsulate null shorting logic, but it doesn't do anything special in
the case of SimpleTypeAnalysisResult.

At the moment, only test code is affected.

Note that this approach isn't free; every time we analyze an
expression we wrap the resulting type in a SimpleTypeAnalysisResult,
so there's a cost of one allocation per expression in the user's
program.  I don't believe this will be a problem in practice, because
the front end uses a similar approach, so it is already paying this
penalty.

Change-Id: Ic407089b6eb9e717c65d73766e7c836e161ef0da
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255080
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-23 17:04:40 +00:00
Johnni Winther 6751831ac6 [parser] Add hasNamedFields parameter to endRecordType
This makes it possible to handle named record fields without peeking
at the last record field (list) on the stack.

Change-Id: I96e2009cf5933ef505459ac8722d5bcbeb67db83
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255983
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2022-08-23 15:43:10 +00:00
Paul Berry 2b06ac466d Flow analysis: rework some testing logic in preparation for pattern support.
- Variable types are no longer specified in the call to the `Var`
  constructor; they are now specified in the call to `declare`.  This
  paves the way for supporting variable pattern syntax, in which a
  single variable might appear in multiple variable patterns, and have
  its type specified in each pattern.  The properties `isFinal` and
  `isLate` are also moved to `declare` for consistency.

- Variables with inferred types are now specified by simply not
  including a type in `declare`; it's no longer necessary to specify
  `isImplicitlyTyped: true`.

- `declare` now supports an `expectInferredType` argument to allow the
  inferred type of an implicitly typed variable to be tested.

- The tests now check that variables are assigned a type before flow
  analysis requests it; previously this was not tested, and the flow
  analysis tests sometimes did things in the wrong order.  (The
  analyzer and CFE have always done this in the proper order though).

- The tests now support some of the crazy types that arise during type
  parameter promotion, e.g. they can now distinguish `(T&int)?` from
  `T&(int?)`.

- Flow analysis tests now properly replicate the analyzer and CFE
  behaviors for converting the static type of an initializer
  expression to the corresponding inferred variable type: (a) `Null`
  is converted to `dynamic`, and (b) type parameter promotions are
  dropped.

Note that this last behavior (dropping type parameter promotions) has
a lot of subtleties, and I'm not convinced the CFE and analyzer do it
soundly in all cases (I've already found one such soundness bug:
https://github.com/dart-lang/sdk/issues/49691).  In a later CL, I plan
to add a more thorough set of language tests to verify that we don't
have other lurking soundness issues.

Change-Id: I6f2cd20db1f07b34e0ad4e7002351c8de846b125
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255600
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-22 22:59:26 +00:00
Paul Berry b7567b1799 Flag additional code as unreachable due to types Null and Never.
Several unusual constructs that lead to unreachable code are now
recognized by flow analysis:

- Control flow after an expression of the form `e ?? other` or `e ??=
  other`, where `e` has static type `Null` and `other` has static type
  `Never`, is considered unreachable.

- Control flow predicated on an expression of the form `e is Never`
  evaluating to `true` is considered unreachable.

- Control flow predicated on an expression of the form `e is! Never`
  evaluating to `false` is considered unreachable.

- Control flow on the RHS of a null-aware access such as
  `e?.property...`, `e?.property = ...` or `e?.method(...)`, where `e`
  has static type `Null`, is considered unreachable (Note: this can
  arise in the presence of extension methods).

Previously, these behaviors only took effect if `e` was a reference to
a local variable.

Note: the change to `regress/issue_31180` is because I’ve corrected
the behavior of implicit temporary variables to not undergo a type
change from `Null` to `dynamic`, so the dead code part of `null?[1]`
is now erroneous.  (I had to make this change in order for the last
bullet above to work properly; without it, the type change to
`dynamic` prevents flow analysis from recognizing that the code to the
right of `?.` is unreachable.)  There's no behavioral change to
correct code, but I've captured the behavioral change to incorrect
code in
`tests/language_2/null_aware/null_aware_index_on_null_error_test.dart`.

Bug: https://github.com/dart-lang/sdk/issues/49635
Change-Id: I8b24b3b040a34f897c0b61dcb9bd105be6d0af6d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/251280
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-08-22 16:50:19 +00:00
Paul Berry e0cebd838e Flow analysis: handle promotable field accessed on a non-promotable field
A get of a promotable field on a non-promotable field shouldn't
promote, otherwise this code would be unsound:

    class C {
      D get _field1 => D();
    }
    class D {
      final int? _field2 = randomBool() ? 1 : null;
    }
    main() {
      var c = C();
      if (c._field1._field2 != null) {
        // Problem: `c._field1` returns a different `D` each time, so there's
        // no guarantee that `c._field1._field2` is non-null the second time we
        // access it!
        print(c._field1._field2.isEven);
      }
    }

This change has no user-visible effect because field promotion is not
yet enabled.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I9a60cd343dff14cded03df700d7c2b62a251487a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255821
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-08-19 19:38:05 +00:00
Paul Berry 39a8a19071 Flow analysis: Rework PromotionKeyStore using a single internal list.
A single internal list pointing to a simple data structure is easier
to reason about than a bunch of parallel lists, and it reduces the
performance cost if we have to add more data to the internal data
structure in the future.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I348cae86781c4d1f15520160a674df381fe2e38f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255815
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-19 18:20:15 +00:00
Jens Johansen 38e5b8f9e0 [parser] Parse Record Types
This is the first stab at implementing the record types from
https://github.com/dart-lang/language/blob/master/working/0546-patterns/records-feature-specification.md

Change-Id: I15c07e05c32a95206d177521c5f2b7fed69b4fbc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255244
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2022-08-19 07:04:44 +00:00
Chloe Stefantsova 1611fe6f45 [cfe] Separate out IntersectionType from TypeParameterType
TEST=Covered by existing tests

Change-Id: Ie7b99b1c109edff5198cfbf5d22e1cfb1dc130d2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253665
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Mayank Patke <fishythefish@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2022-08-18 08:47:29 +00:00
Jens Johansen 8a1dbb160f [parser] Parse record literals
This is the first stab at implementing the record expressions from
https://github.com/dart-lang/language/blob/master/working/0546-patterns/records-feature-specification.md

Change-Id: I2adb6cb3cd50d4ee45e144e86ec7011d046f6170
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253783
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Michael Thomsen <mit@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2022-08-16 06:36:36 +00:00
Martin Kustermann 9a3bcbcdb0 [CFE] Improve [StringCanonicalizer] implementation
The current [StringCanonicalizer] implementation has some issues:

  * It hangs on to large [Uint8List]/[String] objects in it's cache
    => This requires users (such as analyzer) to clear the cache
       frequently

  * Has api that works on dynamic input (which is assumed to be String
    or List<int> / Uint8List)
    => Call sites have typing information we loose when doing the call

  * It uses dynamic [] calls to compare input bytes with cached bytes /
    input strings with cached strings
    => Dynamic calls come with overhead
    => Will cause substring generation for every character comparison
    => Will compare bytes with strings (which doesn't make sense)

To address these issues we

  * Use the canonicalized [String] to compare against instead of the
    (much larger) source strings, thereby no longer hanging on to large
    strings in the canonicalizer cache (it's still an issue with
    [Uint8List]s though)

  * Make seperate API for canonicalization of strings, sub-strings or
    sub-utf8-bytes and use it from the token implementation.

  * For canonicalization of strings use String.== (instead of
    char-by-char comparison)

  * For canonicalization of sub-strings use String.charCodeAt instead of
    [] (which creates substrings)

  * Seperate out cache node entries into two classes and reduce memory
    consumption of the nodes that represent strings by 16 bytes (it
    does an additional `is` check on lookups in the cache, but that is
    better than paying for dynamic calls on the payload - which
    causes the compiler to do implicit checks)

=> This CL reduces RAM consumption and makes CFE scan/scan_bytes benchmarks a little faster.

TEST=ci

Change-Id: I157c298d26d25ac5da82c32eedfa270a590156f0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255121
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2022-08-15 17:06:58 +00:00
Martin Kustermann 403b896d58 Reduce size of StringToken/StringTokenImpl by 16 bytes (25%).
Some instances of analyzer have a heap of 600 MB, containing 400k
StringToken/StringTokenImpl.

This CL combines two existing fields which will shrink those objects
by 16 bytes, which saves 6 MB.

We densly number all token & keyword types so they can be looked up in
an array.

TEST=ci

Change-Id: I8431db243d55a316e7a72678844d031356c40e79
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254920
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2022-08-12 14:07:43 +00:00
Konstantin Shcheglov 5b3a78b675 Prepare to publish analyzer 4.6.0 and _fe_analyzer_shared 46.0.0
Change-Id: I3562b5e3ce0c7f744d4bf80a71adf8e97b9b57b7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254740
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2022-08-11 23:31:18 +00:00
Paul Berry f1043ad932 Flow analysis: discard property promotions after writes/captures.
This change ensures that when a variable `x` is written to or
captured, promotions of its fields (e.g. `x.y`, `x.y.z`, etc.) are
cancelled.  This is necessary for soundness of field promotion.

There is no effect on production code, since field promotion is not
yet enabled.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: Ic0739ca80cc2afe6188ada6209cb558d8cea9b63
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254620
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-08-11 20:14:38 +00:00
Paul Berry ac08c03dc6 Flow analysis: break up libraries.
This change breaks flow_analysis.dart into the following libraries:

- assigned_variables.dart (for the AssignedVariables class and related
  code)

- promotion_key_store.dart (for the PromotionKeyStore class)

- type_operations.dart (for the TypeOperations mixin and related code)

- flow_analysis.dart (for the rest of flow analysis)

And it breaks mini_ast.dart into the following libraries:

- flow_analysis_mini_ast.dart (functionality specifically concerned
  with testing flow analysis)

- mini_ast.dart (functionality not specifically related to flow
  analysis)

This is in preparation for trying to share some more type inference
behaviors between the analyzer and CFE.

Note that although the diff is big, the only changes in this CL are
moving code from one place to another, renaming some class members
from private to public, and updating imports.

Change-Id: I71768f03b1e75ed754c7b7af39f6cf7f03c4fe44
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254462
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-11 13:18:52 +00:00
Paul Berry e6c805a3c5 Flow analysis: split test harness class.
This change splits the `Harness` class into a base class, `Harness`,
which in principle can be used for testing type inference logic in
general, and a derived class `FlowAnalysisTestHarness`, which is
specialized for flow analysis tests.

This is in preparation for trying to share some more type inference
behaviors between the analyzer and CFE.

Change-Id: Ic56b8dd8748065ca59e246e0d804946cc69203c3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254280
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-08-10 20:47:41 +00:00
Konstantin Shcheglov a1b90438e8 Prepare to publish analyzer 4.5.0 and _fe_analyzer_shared 45.0.0
Change-Id: I0ab45251f8a7e87b70ce6dfd1f964da7d2cc6e9c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254282
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2022-08-09 22:42:26 +00:00
Paul Berry d1dcbeb973 Flow analysis: move _promotableFields out of PromotionKeyStore.
This improves separation of concerns, since the `PromotionKeyStore`
class isn't really concerned with what fields are promotable; it's
just a mechanism for assigning unique integer identifiers to
promotable things.

I've moved `_promotableFields` to the main `_FlowAnalysisImpl` class.
To avoid having to add it as a separate argument to a lot of
[FlowModel] methods, I've created a new [FlowModelHelper] interface
that provides these methods with access to several fields in
`_FlowAnalysisImpl`.

Change-Id: I0280d0c0b95714521afbe68a07e7b3b54f23b7df
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/254003
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-08 18:18:10 +00:00
Paul Berry 99616e9da0 Flow analysis: create test harness using a setUp() call.
In a future CL, this will allow me to move flow_analysis_test.dart's
global variable _promotionKeyStore into the testing harness, which is
a prerequisite for some clean-up work I'm doing on flow analysis.

Change-Id: I44943b2706e237213896164910b4c6f488aa2794
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253901
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-08-08 16:58:21 +00:00
Konstantin Shcheglov a34e93f9d3 Prepare to publish analyzer 4.4.0 and _fe_analyzer_shared 44.0.0
Change-Id: I81a6366092ede393546212cfef1746fa06d1aed6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/253709
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2022-08-05 21:27:37 +00:00
Konstantin Shcheglov ea820da673 Prepare to publish analyzer 4.3.1 and _fe_analyzer_shared 43.0.0
Change-Id: I8dba10b407d00abae1ed05341c12470dba136753
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252702
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2022-07-25 22:17:15 +00:00
Anis Alibegić 40e18905f2 Fixed various typos in a lot of files
Closes https://github.com/dart-lang/sdk/pull/49478

TEST=Manual

GitOrigin-RevId: f4c9c6869dfe73639295e86574a021523b3d374d
Change-Id: I134a97caed4eec59d70e9cbca16b7e9a472cf2c1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/251902
Reviewed-by: Michael Thomsen <mit@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Kevin Chisholm <kevinjchisholm@google.com>
Reviewed-by: Alexander Thomas <athom@google.com>
2022-07-25 12:21:59 +00:00
Konstantin Shcheglov 10707a696c Prepare to publish analyzer 4.3.0 and _fe_analyzer_shared 42.0.0
Change-Id: I59df8579a9e0dac289c25f36330a1d64a9280862
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252521
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2022-07-22 16:35:16 +00:00
Paul Berry 2a05924ab3 Flow analysis: core implementation of field promotion.
This includes just the shared logic to track promotion of fields if
they are promotable.  It does not include the logic for figuring out
which fields are promotable, nor does it include the additional
restrictions we need to implement in order to make field promotion
sound.  Those will be addressed in other CLs.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I031a4272035938fd0b1b1103438ebc9642b2cb51
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250347
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2022-07-18 15:07:24 +00:00
Paul Berry 16ed7df34f Flow analysis: clarify behavior of ifNullExpression_rightBegin.
Previously, it was unclear what information was stored in
_IfNullExpressionContext._previous.  It turned out that it was the
"shortcut state", i.e. the flow state if the LHS of the `??` was
non-`null`.  I've renamed variables and added comments to clarify
this.  Also, based on the implementation of
`ifNullExpression_rightBegin`, it looked like it might update
`_current`, but in fact it did not (because `tryMarkNonNullable`
always returns an `ExpressionInfo` whose `ifFalse` is unchanged); I've
refactored the code so that this is clearer, and added a comment
explaining why it is the right thing to do.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: Ib8a5235f9b9482f4b290ad90686385514660a95d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/251142
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-07-18 14:51:24 +00:00
Paul Berry 247f8449d8 Flow analysis: improve debuggability of _CheckReachable in unit tests.
This improvement is helping me debug my code during the development of
field promotion.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I3a1a9334b7a3d094c48063cf39b4d22427ca2571
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/251281
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-07-12 17:27:02 +00:00
Paul Berry 8aafcbfe80 Flow analysis: remove Reference type hierarchy.
Previously, flow analysis used the class `ReferenceWithType` to track
references for which it knew the type, and `Reference` (and its
subclasses) to track references for which it didn't know the type (or
for which the type was unimportant).

This change removes the `Reference` class hierarchy, in favor of just
using the integer promotion keys.  This should reduce the number of
memory allocations that flow analysis needs to make.

A few pieces of information previously maintained by the `Reference`
class hierarchy are now tracked elsewhere: the logic for computing
non-promotion reasons is now in
`_FlowAnalysisImpl._getNonPromotionReasons`, and the property name and
property member (previously maintained by `_PropertyGetReference`) is
now maintained by `_PropertyReferenceWithType` (a new subclass of
`ReferenceWithType`).

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I72f2d80b3256bf8b9c9a30bcc55666ecb7c31e47
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250242
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-07-12 15:36:42 +00:00
Paul Berry 9393b52b67 Flow analysis: Use named arguments for ExpressionInfo constructor.
This should make call sites easier to read.  I also took the liberty
of renaming a few local variables to follow the `ifFalse` / `ifTrue`
nomenclature used by the `ExpressionInfo` constructor.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I7ec4b1039b7f37bfc5773cf98c6aab864e56b5ea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/251141
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2022-07-12 13:38:31 +00:00
Paul Berry 6e9ff48ca2 Flow analysis: rework property tracking logic.
Previously, flow analysis's tracking of properties was done through
the `VariableModel.properties` field.  This field wasn't properly
updated during joins, rebases, and handling of try/finally; as a
result, flow analysis failed to detect some possible promotion
opportunities for fields.  This wasn't previously a problem, because
we didn't support field promotion (the only reason flow analysis
tracked fields at all was so that error messages could explain that
field promotion *wasn't* supported).  However, now that we are going
to be adding field promotion support, it's important that it works
properly with joins, rebases, and try/finally.

So, instead of tracking properties through `VariableModel.properties`,
we assign each property its own promotion key, and include properties
in the `FlowModel.variableInfo`.  This ensures that property promotion
will be properly handled by the existing logic for joins, rebases, and
try/finally, because that logic just treats the promotion keys as
opaque keys--it doesn't even know whether a given entry in
`FlowModel.variableInfo` represents a variable or a property access.

Note that changes to `use_of_nullable_value_test.dart` reflect
improvements to detection of when promotion via properties is being
attempted.  Previously, when analyzing the code in these tests, a
context message was emitted explaining that promotion failed due to
field promotion not being supported, even though no attempt was made
to promote a field; this erroneous context message was codified into
the test expectations.  Now that flow analysis no longer incorrectly
judges there to have been a field promotion attempt, we need to also
correct the test expectations.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I6c5cb1f6f4c98cbfa199baea24cd373408820996
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250181
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-07-11 21:44:21 +00:00
Paul Berry e7deece1fb Flow analysis: use an integer key for FlowModel.variableInfo.
Each variable is mapped to a unique integer "promotion key", which is
used as an index into the `FlowModel.variableInfo` map.  This paves
the way for adding entries to `FlowModel.variableInfo` to represent
promoted properties.  It also cleans up the previous hacky way we used
to use `null` as a map key to represent `this`.

In a future CL I plan to try to replace `FlowModel.variableInfo` with
a list rather than a map.  This should improve both memory and CPU
usage.

As a side effect of this change, many classes related to flow analysis
no loger need a `Variable` type argument, including the `EqualityInfo`
class (which is exposed to clients).

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I853ca835c6b36ab9865bd187973c6524a7471db0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250120
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-07-11 17:52:41 +00:00
Johnni Winther f6846849b9 [cfe] Handle augment super expressions
This adds the generation of access to augmented procedures.

TEST=existing

Change-Id: I5efa9cc541b86c18735bb1f4c51c73976ffa42ca
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250164
Reviewed-by: Tess Strickland <sstrickl@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2022-07-04 12:37:50 +00:00
Ahmed Ashour c7a02bc43e [_fe_analyzer_shared] fix 4 spaces comments as code block
Fixes #49047

Change-Id: I1aa08fc417365ca894467b15d5094ea203f01940
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/245162
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: William Hesse <whesse@google.com>
2022-07-01 15:55:00 +00:00
Paul Berry 99919c69ba Flow analysis: Separate variable and type operations.
This change moves the `variableType` method from the class
`TypeOperations` to a new class, `VariableOperations`, which in turn
allows removing the type parameter `Variable` parameter from
`TypeOperations`.  A new class, `Operations`, is introduced to serve
the role served previously by `TypeOperations` for flow analysis
clients (i.e. it is the base class that clients should extend).

This paves the way for a future CL that will remove the type parameter
`Variable` from other classes inside flow analysis.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: Ic45d07a0f873b692fda4b6f807c1130ac592b010
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250108
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-07-01 14:13:20 +00:00
Paul Berry bd660d9847 Flow analysis: additional debug support.
I've found these changes helpful as part of developing the new "field
promotion" feature.  These changes have no effect unless the
`FlowAnalysisDebug` class is used.

Bug: https://github.com/dart-lang/language/issues/2020
Change-Id: I7badadc14bf901e77b8c166920aedf902093d7e1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250220
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2022-06-29 21:19:05 +00:00
Konstantin Shcheglov a602f74ac0 Prepare to publish analyzer 4.2.0 and _fe_analyzer_shared 41.0.0
Change-Id: I9e49e86e6e5187a055305399193e9e83220e673b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250221
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2022-06-29 20:54:25 +00:00
Paul Berry 1b31046363 Change SDK constraint for _fe_analyzer_shared to 2.17.0.
This allows the _fe_analyzer_shared package to make use of the new
"super parameters" feature.

Change-Id: Iebea526cd91563a0ba603796d2df8af633adfaae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250180
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-06-29 19:31:55 +00:00
Jens Johansen 183863e5bf [parser] More recovery of await in non-async context
Fixes https://github.com/dart-lang/sdk/issues/49116

Change-Id: I6b3f4bd88b17da5703dc268df413b3e5bb2e7d87
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249605
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2022-06-29 07:11:06 +00:00
Jens Johansen 25dcfca3c2 [parser][CFE][analyzer] Function is builtIn keyword
This CL makes `Function` a builtIn keyword instead of a `pseudo` keyword.

See also (and fixes):
https://github.com/dart-lang/sdk/issues/45703
https://github.com/dart-lang/sdk/issues/45704
https://github.com/dart-lang/sdk/issues/45705
https://github.com/dart-lang/sdk/issues/49197

This undoes https://dart-review.googlesource.com/c/sdk/+/195761

This is ~a merge of https://dart-review.googlesource.com/c/sdk/+/195906
and https://dart-review.googlesource.com/c/sdk/+/200080

Change-Id: I8bfee6976d43819fa355de99b3b2429eb67a7cdd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249484
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2022-06-28 14:26:33 +00:00
Jake Macdonald e43aad1984 This is a precursor CL to a client side only InterfaceType class, which requires the ability to look at type parameters for arbitrary types in the program, in the declaration phase.
Move TypeDeclarationResolver to be available in the Declaration phase.

Add IntrospectableType interface, and IntrospectableClassDeclaration which implements it (we will eventually have IntrospectableMixinDeclaration, IntrospectableEnumDeclaration, etc).

Migrate ClassIntrospector to InterfaceIntrospector, which operates on IntrospectableType instances instead of ClassDeclaration instances.

Question: Possibly `InterfaceIntrospector` should have a different name, maybe just `TypeDeclarationIntrospector`?

Change-Id: Ifd202bad61eeae5f7d76d769d9d96a866c0fecdb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/247060
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Jake Macdonald <jakemac@google.com>
2022-06-28 14:02:12 +00:00
Jens Johansen b3917f7d5b [parser] Parse nullable type correctly after is/as when followed by '{'
E.g. nullasble cast last in initializer list followed by constructor
body.

Fixes https://github.com/dart-lang/sdk/issues/49132

Change-Id: I28633762444048b5e26b4b8343494302544344df
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249602
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2022-06-28 12:43:03 +00:00