Skip to content

Poor recovery when using yield in a non-generator function #44633

Open
@bwilkerson

Description

@bwilkerson

Given the following code

Iterable<int> get digits {
  yield [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
}

The analyzer produces three diagnostics:

error: The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type. (body_might_complete_normally at ...)
error: Expected to find ']'. (expected_token at ...)
error: Undefined name 'yield'. (undefined_identifier at ...)

It would be nice if the analyzer could produce a single diagnostic stating that a yield statement is only valid in a generator function.

I believe that in order to do that the parser would need to recognize that the yield is followed by an expression and parse it as a yield statement rather than as an identifier followed by garbage.

It would also require the analyzer to treat the function as if it were a generator for the purpose of flow analysis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4analyzer-recoveryarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.model-error-recoveryError recovery in analyzer/CFE.type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions