sdk
sdk copied to clipboard
Dart Macros Cannot Handle Sealed Classes Correctly (Dart 3)
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.
sorry data_class package
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.
Some Extra Issues I Faced with Macro Unnecessary import prefixes in Dart macros while using covariant