Commit graph

61 commits

Author SHA1 Message Date
Konstantin Shcheglov bd0e77c4fc Issue 52409. Reading a Never typed getter makes the flow unreachable.
Bug: https://github.com/dart-lang/sdk/issues/52409
Change-Id: I675ed8f8cfa0d2e5327c2c6bc0fb1bda7bde038c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/303840
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-05-17 03:28:07 +00:00
Konstantin Shcheglov c0d33e45d1 Issue 52151. Fixes for reachability in switch patterns when unresolved / error type.
Bug: https://github.com/dart-lang/sdk/issues/52151
Change-Id: Ib56df1711cbfb7a593c7b16fd6a67e45baeabc3c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/302455
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-05-12 19:16:42 +00:00
Paul Berry cb0eb28cba Account for differing flow analysis conventions between CFE and shared code.
With one exception (noted below), the shared analysis logic uses the
convention that the expressions passed to flow analysis are the
original (pre-lowered) expressions, whereas the expressions passed to
flow analysis by the CFE are the lowered expressions. This difference
caused two problems:

- If a boolean expression appeared on the right hand side of a pattern
  assignment or pattern variable declaration, and it was lowered, the
  flow analysis implications of that boolean expression would be lost.

- If a boolean expression appeared in a `when` clause, and it was
  lowered, the flow analysis implications of that boolean expression
  would be lost. Exception: for switch statements, the shared analysis
  logic has been passing lowered expressions to flow analysis, so this
  problem didn't occur for `when` clauses in switch statements.

Notably, when compiling for the VM, the CFE lowers expressions like
`x != null` to something more like `!(x == null)`.

Fortunately, the first of these two situations shouldn't cause
problems very often, since typically if the right hand side of an
assignment or variable declaration is a boolean expression, there is
no need for the left hand side to involve patterns.

As for the second of these two situations, it's also not too likely to
cause problems, since typically null checks occur inside patterns
rather than in `when` clauses.

As a short term fix, we remove the exception noted above, and we
account for the difference in conventions by adding a call to
`FlowAnalysis.forwardExpression` to the CFE's implementation of
`dispatchExpression`, so that when transitioning between CFE logic and
shared logic, flow analysis will be informed how to match up the
lowered expressions to their pre-lowered counterparts.

Longer term, I would like to switch everything to the convention of
using the original (pre-lowered) expressions; this will bring the
analyzer and CFE into better alignment with each other and should
eliminate a class of subtle bugs. This long term goal is tracked in
https://github.com/dart-lang/sdk/issues/52189.

Fixes #52183.
Fixes #52241.

Bug: https://github.com/dart-lang/sdk/issues/52183, https://github.com/dart-lang/sdk/issues/52241.
Change-Id: I2449ce34c54325603bc2730d1660a7cfc7d79aec
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/298840
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-05-10 19:31:19 +00:00
Paul Berry 3713c0e264 Patterns parser: prohibit variable/identifier patterns named when/as.
In https://dart-review.googlesource.com/c/sdk/+/299400, the parser was
adjusted so that it no longer accepts `when` and `as` as the names for
variable patterns in cases where there is a possible ambiguity
(e.g. `int when` is not accepted as a pattern because `int` is a
legitimate pattern, therefore `when` could introduce a guard
clause). This change further prohibits `when` and `as` from being the
names of variable patterns or identifier patterns even in the case
where there is no ambiguity. This is in line with the discussion at
https://github.com/dart-lang/sdk/issues/52199#issuecomment-1526297771,
and the spec change at
https://github.com/dart-lang/language/pull/3033.

Three new error codes are introduced, to cover the three circumstances
in which `when` or `as` might be used illegally: in a declared
variable pattern, in an assigned variable pattern, or in an identifier
pattern. I've also added analyzer tests to ensure that the parser
recovers from these errors nicely. Unfortunately, nice error recovery
is only feasible in the non-ambiguous cases.

I've also updated the language test expectations in
`tests/language/patterns/version_2_32_changes_error_test.dart` to
reflect the new error messages, and added a few more examples of uses
of `when` and `as` that are still permitted.

Fixes #52260.

