3 ms·
Rust macros run before type inference, and can change type inference in just about every way. They can introduce new types, implement traits for existing types,
by gpm 5y ago
Rust macros run before type inference, and can change type inference in just about every way. They can introduce new types, implement traits for existing types, introduce new variables, and add new type constraints on existing variables.
Rust macros understand types in the sense that they understand which parts of the syntax tree are specifying a type and which are expressions, but I think it's fundamentally impossible for them to understand the actual types of variables.
- ncmncm 5y ago"Fundamentally impossible" sounds like a challenge. Rust's unbounded type inference imposes quite a large cost that I wonder if the language designers have come to regret. But macros might, in principle, someday be run provisionally, first without inference, and then applying inference to the output, until a fixed point is reached. That would only be if you were willing for Rust compilation to get even slower. Of course it is easier to talk about this than to design and implement it. Considering embedded and mutually recursive expansions, the Halting Problem rears its head, and there may not be a fixed point, or anyway none in the immediate century. But those would be exceptional cases.
- gpm 5y agoOk, maybe that claim is a tiny bit too strong, but I'll defend it anyways. Consider that rust macros are allowed to have side effects and access outside resources, they aren't pure functions. I'd argue that any system that runs them repeatedly in an attempt to find a fixed point is either incorrect (backwards compatibility wise), or an all together new macro system that happens to run in parallel to the current one and call itself the same thing (debatable, I know). Nor can we simulate the proc macro and only let the side effects occur once we reach a fixed point, because the proc macros behavior can depend on the values returned by those external systems. The halting problem is arguably not a theoretical issue, since the type system is already turing complete (up to arbitrary limits on recursion depth that could exist here to).
- ncmncm 5y agoI too am pretty confident that there will be no generally type-aware Rust macro system, which is legitimately disappointing. Still, the types of many named entities will typically be knowable. So, a macro system that can provisionally operate on types when they are known, or even that supports macros that require that the types of certain names be known, seems doable, in principle. It would need a good usage story to get any traction.