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

Run comptime code when a type definition is annotated with a comptime function #5255

Closed
Tracked by #4594
jfecher opened this issue Jun 14, 2024 · 0 comments · Fixed by #5256
Closed
Tracked by #4594

Run comptime code when a type definition is annotated with a comptime function #5255

jfecher opened this issue Jun 14, 2024 · 0 comments · Fixed by #5256
Assignees
Labels
enhancement New feature or request

Comments

@jfecher
Copy link
Contributor

jfecher commented Jun 14, 2024

Problem

Comptime code needs a way to inspect types with the goal of eventually generating its own impls.

Happy Case

Annotations on a type definition should call a comptime function of the same name with the type definition itself as an argument:

#[my_comptime_fn]
struct Foo {
    x: Field,
    y: u32,
}

comptime fn my_comptime_fn(typ: TypeDefinition) {
    ...
}

Once we support quoted top-level items (Decls), such as trait impls, the goal is to have my_comptime_fn return a Decl to be inserted at the call site.

Project Impact

None

Impact Context

No response

Workaround

None

Workaround Description

No response

Additional Context

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@jfecher jfecher added the enhancement New feature or request label Jun 14, 2024
@jfecher jfecher self-assigned this Jun 14, 2024
github-merge-queue bot pushed a commit that referenced this issue Jun 18, 2024
# Description

## Problem\*

Resolves #5255

## Summary\*

Implements the ability to run comptime code when an annotation is put on
a type definition. This annotation must resolve to a comptime function
in scope, which is then called with the type definition as an argument.
There are currently no API functions to actually do anything with a
`TypeDefinition` object. The plan is to add functions in the future to
inspect or add fields & generics.

```rs
#[print_type]
struct Foo {
    bar: Field,
}

comptime fn print_type(typ: TypeDefinition) {
    println("hello from print_type_name at compile-time");
    println(typ); // only prints "(type definition)" currently
}

fn main(){}
```

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [x] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant