Commit Graph

1043 Commits

Author SHA1 Message Date
Johnni Winther 3e409e7b8d [cfe] Report error on map pattern type argument mismatch
+ update message for list pattern type argument mismatch

Change-Id: I49cad4c5b01222592ea535f05be7dcc46639a993
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280172
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-02-01 18:42:19 +00:00
Paul Berry a661e436bd Flow analysis: change return type of assignMatchedPatternVariable to void.
No caller was using the returned value anyway.

Change-Id: I4e4a40918246dcfd3238e77972f3dd07ccc4b02e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280203
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-01 18:12:49 +00:00
Paul Berry baec6e2fff Flow analysis: Get rid of unnecessary call to declare.
It's not necessary to call `FlowAnalysis.declare` from
`TypeAnalyzer.analyzeDeclaredVariablePattern` because the call that
immediately follows, `FlowAnalysis.assignMatchedPatternVariable`,
completely overwrites the variable's flow model.

Change-Id: I8293672c7fefec7eddfb4d43c38d81d8dcbe9aea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280202
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-01 18:12:29 +00:00
Paul Berry 167cf8e637 Flow analysis: implement type promotions for switch cases that share a body.
There are two ways type promotion can occur when switch cases share a body:

(1) the scrutinee variable (if any) might be promoted, e.g.:

    f(Object x) {
      switch (x) {
        case int _ && < 0:
        case int _ && > 10:
          // `x` is promoted to `int` because both cases promote the
          // scrutinee variable to `int`.
      }
    }

(2) explicitly matched variables might be promoted at the time of the
    match, e.g.:

    f<T>(T t) {
      if (t is int) {
        switch (t) {
	  case var x && < 0:
	  case var x && > 10:
            // `x` has type `T` but is promoted to `T&int`, because
            // both declarations of `x` are in a context where the
            // matched value has type `T&int`.
        }
      }
    }

The existing flow analysis logic handles case (1) without any extra
work, because those promotions are joined as a natural consequence of
the flow control join at the end of matching the cases.

However, flow analysis has to do some extra work for case (2), because
the two copies of variable `x` are associated with different variable
declarations (and hence have different promotion keys).  To ensure
that the promotions are joined in this case, we need to copy the flow
model for the two copies of `x` into a common promotion key prior to
doing the flow control join.

The bookkeeping necessary to figure out a common promotion key is
similar to the bookkeeping for logical-or patterns.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I9ee4ec5d797dae28099aafbaf34fbbeeee5cd626
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280201
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-01 18:04:35 +00:00
Paul Berry 459b2bf80e Flow analysis: implement type promotions for or-patterns.
There are three ways type promotion can occur in an or-pattern:

(1) the scrutinee variable (if any) might be promoted, e.g.:

    f(Object x) {
      if (x case int _ && < 0 || int _ && > 10) {
        // `x` is promoted to `int` because both sides of the `||`
        // promote the scrutinee variable to `int`.
      }
    }

(2) the implicit temporary variable that holds the matched value might
    be promoted, e.g.:

    f(Object Function() g) {
      if (g() case (int _ && < 0 || int _ && > 10) && (var x)) {
        // `x` has type `int` because both sides of the `||` promote
        // the matched value to `int`.
      }
    }

    For this sort of promotion to work, we need to

(3) explicitly matched variables might be promoted at the time of the
    match, e.g.:

    f<T>(T t) {
      if (t is int) {
        if (t case var x && < 0 || var x && > 10) {
          // `x` has type `T` but is promoted to `T&int`, because both
          // declarations of `x` are in a context where the matched
          // value has type `T&int`.
        }
      }
    }

The existing flow analysis logic handles cases (1) and (2) without any
extra work, because those promotions are joined as a natural
consequence of the flow control join at the end of matching the
logical-or pattern.

However, flow analysis has to do some extra work for case (3), because
the two copies of variable `x` are associated with different variable
declarations (and hence have different promotion keys).  To ensure
that the promotions are joined in this case, we need to copy the flow
model for the two copies of `x` into a common promotion key prior to
doing the flow control join.

The bookkeeping necessary to figure out a common promotion key is
similar to the bookkeeping necessary to track the association between
the individual declared variable patterns and the joined pattern
variable (and this is bookkeeping that flow analysis is already
doing).  So as part of this change I went ahead and removed the
`getJoinedVariableComponents` method (which was previously used by
flow analysis to query this association).  This reduces the
constraints on the analyzer and CFE implementations by not requiring
them to do this bookkeeping themselves.

