Phantom const generics?

My two cents:

  1. The approach of automatically eliding monomorphization if it does not matter for the generated code is difficult, because in many scenarios (including this one) it both technically generates slightly different code and that difference can sometimes make a big difference for subsequent optimizations. For example, a summation function can be greatly improved for small array lengths, in other cases bounds checks can be removed for array lengths above a threshold, etc. – it is difficult for the compiler to automatically make the right call here (of course, “always monomorphize” is often not the right call, but at least it’s predictable).

  2. Annotating parameters to not monomorphize is conceivable, but any proposal in this space has to do the legwork of nailing down exactly what subset of the language can be used with such a parameter (in other words, which restrictions this attribute adds), and ensuring we can feasibly implement the different codegen path for this subset of Rust (see also Idea: polymorphic baseline codegen).

6 Likes