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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
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 @@ -265,13 +275,13 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs)
};
quote_spanned! { kind.span()=>
#field_name: <#subcmd_type as ::structopt::StructOptInternal>::from_subcommand(
matches.subcommand())
#matches.subcommand())
#unwrapper
}
}

Kind::Flatten => quote_spanned! { kind.span()=>
#field_name: ::structopt::StructOpt::from_clap(matches)
#field_name: ::structopt::StructOpt::from_clap(#matches)
},

Kind::Skip(val) => match val {
Expand Down Expand Up @@ -318,45 +328,45 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs)
let occurrences = *attrs.parser().kind == ParserKind::FromOccurrences;
let name = attrs.cased_name();
let field_value = match **ty {
Ty::Bool => quote_spanned!(ty.span()=> matches.is_present(#name)),
Ty::Bool => quote_spanned!(ty.span()=> #matches.is_present(#name)),

Ty::Option => quote_spanned! { ty.span()=>
matches.#value_of(#name)
#matches.#value_of(#name)
.map(#parse)
},

Ty::OptionOption => quote_spanned! { ty.span()=>
if matches.is_present(#name) {
Some(matches.#value_of(#name).map(#parse))
if #matches.is_present(#name) {
Some(#matches.#value_of(#name).map(#parse))
} else {
None
}
},

Ty::OptionVec => quote_spanned! { ty.span()=>
if matches.is_present(#name) {
Some(matches.#values_of(#name)
if #matches.is_present(#name) {
Some(#matches.#values_of(#name)
.map_or_else(Vec::new, |v| v.map(#parse).collect()))
} else {
None
}
},

Ty::Vec => quote_spanned! { ty.span()=>
matches.#values_of(#name)
#matches.#values_of(#name)
.map_or_else(Vec::new, |v| v.map(#parse).collect())
},

Ty::Other if occurrences => quote_spanned! { ty.span()=>
#parse(matches.#value_of(#name))
#parse(#matches.#value_of(#name))
},

Ty::Other if flag => quote_spanned! { ty.span()=>
#parse(matches.is_present(#name))
#parse(#matches.is_present(#name))
},

Ty::Other => quote_spanned! { ty.span()=>
matches.#value_of(#name)
#matches.#value_of(#name)
.map(#parse)
.unwrap()
},
Expand Down