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
source to share
Something is happening here.
-
ast::Lit
is just a type aliascodemap::Spanned
, not an enumeration or variant of an enumeration. Therefore, you don't need to specify both, but you must specify either. -
ast::ExprLit
is an enumeration variant containingP<Lit>
. So you need to destroyP
usingP(inner)
- At least I remember doing -
P
structuring that way. The documents say otherwise. If that doesn't work, you need to dereference that and add another internal mapping for the content
- At least I remember doing -
-
codemap::Spanned
is a structure, not a tuple structure, which means you cannot match your fields by position, but you have to degrade the fields by name like this:let codemap::Spanned { node: innernode, span: innerspan, } = some_spanned;
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 }
};
source to share
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.
source to share