Commit Graph

1099 Commits

Author SHA1 Message Date
Konstantin Shcheglov 67e7c96962 Fix parser to use correct begin token, to get documentation comments from it.
Change-Id: I9505353c9c6c00082e588aced34e8151c7ab22b7
Bug: https://github.com/dart-lang/sdk/issues/51447
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/284441
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-02-22 17:03:38 +00:00
Paul Berry 8a4f638b07 Shared type analysis: stop tracking MatchContext.initializer
It turns out that some time ago I got rid of the logic that depends on
this; the associated functionality is still there, but it's tracked
automatically by flow analysis via
`_PatternContext._matchedValueInfo`, so we no longer need to track it
in the shared analysis logic.

Change-Id: Ia38e0baf5f599c929aa2000aade41473b997f80a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/284480
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-22 16:41:59 +00:00
Alexander Markov 143e4e9dc8 [vm/aot/tfa] Static weak references to method tearoffs
TEST=pkg/vm/testcases/transformations/type_flow/transformer/weak.dart
TEST=language/vm/static_weak_reference_test
TEST=language/vm/static_weak_reference_error_test

Bug: b/269223463
Change-Id: I23c8229c39217aa1c3f9fb576d8eefa5ceb1d8ad
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283421
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2023-02-22 01:01:29 +00:00
Kallen Tu 1ce23bbd0b [cfe] Disallow subtyping Function with class modifiers enabled.
Fixes issue #51435

Change-Id: I7adaf190246f88dc3bd7a1e1e2f5dff231e03653
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283924
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2023-02-21 22:35:32 +00:00
Konstantin Shcheglov 479a98f5b5 Prepare to publish analyzer 5.6.0 and _fe_analyzer_shared 54.0.0
Change-Id: I6b36a25adfa78cb8d0534afa006daa66a6a8ed78
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/284380
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-02-21 21:26:42 +00:00
Kallen Tu 57107ae59b [analysis_server] Add code completion for class modifiers at the beginning of a declaration.
Change-Id: I9dc1d80264d052c8f2ac75978e64280bd271f8d7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282440
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2023-02-21 19:31:55 +00:00
Kallen Tu 3118002665 Revert "[analyzer/cfe] Report an error when declaring a mixin class with final, interface, or sealed."
This reverts commit f17e9732c5.

Reason for revert: Merged this and forgot there were dependencies that needed to be merged first.

Original change's description:
> [analyzer/cfe] Report an error when declaring a mixin class with final, interface, or sealed.
>
> Change-Id: Ia1393851dffaab55c31d04d4e81bfae83a7bd67f
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283126
> Commit-Queue: Kallen Tu <kallentu@google.com>
> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
> Reviewed-by: Johnni Winther <johnniwinther@google.com>

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

Change-Id: Ia339aac26ecd20aa8725cc9109919e3cbef6363b
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283741
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
2023-02-16 22:10:33 +00:00
Kallen Tu f17e9732c5 [analyzer/cfe] Report an error when declaring a mixin class with final, interface, or sealed.
Change-Id: Ia1393851dffaab55c31d04d4e81bfae83a7bd67f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283126
Commit-Queue: Kallen Tu <kallentu@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-16 21:27:28 +00:00
Paul Berry 6e6f89a255 Flow analysis: fix handling of promotions in when clauses with shared case bodies.
Previously, if multiple switch cases shared a body, and at least one
of those cases promoted a match variable using a `when` clause, the
promotion would not be carried over to the merged variable seen in the
shared case body.  This was happening because promotions performed in
the `when` clause were applied to the promotion key associated with
the client's representation of the variable, whereas flow analysis was
computing the state of the merged variable based on the promotion keys
used internally while visiting the pattern (which don't include
promotions from the `when` clause).

With this change, flow analysis now computes the merged variable state
based on the promotion keys associated with the client's
representation of the variable components, so promotions from `when`
clauses take effect.

In the process, I've moved a lot of the tracking of these promotion
keys from the `TypeAnalyzer` class to the `_FlowAnalysisImpl` class.
This avoids the need to expose more flow analysis internals through
its public API.

Fixes #51399.

