Unify `ast/hir::Mutability`

Context: I wanted to create a helper function in clippy_utils which would turn <expr> (a snippet) into &<expr> or &mut <expr>, depending on provided mutbl: Mutability. In the middle of writing it, I realized that it won't work as is, because Clippy has both early and late lints, thus works with bost AST and HIR data structures, among which there are ast::Mutability and hir::Mutability, respectively, and so the callers would want to call the function with either of these.

One way to solve this would be to provide two methods, "addrify_ast" and "addrify_hir", but that would of course be less than ideal -- hence my question: wouldn't it be desirable to deduplicate these enums? I completely understand why this isn't done for ast/hir/middle::Expr/Ty/Pat etc., but AFAICS the Mutabilitys have pretty much identical methods and trait impls, so I can't imagine this being a controversial idea?

Another option would be to provide a From impl, but I guess it would be wasteful to make rustc_ast or rustc_hir depend on each just to be able to name the other one's Mutability in said From impl -- not to mention that that would create a circular dependency.

Do let me know if there's something else I'm missing.

hir's "copy" of the type is actually just a re-export. It's really weird that rustdoc makes them seem like totally different types.

Oh, you're right -- apparently they're actually both re-exports from rustc_ast_ir. Unfortunately that doesn't change the fact that I can't use the types interchangeably.

EDIT: apparently I can? I can swear I wasn't able to just yesterday. That's what you get for posting at 2am I guess.. Thanks for your help @beepster :slight_smile:

2 Likes