Method "span" not found for reference "&ExprPath" (from "syn" crate)

Viewed 112

I am trying to implement a function-like macro in Rust. It should be used as follows:

service!(FooService, "/foo_service");

This macro call shall generate a struct FooServiceClient and an impl FooServiceClient with various methods.

I am using the syn + quote duo and here are some relevant bits of my code:

struct ServiceDefinition {
    pub service_name: String,
    pub scope: String,
    pub operations: Vec<Operation>,
}

impl Parse for ServiceDefinition {
    fn parse(input: ParseStream) -> Result<Self> {
        let params = Punctuated::<Expr, Token![,]>::parse_terminated(&input)?;
        let service_name = {
            let expr = &params[0];
            match expr {
                Expr::Path(ref expr_path) => {
                    let leading_colon = expr_path.path.leading_colon;
                    if let Some(leading_colon) = leading_colon {
                        return Err(Error::new(expr_path.span(), "expected unscoped identifier"));
                    }
                    if expr_path.path.segments.len() != 1 {
                        return Err(Error::new(expr_path.span(), "expected unscoped identifier"));
                    }

                    expr_path.path.segments.first().unwrap().ident
                },
                _ => {
                    return Err(Error::new(expr.span(), "expected service name"));
                }
            }
        };

        Ok(ServiceDefinition {
            service_name: service_name.to_string(),
            scope: "/foo".to_string(),
            operations: vec![],
        })
    }
}

As you can see, this is still at the early stages of work in progress, but when I try to compile it, this is the error I'm getting:

error[E0599]: no method named `span` found for reference `&ExprPath` in the current scope
   --> service_commons/src/lib.rs:58:57
    |
58  |                         return Err(Error::new(expr_path.span(), "expected unscoped identifier"));
    |                                                         ^^^^ method not found in `&ExprPath`
    |

As far as I can see in the docs, the method span should be on these types, but it's obviously missing for some reason.

I am using syn = {version = "1.0.70", features = ["full"]} in my Cargo file.

What am I missing here?

1 Answers
Related