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

Bugfix #448

Merged
merged 4 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v0.3.21 (2020-11-30)

* Fixed [another breakage](https://github.com/TeXitoi/structopt/issues/447)
when the struct is placed inside a `macro_rules!` macro.

# v0.3.20 (2020-10-12)

* Fixed [a breakage](https://github.com/TeXitoi/structopt/issues/439)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "structopt"
version = "0.3.20"
version = "0.3.21"
edition = "2018"
authors = ["Guillaume Pinot <[email protected]>", "others"]
description = "Parse command line argument by defining a struct."
Expand Down Expand Up @@ -28,7 +28,7 @@ travis-ci = { repository = "TeXitoi/structopt" }

[dependencies]
clap = { version = "2.33", default-features = false }
structopt-derive = { path = "structopt-derive", version = "=0.4.13" }
structopt-derive = { path = "structopt-derive", version = "=0.4.14" }
lazy_static = "1.4.0"
paw_dep = { version = "1", optional = true, package = "paw" }

Expand Down
2 changes: 1 addition & 1 deletion structopt-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "structopt-derive"
version = "0.4.13"
version = "0.4.14"
edition = "2018"
authors = ["Guillaume Pinot <[email protected]>"]
description = "Parse command line argument by defining a struct, derive crate."
Expand Down
17 changes: 10 additions & 7 deletions structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,16 +647,19 @@ fn gen_from_subcommand(
_ => false,
});

let other = format_ident!("other");
let matches = format_ident!("matches");

let external = match ext_subcmd {
Some((span, var_name, str_ty, values_of)) => quote_spanned! { span=>
match other {
match #other {
("", ::std::option::Option::None) => None,

(external, Some(matches)) => {
(external, Some(#matches)) => {
::std::option::Option::Some(#name::#var_name(
::std::iter::once(#str_ty::from(external))
.chain(
matches.#values_of("").into_iter().flatten().map(#str_ty::from)
#matches.#values_of("").into_iter().flatten().map(#str_ty::from)
)
.collect::<::std::vec::Vec<_>>()
))
Expand All @@ -682,7 +685,7 @@ fn gen_from_subcommand(
Unit => quote!(),
Unnamed(ref fields) if fields.unnamed.len() == 1 => {
let ty = &fields.unnamed[0];
quote!( ( <#ty as ::structopt::StructOpt>::from_clap(matches) ) )
quote!( ( <#ty as ::structopt::StructOpt>::from_clap(#matches) ) )
}
Unnamed(..) => abort!(
variant.ident,
Expand All @@ -691,7 +694,7 @@ fn gen_from_subcommand(
};

quote! {
(#sub_name, Some(matches)) => {
(#sub_name, Some(#matches)) => {
Some(#name :: #variant_name #constructor_block)
}
}
Expand All @@ -704,7 +707,7 @@ fn gen_from_subcommand(
let ty = &fields.unnamed[0];
quote! {
if let Some(res) =
<#ty as ::structopt::StructOptInternal>::from_subcommand(other)
<#ty as ::structopt::StructOptInternal>::from_subcommand(#other)
{
return Some(#name :: #variant_name (res));
}
Expand All @@ -723,7 +726,7 @@ fn gen_from_subcommand(
) -> Option<Self> {
match sub {
#( #match_arms, )*
other => {
#other => {
#( #child_subcommands )else*;
#external
}
Expand Down
20 changes: 20 additions & 0 deletions tests/regressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ fn invisible_group_issue_439() {
assert!(!help.contains("--x <x>"));
Opts::from_iter_safe(&["test", "--x"]).unwrap();
}

#[test]
fn issue_447() {
macro_rules! Command {
( $name:ident, [
#[$meta:meta] $var:ident($inner:ty)
] ) => {
#[derive(Debug, PartialEq, structopt::StructOpt)]
enum $name {
#[$meta]
$var($inner),
}
};
}

Command! {GitCmd, [
#[structopt(external_subcommand)]
Ext(Vec<String>)
]}
}
6 changes: 3 additions & 3 deletions tests/ui/external_subcommand_wrong_type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/external_subcommand_wrong_type.rs:13:15
|
13 | Other(Vec<CString>)
| ^^^^^^^ expected struct `std::ffi::CString`, found struct `std::ffi::OsString`
| ^^^^^^^ expected struct `CString`, found struct `OsString`
|
= note: expected struct `std::vec::Vec<std::ffi::CString>`
found struct `std::vec::Vec<std::ffi::OsString>`
= note: expected struct `Vec<CString>`
found struct `Vec<OsString>`
4 changes: 2 additions & 2 deletions tests/ui/non_existent_attr.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0599]: no method named `non_existing_attribute` found for struct `structopt::clap::Arg<'_, '_>` in the current scope
error[E0599]: no method named `non_existing_attribute` found for struct `Arg<'_, '_>` in the current scope
--> $DIR/non_existent_attr.rs:14:24
|
14 | #[structopt(short, non_existing_attribute = 1)]
| ^^^^^^^^^^^^^^^^^^^^^^ method not found in `structopt::clap::Arg<'_, '_>`
| ^^^^^^^^^^^^^^^^^^^^^^ method not found in `Arg<'_, '_>`
6 changes: 2 additions & 4 deletions tests/ui/skip_without_default.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
error[E0277]: the trait bound `Kind: std::default::Default` is not satisfied
error[E0277]: the trait bound `Kind: Default` is not satisfied
--> $DIR/skip_without_default.rs:22:17
|
22 | #[structopt(skip)]
| ^^^^ the trait `std::default::Default` is not implemented for `Kind`
| ^^^^ the trait `Default` is not implemented for `Kind`
|
= note: required by `std::default::Default::default`

For more information about this error, try `rustc --explain E0277`.