Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[analyzer] An explicitly instantiated instance method tearoff is parsed as starting with a prefix #46721

Closed
eernstg opened this issue Jul 26, 2021 · 2 comments
Assignees
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on

Comments

@eernstg
Copy link
Member

eernstg commented Jul 26, 2021

Consider the following program:

class A {
  List<X> m<X>(X x) => [x];
}

extension FunctionApplier on Function {
  void applyAndPrint(List<Object?> positionalArguments) =>
      print(Function.apply(this, positionalArguments, const {}));
}

void main() {
  var a = A();
  a.m<int>.applyAndPrint([2]);
  a.m<String>.applyAndPrint(['three']);
}

This program is rejected (as of ef8add0) by dartanalyzer with the following error messages:

ERROR|COMPILE_TIME_ERROR|PREFIX_SHADOWED_BY_LOCAL_DECLARATION|/usr/local/google/home/eernst/lang/dart/scratch/202107/n038.dart|12|3|1|The prefix 'a' can't be used here because it is shadowed by a local declaration.
ERROR|COMPILE_TIME_ERROR|PREFIX_SHADOWED_BY_LOCAL_DECLARATION|/usr/local/google/home/eernst/lang/dart/scratch/202107/n038.dart|13|3|1|The prefix 'a' can't be used here because it is shadowed by a local declaration.

However, a is not an import prefix, it is a local variable. Presumably, the analyzer parser (or some other early step of the analysis) assumes that with constructs of the form <identifier> '.' <identifier> <typeArguments> '.', the first identifier must be an import prefix. That may have been true before constructor-tearoffs, but it isn't true with that feature, so the analyzer will need to investigate the situation more closely before it concludes that it is looking at an import prefix.

@eernstg eernstg added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-spec Issues with the analyzer's implementation of the language spec labels Jul 26, 2021
@devoncarew
Copy link
Member

cc @srawlins

@srawlins
Copy link
Member

Must handle new rewriting spelled out in @stereotype441 's Constructor Tearoff parsing strategy doc.

Analyzer's resolver gets the following from the parser:

  • ExpressionStatementImpl: (a.m<int>.applyAndPrint([2]);)
    • ._expression: InstanceCreationExpressionImpl (a.m<int>.applyAndPrint([2]))
      • ._constructorName: ConstructorNameImpl (a.m<int>.applyAndPrint)
        • ._type: TypeNameImpl (a.m<int>)
          • ._name: PrefixedIdentifierImpl (a.m)
            • ._prefix: SimpleIdentifierImpl (a)
            • ._identifier: SimpleIdentifierImpl (m)
          • ._typeArguments: TypeArgumentListImpl (<int>)
        • ._name: SimpleIdentifierImpl (applyAndPrint)

This must be re-written into something like:

  • ExpressionStatementImpl: (a.m<int>.applyAndPrint([2]);)
    • ._expression: MethodInvocationImpl (a.m<int>.applyAndPrint([2]))
      • ._target: FunctionReferenceImpl (a.m<int>)
        • ._function: PrefixedIdentifierImpl (a.m)
          • ._prefix: SimpleIdentifierImpl (a)
          • ._identifier: SimpleIdentifierImpl (m)
        • ._typeArguments: TypeArgumentListImpl (<int>)
      • ._methodName: SimpleIdentifierImpl (applyAndPrint)

@scheglov scheglov added the P2 A bug or feature request we're likely to work on label Jul 29, 2021
@srawlins srawlins self-assigned this Jul 30, 2021
dart-bot pushed a commit that referenced this issue Aug 1, 2021
This resolver may need to rewrite the AST if the TypeName resolves to be
a function reference or a constructor reference with type-instantiation.

Bug: #46020 #46721
Change-Id: Ie6a9aa3c04d739becc0c902c117a7151f9c1fcf1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208540
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-spec Issues with the analyzer's implementation of the language spec area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

4 participants