Skip to content

Method/macro parameter annotation support#12044

Merged
straight-shoota merged 6 commits intocrystal-lang:masterfrom
Blacksmoke16:arg-annotations
Jun 22, 2022
Merged

Method/macro parameter annotation support#12044
straight-shoota merged 6 commits intocrystal-lang:masterfrom
Blacksmoke16:arg-annotations

Conversation

@Blacksmoke16
Copy link
Member

@Blacksmoke16 Blacksmoke16 commented May 4, 2022

Resolves #12039

  • Includes support for annotating macro/method parameters
    • Parsing, reading, and rendering them on to_s
    • There isn't actually a way to access annotations on macros, or anything about them for that matter, so I didn't write tests for that. However it does parse/render correctly.

Based on #9322 (comment) i took a bit of a diff approach and stored the parsed annotation instances within the Arg node itself. Ended up making the implementation much easier and figured we wouldn't have to worry about breaking compatibility since this is a new feature.


def foo(
id : Int32,
@[Ann] &block : Int32 ->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: #5334

@Blacksmoke16
Copy link
Member Author

Blacksmoke16 commented May 4, 2022

Seems something is broken with to_s.cr when expanding macros. The Def in record_spec.cr is being generated as like def initialize(, __id id : Int32), even tho it shows up correctly in the test output?

I'll see if I can figure out what's wrong tomorrow.

EDIT: Seems a single * arg doesnt ever call the visit(node : Arg) overload?
EDIT2: Figured it out I think. semantic/to_s.cr redefines that method 🙃, so the one in syntax/to_s.cr doesn't actually do anything, which broke things because it was no longer adding the * in the Def visitor method

Reset arg type after processing an `Arg`, versus a `Def`
@straight-shoota straight-shoota self-requested a review May 4, 2022 17:11
@Blacksmoke16
Copy link
Member Author

Bump. Another review on this would be much appreciated 🙂.

@asterite
Copy link
Member

How's the memory consumption after this PR for, say, compiling the compiler? You can check this with "-s"

@Blacksmoke16
Copy link
Member Author

@asterite

On this branch:

make clean && make stats=1 progress=1

...

Parse:                             00:00:00.000038766 (   1.08MB)
Semantic (top level):              00:00:00.267053231 ( 129.96MB)
Semantic (new):                    00:00:00.001583005 ( 129.96MB)
Semantic (type declarations):      00:00:00.030205549 ( 137.96MB)
Semantic (abstract def check):     00:00:00.024382963 ( 137.96MB)
Semantic (ivars initializers):     00:00:04.060857175 (1037.90MB)
Semantic (cvars initializers):     00:00:00.008242924 (1045.90MB)
Semantic (main):                   00:00:00.850217616 (1213.90MB)
Semantic (cleanup):                00:00:00.000485844 (1213.90MB)
Semantic (recursive struct check): 00:00:00.000968438 (1213.90MB)
Codegen (crystal):                 00:00:04.155784348 (1497.90MB)
Codegen (bc+obj):                  00:00:00.616157028 (1505.90MB)
Codegen (linking):                 00:00:02.735330050 (1505.90MB)

On the commit I branched off of:

make clean && make stats=1 progress=1

...

Parse:                             00:00:00.000033139 (   1.08MB)
Semantic (top level):              00:00:00.270554537 ( 129.96MB)
Semantic (new):                    00:00:00.001891485 ( 129.96MB)
Semantic (type declarations):      00:00:00.029909324 ( 137.96MB)
Semantic (abstract def check):     00:00:00.024808537 ( 137.96MB)
Semantic (ivars initializers):     00:00:04.090095406 (1037.90MB)
Semantic (cvars initializers):     00:00:00.008378645 (1045.90MB)
Semantic (main):                   00:00:00.846730859 (1213.90MB)
Semantic (cleanup):                00:00:00.000431342 (1213.90MB)
Semantic (recursive struct check): 00:00:00.001096690 (1213.90MB)
Codegen (crystal):                 00:00:04.213905891 (1497.90MB)
Codegen (bc+obj):                  00:00:06.765188742 (1497.90MB)
Codegen (linking):                 00:00:02.735254769 (1497.90MB)

So seems pretty much the same?

@asterite
Copy link
Member

Interesting! Since we are adding something to every Arg I thought the memory was going to be way up... but no 🤷 😄

property default_value : ASTNode?
property restriction : ASTNode?
property doc : String?
property parsed_annotations : Array(Annotation)?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of keeping the annotations on each arg, I think we can move this to the Def and Macro objects, similar to how the splat index is kept there.

But we could do this refactor later on if we see memory issues. I just don't think that annotations on args will be that common, and so adding this overhead to every arg instead of adding just a nilable attribute to Def and Macro might be better.

Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@straight-shoota straight-shoota added this to the 1.5.0 milestone Jun 21, 2022
@straight-shoota straight-shoota merged commit d173d12 into crystal-lang:master Jun 22, 2022
@Blacksmoke16 Blacksmoke16 deleted the arg-annotations branch June 22, 2022 23:34
beta-ziliani pushed a commit that referenced this pull request Sep 7, 2022
Also added an optional focus parameter to assert_format to make debugging a specific failure a bit easier.

Fixes formatter after #12044.
beta-ziliani pushed a commit to beta-ziliani/crystal that referenced this pull request Sep 7, 2022
Also added an optional focus parameter to assert_format to make debugging a specific failure a bit easier.

Fixes formatter after crystal-lang#12044.
beta-ziliani pushed a commit that referenced this pull request Sep 7, 2022
Also added an optional focus parameter to assert_format to make debugging a specific failure a bit easier.

Fixes formatter after #12044.
beta-ziliani pushed a commit that referenced this pull request Sep 7, 2022
Also added an optional focus parameter to assert_format to make debugging a specific failure a bit easier.

Fixes formatter after #12044.
straight-shoota pushed a commit that referenced this pull request Jul 31, 2025
…escriptor` constructors (#16034)

The final patch to the blocking behavior rework. Each event loop configures the system file descriptor or handle as per its internal requirement, and we shall deprecate the `blocking` parameter.

Parser support for annotations on def parameters was only added in #12044 for Crystal 1.5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow annotations on method/macro parameters

4 participants