Bug: https://github.com/dart-lang/sdk/issues/52260
Change-Id: I229f627aa639659c30b83c74895759207da279f7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301482
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-05-08 19:06:02 +00:00
Paul Berry a29487d803 Add tests for proposed version 2.32 changes to patterns.
Bug: https://github.com/dart-lang/sdk/issues/52199
Change-Id: I04fcac4a2b9a7e49a0231441feb764a022dd5ab8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/300320
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-05-04 17:28:50 +00:00
Robert Nystrom af07557984 Add (passing) test for type parameters in exhaustiveness checking.
Change-Id: I2b2dc6a586d66c93e918b077bd008c3683986d18
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301300
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2023-05-04 12:18:49 +00:00
Paul Berry 94862482d2 Test object pattern behavior with nullable and potentially nullable types.
Change-Id: Ic9075cdbb0aedd17a00d500c44dc33e1516b940b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/300323
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
2023-05-02 18:01:53 +00:00
Paul Berry 761b8019ef Update CFE expectation for error test.
Also update the test name to follow test naming conventions (tests
with error expectations are named `*_error_test.dart`), and add a
copyright notice and test description.

Bug: https://github.com/dart-lang/sdk/issues/52202
Change-Id: I762dcbf6ebd02190250ccdd9767e6db29cac1d61
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/300322
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2023-05-01 21:27:12 +00:00
Paul Berry 637dd76c7f Patterns parsing: fix ambiguity resolution for when and as.
This change fixes parsing of case clauses such as:

    case foo when !flag:

