Commit Graph

1015 Commits

Author SHA1 Message Date
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
Konstantin Shcheglov 55ffa4a4a3 Resolve pattern-for-in elements.
Change-Id: Id220aaee23aeffc5506d4ec967032adc9977c05a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278893
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-01-12 20:15:26 +00:00
Konstantin Shcheglov f486b00c9b Report INCONSISTENT_PATTERN_VARIABLE_SHARED_CASE_SCOPE.
Change-Id: Icb7b38ed433d72962e59b2653341623f0f8af388
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278890
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-01-12 19:08:01 +00:00
Kallen Tu 4aaf1a2cb1 [analyzer/cfe] Parse base and interface class modifiers
Change-Id: I52f8ac53ec504b4c1d07c04744b4b079a1163dfd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277721
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-12 18:47:39 +00:00
Paul Berry 7da7a32b93 Flow analysis: pass variable type to FlowAnalysis.declare.
This will help facilitate a follow-up CL in which I plan to modify
flow analysis data structures to track the static (unpromoted) type of
each variable, rather than querying that information from the client.
That in turn will help address some subtle bugs in the flow analysis
of patterns wherein an invalid promotion chain is getting created
because the wrong variable is being queried.

As part of this change, I've cleaned up the logic that calls
`FlowAnalysis.declare` for the synthetic variables associated with
or-patterns and switch cases that share a body; previously this logic
was making redundant calls to `declare`; now it is only calling
`declare` once per synthetic variable.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I8e8e4aa71afe3ac7ecdc474f023ec513dc7286b1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278693
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-11 19:22:15 +00:00
Konstantin Shcheglov dcc27c757e Report DUPLICATE_PATTERN_ASSIGNMENT_VARIABLE
Change-Id: Id42c5868e9face4e266fac8dd067270dd2ee042d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278814
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-01-11 17:41:58 +00:00
Jens Johansen 82e1b443a6 [parser] Recover when there's space between metadata and parenthesis
In https://dart-review.googlesource.com/c/sdk/+/272600 the change to
disallow spaces between metadata and parenthesis is the parenthesis
is supposed to be arguments for the metadata. Recovery was requested
on that CL and this is it.

Change-Id: I26c809f09cf8411365a2b758f635a494e7ebbd49
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278516
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-11 07:56:47 +00:00
Jens Johansen 76a834c42f [parser] Fix seeing required named parameters as required
In 815ef172a8 the concept of requiredNamed
was introduced in the parser, but not everywhere was updated.
One of these places introduced the bug noticed in
https://github.com/dart-lang/sdk/issues/50908

This CL fixes that.

Change-Id: I2a7926214349f6c6236c25b9401ccf2ead0b4b06
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278744
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-01-11 07:08:05 +00:00
Paul Berry 78ac63303b Flow analysis: fix handling of patterns inside guards.
Thanks to Konstantin for noticing this issue.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I0380b846839c5afa1b20a52d55d980ddb6d09fec
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278810
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-11 00:02:16 +00:00
Konstantin Shcheglov 4c7fb344ab Resolve pattern-for-in statement.
Change-Id: I6b2ec77dd2caff5a9693dfa33d9b3d7e91344f74
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278697
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2023-01-10 18:51:52 +00:00
Konstantin Shcheglov 10c22134b3 Report UNNECESSARY_NULL_ASSERT_PATTERN and UNNECESSARY_NULL_CHECK_PATTERN.
Change-Id: Icf2fcca95cb461e2e6bc1547acb8ee29a2df4148
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278687
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-01-10 18:37:40 +00:00
Paul Berry 6a4a6a167d Flow analysis: remove logic to handle implicitly declared variables.
The analyzer and CFE now properly declare all variables, so we don't
need this logic anymore.

Change-Id: I8e0075fe78986685f4b3749b3c20d1d3610cc4af
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278538
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-10 13:24:01 +00:00
Paul Berry 46da2f51d0 Flow analysis: add assertions to verify that declare isn't called unnecessarily.
In a follow-up CL I'll be making changes to what code is responsible
for calling `FlowAnalysis.declare`.  This assertion will help make
sure I make those changes correctly.

Change-Id: I8b390e339b45faa8ee46f2db856d9aaa1f9a16c7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278649
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-10 13:15:58 +00:00
Brian Wilkerson 14ff1f2d35 Generate a diagnostic for language overrides prior to 2.12
Change-Id: Ia10cf89443208ef133dd1ce8d68ee7c6b4129fbb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276800
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Stephen Adams <sra@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2023-01-10 05:27:48 +00:00
Kallen Tu 30b9b20965 [analyzer] Report error if class mixed in outside of its library unless it's a mixin class.
Classes can't be implicitly used as mixins outside of its library unless its in a language version prior to sealed classes.

See: https://github.com/dart-lang/language/blob/master/working/base-interface-final/feature-specification.md#mixin-classes
Change-Id: I77762a1c962e10f89430e2b4c6141d00ace1cb30
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/274601
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-01-09 20:22:14 +00:00
Paul Berry 5b234e4b0d Flow analysis: properly model if/else nature of switches.
A switch statement like this one:

    switch (E) {
      case P1 when G1:
        S1;
      case P2 when G2:
        S2;
      case P3 when G3:
    }

Is equivalent to an if/else chain like this:

    var tmp = E;
    if (tmp case P1 when G1) {
      S1;
    } else if (tmp case P2 when G2) {
      S2;
    } else if (tmp case P3 when G3) {
      S3;
    }

Therefore, if the failure of a particular pattern/guard combination to
match implies a type promotion, it makes sense for that promotion to
be carried into later cases.  For example:

    int? x = ...;
    switch (E) {
      case _ when x == null:
        break;
      default:
        x.isEven; // OK because `x` known to be non-null.
    }

This enabled some more thorough testing of type promotion in switches,
which then caught a bug introduced in a previous CL: when the switch
scrutinee is a variable reference, and we are trying to determine
whether it is safe for a pattern to promote the scrutinee variable, we
were checking the wrong SSA node to determine whether the variable had
been reassigned.  For example:

    Object x;
    switch (x) {
      case _ when f(x = ...);
        break;
      case int _:
        // `x` is not promoted to `int` because it is no longer the
	// same as the cached scrutinee.
        break;
    }

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: Ie8d6cf0fc662aa5ef0ac81eb2343952028dd2abb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278533
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2023-01-09 18:22:51 +00:00
Paul Berry 773bd2f694 Rename TypeAnalyzer.getVariableType's parameter.
The previous name, `node`, was confusing, because the type is
`Variable`, not `Node`.  Although the concrete type used by the CFE
for `Variable` (`VariableDeclaration`) happens to be a type of `Node`,
the same is not true for the analyzer (which uses the type
`PromotableElement`).

Change-Id: Idf3a7c3101786f7d36fa9075053544307ba18519
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278648
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-09 18:12:48 +00:00
Chloe Stefantsova 4b1eba51eb [cfe] Handle rest patterns in map patterns
Part of https://github.com/dart-lang/sdk/issues/49749

Closes https://github.com/dart-lang/sdk/issues/50886

Change-Id: Ie44c35030aa3e74ab0b65f2e0abd0c5fb207976e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278501
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2023-01-07 00:09:45 +00:00
Konstantin Shcheglov 5879990be0 Separate WildcardPattern and its shared analysis.
Change-Id: I39fc26fa2f3390338cd14433248dd58c51fd3d1d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278527
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-01-06 20:32:26 +00:00
Jens Johansen 79fd3dde08 [parser] 'on' after 'try' is always an on-clause
https://github.com/dart-lang/language/blob/master/accepted/future-releases/records/records-feature-specification.md#ambiguity-with-on-clauses

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

Change-Id: I0dadbdd5bf7ac1fef69191e38eaaa705cda60f2e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/275922
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-01-06 11:11:59 +00:00
Chloe Stefantsova e925066175 [cfe] Adjust scoping for variable patterns in switch expressions
Part of https://github.com/dart-lang/sdk/issues/49749

Change-Id: I32f3ebf6c45afbbdc291d8f919b14266b646b998
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278347
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2023-01-05 15:49:44 +00:00
Konstantin Shcheglov 18566d0cb6 Report duplicate rest patterns in list/map patterns, and not-last rest pattern in map pattern.
Change-Id: I48238cc230f5a1bcff1e1cb4ab80731c2b978396
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278369
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-01-05 15:40:18 +00:00
Ahmed Ashour 17abdd6d2d [flow analysis] fix label statement
Fixes #50294

Change-Id: I39503270f82788a9edb4ce6c53251b0e0ebb05fb
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277400
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-05 13:49:40 +00:00
Chloe Stefantsova 4a8c6fa480 [cfe] Support guards and multiple heads in pattern switch statements
Part of https://github.com/dart-lang/sdk/issues/49749

Change-Id: I6c0dcd4bd2cb7921c27168b71f61f1793c8b2a12
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278182
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2023-01-05 11:36:28 +00:00
Konstantin Shcheglov 79e6beaf7d Prepare to publish analyzer 5.4.0 and _fe_analyzer_shared 52.0.0
Change-Id: Iafb9a1f8d5ce9f8502795192c9cbc6b26cb337c0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278300
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-01-04 19:33:48 +00:00
Sam Rawlins 160f64c672 Avoid implicit dynamic in TypeAnalyzerErrors
Change-Id: Ieeba38c75cb674ac79dbfa8259cdccd7e1cd5003
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277583
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
2023-01-04 15:41:12 +00:00
Johnni Winther dffa3191b9 [cfe] Handle case const
This push the constant context for constant patterns such that
the constness is propagated to subexpressions.

Closes #50629

Change-Id: Idcb279765c5464ba17abb98b3f9eb852a7c87b96
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277860
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-01-04 13:37:54 +00:00
Konstantin Shcheglov 922b56931b Join variables after switch expression patterns.
Change-Id: I854d77acf787d54fd51c9beb08e6fc0ee1566887
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276768
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-01-03 22:11:58 +00:00
Paul Berry e64c60dbdc Flow analysis: replace context toString methods with a less fragile mechanism.
Adds getters `_debugFields` and `_debugType` to the `_FlowContext`
base class, and adds a single implementation of
`_FlowContext.toString` that builds a representation of the context
based on them.  This replaces the implementations of `toString` in all
the classes derived from `_FlowContext`, which were more difficult to
get right and keep synchronized with code changes.

Also changes `_TryFinallyContext` from a `late final` variable to a
nullable variable.  This sacrifices a tiny bit of safety, but has the
advantage of allowing `toString` to show the value if it's been
initialized, and avoid crashing if it hasn't.

Change-Id: I0ca3ddfed21934c54eaba912c84edda79c8eadfc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/276202
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-01-03 20:30:42 +00:00