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

Invertible attributes #2393

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

neunenak
Copy link
Contributor

@neunenak neunenak commented Sep 27, 2024

This PR implements a not() syntax for certain OS-target attributes.

Resolves #1895

@neunenak neunenak force-pushed the invertable-attribute branch from 9dc142a to 1c69b18 Compare September 28, 2024 19:49
@neunenak neunenak mentioned this pull request Oct 8, 2024
@neunenak neunenak force-pushed the invertable-attribute branch 7 times, most recently from c36db49 to 148542b Compare December 18, 2024 02:01
@neunenak neunenak marked this pull request as ready for review December 18, 2024 02:01
@neunenak neunenak changed the title WIP Invertable attributes Invertible attributes Dec 18, 2024
@neunenak
Copy link
Contributor Author

@casey this is ready for review

Copy link
Owner

@casey casey left a comment

Choose a reason for hiding this comment

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

See some initial comments!

InvertedStatus::Normal
}
}
_ => return None,
Copy link
Owner

Choose a reason for hiding this comment

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

I think this function should panic if we call it with a non-invertible attribute.

};

if cfg!(target_os = "linux") {
return !(disabled.linux || disabled.unix)
Copy link
Owner

Choose a reason for hiding this comment

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

Are these unix checks redundant with the ones below? I notice that the other Unix OS's don't have the same checks.

Copy link
Owner

Choose a reason for hiding this comment

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

I think this maybe all gets easier if we have a System enum which represents the current system:

enum System {
  Windows,
  Linux,
  Unix,
  ...
}

impl System {
  fn current() -> &'static [System] {
    if cfg(target_os = "linux") {
       &[Self::Linux, Self::Unix]
    }
  }
}

And then we can handle all these checks in the same way.

src/recipe.rs Outdated
|| (cfg!(target_os = "windows") && windows)
|| (cfg!(unix) && unix)
|| (cfg!(windows) && windows)
use attribute_set::InvertedStatus;
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure I like introducing inverted status only to turn it back to a bool with matches!. I think it would probably be better to just use a bool the whole way.

src/attribute.rs Outdated
@@ -13,17 +13,17 @@ pub(crate) enum Attribute<'src> {
Doc(Option<StringLiteral<'src>>),
Extension(StringLiteral<'src>),
Group(StringLiteral<'src>),
Linux,
Macos,
Linux { inverted: bool },
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should probably use:

Suggested change
Linux { inverted: bool },
Linux { enabled: bool },

And invert the meaning of the boolean, to make the downstream code make more sense.

src/attribute.rs Outdated
AttributeDiscriminant::Private => Self::Private,
AttributeDiscriminant::Script => Self::Script({
Ok(match (inverted, discriminant) {
(inverted, AttributeDiscriminant::Linux) => Self::Linux { inverted },
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
(inverted, AttributeDiscriminant::Linux) => Self::Linux { inverted },
(inverted, AttributeDiscriminant::Linux) => Self::Linux { enabled: !inverted },

@@ -1135,7 +1135,18 @@ impl<'run, 'src> Parser<'run, 'src> {
token.get_or_insert(bracket);

loop {
let name = self.parse_name()?;
let (name, inverted) = {
Copy link
Owner

Choose a reason for hiding this comment

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

I think this is probably cleaner than using mutable variables:

Suggested change
let (name, inverted) = {
let (name, inverted) = {
let name = self.parse_name()?;
if name.lexeme() == "not" {
self.expect(ParenL)?;
let name = self.parse_name()?;
(name, true)
} else {
(name, false)
}
}

Copy link
Owner

Choose a reason for hiding this comment

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

And I think we should actually, since we don't have any invertable variables which aren't system variables, change the argument to Attribute::new to be !inverted, and within Attribute::new it's called enabled.

@neunenak neunenak force-pushed the invertable-attribute branch from 7d30976 to 78c67e2 Compare December 24, 2024 04:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow inverted cfg attributes, like [not(macos)]
2 participants