Skip to content

CFE allows trailing comma and multiple arguments in extension type representation declaration #53625

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

Closed
lrhn opened this issue Sep 27, 2023 · 5 comments
Assignees
Labels
cfe-feature-extension-types Implement extension types feature in the CFE feature-extension-types Implementation of the extension type feature front-end-fasta legacy-area-front-end Legacy: Use area-dart-model instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@lrhn
Copy link
Member

lrhn commented Sep 27, 2023

The current extension type specification proposal does not allow a trailing comma in the representation type declaration.
Example:

extension type E(int x,) {} // Not allowed

The CFE currently allows this, and it should not.

Further, if I add a second argument, extension type E(int x, int y) {}, the CFE accepts that too, and also an invocation of it:

extension type E(int x, String y) {}
void main() {
 var e = E(1, "b");  // Complains if giving too few arguments.
 print(e.x); // b 
 // print(e.y); // y
 print(e.runtimeType); // String
 int x = e.x; // succeeds.
 print(x.runtimeType); // String, **unsounds**
}

Or make the paramters optional or named:

extension type E({int x = 0}) {}
void main() {
  print(E(x: 42).x); // 42
}

Or I can omit the type entirely: extension type E(x) {} void main() { print(E(42).x); } prints "42".

All this is invalid syntax. While it's nice that the grammar parser allows it, so that we can give better error messages, the syntax should be rejected by a later validation step, preferably still in the parser.

(The analyzer has this validation step. The error given for the trailing comma isn't great,

  error - trailcom.dart:1:23 - Each extension type should have                             
          exactly one representation field. Try combining fields                           
          into a record, or removing extra fields. -                                       
          multiple_representation_fields                        

but for two arguments, or named arguments, an error of

  error - trailcom2.dart:1:18 - Expected a representation field.
          Try providing the representation field for this
          extension type. - expected_representation_field

is reasonable. It has some further down-stream errors from not having a valid representation type declaration, which could be quenched.

The analyzer AST model also has a trailingComma token in the RepresentationDeclaration which should always be null.)

If we ever introduce primary constructors in general, with a syntax compatible with extension type declarations, it's likely that the syntax for extension types will be opened up to allow other singleton parameter lists, and trailing commas (and at that point the analyzer AST could give RepresentationDeclaration an asPrimaryConstructorParameterList method).

So far then the syntax is restricted to '(' <metadata> <type> <identifier> ')'. No more and no less.

@lrhn lrhn added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) front-end-fasta legacy-area-front-end Legacy: Use area-dart-model instead. labels Sep 27, 2023
@johnniwinther johnniwinther added the feature-extension-types Implementation of the extension type feature label Sep 27, 2023
@eernstg
Copy link
Member

eernstg commented Sep 27, 2023

Do you wish to enforce that there cannot be <metadata> on the parameter? I think there was a request for allowing the metadata already now (in spite of the fact that the <representationDeclaration> is currently as minimal as we could make it).

@lrhn
Copy link
Member Author

lrhn commented Sep 27, 2023

No, I just forgot about metadata. I never use it :)
Added to the original message now.

@scheglov
Copy link
Contributor

AFAIK we don't have trailingComma in RepresentationDeclaration in the analyzer.
I will add a separate error for trailing comma.
https://dart-review.googlesource.com/c/sdk/+/328340

@lrhn
Copy link
Member Author

lrhn commented Sep 27, 2023

There is indeed no trailing comma in the current analyzer AST. I mistook the general commaAfter for being that.

copybara-service bot pushed a commit that referenced this issue Sep 28, 2023
Bug: #53625
Change-Id: I89d34b35b3e8b5977d1f99d550f91ce58e1c2150
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/328340
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
copybara-service bot pushed a commit that referenced this issue Oct 9, 2023
Bug: #53625
Change-Id: I17994508f2e50c63d656d89e102c8a0d06e45d96
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/329821
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
@johnniwinther johnniwinther added the cfe-feature-extension-types Implement extension types feature in the CFE label Oct 13, 2023
@chloestefantsova chloestefantsova self-assigned this Oct 19, 2023
copybara-service bot pushed a commit that referenced this issue Oct 24, 2023
Part of #53625
Part of #49731

Change-Id: I793ef6329d99b1a4e829491f454f42c2ede941b4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/331185
Commit-Queue: Chloe Stefantsova <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
@chloestefantsova
Copy link
Contributor

The issue is addressed by the following: ffd43b2, 0e2ed5a, 75920dd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cfe-feature-extension-types Implement extension types feature in the CFE feature-extension-types Implementation of the extension type feature front-end-fasta legacy-area-front-end Legacy: Use area-dart-model instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants