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

Turn cfg_match into a builtin #116323

Closed
wants to merge 1 commit into from
Closed

Conversation

c410-f3r
Copy link
Contributor

@c410-f3r c410-f3r commented Oct 1, 2023

cc #115585

Such thing allows stuff that can't be easily done with declarative macros. For example, it is now possible to create arms without brackets.

cfg_match! {
    cfg(unix) => fn foo() -> i32 { 1 },
    _ => fn foo() -> i32 { 2 },
}

// Expands to =>

#[cfg(all(unix, not(any())))]
fn foo() -> i32 { 1 }

#[cfg(all(not(any(unix))))]
fn foo() -> i32 { 2 }

cc @petrochenkov in case you want to say something

@rustbot
Copy link
Collaborator

rustbot commented Oct 1, 2023

r? @compiler-errors

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 1, 2023
@rust-log-analyzer

This comment has been minimized.

@@ -229,35 +229,61 @@ fn ident_can_begin_type(name: Symbol, span: Span, is_raw: bool) -> bool {
#[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
pub enum TokenKind {
/* Expression-operator symbols. */
/// `=`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have often wondered about the meaning of some variants, so let's just comment what they actually are.

Copy link
Contributor

Choose a reason for hiding this comment

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

This change and the // tidy-alphabetical stuff can be submitted separately and I'll approve them right away.

Comment on lines +236 to +293
struct CfgMatchOutput(Vec<P<Item>>);

impl MacResult for CfgMatchOutput {
fn make_impl_items(self: Box<Self>) -> Option<SmallVec<[P<AssocItem>; 1]>> {
let mut rslt = SmallVec::with_capacity(self.0.len());
rslt.extend(self.0.into_iter().filter_map(|item| {
let Item { attrs, id, span, vis, ident, kind, tokens } = item.into_inner();
let ItemKind::Fn(fun) = kind else {
return None;
};
Some(P(Item { attrs, id, ident, kind: AssocItemKind::Fn(fun), span, tokens, vis }))
}));
Some(rslt)
}

fn make_items(self: Box<Self>) -> Option<SmallVec<[P<Item>; 1]>> {
Some(<_>::from(self.0))
}

fn make_stmts(self: Box<Self>) -> Option<SmallVec<[Stmt; 1]>> {
let mut rslt = SmallVec::with_capacity(self.0.len());
rslt.extend(self.0.into_iter().map(|item| {
let id = item.id;
let span = item.span;
Stmt { id, kind: StmtKind::Item(item), span }
}));
Some(rslt)
}
}
Copy link
Contributor Author

@c410-f3r c410-f3r Oct 1, 2023

Choose a reason for hiding this comment

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

Only items are acceptable as input but the output can be three different things at the current time and I don't know if there are more expected variants. This is a best effort.

let mut parser = cx.new_parser_from_tts(tts);
let mut does_not_have_wildcard = true;
let mut items = Vec::with_capacity(4);
let mut items_offsets = Vec::with_capacity(4);
Copy link
Contributor Author

@c410-f3r c410-f3r Oct 1, 2023

Choose a reason for hiding this comment

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

This auxiliary items_offsets vector is used to avoid creating Vec<Vec<P<Item>>>, i.e., all items are stored in the same contiguous items memory.

`items`: [ Items of first arm, Items of second arm, .. ]
          |_____0 to 10______| |_____10 to 25_____|    
                   |      ______________/
                   |     /
`items_offsets`: [ 10, 25, ..]

@rust-log-analyzer

This comment has been minimized.

@@ -478,6 +478,7 @@ make_MacEager! {
trait_items: SmallVec<[P<ast::AssocItem>; 1]>,
foreign_items: SmallVec<[P<ast::ForeignItem>; 1]>,
stmts: SmallVec<[ast::Stmt; 1]>,
token_stream: P<TokenStream>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary?

@petrochenkov
Copy link
Contributor

For example, it is now possible to create arms without brackets.

Why is this a goal in the first place?
I don't want to turn library functionality into compiler built-ins without really strong reasons.
No brackets doesn't look like a strong reason to me.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2023
@c410-f3r
Copy link
Contributor Author

c410-f3r commented Oct 11, 2023

For example, it is now possible to create arms without brackets.

Why is this a goal in the first place? I don't want to turn library functionality into compiler built-ins without really strong reasons. No brackets doesn't look like a strong reason to me.

  • Avoid duplicated arms (Tracking issue for cfg_match #115585 (comment))? Not sure if it is possible but surely not as a declarative macro.
  • The possibility of creating better diagnostics.
  • Easily accept expressions and/or statements and/or items as input. Declarative macros will probably require a lot of private @__ ... declarations (I guess).
  • More tools for future improvements.

Personally, I am just trying to resolve concerns in order to use this feature on stable ASAP so it is not very important if cfg_match is or isn't a builtin. You know... There are several features that have been inactive for years :/

Anyway let me know if this PR isn't really necessary.

@c410-f3r c410-f3r mentioned this pull request Oct 13, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 14, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Oct 14, 2023
Rollup merge of rust-lang#116696 - c410-f3r:in-doc, r=petrochenkov

Misc improvements

cc rust-lang#116323 (comment)

r? `@petrochenkov`
@bors
Copy link
Contributor

bors commented Oct 29, 2023

☔ The latest upstream changes (presumably #117335) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@c410-f3r any updates on this? thanks

@c410-f3r
Copy link
Contributor Author

Well, the future of this PR depends on the above conversation 👀

@JohnCSimon JohnCSimon removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 11, 2024
@JohnCSimon JohnCSimon added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Feb 11, 2024
@JohnCSimon
Copy link
Member

@c410-f3r

Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this.
Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Apr 14, 2024
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Apr 14, 2024
@c410-f3r
Copy link
Contributor Author

In regards to code, there is no missing feature or incomplete functionality so this PR is technically ready.

But it seems there is no traction so whatever 🤷

@Dylan-DPC
Copy link
Member

ah, there wasn't much waiting in the original form but there was less/no interest in this so i think closing it would be the better option here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants