Skip to content

Constructor like syntax is parsed as method if the name is wrong #31001

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

Open
scheglov opened this issue Oct 4, 2017 · 4 comments
Open

Constructor like syntax is parsed as method if the name is wrong #31001

scheglov opened this issue Oct 4, 2017 · 4 comments
Labels
front-end-fasta legacy-area-front-end Legacy: Use area-dart-model instead. model-error-recovery Error recovery in analyzer/CFE. P3 A lower priority bug or feature request

Comments

@scheglov
Copy link
Contributor

scheglov commented Oct 4, 2017

class A {
  B() : super();
}
library;
import self as self;
import "dart:core" as core;

class A extends core::Object {
  default constructor •() → void
    : super core::Object::•()
    ;
  abstract method B() → dynamic;
}
static const field dynamic #errors = const <dynamic>["file:///test.dart:2:16: Error: Can't have initializers: B\n  B() : super();\n               ^"]/* from null */;

There is inconsistency between Analyzer parser and Fasta.

Analyzer reports an error and treats B() : super() as A() : super() because there is : after parameters.

Fasta says that this is a method, and it should not have constructor initializers.

I think Analyzer's behavior is a bit better because we lose less nodes when we recover this way.

@peter-ahe-google
Copy link
Contributor

I agree that the analyzer's way is better. Later today, I hope the two of us can walk through how to recover from errors with constructors and factories. What Fasta is currently doing doesn't work really well. See also #30981.

Furthermore, if it had been a factory, Fasta would create a factory called "B", as if the user had written factory A.B() {...}. I think it would be better to assume the user meant factory A() { ... }.

@peter-ahe-google
Copy link
Contributor

The factory problem is reported as #30999.

@bwilkerson
Copy link
Member

I believe that this CL fixes the AstBuilder: https://dart-review.googlesource.com/c/sdk/+/25640.

@jensjoha
Copy link
Contributor

This currently gives this error message:

t.dart:2:3: Error: The name of a constructor must match the name of the enclosing class.
  B() : super();
  ^

and compiles like this:

import self as self;
import "dart:core" as core;

class A extends core::Object {
  constructor B() → self::A
    : super core::Object::•()
    ;
}

(i.e. as a named constructor called B)

There's probably still an inconsistency so I'll leave it open, but I don't think the CFE recovery here is bad.

@jensjoha jensjoha added the P3 A lower priority bug or feature request label Jun 15, 2021
@johnniwinther johnniwinther added model-error-recovery Error recovery in analyzer/CFE. and removed front-end-fasta-recovery labels Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
front-end-fasta legacy-area-front-end Legacy: Use area-dart-model instead. model-error-recovery Error recovery in analyzer/CFE. P3 A lower priority bug or feature request
Projects
None yet
Development

No branches or pull requests

5 participants