Bug: https://github.com/dart-lang/sdk/issues/51399
Change-Id: I8f1bb6e49ceb8441bb743c63af61c119df9041f2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283441
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-16 18:11:14 +00:00
Johnni Winther 763edcaf86 [_fe_analyzer_shared] Support switch expressions and errors in exhautiveness id testing
Change-Id: Ic1846f7a8b56fc72a816d9b6d4b930f80974759f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283321
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-02-16 11:53:16 +00:00
Johnni Winther f583a2bf73 [parser] Fix error reporting on const ()
The parser wrongly flagged any `const (...)` in a constant pattern
context and not just the intended `const ()`.

Closes #51415

Change-Id: Id7b0621cda4a144ac186dea55dbcbeaf041349bf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283602
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-02-16 11:35:28 +00:00
Johnni Winther ac20881b23 [_fe_analyzer_shared] Handle record types in exhaustiveness checking
This adds support for record types in the exhaustiveness algorithm.

The original algorithm was based on that record pattern would
match fields on all types, but that is no longer the case. Instead
record patterns only match corresponding to their own type. For this
reason the testing code is updated to create record spaces in relation
to a type. For instance, when the test create a record space {x: B}, it
is know create in relation to a type, say (x: A, y: A), and the create
space will therefore have (x: *, y: *) structure where the y: component
is implicitly Top, similar to how object patterns are used.

Unlike the Dart record types used for type checking and inference, the
record types used for exhaustiveness do not take the field types into
account for its subtype relation. This is avoid conclusions like
(int i, Object o) and (Object o, int i) having no values in common because
their corresponding types (int, Object) and (Object, int) are not subtypes
of each other. Instead, the subtype relation for record types used for
exhaustiveness only use the structure of the record to determine whether
two types are related.

Note though, that fields of a record type still know the type of the field.
This is used when expanded a record type into a space; the field spaces
will be derived from the field types in this case.

Change-Id: I84735d827494bcf384fd5f419d71933830ff5d15
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283182
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-02-16 00:36:01 +00:00
Paul Berry c964f14a0d Shared type analysis: add optional exhaustiveness error.
This optional error fires if patterns are enabled and the type
analyzer encounters a switch that is required to be exhaustive (i.e. a
switch expression, or a switch statement with an always-exhaustive
type), and flow analysis cannot prove that the switch is exhaustive.

This is intended to be available as a temporary workaround if we
decide to ship an early beta of the "patterns" feature before
exhaustiveness checking is sufficiently ready.  We won't enable it
unless we need to, and we won't ship the final patterns feature with
it enabled.

Change-Id: I8c4c65c21abdc32b91537b3f30e7fd86f446f571
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283060
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-15 20:18:32 +00:00
Paul Berry 9a608aa7d8 Shared analysis: switch all error methods to named arguments.
This makes the unit tests clearer, since it's possible to tell the
meaning of each argument.  It also simplifies the underlying unit
testing logic that formats the errors.

Change-Id: I7212fe0c909ca0b627f8a8889621af3a62a63f00
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283132
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-15 15:30:18 +00:00
Johnni Winther 0937087af8 [parser] Improve error recovery for constant patterns
This allows the parser to parse constant patterns at lower precedence
level in order to recognize more expressions in this context. To
support this, _parsePrecedenceExpressionLoop special cases a few cases
that should _not_ be parsed as expression in a constant pattern context.

Closes #50996

Change-Id: I43bb0ce52d366bd2dfcf47e12eec5883402f668a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282100
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2023-02-15 13:49:37 +00:00
Johnni Winther d4a73e3b82 [cfe] Report errors on invalid const patterns with explicit 'const'.
Change-Id: I4f5994fd1f0d5e5289684e0da2f03720706f6d28
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281840
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-02-15 10:34:28 +00:00
Paul Berry d77ea3b883 Shared analysis: clean up switchCaseCompletesNormally error.
The `numMergedCases` argument was a holdover from the old analyzer
representation of switch statements (in which cases sharing a body
were not merged); it is no longer useful.

Also, the analyzer tests in `switch_case_completes_normally_test.dart`
have been adjusted so that they no longer expect this error to be
issued when pattern support is enabled; this is consistent with the
spec (which removes this error).

Change-Id: Ia22a35b7a94ee585e0614d85012ee80b9dbc12d8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283125
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-14 22:37:37 +00:00
Johnni Winther 6c8faaca03 [_fe_analyzer_shared] Add TestEnvironment for exhaustiveness testing
This replaces the StaticTypeImpl with an implementation of the shared
exhaustiveness classes for a test type system.

This will ensure that the unittests test the shared part of the code
and make it easier to handle for instance fields on Object and
non-interface types.

Change-Id: I101cdd41cf86b2847bd5959c15d4138640e34679
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282882
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-02-14 21:04:41 +00:00
Konstantin Shcheglov 2783b50f90 If a field does not exist, pretend that it does, and matches everything.
Change-Id: Ifdd297642a4588ed3620ce5d453ace52a9ce38cf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283123
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-14 20:27:38 +00:00
Konstantin Shcheglov 4ddfa7bac5 Prepare to publish analyzer 5.5.0 and _fe_analyzer_shared 53.0.0
Change-Id: If616813aeab905026f936015d86560388aa80044
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/283101
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-02-14 18:41:48 +00:00
Johnni Winther 2b28b97e7a [_fe_analyzer_shared] Add new exhaustiveness algorithm
This adds the new exhaustiveness algorithm and uses it instead of the
old algorithm. The old algorithm is left in, for now.

Change-Id: I180934f05525aed3d4d43cd8a00bcc6e50305347
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282720
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-02-14 17:26:05 +00:00
Paul Berry b10ec24fe9 Flow analysis: implement type promotion for record patterns.
For each subpattern of a record pattern, we define its "demonstrated
type" to be the type that the matched value has been promoted to if
the subpattern match succeeds.  At the conclusion of visiting a record
pattern, we promote the whole record pattern's matched value to a
record type formed by combining together the demonstrated types of the
subpatterns.  So, for example, the pattern `(int _, String _)`
promotes the matched value to `(int, String)`.

This change contains a lot of specific tests, to make sure the
demonstrated type of each kind of pattern behaves as expected; but the
actual machinery is general, since it just takes advantage of the type
promotion performed by the subpatterns.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: Ie4da81964b5ade657c23a598ee18982b793fb4ac
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282806
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-14 14:42:36 +00:00
Konstantin Shcheglov 6a6b54c7d3 Report unnecessaryWildcardPattern()
Change-Id: I6bca9eb8ec95796bdbcb02a017e919c424d8415d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282482
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-02-14 01:07:33 +00:00
Ahmed Ashour 875c10f6fe [flow analysis] list pattern and map pattern
to be reachable for `ifTrue` and `ifFalse`.

This is because checking only the type to determine the reachability is not correct, since for the list and map patters, the expression can match, but this is not always true.

Fixes #51353

Change-Id: Iea53fd192d16e786fa8f2540425fc3cb97664d10
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282580
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-14 00:10:54 +00:00
Paul Berry e6a9446813 Flow analysis: don't de-promote pattern matched values.
Previously, we kept track of the promotion information for a matched
value using a `ReferenceWithType` stored in the `_PatternContext`.
This information did not get updated in the event of a promotion, so
that meant that if a pattern tried to promote the same value twice
(e.g. `int _ && num _`), the second promotion attempt would not see
the effects of the first attempt, so it might wind up demoting the
matched value.

