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

Structs that implement Parser from clap::Parser throw an error no-such-field #13904

Closed
lever1209 opened this issue Jan 7, 2023 · 8 comments
Closed
Labels
A-diagnostics diagnostics / error reporting A-proc-macro proc macro C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now

Comments

@lever1209
Copy link

rust-analyzer version: 0.4.1352-standalone

rustc version: rustc 1.66.0 (69f9c33d7 2022-12-12)

relevant settings:

{
	"rust-analyzer.files.excludeDirs": [
		"target"
	],
	"rust-analyzer.cargo.features": ["interactive-cli", "debug"]
}
[dependencies]
clap = { version = "4.0.32", features = ["derive"] }
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
	#[arg(action = ArgAction::Set, id = "config_path", help = "path to an alternate config file.", short, long)]
	config: String,

	#[cfg(feature = "linux-qt")]
	#[arg(action = ArgAction::SetTrue, long = "qt")]
	use_qt: bool,

	#[cfg(feature = "linux-gtk")]
	#[arg(action = ArgAction::SetTrue, long = "gtk")]
	use_gtk: bool,

	#[cfg(any(
		feature = "linux-gtk",
		feature = "linux-qt",
		feature = "windows-uwp",
		feature = "windows-native"
	))]
	#[arg(action = ArgAction::SetTrue, long = "nogui")]
	nogui: bool,
}
@Veykril Veykril added A-diagnostics diagnostics / error reporting A-proc-macro proc macro labels Jan 7, 2023
@lnicola
Copy link
Member

lnicola commented Jan 7, 2023

You haven't said which field is missing. Does config work?

@lever1209
Copy link
Author

use_qt, use_gtk, and nogui, anything that is currently disabled because of features

@lnicola
Copy link
Member

lnicola commented Jan 7, 2023

You'll have to enable those features for the code to be visible.

@lever1209
Copy link
Author

im fully aware, just replicate it and see what i mean

@lowr
Copy link
Contributor

lowr commented Jan 9, 2023

Given

#[derive(clap::Parser)]
struct Args {
    #[cfg(feature = "linux-qt")]
	use_qt: bool,
}

we expand the derive macro to

#[deny(clippy::correctness)]
impl clap::FromArgMatches for Args {
    // ...
    fn from_arg_matches_mut(
        __clap_arg_matches: &mut clap::ArgMatches,
    ) -> ::std::result::Result<Self, clap::Error> {
        #![allow(deprecated)]
        let v = Args {
            use_qt: /* ... */
        };
        ::std::result::Result::Ok(v)
    }
    // ...
}

which emits no-such-field. We should cfg out fields but I'm not sure when that should happen (before proc macro invocation or after that?).

@lowr lowr added S-actionable Someone could pick this issue up and work on it right now C-bug Category: bug labels Jan 9, 2023
@Veykril
Copy link
Member

Veykril commented Jan 9, 2023

This seems to be #8434 if I am not mistaken (but for attributes, not derives)

@rbtcollins
Copy link

Similar case I think?

#[derive(Parser)]
struct Cli {
    #[clap(long, short, value_delimiter = ',', num_args=1..)]
    things: Vec<ThingName>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum ThingName {
    #[cfg(feature = "test")]
    SomeThing
}

will error in rust-analyzer unless the feature is enabled

bors added a commit that referenced this issue Mar 12, 2024
cfg Attribute Stripping for Proc Macro Expansion

This will attempt to process cfg attributes and cfg_attr attributes for proc macro expansion.
![image](https://github.com/rust-lang/rust-analyzer/assets/11785959/b85ef203-14a5-44c9-9b67-59a65a5f2d96)

Closes #8434 , #11657, and #13904
@Veykril
Copy link
Member

Veykril commented Mar 12, 2024

Fixed in #16789

@Veykril Veykril closed this as completed Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics diagnostics / error reporting A-proc-macro proc macro C-bug Category: bug S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

5 participants