Matching templates by AST structure in the compiler

So, I am trying to match a pattern in a structure. This particular structure consists of multiple enums that contain pointers to enumerations or at the most basic level, an un-signed 8-byte vector. I would like to work with a vector, however would like to know if the template can match my vec path.

ast::ExprUnary(ast::UnDeref, ref exprs) =>{
        let pat = match exprs.node {

            ast::ExprLit(ast::Lit(codemap::Spanned(ast::LitBinary(string),innerspan))) => {
                 //stuff to string
                 //primary issue here is that these enums, which ExprLit is 
                 //comprised of, do not pattern match correctly
            }
            _ => //other stuff
        };

      

I get the following two errors when compiling this code:

unresolved enum variant, struct or const `Spanned`
unresolved enum variant, struct or const `Lit`

      

both of these enums exist within ast, however I don't know why they are not allowed in this particular context. Any advice would be appreciated

+3


source to share


2 answers


Something is happening here.



Overall, I believe your match should look something like this:

let pat = match exprs.node {
    ast::ExprLit( P( codemap::Spanned {
        node: ast::LitBinary(string),
        span: innerspan,
    })) => {
        //stuff to string
        //primary issue here is that these enums, which ExprLit is 
        //comprised of, do not pattern match correctly
    }
    _ => { //other stuff }
};

      

+7


source


I used to write code like this, but it's tedious. Now I am simple use syntax::ast::*

and leave everything ast::

in the code. This will also take care of Lit

(as in syntax::ast

)



I can't say exactly what the actual problem is because your code is incomplete, but take a look at https://github.com/Manishearth/rust-clippy/blob/master/src/len_zero.rs for a successful compilation example.

0


source







All Articles