From 83894e2a6b6b4d943eee6560084375ad7e55db65 Mon Sep 17 00:00:00 2001 From: CreepySkeleton Date: Fri, 12 Jun 2020 13:08:07 +0300 Subject: [PATCH] Some tweaks --- structopt-derive/src/lib.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/structopt-derive/src/lib.rs b/structopt-derive/src/lib.rs index 1630864c..a5cd7c26 100644 --- a/structopt-derive/src/lib.rs +++ b/structopt-derive/src/lib.rs @@ -28,7 +28,7 @@ use crate::{ use proc_macro2::{Span, TokenStream}; use proc_macro_error::{abort, abort_call_site, proc_macro_error, set_dummy}; -use quote::{quote, quote_spanned}; +use quote::{format_ident, quote, quote_spanned}; use syn::{punctuated::Punctuated, spanned::Spanned, token::Comma, *}; /// Default casing style for generated arguments. @@ -239,6 +239,16 @@ fn gen_augmentation( } fn gen_constructor(fields: &Punctuated, parent_attribute: &Attrs) -> TokenStream { + // This ident is used in several match branches below, + // and the `quote[_spanned]` invocations have different spans. + // + // Given that this ident is used in several places and + // that the branches are located inside of a loop, it is possible that + // this ident will be given _different_ spans in different places, and + // thus will not be the _same_ ident anymore. To make sure the `matches` + // is always the same, we factor it out. + let matches = format_ident!("matches"); + let fields = fields.iter().map(|field| { let attrs = Attrs::from_field( field, @@ -248,7 +258,6 @@ fn gen_constructor(fields: &Punctuated, parent_attribute: &Attrs) ); let field_name = field.ident.as_ref().unwrap(); let kind = attrs.kind(); - let matches = quote! { matches }; match &*kind { Kind::ExternalSubcommand => abort!( kind.span(),