The solution is to just track the promotion key in `_PatternContext`,
and resynthesize the `ReferenceWithType` object (with the proper type)
whenever we need it.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I026a4d2f42875a003ce8840dfc88d65484bb33ae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282800
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-13 20:18:47 +00:00
Konstantin Shcheglov 2026b38fcc Report DEAD_CODE in logical-or pattern, when LHS always matches.
Change-Id: I54098d9bd611daf3b287fac8d35c2de8ffb40a09
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282561
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-02-13 18:14:58 +00:00
Paul Berry 90feb064f3 Flow analysis: promote when assigning to variables in patterns.
Most pattern variable assignments don't need to promote, because
pattern variable assignments must be irrefutable, and thus the pattern
match cannot fail.  There's one exception, though: when the assigned
value has type `dynamic`, an implicit downcast is performed.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: Ic8f572303d60c354e9e342f79af03e669fa248c3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282381
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-13 15:55:19 +00:00
Paul Berry 95e6dba347 Flow analysis: add missing tests for wildcard patterns
Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: If38ad38cefbdca97144dd853c05aeca3ddb61cf2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282320
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-13 15:34:17 +00:00
Paul Berry 98f585d092 Flow analysis: fix improper handling of scrutinee EqualityInfo.
Previously, we stored an EqualityInfo object representing the
scrutinee at the time of entry to the top level of the pattern; then
when handling, for example, a relational pattern using `==`, we used
that EqualityInfo object to decide how to handle the equality check,
*even if we were no longer at the top level of the pattern*.  As a
result, we would sometimes come to incorrect conclusions about
relational patterns inside subpatterns.  For example, we would fail to
see that `if ((null,) case (== null,))` is a guaranteed match, because
when processing the `== null` pattern, we would erroneously use the
EqualityInfo for `(null,)` (which is *not* `null`).

To solve this, we only store the scrutinee *reference* at the top
level of the pattern (this is sufficient to allow us to decide whether
or not to promote the scrutinee).  When we encounter a pattern like
`== null`, we compute the appropriate EqualityInfo directly based on
the ExpressionInfo, type, and reference that are appropriate to the
current pattern level.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I4d1bc34fcb2d238e7b69e1b88df50b389410963c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282162
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-10 15:24:08 +00:00
Paul Berry 0bdaafe395 Flow analysis: remove _scrutineeType fields.
These fields are no longer being used.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I6305785d32c5ca2ff55aee297a5977dbd6ea4189
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/282300
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-10 14:44:59 +00:00
Paul Berry 0f11c4ed5a front_end: make use of TypeAnalyzer.analyzeConstantPattern.
This will allow the front end to take advantage of flow analysis
implied by a constant pattern (e.g. `case null` cause the scrutinee to
be promoted in later cases).

Fixes #51206.

Change-Id: Ic8516abb4e051ffca0d4e7698339282812020cae
Bug: https://github.com/dart-lang/sdk/issues/51206
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281261
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-02-09 18:41:01 +00:00
Paul Berry c72a10f5c5 Flow analysis: fix reachability for cast patterns.
If a cast pattern fails to match, it throws an exception, so the
"match failure" code path should be unreachable.

To fix this, I added an optional argument `updateUnmatched` to
`patternRequiredType` to allow the type analyzer to tell flow analysis
whether a required type is enforced via a match failure or an
exception.  I also renamed `patternRequiredType` to
`promoteForPattern`; this is in anticipation of an upcoming CL which
will use this method to promote to a type which isn't necessarily the
same as the required type.

I also discovered a few testing gaps while double-checking that every
call to `promoteForPattern` makes the proper choice about what to pass
for `updateMatched`; I've added test cases to cover these gaps.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: Id6f63138d01b481e7c9442469ce45e2aaa507a5a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281740
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-09 14:47:48 +00:00
Johnni Winther d89ae3c2c6 [parser] Report error on invalid const patterns
This handles the restriction imposed on the expression occurring
within a constant pattern.

Change-Id: I946d85f908609340a61e060c6bdbee143666fd91
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280460
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-02-09 12:43:31 +00:00
Aske Simon Christensen 321f2e9b65 [dart2wasm] Implement sync*
This is an implementation of `sync*` via two main mechanisms:

- The closure context infrastructure is used for preserving local
  state. All local variables in `sync*` functions are implicitly
  captured in the contexts even if they are not captured by a lambda.

- Suspension and resumption of the body is implemented via a state
  machine as a switch in a loop. This allows for an arbitrary control
  flow graph that can be resumed at any point. A subclass of the code
  generator generates control constructs containing any `yield` or
  `yield*` statements as jumps around this CFG while delegating the
  rest of the code generation to the normal member code generator.

This version does not support `switch` or `try` inside a `sync*`
function. Support for these statements will be added later.

