(My apologies if this is the wrong place to ask this question!)
We are trying to build a
rustc plugin -- and am having a bit of trouble trying to
FnSig (type) of a
DefId. When you have a
LocalDefId (which, as I
understand it, means a
DefId defined in the current crate, you can simply do something like
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let hir_fn_sig = tcx.hir().fn_sig_by_hir_id(hir_id).unwrap();
However, I'd like to do the same thing but for identifiers defined in external crates
std::process::exit to pick a random example).
Can anyone give some pointers? Maybe there is some API to
- Convert a
DefId to a
HirId and then extract the
Or some other route? [Or do we only have
and should I be looking elsewhere entirely?]
Any pointers will be most welcome, thanks in advance!
Thanks! That moves things along quite a bit, however I am next stuck on the following.
- For the
LocalDefId,we get the
- For the
DefId (following the code above) we get a
The two are slightly different, e.g. the constituent types (inputs and outputs) have respectively
Do you know if it is possible to convert the
middle versions to the
hir versions (or vice versa)?
No, going from hir::Ty -> ty::Ty only happens during hir lowering. The other way around is inpossible. You can convert a LocalDefId to a DefId though and then use the linked code.
ty::Tys are more useful to perform analyses on, since they are the semantic rather than syntactic representation of a type. The main limitation of
ty::Tys I know of is that type aliases are fully expanded and aren't represented.
You can't get HIR from external crates because it's not stored in the crate metadata files.
TyCtxt::fn_sig() is probably the function you want.
tcx.fn_sig doesn't work for closures and generators. The code I linked does work for them.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.