dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.06k stars 1.55k forks source link

[Question] support annotated types in records #55604

Open d-markey opened 4 months ago

d-markey commented 4 months ago

Hello,

I'm using build_runner: ^2.4.8 on Dart 3.3.4 (stable) from Flutter 3.19.6.

I've developed a builder and would like to use annotations on types in records, eg:

(int, @CustomSerializer() CustomType) myMethod(...) { ... }

The syntax is accepted by Dart and the program compiles, however I found no way to access the @CustomSerializer() annotation for CustomType in my builder, presumably because the method element carries a resolved DartType in returnType. I can inspect the types that are part of the record, but can't navigate to the location in the AST that defined that record type.

Is there a way to achieve this, or is it considered at all?

jakemac53 commented 4 months ago

This sounds to me like an analyzer question - transferred the issue to the SDK

bwilkerson commented 3 months ago

There isn't currently any way to get that information without visiting the AST nodes, and I'm not seeing any reasonable way to find the AST node associated with that annotation.

I don't think it would make sense to attempt to associate metadata with a DartType, and there isn't any Element representing the declaration of the record type because record types are structural types rather than nominative types.

I can't think of any reasonable way for us to provide access to this information through the element model (much as I'd like to say "sure we can do that"), but I'll keep this issue open for a bit in case it might prompt some ideas that we haven't thought about yet.