First-class compile time type manipulation
Nobody has claimed this yet.
- Dominant language
- Markdown
- Stars
- 6.6k
- Forks
- 1.7k
- Avg merge
- 16h 14m
- Merged PRs (30d)
- 1
Description
Zig supports comptime, which is a keyword that allows for the evaluation of arbitrary expressions and entire functions at compile time. It is also used in Zig's implementation of generics, so that types can be manipulated at compile time to generate code. In particular, Zig supports the following compile time features:
- Treating types as first-class compile-time values
- Treating arguments as compile-time arguments as opposed to runtime arguments (that is, their value must be known at compile time)
- Generating structs/enums at compile time
- Emitting compiler errors from compile-time contexts, compile-time type validation
- Manually unroll loops (compile time for) and generate runtime code at compile time
Rust has a few existing features that address compile-time code evaluation:
- Const fns allow execution of a subset of Rust at compile time when a function is explicitly marked as const
- Inline const is already in progress
- Procedural macros allow for execution of code at compile time to manipulate syntactical elements. They exist at compile time but work at a domain above the type system, so are limited in the kinds of analysis they can perform (especially around types)
These are a few of the goals that could be addressed with compile-time code evaluation and type manipulation:
- Variadic generics could be represented as a list of compile-time types without needing to worry about their runtime representation (including tuple byte layout issues in #376)
- Reflection can be supported without any runtime type representation necessary
- Manipulating types with regular code is a lot simpler than trying to generate code using procedural or even declarative macros in many cases
- Compile-time code can be run to validate preconditions necessary for generated code
- Optimized or specialized code can be generated using compile time code evaluation, without needing partial specialization, based on arbitrary conditions
- Contracts
- Tools (such as linters and IDEs) can run compile time code during development to ensure that types validate and produce correct code
- Compile-time code could access compiler features that aren't available at runtime (IE, ask the compiler to check for trait conformances)
I have some ideas for how this could be implemented, but ultimately I wanted to build a path towards variadic generics through compile-time code evaluation and generation. There are many features we would need to expand on first before we could work on many of the features listed above, mostly around making const and generics more powerful in Rust. And we would need a way to manipulate types in const fns and return them to be used as generic arguments.
What are the community's thoughts about this as a future direction for Rust?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation files, tests, or entry points are named. Start by reading the issue's discussion and the linked const, inline-const, and contracts materials; the work is not ready to begin until the broad future direction is narrowed into a specific, scoped proposal with clear acceptance criteria.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust, zig
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100