Constructions like this require some lookahead in order to parse
correctly, because the token `when` is valid both as an identifier and
as a part of the grammar for a case clause. Therefore, at the time
`foo` is encountered, the parser must decide whether it is looking at
a variable pattern (`foo when`, where `when` is the name of the
variable) or an identifier pattern (`foo`, where `when` begins the
case's guard clause). Previous to this fix, the algorithm for
disambiguating these two choices was as follows:

- If the token sequence starting at `foo` looked like a type, and the
  token that follows was an identifier, the parser assumed it was
  looking at a variable pattern with a type; otherwise it assumed it
  was looking at an identifier pattern.

- EXCEPT that if the token that followed the supposed type was `when`
  or `as` (both of which are valid identifiers), then it probed
  further:

- If the token that followed `when` or `as` was a token that could
  legitimately follow a pattern, then it assumed that it was looking
  at a variable pattern with a type. (The tokens that could
  legitimately follow a pattern are `,`, `:`, `||`, `&&`, `)`, `}`,
  `]`, `as`, `when`, `?`, `!`).

- Otherwise it assumed that it was looking at an identifier pattern.

This didn't fully disambiguate, because the third bullet didn't
account for the fact that the tokens `as`, `when`, and `!` could
_either_ legitimately follow a pattern _or_ legitimately begin an
expression (or, in the case of `when`, a type), therefore constructs
like the following were incorrectly parsed:

- `case foo when as:` (where `as` is a local boolean variable)
- `case foo when when:` (where `when` is a local boolean variable)
- `case foo when !flag:` (where `flag` is a local boolean variable)
- `case foo as when:` (where `when` is the name of a type)

The solution is to simplify the disambiguation logic so that if if the
token that follows the supposed type is `when` or `as`, then the
parser assumes that it's looking at an identifier pattern, _not_ a
typed variable pattern.

The consequence of this is that the above four constructions are
parsed correctly; however it is no longer possible for a typed
variable pattern to name a variable `when` or `as`.

For consistency we would like to prohibit _any_ variable pattern from
naming a variable `when` or `as`, however to keep this change as small
as possible (and reduce the risk involved in a possible cherry-pick)
that will be postponed until a later CL.

Fixes #52199.

Bug: https://github.com/dart-lang/sdk/issues/52199
Change-Id: Ibab9b92f01e3e4020d7d64f1ff000a9b964a4564
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/299400
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2023-04-28 13:22:28 +00:00
Konstantin Shcheglov 7f503e3688 Issue 52202. Fix crash when object pattern type is potentially nullable.
Bug: https://github.com/dart-lang/sdk/issues/52202
Change-Id: I54068a58f68f46c3d75c1586e6ed575b361a22fc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/299380
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
2023-04-28 03:20:39 +00:00
Paul Berry 96a346fc5e Front end: move anonymous mixin sealed/final inference to checkSupertypes phase.
Previously this step happened during `buildOutlineNodes`, but since
`buildOutlineNodes` happens in source order, this means that anonymous
mixins would only get their sealed and final attributes properly
inferred if they appeared *after* their immediate supertypes in source
order.  By moving this step to `checkSupertypes`, we ensure that the
computation is correct regardless of source order, because
`checkSupertypes` happens in class hierarchy order.

Fixes #52048.

Bug: https://github.com/dart-lang/sdk/issues/52048
Change-Id: Ib9f1f3dafded88681a26f09e4d21dfd44e70dfd3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/297901
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-04-25 12:26:38 +00:00
Paul Berry dc639c13cb Patterns flow analysis: recognize [...] (and related patterns) as trivially exhaustive.
If a list pattern consists of a single rest pattern, and that rest
pattern is guaranteed to match, then the whole list pattern is
guaranteed to match as well (provided that the matched value type is a
subtype of the list pattern's required type).

Bug: https://github.com/dart-lang/language/issues/2980
Change-Id: I316cc93d4e696f094716be92e1fbc1cd3a43a73c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/294622
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-04-11 19:54:20 +00:00
Paul Berry 2ec3b513db Patterns flow analysis: recognize trivially exhaustive switches.
This fixes a minor bug in flow analysis which was preventing it from
recognizing when a switch statement was trivially exhaustive, meaning
one of its reachable cases was guaranteed to always match.

This mostly addresses
https://github.com/dart-lang/language/issues/2980, but flow analysis
still fails to recognize that:

- A list pattern containing a just a single rest pattern always
  matches (unless the rest pattern has a subpattern that may fail to
  match).

- A null check pattern always matches if its subpattern always matches
  and the matched value type is non-nullable.

- The relational pattern `!= null` always matches if its subpattern
  always matches and the matched value type is non-nullable.

Fortunately, these drawbacks are small and don't lead to unsoundness.
I'll try to address them in follow up CLs.

Bug: https://github.com/dart-lang/language/issues/2980
Change-Id: Ie9f8564cde66a5a2c41114033ca3ff0e1a0f139a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293860
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-04-05 23:32:28 +00:00
Leaf Petersen 1694bb85c8 Update some test expectations with fixed CFE errors
Change-Id: I69f09a4f854b6c84dfaff82abbf0214565bf6aa6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293881
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
2023-04-05 22:55:22 +00:00
Robert Nystrom ed1633ac6c Add some more tests around pattern variable scoping.
Change-Id: I4cf3a34e73e754c9f783585612fa6c7f277ac5dd
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293523
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2023-04-05 16:29:45 +00:00
Paul Berry a3f5cfdc7f Patterns parsing: recover when switch statement syntax used for switch expressions.
This change ensure that if the user makes an erroneous switch
expression by imitating the syntax of case statements (i.e. using
`case`, `default`, `:` instead of `=>`, or `;` instead of `,`), error
recovery will understand the user's intent, avoiding follow-on errors.

Fixes #51886.

Bug: https://github.com/dart-lang/sdk/issues/51886, https://github.com/dart-lang/sdk/issues/51943
Change-Id: Icd405d4fd4ddfb1aadcf0867e5a51ba898d4cdbc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293200
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-04-04 18:52:59 +00:00
Chloe Stefantsova c747272f1f [cfe] Report mismatching joint variables on the first use
Closes https://github.com/dart-lang/sdk/issues/51929

Part of https://github.com/dart-lang/sdk/issues/49749

Change-Id: Ib20bdb5eb9b0b5c1e8b34cfbac6498e642350875
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292782
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2023-04-04 11:56:48 +00:00
Johnni Winther 5518abf1c7 [_fe_analyzer_shared] Update non-exhaustive message
This updates the message report for non-exhaustive switch statements
and expressions to include the witness in the problem message and
a reduced witness, which doesn't include properties that fully cover the
static type. The message is also split into two messages; one for
switch statements and one for switch expressions, allowing for a
better wording regarding the default/wildcard pattern case.

Change-Id: I17db657ef12ade5d47fa96bf69b8807e33ed5b8c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/293040
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-04-04 09:44:59 +00:00
Konstantin Shcheglov e2520f88ef Fixes for a couple pattern tests.
Change-Id: I117890c1117dacac9be11bd1775e8bc42a935c52
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292500
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-04-03 17:35:10 +00:00
Paul Berry 406bdf33e8 Fix precedence of expression inside relational patterns.
According to the spec, the expression inside a relational pattern is
`bitwiseOrExpression`.  We were only allowing `shiftExpression`.

Bug: https://github.com/dart-lang/sdk/issues/50035
Change-Id: Ie1a5746f1060b84e6e1b856a622e89db698b4684
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292285
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2023-04-01 16:30:49 +00:00
Robert Nystrom cd519446b6 Fix the exhaustiveness language tests.
The implementations are doing the right thing, but the tests are
slightly off:

- The CFE doesn't report unreachable case warnings because they are
  non-fatal warnings, so remove those expectations. (But analyzer does,
  so continue to check that analyzer reports them.)

- The CFE reports shared case variable errors at a different location.

Change-Id: I7143a7705d3c8879fb91e5fdb8df8599bfb96165
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292520
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
2023-04-01 01:32:56 +00:00
Robert Nystrom 39e128529c Add a couple of exhaustiveness tests.
Just corner cases that occurred to me while specifying the
implementation. There's probably already front end tests for it too, but
I like the idea of having some language coverage as well.

Change-Id: I158f274483799c3ccb29c32f0e7c5c42d31f327f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291962
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-04-01 01:24:36 +00:00
Robert Nystrom 0196a5b296 Allow static error tests to detect unreachable case hints from analyzer.
Eventually, these hints should probably be moved over to warnings. But
for now, this makes it possible to write static error language tests
that validate that analyzer produces unreachable case warnings/hints
where expected.

Also updated the patterns/ and switch/ tests now that those errors must
be expected by the test.

Change-Id: If1fb92602c4bde2819b9eec73598033009054947
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291967
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
2023-03-31 20:50:52 +00:00
Paul Berry d2184a9d03 Patterns parsing: disallow unaryPattern or relationalPattern in unaryPattern.
The precedence-based pattern parser can understand a unary pattern
inside another unary pattern (e.g. `_ as int as num`) or a relational
pattern inside a unary pattern (e.g. `> 1?`), but the specification
prohibits these constructions because they're difficult to read and
not very useful.

This change updates the implementation to match the spec, by producing
the appropriate error.  The offset and length of the error cover the
inner pattern, so it should be easy to construct an analysis server
quick fix that inserts the necessary parentheses.

Bug: https://github.com/dart-lang/sdk/issues/50035
Change-Id: I33e74d6d1f863e7162851d26fefbacd4fd17277c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292120
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-03-31 16:43:01 +00:00
Paul Berry 452dcdf517 Fix parsing of PATTERN as T? when
The logic for parsing types has special disambiguation rules for
deciding whether a trailing `?` should be included in the type, based
on what token(s) follow the `?`.  In the case where the token that
follows the `?` is `when`, we need to look further ahead to
disambiguate, to distinguish `PATTERN as T? when guard` from something
like `EXPRESSION is T ? when : otherwise`.

(Note: an alternative implementation would be to disambiguate based on
whether we're parsing a pattern or an expression.  But in the future I
want to move toward an architecture where expression parsing and
pattern parsing are combined, so that if the parser makes the wrong
decision about whether it's looking at a pattern or an expression,
error recovery will do a better job.  So I'm disambiguating based
solely on what follows the `?`.)

Bug: https://github.com/dart-lang/sdk/issues/50035
Change-Id: Idbc780b7b54fecc7fd01cae868c34771564dd804
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292282
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2023-03-31 13:12:48 +00:00
Chloe Stefantsova 9598da006c Remove duplicated keys from a language test
Change-Id: Ic88e9ee5204167184bb69a605c3b38d951867629
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292321
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2023-03-31 12:24:13 +00:00
Robert Nystrom 15e5ec93f3 Update test for 2.29/2.30 changes to map patterns.
It is still an error to have duplicate keys.

Change-Id: Id86fa7b50c3620540f4bef0399f5f70316848662
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292125
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Jake Macdonald <jakemac@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
2023-03-31 01:30:54 +00:00
Paul Berry 134007af32 Patterns parsing: make it an error to use var before a type in a variable pattern.
Bug: https://github.com/dart-lang/sdk/issues/50035
Change-Id: I46b23ef0d1856401eac4b0a05c6bc6008711d35a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291780
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
2023-03-30 18:25:03 +00:00
Konstantin Shcheglov 1ec1e7358a Changes for map pattern: report an error for rest elements, empty map pattern.
Bug: https://github.com/dart-lang/language/issues/2861
Change-Id: I00ccb3ea03aa476f96c2ecf3e3a9e13bd4926193
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291940
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Marya Belanger <mbelanger@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
2023-03-30 17:54:06 +00:00
Paul Berry 104ac30cf4 Parser: clean up handling of variable patterns.
The listener API for variable patterns is split into three separate
functions, to handle the three separate behaviors:

- `handleAssignedVariablePattern` for variable names appearing in an
  assignment context (these assign to an existing variable upon a
  successful match).

- `handleDeclaredVariablePattern` for variable declarations appearing
  in a declaration or matching context (these cause a new variable
  name to come into scope).

- `handleWildcardPattern` for wildcards in any context (these don't
  capture the matched value).

Also, responsibility is shifted to the parser for reporting the
following error conditions:

- VariablePatternKeywordInDeclarationContext (e.g.
  `var (var x) = ...;`)

- PatternAssignmentDeclaresVariable (e.g. `[x, var y] = ...;`)

Previously these errors were detected by the implementations, and
weren't fully covering all possible error scenarios.

In the case of VariablePatternKeywordInDeclarationContext, the
listener method `handleDeclaredVariablePattern` is called instead of
`handleAssignedVariablePattern`.  This ensures that no tokens are
dropped from the analyzer AST.  The CFE uses the `inAssignmentPattern`
argument of `handleDeclaredVariablePattern` to distinguish this error
recovery case from a legitimate declared variable pattern.

Fixes #51868.

Bug: https://github.com/dart-lang/sdk/issues/51868
Change-Id: I28ec679b73d64033166721c6460be35f15e23171
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291583
Reviewed-by: Jens Johansen <jensj@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-03-30 14:12:03 +00:00
Chloe Stefantsova 81df57636f [cfe] Coerce operands of relational patterns
Closes https://github.com/dart-lang/sdk/issues/51871

Part of https://github.com/dart-lang/sdk/issues/49749

Change-Id: I13a28c4849002f189ce6c14df2efaacac2ea4bf8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/292001
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-03-30 14:11:09 +00:00
Leaf Petersen a7f2562b38 Fix syntax errors and add missing cases in guard_error_test.
Change-Id: I2cc53a7f41721fd146051502ee2f9e102656ed61
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291968
Auto-Submit: Leaf Petersen <leafp@google.com>
Commit-Queue: Erik Ernst <eernst@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2023-03-30 10:29:22 +00:00
Robert Nystrom 5138b803c9 Add tests for the proposal version 2.29 changes to patterns.
Change-Id: I4b3325213905bc8a80b6fbacbfcf97694e98a60c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/291960
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
2023-03-29 23:17:20 +00:00
Paul Berry df7c4bb439 Allow empty switch expressions.
This an experimental change; we haven't yet decided whether we want to
allow this.  See https://github.com/dart-lang/language/issues/2939 for
discussion.

Bug: https://github.com/dart-lang/language/issues/2939
Change-Id: If69ab66d18ae7ec3dfc79496ce13517ef1c15227
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290907
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-03-24 20:22:52 +00:00
Paul Berry c175d48343 Front end: fix type inference for object patterns that refer to typedefs.
I've also added some runtime checks to
`tests/language/patterns/object_pattern_inference_test.dart` to verify
that the object patterns match when they should match and don't match
when they shouldn't; this helps verify that not only has the front end
inferred the correct type for the object patterns, but it has also
stored the correct type in the kernel representation.

Fixes #51795.

Bug: https://github.com/dart-lang/sdk/issues/51795
Change-Id: I73ce43e440db50d9942deb4a1eb4ee68a5c23142
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290900
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-03-24 12:20:08 +00:00
Robert Nystrom c89ec96b96 Fix int-to-double test to work on the web.
Change-Id: I5e208edc53543de8d11aa6e81072ad7f61a23d52
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290913
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
2023-03-23 22:47:49 +00:00
Paul Berry c358118e9f When type inference fails for object patterns, fall back on instantiate-to-bounds
(As discussed in https://github.com/dart-lang/language/issues/2770#issuecomment-1479624850)

Fortunately this was easy to do because we already have the necessary
logic to fall back on instantiate-to-bounds when type inference fails;
it's a standard part of the "upwards inference" algorithm.

Bug: https://github.com/dart-lang/language/issues/2770, https://github.com/dart-lang/sdk/issues/51795
Change-Id: I91847fc01d1420e6bfb584e8536ebca803133bd1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290613
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-03-23 13:13:20 +00:00
Paul Berry 6b4878b55f Front end: initial implementation of type inference for object patterns.
This code handles simple cases.  Still TBD:
- Object patterns that refer to typedefs
- Object patterns that refer to classes with f-bounded polymorphism
- Inference of `dynamic` when there is no bound

Bug: https://github.com/dart-lang/sdk/issues/51795
Change-Id: I00acae6ba111f7b170650cfeffbfd2aaf7f71e42
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290347
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2023-03-22 17:19:23 +00:00
Johnni Winther fd63f7ac64 [test] Add test for switch on values with primitive equality
Change-Id: I8de3234f1224e7b0e68b4c1ed190eaa49cbae68f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290500
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-03-22 13:54:52 +00:00
Johnni Winther 94c165d189 [cfe] Implement primitive equality
Closes #51045
Closes #51565
Closes #51566
Closes #51688
Closes #51800

Change-Id: I4a679ef9cf496a22f4fdc2047a2dc4753e796e2b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290320
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-03-22 12:25:41 +00:00
Robert Nystrom 10f53cb4c6 Remove incorrect part of int-to-double test.
Switch statements on ints aren't required to be exhaustive.

Change-Id: I85701b098cacf9d01241dab821ae4c70f04a0710
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289450
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2023-03-18 01:54:56 +00:00
Robert Nystrom 6de72778ff Add tests for where coercions do and don't happen in patterns.
These tests reflect the intended behavior after
https://github.com/dart-lang/language/pull/2926 lands.

Change-Id: I7ce1991dfc9b247b0c6ce6ee2714b5f207a4040d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/289404
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Bob Nystrom <rnystrom@google.com>
2023-03-17 23:04:25 +00:00
Johnni Winther 7cc2553fa1 [_fe_shared_analyzer] Use real exhaustiveness checking algorithm
This changes the analyzer and CFE to use the real exhaustiveness
checking algorithm. The fallback algorithm is kept and will be
removed once we know that the real exhaustiveness algorithm sticks.

Change-Id: Ic9df92c1ca9f7dec4cbdfa138dc6ed39ef2d4df5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288703
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2023-03-17 16:45:35 +00:00
Johnni Winther 5e537e1cd2 [cfe] Integrate exhaustiveness checking into constant evaluator
This removes the use of [SwitchInfo], [SwitchCaseInfo] and
[ExhaustivessInfo] to pass on information for exhaustiveness checking
and instead uses the AST nodes directly in the constant evaluator.

Change-Id: Ie8d72b03e38334d1be9857794c38ed142dba5783
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288402
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-03-13 21:23:21 +00:00
Johnni Winther 5ab237da86 [cfe] Move lowering to constant evaluator
This moves the lowering of patterns in switch statement/expression,
pattern assignment, pattern variable declaration and if-case statement
to the constant evaluator.

Change-Id: Ic5810e96b26a74987c50fd71b306e41b59504e1f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288401
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
2023-03-13 15:18:42 +00:00
Konstantin Shcheglov ad15774558 Issue 51567. Implement constant equality as primitive equality
Bug: https://github.com/dart-lang/sdk/issues/51567
Change-Id: I7821598a761573519205b0ea06b13f433639282b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/286241
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2023-03-01 20:58:57 +00:00
Paul Berry 7e5297e5f8 Enable the fallback exhaustiveness algorithm (with improvements).
This change improves the fallback exhaustiveness algorithm so that it
also takes advantage of the old, pre-patterns exhaustiveness logic for
enums.  This ensures that the fallback exhaustiveness algorithm won't
seem like a regression to users who are used to the old pre-patterns
behaviour.  This required extending the old algorithm to handle
new-style switch statements (which have a different representation
from classic switch statements in the CFE) as well as switch
expressions.

We also turn on the fallback exhaustiveness algorithm by default.
Unit tests can still disable it by temporarily setting the global
variable `useFallbackExhaustivenessAlgorithm` to `false`.

The hope is that this algorithm will be short-lived; we are just using
it so that if we decide to enable pattern support in the near future,
users will experience something that is sound.  That is, it may
require `default` or `_` cases more often than is strictly necessary,
but it shouldn't ever allow a non-exhaustive switch in a place where
an exhaustive switch is required.

Change-Id: I4a8b7f996b109c4ee8832f286c3b3bf3b216fe8b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/284840
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2023-02-27 17:36:02 +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
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