Change-Id: Iec8236f64500d823f574aa628ddb0d22fe4ac2d0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280166
Reviewed-by: Joshua Litt <joshualitt@google.com>
Commit-Queue: Aske Simon Christensen <askesc@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-09 11:07:01 +00:00
Brian Wilkerson 20fb4593e3 Fix a bug in the parsing of incomplete map pattern entries
Change-Id: I7e07bd6c9fd627d222feff3936f6ea07d692a09e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281860
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
2023-02-09 05:31:57 +00:00
Konstantin Shcheglov b880af5bca Support for 'await' in pattern for-in statement/element.
Change-Id: Ib629768add8454a7f8e02824237c378ea78c9562
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281862
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-02-08 22:29:53 +00:00
Ahmed Ashour d23c304a58 Fix year and comma in header.
Bug #51184

Change-Id: I0f93d6aefcf693fbddbefe4cf96bf275a6bf702b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280168
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
2023-02-08 17:45:40 +00:00
Konstantin Shcheglov 55e0784a62 Use pattern type schema for expression inference.
https://github.com/dart-lang/language/commit/0277f0358606378a300ac3d9041417243ac08c02

Change-Id: Ie305303ad85c0c7c920f9a28f4357abd9a02de3f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281580
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-02-08 17:15:39 +00:00
Paul Berry a66dcf1b77 Flow analysis tests: use a trailing comma for unary types
Change-Id: I0183c332492b298d5d3db70f7222111757a0e364
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281544
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-08 15:38:48 +00:00
Paul Berry 832ae6722d Flow analysis: add support for relational patterns.
There are two new API methods: `equalityRelationalPattern_end` (for
relational patterns using `==` or `!=`, where some degree of flow
analysis is warranted) and `nonEqualityRelationalPattern_end` (for all
other relational patterns, where all we care about is making sure that
both the "matched" and "unmatched" branches are reachable).

I've moved most of the logic for constant patterns into
`equalityRelationalPattern_end`, since it's essentially the same logic
(except that `equalityRelationalPattern_end` also has support for
`!=`).  So now, `constantPattern_end` simply calls
`equalityRelationalPattern_end` in the case where pattern support is
enabled.

I also had to make a change to `RelationalOperatorResolution` to make
it possible to distinguish `==` from `!=`.

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: Idc5dbcfb02e3f8b57cfcccb3f46364fe34268ded
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280900
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-07 21:08:50 +00:00
Paul Berry 3d01b0aac3 Flow analysis: add support for constant patterns.
Since matching a constant pattern implicitly performs an equality
check, we do the same flow analysis for constant patterns that we
would have done for an explicit equality test.  There are only two
user-visible behaviours:

- If the constant pattern is a `null` literal, then in the code path
  where the pattern fails to match, the scrutinee is promoted to a
  non-nullable.

- If the constant pattern and the scrutinee are both `null` literals,
  the pattern match is known to always succeed (so the code path where
  the pattern fails to match is marked as unreachable).

The first of these two behaviours is genuine useful to the user, since
it allows things like:

    switch (expr) {
      case null:
        ...
      ...
      default:
        // expr is known to be non-`null`.
    }

The second behaviour is not so useful, but seemed worth doing to
maximize code sharing and to keep the behaviour consistent between
pattern matching and explicit null checks.

Making this work required extracting some of the logic that was
formerly in `_FlowAnalysisImpl.equalityOperation_end` into a method
`_equalityCheck`, which understands how to analyze an equality check
regardless of whether it's an explicit equality expression or an
implicit part of a pattern.  It returns an `_EqualityCheckResult`,
which `equalityOperation_end` examines to determine exactly what needs
to be done to for an equality test in an expression context;
similarly, `_FlowAnalysisImpl.constantPattern_end` now calls
`_equalityCheck` and then does the right thing for patterns.

It was also necessary to extract the logic from
`nullCheckOrAssertPattern_begin` for handling null checks in patterns;
that logic is now in `_nullCheckPattern`, which is also called by
`constantPattern_end`.