In the process I've made two additional small changes:

- I modified the logic for assigning types and finality to joined
  variables so that if there is a finality conflict but no type
  conflict, the common type is used; conversely, if there is a type
  conflict but no finality conflict, the common finality is used.
  This should help reduce follow-on errors.

- I added logic to ensure that if a variable is only declared on one
  side or the other of a logical-or, flow analysis still considers
  that variable to be definitely assigned.  This should help reduce
  follow-on errors.

Change-Id: I62f17adb6a51a583707c216ed48d941d1c621eea
Bug: https://github.com/dart-lang/sdk/issues/50419
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279756
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-01 17:18:17 +00:00
Johnni Winther 7774ab46b7 [cfe] Report error on missing variables in or-pattern
Change-Id: Iaea20e0cab22c1d9b82229adc27aa18bcdc2e8c6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280170
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-02-01 15:07:47 +00:00
Ahmed Ashour e663ff334d [cfe] better console messages
Bug #50938

Change-Id: If7ea9478f0b0fc24d45e453a82231bae1ebd50e2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280098
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-02-01 08:57:47 +00:00
Jens Johansen ee32255b21 [parser] Support record type in pattern
Fixes http://dartbug.com/51169
Fixes http://dartbug.com/51176

Change-Id: Iaa38585ce311319f2f4c106fc979c99d931eaa15
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280106
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2023-01-31 18:24:42 +00:00
Paul Berry 9b9cf3c928 Flow analysis: allow null to be passed to handleBreak/handleContinue
This should allow for better error recovery in the case where a label
target can't be found.

Change-Id: I05ec107a4ecef3f73cfba5931b77b8250707d2ec
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280120
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-31 17:00:40 +00:00
Kallen Tu 83a9a0a3f4 [analyzer/cfe] Make sealed and mixin class errors more precise and synced between the CFE and analyzer.
Updated corresponding language tests. No new behaviour, updated test messages and location.

Change-Id: Ib3c7e2d701bdfbb68757c532b945348f6cbebad0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280062
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2023-01-31 01:53:09 +00:00
Johnni Winther ed7124884a [cfe] Implement shared error reporting
Change-Id: Ief8d557d0e6553fa79e7281172caccb00dfc5cfb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280086
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-01-30 18:42:38 +00:00
Josh Soref 77978889eb Spelling
Closes https://github.com/dart-lang/sdk/pull/51143

GitOrigin-RevId: 9e21c99a222d588e4fc95980725a2f8c9784965c
Change-Id: If0870e8936c7649935dce7e23cd783d62aa5610c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279916
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
2023-01-30 18:29:59 +00:00
Martin Kustermann 6cbc89d6f7 [cfe] Avoid repeated calls to .codeUnit() - use startsWith with offset instead
In AOT compiled programs we may not know whether a String is one-byte or
two-byte (internal or external) and as a result, calls to .codeUnit()
are not inlined.

The string canonicalizer uses it for two purposes: creating hashes for
substrings and comparing substrings.

=> The ladder we can replace by a call to .startsWith() with a specific
offset.

Change-Id: I31025b3be03277a9c9505ef09d9c8a41a57e7839
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279974
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
2023-01-30 17:16:15 +00:00
Ahmed Ashour 63180b95ed Remove superfluous words.
Fixes #51095

TEST=ci

CoreLibraryReviewExempt: There are no API changes, just removal of superfluous words in the comments.
Change-Id: Ib1020c62fe6baed5ca68f0074323f025cc90e9f8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279500
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
2023-01-30 15:58:38 +00:00
Kallen Tu f48a1fb3da [analyzer] Report error when implementing a base class.
Change-Id: Ia55354350451849a112a32bb048a4fcbe5afe1b6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279655
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-26 22:15:57 +00:00
Kallen Tu 7cf49ba433 [cfe] Report error when implementing a base class.
Change-Id: Iff3c31b4883d3d4fc4c392c6ac8207617ce65761
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279648
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2023-01-26 16:01:55 +00:00
Paul Berry 5668aa4732 Flow analysis: Add patternRequiredType.
This method is used for declared variable patterns, list patterns, map
patterns, record patterns, object patterns, and wildcard patterns to
cause the value being matched to be promoted.

As a temporary measure it's also used for cast patterns; this will be
fixed in a later CL.

