How to find out what type of rustc :: middle :: ty :: Ty represents?

To write another lint in Rust, I need to make sure that the Expr type is actually Option<_> (or any pointer to one). I already went through any ptr and rptr to their conclusion and stayed with rustc::middle::ty , which in my test example debugs (manually formatted for better readability):

 TyS { sty: ty_enum( DefId { krate: 2, node: 117199 }, Substs { types: VecPerParamSpace { TypeSpace: [ TyS { sty: ty_int(i32), flags: 0, region_depth: 0 } ], SelfSpace: [], FnSpace: [], }, regions: NonerasedRegions( VecPerParamSpace { TypeSpace: [], SelfSpace: [], FnSpace: [], } ) } ), flags: 0, region_depth: 0 } 

However, now I am a little lost - how do I know if TyS is really an Option <_> type?

+6
source share
1 answer

You need to use with_path in DefId. You will be given an iterator over the PathElem that you should use.

The following is a rough sketch, but it should give you an array of Name if you change it a bit.

 if let ty_enum(did, ..) = ty.sty { tcx.with_path(did, |iter| iter.map(|elem| elem.name())).collect::<Vec<Name>>; } 
+2
source

Source: https://habr.com/ru/post/988526/


All Articles