In the process of testing this change, I discovered (and fixed) a
minor bug: when analyzing a switch statement or switch expression, we
were re-capturing the value of the scrutinee before visiting each
pattern.  This meant, for example, that we would analyze the following
code incorrectly:

    int? i = ...;
    switch (i) {
      case _ when f(i = ...):
        ...
      case null:
        ...
      default:
        // `i` should *not* be promoted to non-null here, because it's
        // not guaranteed to be the same as the value matched by `case
        // null` above.
    }

Bug: https://github.com/dart-lang/sdk/issues/50419
Change-Id: I4d30d6bc2673d9968e69f3384a37e1b2b9cc4a8f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280861
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-07 18:24:54 +00:00
Konstantin Shcheglov ef474c9399 Add 'patternTypeSchema' to PatternAssignmentImpl and PatternVariableDeclarationImpl.
Change-Id: I02c3212ebf564755dfb1c0029d683bee1193d80c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281301
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-02-07 16:56:19 +00:00
Paul Berry 7532b5b138 Flow analysis: store full EqualityInfo for pattern scrutinees.
This will make it possible to generalize the flow analysis logic we
already have for equality expressions and calls to `identical`, so
that we can use it for constant patterns (which implicitly do an
equality check) and relational patterns involving `==` or `!=`.

This requires adding a new method to the flow analysis API,
`FlowAnalysis.assignedVariablePattern`, to handle variable references
appearing inside a pattern assignment.  Previously we were able to use
`FlowAnalysis.write` for this purpose, relying on its call to
`getExpressionInfo` to gather the relevant information about the RHS
of the assignment.  But now, since we are calling
`equalityOperand_end` from `_pushScrutinee`, the expression info is no
longer available at the time we visit the variable reference.

Change-Id: I9a28bced97dcf6ee15674eaa4430910121185489
Bug: https://github.com/dart-lang/sdk/issues/50419
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280219
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-02-07 14:22:56 +00:00
Paul Berry 52ca2d74f7 Shared analysis tests: Fix error messages around isLegacyExhaustive.
The named parameter `isExhaustive` was renamed to `isLegacyExhaustive`
in https://dart-review.googlesource.com/c/sdk/+/274720, but the error
messages that result from not using it properly were not updated.
This change updates the error messages to reduce confusion when
writing tests.

Change-Id: I20664ef53b0e0e758f2d43d52d5d1747e4c499f0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280660
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-07 13:55:48 +00:00
Konstantin Shcheglov 007562a06a Report matchedTypeIsSubtypeOfRequired for cast pattern.
https://github.com/dart-lang/language/pull/2813

Change-Id: If8942efca4fa736cb24c625bc83963bbd69302f0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/281060
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-02-06 20:53:50 +00:00
Daco Harkes 4e7d71b593 [vm/ffi] Disallow nullable struct fields
TEST=tests/ffi/regress_51041_test.dart

Closes: https://github.com/dart-lang/sdk/issues/51041
Change-Id: I44d82c5f378c5d001bfb7526be87c179b8160be1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279511
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-06 16:29:42 +00:00
Jens Johansen 1a6d6f7012 [parser] Add comments to calls
Change-Id: I66756be2c2d979dfbdd50c304f8b083abf2fc034
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280462
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-06 11:42:31 +00:00
Jens Johansen 76e2c48428 [parser] Support record type in variable pattern
When seing a '(' after a var/final in `parseVariablePattern` require a
type --- the only allowed thing is a record type, but we can't
generally allow for instance `() async =>` as a record type as that
normally means something else.

Fixes #51230

Bug: https://github.com/dart-lang/sdk/issues/51230
Change-Id: I2e4797595b545d4cb726e36ce4e70a4d775e7e25
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280720
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-02-06 09:12:54 +00:00
Johnni Winther 0aff070607 [cfe] Report pattern assignment of non-variables
Change-Id: Ieed7be9ec7e75e64f6f24f729e6211a0a4689c3a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280722
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-02-03 12:47:28 +00:00
Kallen Tu b7d0a982b6 [cfe] Report errors for final class and mixin subtyping outside of its library.
Change-Id: I958fd30e2ed37ae3ca4311967575c8176ca2cd48
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/280249
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
2023-02-03 01:17:41 +00:00