Is it possible to get a Span from a DefId or TyS or FieldDefData

I’ve got my hands on a rustc::middle::ty::FieldDefData struct, while hacking around inside a custom lint pass, and I’d like to ideally get the corresponding Span for that so I can display helpful error messages. I have searched the API documentation of all the Rust compiler crates (thanks to http://manishearth.github.io/rust-internals-docs) to try and find something but I haven’t found anything.

I suspect a Span should be able to be retrieved from a DefId, as it identifies a particular definition. I just can’t find the function to do it.

If the field comes from a remote crate, then you can’t get its span, as it is not saved. Otherwise, you can use tcx.map.get_if_local.

Thanks very much! I was tripped up at first as I was still running a slightly earlier build of the nightly and that function didn’t exist, but after upgrading it all works fine.

I suppose if the Spans from other crates aren’t saved then the AST isn’t either? So far I’ve been able to inspect all the type information for items like structs/enums but the next step is to inspect function body for each function that is called (ideally across the crate boundary). Is there a way to get the AST for that? or perhaps even just another IR that I could inspect (eg: LLVM IR).

I must confess I’m trying to do stuff inside a lint pass that really isn’t a lint (so if anything blows up I accept it’s probably my fault). I am working on a translator that takes every function marked with the #[shader] attribute and converts it and all of its dependencies into a GLSL shader. So far it is working much better than my previous version which was written as a syntax extension and thus had no knowledge of the type system.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.