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

"Error: The macro can only be applied to enums or enum values." - What interface to use instead? #56305

Open
Lootwig opened this issue Jul 23, 2024 · 8 comments
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-feature-macros Implement macros features in the CFE feature-macros Implementation of the macros feature type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) type-question A question about expected behavior or functionality

Comments

@Lootwig
Copy link
Contributor

Lootwig commented Jul 23, 2024

The message from the title is occuring when trying to apply below macro to this declaration, which looks a lot like an enum to my naive eyes:

@X()
enum LetsTry {
  one;
}
import 'dart:async';

import 'package:macros/macros.dart';

macro class X
    implements
        EnumDefinitionMacro,
        EnumValueTypesMacro,
        EnumDeclarationsMacro,
        EnumValueDeclarationsMacro,
        EnumValueDefinitionMacro,
        EnumTypesMacro {
  const X();

  @override
  FutureOr<void> buildDeclarationsForEnum(EnumDeclaration enuum, EnumDeclarationBuilder builder) {
    builder.report(Diagnostic(DiagnosticMessage('I ran'), Severity.warning));
  }

  @override
  FutureOr<void> buildDeclarationsForEnumValue(EnumValueDeclaration entry, EnumDeclarationBuilder builder) {
    builder.report(Diagnostic(DiagnosticMessage('I ran'), Severity.warning));
  }

  @override
  FutureOr<void> buildDefinitionForEnum(EnumDeclaration enuum, EnumDefinitionBuilder builder) {
    builder.report(Diagnostic(DiagnosticMessage('I ran'), Severity.warning));
  }

  @override
  FutureOr<void> buildDefinitionForEnumValue(EnumValueDeclaration entry, EnumValueDefinitionBuilder builder) {
    builder.report(Diagnostic(DiagnosticMessage('I ran'), Severity.warning));
  }

  @override
  FutureOr<void> buildTypesForEnum(EnumDeclaration enuum, EnumTypeBuilder builder) {
    builder.report(Diagnostic(DiagnosticMessage('I ran'), Severity.warning));
  }

  @override
  FutureOr<void> buildTypesForEnumValue(EnumValueDeclaration entry, TypeBuilder builder) {
    builder.report(Diagnostic(DiagnosticMessage('I ran'), Severity.warning));
  }
}
@dart-github-bot
Copy link
Collaborator

Summary: The user is attempting to apply a macro to a class that resembles an enum, but the macro only supports enums. They are seeking an alternative interface to use for their macro.

@dart-github-bot dart-github-bot added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-question A question about expected behavior or functionality labels Jul 23, 2024
@lrhn lrhn added feature-macros Implementation of the macros feature and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Jul 23, 2024
@mit-mit mit-mit added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Aug 1, 2024
@mit-mit
Copy link
Member

mit-mit commented Aug 1, 2024

cc @jakemac53

@jakemac53
Copy link
Contributor

That just looks like a bug

@jakemac53 jakemac53 added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) area-front-end Use area-front-end for front end / CFE / kernel format related issues. labels Aug 1, 2024
@jakemac53
Copy link
Contributor

Looks like this is an error produced by the CFE so that is probably where the issue is. There is test coverage for the shared code for this use case so it should generally work.

@jakemac53 jakemac53 removed the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Aug 1, 2024
@Lootwig
Copy link
Contributor Author

Lootwig commented Aug 1, 2024

@jakemac53 anything I can do to help?

@johnniwinther johnniwinther added the cfe-feature-macros Implement macros features in the CFE label Aug 2, 2024
@jakemac53
Copy link
Contributor

@jakemac53 anything I can do to help?

The repro you provided is already great, that is probably all we need to investigate (and thanks for that!)

@albinpk
Copy link

albinpk commented Nov 9, 2024

Is there any update on this issue?

@jakemac53
Copy link
Contributor

jakemac53 commented Nov 11, 2024

No meaningful updates, and it will still be quite some time before the underlying support needed for reading annotations is there.

Although, this package does support any type with a toJson method and fromJson constructor - so you could try using enhanced enums to provide those members on your enum type, and then it might just work. Not sure if enums allow factory constructors though, or may be this package could allow a static method called fromJson instead of a constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues. cfe-feature-macros Implement macros features in the CFE feature-macros Implementation of the macros feature type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) type-question A question about expected behavior or functionality
Projects
Development

No branches or pull requests

7 participants