sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Dart Macros Cannot Handle Sealed Classes Correctly (Dart 3)

Open manishgautammg3994 opened this issue 1 year ago • 3 comments
trafficstars

In Dart 3, the introduction of sealed classes is causing an issue with Dart macros. When attempting to use a macro with a sealed class, the generated code includes an unnecessary abstract keyword, leading to a compilation error.

// buildDeclarationsForClass
return builder.declareInType(DeclarationCode.fromParts(['  external ', boolean, ' operator==(', object, ' other);
// buildDefinitionForClass
final equalsMethod = (equality(methods,"==") == null)? null :await  builder.buildMethod(equality(methods,"==")!.identifier);
// Helper method
 MethodDeclaration? equality(List<MethodDeclaration> methods ,String op) {
    return
     methods.firstWhereOrNull(
      (m) => m.identifier.name == op,
    );}

Example

@Equatable() // consider we are using dataclass package 
sealed class CodePushState {
  const CodePushState();
}

Error

A 'sealed' class can't be marked 'abstract' because it's already implicitly abstract.
Try removing the 'abstract' keyword.
augment abstract sealed class CodePushState {

Steps to Reproduce:

Define a sealed class in Dart 3. Apply a Dart macro to the sealed class. Observe the generated code, which incorrectly adds the abstract keyword. Expected Behavior: The Dart macro should correctly handle the sealed class without adding the abstract keyword since sealed classes are implicitly abstract in Dart 3.

Actual Behavior: The Dart macro incorrectly adds the abstract keyword to the generated code, resulting in a compilation error.

Proposed Solution: Update the Dart macro generation logic to detect when a class is sealed and avoid adding the abstract keyword.

manishgautammg3994 avatar Aug 20 '24 14:08 manishgautammg3994

sorry data_class package

manishgautammg3994 avatar Aug 20 '24 15:08 manishgautammg3994

Summary: Dart macros incorrectly add the abstract keyword to generated code for sealed classes in Dart 3, leading to compilation errors. This issue arises because sealed classes are implicitly abstract, and the macro generation logic needs to be updated to avoid adding the unnecessary keyword.

dart-github-bot avatar Aug 20 '24 16:08 dart-github-bot