Provide an extended framework for type visit, for use in rust-analyzer by ChayimFriedman2 · Pull Request #149856 · rust-lang/rust (original) (raw)

rust-analyzer needs to be able to visit types when treating not only Ty, Const, Region and Predicate specifically, but all rust-analyzer-made types specifically (excluding e.g. TraitRef, that is declared in rustc_type_ir). This is needed to implement garbage collection.

To support this, we introduce a second, rust-analyzer-only visit trait, named, without much thought, CustomizableTypeVisitable. It's simpler than TypeVisitable (for example, it does not have a trait for the visitor, and does not support early-returning) because this is what rust-analyzer needs, but its most distinguished feature is that the visitor is a generic of the trait instead of the method. This way, specific types can treat specific visitor types specifically and call their methods.

In rustc_type_ir we implement it for a bunch of basic types, and using a derive macro for the rest. The macro and trait are completely disabled when compiling for rustc (feature = "nightly"), so not even a compile time penalty will be paid.

r? types

This is a replacement to other efforts to support non-Copy type in the solver, replacing them with a GC in r-a, as decided by @rust-lang/rust-analyzer. The code is tiny in comparison, and I believe T-types will have no problem maintaining it, which mostly means adding the derive on new things when they are added and things break on the r-a side.