Skip to content

ElementLocationImpl.fromElement assumes LocalFunctionElementImpl.wrappedElement._enclosingElement3 is always ExecutableElementImpl; it is not #59753

Open
@stereotype441

Description

@stereotype441

During my development of https://dart-review.googlesource.com/c/sdk/+/400660, I ran into a bug where ElementLocationImpl.fromElement is not always correct in assuming that LocalFunctionElementImpl.wrappedElement._enclosingElement3 is always of type ExecutableElementImpl:

  ElementLocationImpl.fromElement(Element2 element) {
    List<String> components = <String>[];
    Element2? ancestor = element;
    while (ancestor != null) {
      if (ancestor is! ElementImpl2) {
        ...
      } else {
        components.insert(0, ancestor.identifier);
        if (ancestor is LocalFunctionElementImpl) {
          ancestor = (ancestor.wrappedElement._enclosingElement3
                  as ExecutableElementImpl)
              .element;
        } else {
          ancestor = ancestor.enclosingElement2;
        }
      }
    }
    _components = components.toFixedList();
  }

To repro this failure, check out patchset 4 of https://dart-review.googlesource.com/c/sdk/+/400660 and change line 4435 of pkg/_fe_analyzer_shared/lib/src/flow_analysis/flow_analysis.dart from this:

      new Set.identity();

to this:

      {};

Then run the test suite pkg/analyzer/test/generated/invalid_code_test.dart.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onanalyzer-apiIssues that impact the public API of the analyzer packagearea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions