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

[Macro] Incorrect report: macro_not_allowed_declaration. #56747

Open
microtears opened this issue Sep 18, 2024 · 2 comments
Open

[Macro] Incorrect report: macro_not_allowed_declaration. #56747

microtears opened this issue Sep 18, 2024 · 2 comments
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. cfe-feature-macros Implement macros features in the CFE feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@microtears
Copy link

The following example will report a macro_not_allowed_declaration error. Nevertheless, in the code, the factory constructor is augmented during the definition phase. The expected behavior is to have no error reported.

Macro Example

@Macro1()
class A {
  external factory A();
}


macro class Macro1 implements ClassDefinitionMacro {
  const Macro1();

  @override
  FutureOr<void> buildDefinitionForClass(
      ClassDeclaration clazz, TypeDefinitionBuilder builder) async {
    final constructors = await builder.constructorsOf(clazz);
    final factoryConstructor = constructors.single;
    final constructorBuilder =
        await builder.buildConstructor(factoryConstructor.identifier);
    constructorBuilder.augment(body: FunctionBodyCode.fromParts([
      '=> throw UnimplementedError();',
    ]));
  }
}

Error Message

The macro attempted to add declaration(s) not allowed during the definitions phase.
Locations: (129, 42)
---
part of 'file:///Users/anya/workspace/workspace/dart/pkgs/common/example/macro_issue_example.dart';

augment class A {
  augment factory A() => throw UnimplementedError();
}
---
Try adding these declaration during an earlier phase.dart(macro_not_allowed_declaration)

Dart info

#### General info

- Dart 3.6.0-238.0.dev (dev) (Tue Sep 10 17:07:55 2024 -0700) on "macos_arm64"
- on macos / Version 14.1 (Build 23B74)
@dart-github-bot
Copy link
Collaborator

Summary: The issue reports a false positive error "macro_not_allowed_declaration" when augmenting a factory constructor within a macro. The error occurs despite the augmentation happening during the definition phase, where it should be allowed.

@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-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Sep 18, 2024
@microtears
Copy link
Author

I guess it might be the problem caused here, but I'm not sure.

@lrhn lrhn added area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library 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 Sep 19, 2024
@johnniwinther johnniwinther added the cfe-feature-macros Implement macros features in the CFE label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-fe-analyzer-shared Assigned by engineers; when triaging, prefer either area-front-end or area-analyzer. cfe-feature-macros Implement macros features in the CFE feature-macros Implementation of the macros feature pkg-macros The experimental package:_macros library type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
Development

No branches or pull requests

4 participants