Change-Id: Iccc9ce4a53e2a10753847f9ecade091f1b230111
Bug: https://github.com/dart-lang/sdk/issues/50419
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279653
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-26 15:54:17 +00:00
Paul Berry e396024712 Shared inference testing: replace factor and subtype tables with an algorithm.
The tables of subtypes and "factor" results used in testing the shared
analysis logic were becoming unwieldy and difficult to maintain.
Replace them with implementations of the subtype and factor algorithms
from the spec.

Change-Id: Ic607c3fda45a69661e094292a38a7bc8fd22859a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279748
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-26 15:53:47 +00:00
Josh Soref 01b28894e7 Spelling pkg dev compiler
Closes https://github.com/dart-lang/sdk/pull/50861

GitOrigin-RevId: 71005e6f5bf5a151cb5c1aefb6a2a300fc40f592
Change-Id: Iadfafb5787a62e9a379437f6a3763d31f99ba7c6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277743
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
2023-01-26 09:12:41 +00:00
Paul Berry 6fa2f50380 Shared analysis tests: Use a map for record named fields.
This is consistent with the analyzer implementation and more intuitive
to work with.

In a follow-up CL I'll be adding logic that performs additional
manipulations on record types, and this change will make that CL
easier.

Change-Id: I4a0592e240a98f62d3730068600a84adeddfe09d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279656
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-25 22:26:19 +00:00
Paul Berry 61f23df80f Flow analysis for null-check/null-assert patterns.
Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I7fd845b0e465b63a2a9fd5e17bf254b9073a7d1f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279459
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-25 16:37:30 +00:00
Konstantin Shcheglov c631353906 When switch expression has no cases, make its type 'dynamic'.
Change-Id: I77358be2373ce9a70860d783b23fd671b5d021c3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279339
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-01-25 16:32:00 +00:00
Martin Kustermann 3378c55f7d [CFE/Analyzer] Avoid many duplicate strings in analyzer heaps
Right now the analyzer clears out the string canonicalization cache
after each file parse.

Similarly when the analyer serializes various information into
*.{unlinked2,linked,resolved} files & deserializes them again, it will
only have canonicalized strings per unit.

This means that strings that are common across compilation units will
exist many times in the heap, thereby increasing memory consumption of
the process.

This CL tries to avoid that by

  * Using one string canonicalization cache across CFE & Analyzer

  * Making the string canonicalization cache remember how often an entry
    was used

  * Pruning the cache instead of clearing it: We keep the most frequently
    used elements while maintaining a cache that is <= 5 MB.

  * Change analyzer code to use the string canonicalization in various
    places to avoid many duplicate strings in the heap.

When running analyzer on flutter (and it's transitive code), the
analyzer itself has a heap of around 610 MB.

This CL reduces the memory consumed by strings from ~43 MB to ~33 MB
(~ 20 MB are alone dart source files which are unique). We do this at
the expensive of maintaining the string cache, which costs around 2 MB.

That leads to 8 MB of savings.

TEST=ci

Change-Id: Ic28d70492ab0d376e10714c7ecf2c258c790a022
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/255245
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2023-01-25 14:38:23 +00:00
Paul Berry c90d7839eb Patterns flow analysis: don't promote scrutinee based on subpatterns.
A variable or wildcard pattern should only promote the scrutinee if
the expression is being matched is the scrutinee.  If the variable
pattern is a subpattern, no promotion should occur.

E.g. promotion occurs here:

    f(Object? x) {
      if (x case int _) {
        // `x` is promoted to `int`
      }
    }

But not here:

    f(Object? x) {
      if (x case num(sign: int _)) {
        // `x.sign` is known to be an `int`, but `x` is simply a `num`.
      }
    }

Change-Id: Iaa5bab8ce5b81db9d5496ac0bf2ee10473302371
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279641
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-25 00:02:21 +00:00
Johnni Winther 3184f2224b [_fe_analyzer_shared][cfe][analyzer] Initial implementation of exhaustiveness checking
This adds the initial implementation of exhaustiveness checking in
the analyzer and CFE. The checking is currently only performed in
switch statements and only handle a subset of the patterns.

Change-Id: Ia0050c2c80fbefe3e22615599136f9d919ebe4ef
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279173
Reviewed-by: Jonas Termansen <sortie@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2023-01-24 13:34:46 +00:00
Paul Berry cdfc07a73d Restore expectInferredType functionality for wildcard pattern tests.
This test functionality was inadvertently dropped in
5879990be0.  Fortunately none of the
tested behaviours have regressed.

Change-Id: I403976baf82c000ecc9da1be108eaee3db8a0bba
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279457
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-23 18:08:32 +00:00
Paul Berry ba8f0bd947 Shared type analysis: rework testing of joined pattern variables.
Rather than allocate instances of PatternVariableJoin during the
"previsit" stage, the test is responsible for allocating them prior to
calling `run`.  This will allow the test to include assertions in the
data structure passed to `run` that interrogate the state of the
joined variables (e.g. to check that they're properly promoted).

Change-Id: I7524c540872be153eb0a4cdf8bff679843335231
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279277
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-21 01:02:49 +00:00
Paul Berry c498870e85 Shared analysis: API changes for pattern variables.
This change is part of a process of shifting the responsibility of
tracking the set of variables defined by a pattern into the shared
analysis logic.  A `variableName` parameter is added to
`analyzeDeclaredVariablePattern`, and a field `componentVariables` is
added to `MatchContext` to track the set of variables associated with
each variable name in a pattern.  In the CLs that follow, I plan to
use this information in the place of the `getJoinedVariableComponents`
method.  This will give the client more flexibility in deciding when
and how to join variables in logical-or patterns.  (This is needed
because the CFE joins variables eagerly as a necessary part of
lowering, whereas the analyzer joins variables only at the end of the
pattern, to improve the quality of error messages).

Also, a `variables` parameter is added to `analyzeIfCaseElement` (to
make it consistent with `analyzeIfCaseStatement`), and the
`patternVariables` parameter is removed from `analyzePatternForIn`.
With these changes, we now consistently have a `variables` map in
situations where the pattern match is refutable (and thus logical-or
patterns are allowed), and we have no `variables` map in situations
where the pattern match is irrefutable (and thus no variable joining
is needed).



Change-Id: I1c511d12e827c52ef9bd8b7ff2b561ea9713a932
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279239
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-20 23:36:39 +00:00
Johnni Winther d1b763a2e0 [cfe] Add stack checking to inference visitor
Change-Id: Ic09e772fab088fd3415e3d68cd4ca63974fae53a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279341
Reviewed-by: Jens Johansen <jensj@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-01-20 14:58:17 +00:00
Alexander Thomas b9b6511ca6 Spelling sdk
Closes https://github.com/dart-lang/sdk/pull/50918

Co-authored-by: Josh Soref <jsoref@gmail.com>
GitOrigin-RevId: 1fd275051c561b63d374fb47e76a22424c4a12a9
Change-Id: I97790d9c79ff659f2c1fa2d2d46d041fe67957cb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278530
Reviewed-by: William Hesse <whesse@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
Reviewed-by: Tess Strickland <sstrickl@google.com>
2023-01-20 12:37:49 +00:00
Paul Berry c04c9ab27a Flow analysis: change declare's initialize parameter to required named.
This makes it easier to understand what's going on at call sites.

Change-Id: I9bce1b453b8537121c5aa929ecf5cd5451ee8d96
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278885
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-19 22:32:32 +00:00
Paul Berry 30f19bb727 Shared patterns analysis: simplify handle_ifCaseStatement_afterPattern.
The `variables` parameter was just a copy of
`node._candidateVariables.values`, which the client can just as easily
access on its own.

Change-Id: I0dc118ec51b31e9bcb2abf685145eec3a9b4fe9f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279236
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-19 21:26:34 +00:00
Paul Berry 1110192d62 Flow analysis: refactor pattern variable initialization logic.
In order to handle variable patterns inside logical-or patterns, flow
analysis will need to model the implicit temporary variables that
represent the variables before they are joined to form the final
variable value.  This CL adds the necessary logic to model this:

- `declaredVariablePattern` is now responsible for initializing the
  temporary variable (the client no longer needs to call `initialize`
  when analyzing a variable pattern).  It returns an integer
  representing the implicit temporary variable.

- A new API call, `assignMatchedPatternVariable` can be used by the
  client to transfer the variable from the implicit temporary variable
  to a user-accessible variable.  For now, the shared analysis logic
  always calls this from `analyzeDeclaredVariablePattern`, after
  calling `declaredVariablePattern`.  However, in the future, it will
  postpone the call until after these temporary variables are
  implicitly joined by logical-or patterns.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I2b78a46c11d0d46c8e0a8691c2a2ce49dceb2a24
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279078
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-19 19:49:24 +00:00
Kallen Tu ca974e5c4f [analyzer/cfe] Reland: Parse final class modifier.
Reland same files as https://dart-review.googlesource.com/c/sdk/+/278090
The only major change is parser_impl.dart parsing changes for 'final'

This change should allow 'final' for classes and mixins and maintain prior final behaviour for other constructs such as enums, typedefs, or the breaking line which was 'final library = ...;'

Change-Id: I3d84fc47479f39df6d5b0dcbe8f92d1fb78a826a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279076
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-19 16:08:28 +00:00
Johnni Winther dc12e2982b [_fe_analyzer_shared] Add support for stack base checking
This adds support for checking the stack content against a stack base,
which enables testing for an empty stack, relative to the stack base.

Test added for the stack checker.

Change-Id: I4937a4c77c0c2d8d6673e7848ccedfd8b1491ab6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279340
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-01-19 10:44:42 +00:00
Johnni Winther 60fd2a131c [_fe_analyzer_shared] Extract stack checker for reuse
This move the stack checking mechanism to a mixin to prepare for
reuse in the inference visitor.

Change-Id: I40a3ecb9ddd9e23a7044e28f8534cfb460a158bb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279171
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-01-19 07:57:06 +00:00
Konstantin Shcheglov 68c32274c7 Fix joining variables in switch statement.
Instead of joining pattern variables in shared case scope after every
top-level pattern, we now accumulate them, and join at the end of the
shared scope.

Change-Id: Icc1d426e53ccfe48a72e3194e1d7e96e6e90d781
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279264
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-01-18 19:32:42 +00:00
Ahmed Ashour 49c7d93042 [cfe/analyzer] add CompileTimeErrorCode.CONTINUE_LABEL_INVALID
Fixes #49852

Change-Id: Ic4d475d383dba51ad438de5ef7f928ce5d105e12
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277481
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-01-18 19:10:46 +00:00
Kaushik Iska 885457e1d3 Revert "[analyzer] new warning for nullable '==' parameter type"
This reverts commit 48ee1f218d.

Reason for revert: https://github.com/flutter/flutter/issues/118632

Original change's description:
> [analyzer] new warning for nullable '==' parameter type
>
> This rule checks that a parameter to an `operator ==` implementation has
> a non-nullable type.
>
> I intentionally did not enforce, in this rule, that the parameter is
> exactly `Object`. It is legal to narrow the parameter type to a
> different non-nullable type, like `int`. I can't imagine doing it, but
> it seems to be unrelated to whether the type should be nullable or not.
>
> Fixes https://github.com/dart-lang/linter/issues/3441
>
> Replaces https://github.com/dart-lang/linter/pull/3923
>
> Change-Id: I61d4a7b1ab8318dc9403da1633c352de95bfac61
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277700
> Reviewed-by: Mark Zhou <markzipan@google.com>
> Commit-Queue: Samuel Rawlins <srawlins@google.com>
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>

# Not skipping CQ checks because original CL landed > 1 day ago.

Change-Id: I6f96936b8d4e785b5f8e9719751e4b61c2a6ca2a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279141
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Siva Annamalai <asiva@google.com>
Reviewed-by: Mark Zhou <markzipan@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
2023-01-17 23:53:18 +00:00
Paul Berry 03e1659cc4 Flow analysis: reduce the extent to which variable is used by initialize().
In a future CL I will need the ability for flow analysis to initialize
promotion keys that aren't associated with any particular variable.
In anticipation of that, this CL refactors `FlowAnalysis.initialize`
so that it immediately looks up the unpromoted type of the variable
(which is the only information it needs), and thereafter just uses the
variable's promotion key.

From the point of view of flow analysis clients, there is no
functional change.

Change-Id: I54794bde49c7af745b43a09914f70c9c4e6d48da
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279074
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-17 22:55:39 +00:00
Paul Berry 7612b4ba92 Shared analysis: clear Node._nodesWithUnusedErrorIds even on test failure.
Without this, a failure in one shared type analysis unit test might
cause the next test to incorrectly fail with "Unused error ids".

Change-Id: I0d8b507b32b93edb9bf8f86c93f2fd2fa8447201
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278996
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-17 21:57:23 +00:00
Konstantin Shcheglov 5fbbb75c53 Compose error text from arguments in _MiniAstErrors.
Change-Id: If02bc4a814eed2be11dffdc769d7784b68e2b972
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278988
Reviewed-by: Paul Berry <paulberry@google.com>
2023-01-17 19:25:11 +00:00
Konstantin Shcheglov 7e6ad397b0 Resolve ForPartsWithPattern in ForElement and ForStatement.
Change-Id: I66e91a31213a56f6cab1f7b0a826752ca4a13d96
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278980
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-01-17 19:24:34 +00:00
Johnni Winther 4ffe1b2768 [_fe_analyzer_shared] Refactor NullValue and ValueKind
This moves the ValueKind and NullValue interface to the util package
and add the NullValue as an interface to the NullValue enum, which has
now been renamed to NullValues.

This prepares for reusing the stack checking mechanism from the
StackListener in the inference visitor of the CFE. The adding of
NullValue as an interface, allows for using ValueKind and NullValue
that are specific to the types used in the stack.

Change-Id: I7d6b3d3932753898d87ef774a95460832efdc969
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279084
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2023-01-17 07:44:40 +00:00
Srujan Gaddam 0673894cd9 [pkg:js] Disallow @staticInterop synthetic and @anonymous constructor tear-offs
Generative synthetic constructors of @staticInterop classes are already
disallowed, but this adds errors for tear-offs of such members as well.
This also disallows tear-offs of @anonymous @staticInterop factory
tear-offs. This aligns with what we want to do with object literal
constructors going forward, as tear-offs will implicitly have different
semantics than direct invocations. To avoid that inconsistency, we
disallow tear-offs here.

Change-Id: Ifc9e4a9251743613ee1ea2eca6e42e36c3b20461
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278645
Reviewed-by: Riley Porter <rileyporter@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-16 22:31:33 +00:00
Aske Simon Christensen cffd49e97c [dart2wasm] Report warning on sync*
Change-Id: If5b0492438830b8813d86a031193fa8d64ed1b1c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279017
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-16 14:05:08 +00:00
Kallen Tu ed1157acc4 Revert "[analyzer/cfe] Parse final class modifier."
This reverts commit 2f1372b460.

Reason for revert: Breaks the build. Unsure if there are other affected failures.

Original change's description:
> [analyzer/cfe] Parse final class modifier.
>
> Change-Id: Ia554c4f8f9617cc883a472eeb819aab2fd849077
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278090
> Reviewed-by: Johnni Winther <johnniwinther@google.com>
> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
> Commit-Queue: Kallen Tu <kallentu@google.com>

TBR=scheglov@google.com,johnniwinther@google.com,kallentu@google.com,dart-scoped@luci-project-accounts.iam.gserviceaccount.com

Change-Id: I5dd74202bd46d69b7261d6d30d8cf3b1a40dbc99
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279040
Reviewed-by: Jake Macdonald <jakemac@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2023-01-13 21:04:01 +00:00
Sam Rawlins 48ee1f218d [analyzer] new warning for nullable '==' parameter type
This rule checks that a parameter to an `operator ==` implementation has
a non-nullable type.

I intentionally did not enforce, in this rule, that the parameter is
exactly `Object`. It is legal to narrow the parameter type to a
different non-nullable type, like `int`. I can't imagine doing it, but
it seems to be unrelated to whether the type should be nullable or not.

Fixes https://github.com/dart-lang/linter/issues/3441

Replaces https://github.com/dart-lang/linter/pull/3923

Change-Id: I61d4a7b1ab8318dc9403da1633c352de95bfac61
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277700
Reviewed-by: Mark Zhou <markzipan@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-01-13 19:51:29 +00:00
Kallen Tu 2f1372b460 [analyzer/cfe] Parse final class modifier.
Change-Id: Ia554c4f8f9617cc883a472eeb819aab2fd849077
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278090
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2023-01-13 19:16:20 +00:00
Paul Berry 510cf2b258 Flow analysis: fix synthetic break insertion for unreachable switch cases
Previously, if a switch case was unreachable, we would not insert a
synthetic break at the end of it.  Technically this is not a problem
(since a synthetic break is only required to prevent one case from
falling through to another at runtime, and unreachable code is by
definition never reached at runtime).  However, it makes it confusing
for CFE and back-end developers, and there's little harm in adding the
synthetic break anyway.

So with this change, we determine whether to add the synthetic break
by checking whether the bottom of the case block is reachable *from
its top* (rather than globally reachable).

Fixes #50994.

Bug: https://github.com/dart-lang/sdk/issues/50994
Change-Id: I17757b182c29da782457adc057b1b8a6fc91e55e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278897
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-13 14:25:58 +00:00