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

Macros: Are macros compatible with class modifiers? #55749

Closed
felangel opened this issue May 16, 2024 · 3 comments
Closed

Macros: Are macros compatible with class modifiers? #55749

felangel opened this issue May 16, 2024 · 3 comments

Comments

@felangel
Copy link

felangel commented May 16, 2024

I'm wondering whether it's possible to use class modifiers (e.g. sealed, final, etc.) with macros.

Steps to reproduce:

  1. Clone https://github.com/felangel/struct
  2. Run the example dart --enable-experiment=macros run example/main.dart
  3. Note that it runs fine
  4. Add the sealed class modifier to example/main.dart
    import 'package:struct_annotation/struct_annotation.dart';
    
    @Struct()
    + sealed class Person {
    - class Person {
      final String name;
      final int age;
    }
    
    void main() {
      final jane = Person(name: 'Jane', age: 42);
      final john = jane.copyWith(name: 'John');
    
      print(jane); // Person(name: Jane, age: 42)
      print(john); // Person(name: John, age: 42)
    
      print(jane == jane.copyWith()); // true
      print(john == john.copyWith(age: 21)); // false
    }
  5. Run the example again
  6. Observe error:
    dart --enable-experiment=macros run example/main.dart     
    org-dartlang-augmentation:/struct/example/main.dart-0:3:18: Error: A 'sealed' class can't be marked 'abstract' because it's already implicitly abstract.
    Try removing the 'abstract' keyword.
    augment abstract sealed class Person {
                     ^^^^^^
    org-dartlang-augmentation:/struct/example/main.dart-1:7:18: Error: A 'sealed' class can't be marked 'abstract' because it's already implicitly abstract.
    Try removing the 'abstract' keyword.
    augment abstract sealed class Person {
                     ^^^^^^
    org-dartlang-augmentation:/struct/example/main.dart-1:10:82: Error: The class 'Person' is abstract and can't be instantiated.
      augment prefix1.Person copyWith({prefix0.String? name, prefix0.int? age, }) => Person(name: name ?? this.name,age: age ?? this.age,);
                                                                                     ^^^^^^
    example/main.dart:10:16: Error: The class 'Person' is abstract and can't be instantiated.
      final jane = Person(name: 'Jane', age: 42);
    
@felangel felangel changed the title Are macros compatible with class modifiers? Macros: Are macros compatible with class modifiers? May 16, 2024
@lrhn
Copy link
Member

lrhn commented May 16, 2024

That looks like two problems.

One is the abstract which shouldn't be there. That's probably a bug in the macros implementation.

The other is that the Struct macro tries to generate a copyWith on an abstract class. That's a bug in that macro, or at least something that can do with a better error message, since it's not possible.

@felangel
Copy link
Author

felangel commented May 16, 2024

That looks like two problems.

One is the abstract which shouldn't be there. That's probably a bug in the macros implementation.

The other is that the Struct macro tries to generate a copyWith on an abstract class. That's a bug in that macro, or at least something that can do with a better error message, since it's not possible.

The copyWith implementation generated by the Struct macro appears to work just fine but yeah I agree that the abstract likely shouldn't be there (that was mainly what I was confused about).

@felangel
Copy link
Author

This appears to go away when I import the the macro directly from src: Dart-Code/Dart-Code#5110 (comment).

Closing this since it seems to be fixed already in #55746. Thanks for the quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants