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

Types Generated using the LibraryTypesMacro throws an error when instantiating #56107

Open
kwasi-dev opened this issue Jun 29, 2024 · 2 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 P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@kwasi-dev
Copy link

I've been experimenting with the new feature Macros and I'm trying to use it to introduce a new class/Type in a library. The editor successfully shows me the library augment but I cannot instantiate this new class.

This was tested using the following:

  • macros: ^0.1.0-main.5 using Dart SDK version: 3.5.0-180.3.beta (beta) (Wed Jun 5 15:06:15 2024 +0000) on "linux_x64"
  • macros: ^0.1.2-main.4 using Dart SDK version: 3.5.0-307.0.dev (dev) (None) on "linux_x64"

Minimally reproducible example:

  1. Create a new package dart create -t package macro_library_bug
  2. Edit the analysis_options.yaml file to enable macros
... redacted contents of analysis_options.yaml
analyzer:
  enable-experiment:
    - macros
  1. Edit the pubspec.yaml file and add a dependency on macros
... redacted contents of pubspec.yaml
dependencies:
  macros: ^0.1.2-main.4

  1. Run dart pub get
  2. Edit the lib/src/macro_library_bug_base.dart file and create a new LibraryTypeMacro
import 'dart:async';
import 'package:macros/macros.dart';

macro class LibraryAnnotator implements LibraryTypesMacro{
  const LibraryAnnotator();
  
  @override
  FutureOr<void> buildTypesForLibrary(Library library, TypeBuilder builder) {
    final randomType = "MyLibType";
    builder.declareType(randomType, DeclarationCode.fromParts([
      "class $randomType {",
      "\n\tconst $randomType();",
      "\n}"
    ]));
  }
}
  1. Edit the lib/macro_library_bug.dart file and annotate the library with @LibraryAnnotator()
@LibraryAnnotator()
library macro_library_bug;

import 'package:macro_library_bug/src/macro_library_bug_base.dart';
export 'src/macro_library_bug_base.dart';
  1. Edit the example/macro_library_bug_example.dart file
import 'package:macro_library_bug/macro_library_bug.dart';

void main() {
  final newLibType = MyLibType();
  print(newLibType);
}
  1. Run the example file with the command dart --enable-experiment=macros example/macro_library_bug_example.dart

Expected Result
The console should show Instance of MyLibType

** Actual Result **

example/macro_library_bug_example.dart:4:9: Error: 'MyLibType' isn't a type.
  final MyLibType newLibType = MyLibType();
        ^^^^^^^^^
example/macro_library_bug_example.dart:4:32: Error: Method not found: 'MyLibType'.
  final MyLibType newLibType = MyLibType();
@dart-github-bot
Copy link
Collaborator

Summary: The LibraryTypesMacro generated type MyLibType is not recognized as a type when attempting to instantiate it in a separate file. The editor correctly shows the library augmentation, but the type is not available for use at runtime.

@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 Jun 29, 2024
@devoncarew devoncarew added the feature-macros Implementation of the macros feature label Jun 30, 2024
@jakemac53 jakemac53 added area-front-end Use area-front-end for front end / CFE / kernel format related issues. P2 A bug or feature request we're likely to work on and removed area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jul 10, 2024
@jakemac53
Copy link
Contributor

Looks like this is just not implemented in the front end, but is implemented in the analyzer (which is why you can see the class, but it isn't actually there when you run the app).

It is expected that you will run into unimplemented stuff at this time, which is why I marked the priority as P2 for now, but this definitely should get implemented prior to launch.

@johnniwinther johnniwinther added the cfe-feature-macros Implement macros features in the CFE label Jul 11, 2024
@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Jul 19, 2024
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 P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
Development

No branches or pull requests

6 participants