Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/matches span #400

Merged
merged 2 commits into from Jun 12, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions structopt-derive/src/lib.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -239,6 +239,16 @@ fn gen_augmentation(
}

fn gen_constructor(fields: &Punctuated<Field, Comma>, 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that a plain quote { } or format_ident will always use Span::call_site() for hygiene, so the generated matches token will always be 'the same' from the perspective of the compiler's name resolution. However, this change is still a good idea, since it makes it clear that matches is logically independent of the loop.

// 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,
Expand All @@ -248,7 +258,6 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, 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(),
Expand Down