sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Types Generated using the LibraryTypesMacro throws an error when instantiating

Open kwasi-dev opened this issue 1 year ago • 1 comments
trafficstars

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();

kwasi-dev avatar Jun 29 '24 21:06 kwasi-dev

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 avatar Jun 29 '24 21:06 dart-github-bot

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.

jakemac53 avatar Jul 10 '24 21:07 jakemac53