Commit Graph

7 Commits

Author SHA1 Message Date
Paul Berry 69a1a497f1 [messages] Stop using withArgumentsOld, batch 32.
This is one of several planned batches of changes to transition the
front end (and related packages) away from the old `withArgumentsOld`
diagnostic reporting method, and to the new `withArguments`
method. The difference between the two is that `withArguments` has
named parameters rather than positional ones, so (a) it's less likely
for parameters to be mixed up, and (b) it's compatible with the
calling conventions used by analyzer diagnostics.

As part of this transition, I'm taking the opportunity to rename the
parameters themselves (since the names previously were restricted to a
very small set of possibilities based on type), and to document them
in `messages.yaml`. To avoid fatigue (both for myself and code
reviewers), I've chosen to break the change into many smaller batches.

The only changes in this CL that were manually written are those in
`messages.yaml`. The others were produced by running the standard
diagnostic message code generator, running the script
`use_new_with_arguments.dart`, and then updating front end parser
expectations.

Change-Id: I6a6a6964699f00e7e91af2e682ce1baa1846ca13
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/478060
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
2026-02-04 10:05:41 -08:00
Paul Berry ff5962306a [front end] Make code... objects usable as error templates.
This is part of a series of CLs that will standardize CFE error
reporting to always use `codeFoo.withArguments(...)` when reporting
errors that take arguments and `codeFoo` when reporting errors that
don't take arguments, rather than `templateFoo.withArguments(...)`
when reporting errors that take arguments and `messageFoo` when
reporting errors that don't take arguments. This change will have two
advantages:

- It will lend greater consistency to the CFE codebase, by allowing
  the same `code...` objects to be used both to name error codes
  (e.g., in test expectations) and to report errors. This will allow
  everything associated with a certain error code to be found using a
  single invocation of "Find References" in the editor, rather than
  having to search separately for uses of the code and the message or
  template.

- It should hopefully make the experience of writing code that reports
  errors more pleasant, since it will no longer be necessary to look
  up an error to see whether it takes arguments before using it;
  instead, the developer will be able to type the name of the message
  `code...` declaration, and then use autocompletion to see whether
  `.withArguments(...)` is required.

In this CL, the code generator for error messages is changed so that
both `template...` and `code...` declarations have the static type
`Template` (and, indeed, refer to the same constant
object). Previously, `code...` declarations had the static type
`Code`, and referred to a constant object with runtime type
`Code`. This will allow uses of `template...` to be replaced with
`code...`. In follow-up CLs, the uses of `template...` will be changed
to `code...`, and the `template...` declarations will be removed.

Since the `toString` methods on `Template` and `Code` differ, I had to
make some changes to ID tests and parser tests, both of which depend
on the behavior of `toString`:

- For ID tests, I was able to preserve the existing test expectations
  by modifying the `errorsToText` function (in
  `pkg/front_end/lib/src/testing/id_testing_utils.dart`).

- For parser tests, I wasn't able to find an easy way to preserve the
  existing test expectations, so I updated the expectations to match
  the new behavior.

Change-Id: I8a461d451b06dd2f88b3e59f0cb2153711b09461
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/442731
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2025-08-06 06:37:31 -07:00
Jens Johansen e30f60791a [parser] Don't use the next token as the end on handleSend
Change-Id: Ic4bacffab3fdd9d23bea9b1ecd0f2cfdecb82e2e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/399060
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2024-12-05 15:10:34 +00:00
Jens Johansen 40dad48ede [parser] Add more tokens
Add
 * endToken to endBinaryExpression
 * endToken to handleEndingBinaryExpression
 * endToken to endConditionalExpression
 * beginToken (and rename to endToken) on handleExpressionStatement
 * rightSeparator to handleForLoopParts

Change-Id: I55d5a0e6c795b82d8f3480365e4cc3fbb59c65b5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372482
Commit-Queue: Jens Johansen <jensj@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2024-06-24 08:12:41 +00:00
Jens Johansen fc67d31862 [parser] Listener calls should not be on the next (unrelated) token
When the parser sends events to the listener some events has a pair of
parameters, `beginToken` and `endToken`. Most of these are constructed
in such a way that the `beginToken` is the first token and the
`endToken` is the last token in that construct. This is for instance the
case with `endClassDeclaration`.
It was, however, not the case for `endMetadata` where `endToken` instead
was the next token *not* in the metadata.

In this CL I've found a changed the following to point to the last token
in the construct instead of the next token not in the construct and
renamed the parameter where it made sense:

* `endAssert` --- and renamed `semicolonToken` (which only pointed to
  a semicolon for statements) to `endToken`.
* `endAwaitExpression`
* `endConstLiteral` --- and renamed `token` to `endToken`
* `endConstructorReference`
* `endFieldInitializer` --- and renamed `token` to `endToken`
* `endForIn`
* `endForInBody` --- and renamed `token` to `endToken`
* `endForStatement`
* `endForStatementBody` --- and renamed `token` to `endToken`
* `endFunctionExpression` --- and renamed `token` to `endToken`
* `endInitializer` --- and renamed `token` to `endToken`
* `endInitializers`
* `endInvalidAwaitExpression`
* `endMetadata`
* `endSwitchCase`
* `endTopLevelDeclaration` --- and renamed `nextToken` to `endToken`
* `endWhileStatement`
* `endWhileStatementBody` --- and renamed `token` to `endToken`
* `handleNoConstructorReferenceContinuationAfterTypeArguments`

In the few places in listeners where these values were used I've mostly
updated to do e.g. `endToken.text!` to retain the current behavior.

Change-Id: I25495e160d1eec5c75bcf1313b512cd04bcb1533
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/364322
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2024-04-26 06:59:23 +00:00
Chloe Stefantsova 6b0acfecc1 [cfe] Put patterns in the scopes of their declared variables
Closes https://github.com/dart-lang/sdk/issues/51971

Change-Id: I8ba634e7303bafdb1895c50970fbc80321f4bf6a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/296581
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Chloe Stefantsova <cstefantsova@google.com>
2023-04-24 07:30:00 +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