-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
rustc_hir: add Expr! pattern macro and try it out in a couple places. #68320
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1330,6 +1330,18 @@ pub struct Expr<'hir> { | |
pub span: Span, | ||
} | ||
|
||
/// `Expr` pattern alias to facilitate pattern-matching. | ||
/// | ||
/// Usage examples: | ||
/// * `hir::Expr!(Unary(_, hir::Expr!(Lit(_))))` | ||
/// * `hir::Expr! { Loop(..), hir_id: loop_id }` | ||
pub macro Expr($variant:ident $($rest:tt)*) { | ||
$crate::hir::Expr { | ||
kind: $crate::hir::ExprKind::$variant $($rest)*, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to be conservative, presumably I can even write |
||
.. | ||
} | ||
} | ||
|
||
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger. | ||
#[cfg(target_arch = "x86_64")] | ||
rustc_data_structures::static_assert_size!(Expr<'static>, 64); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use
pat
fragment?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because then people would have to write
hir::ExprKind::Foo
instead ofFoo
, inside the macro arguments.And also this allows some trickery like matching on the
hir_id
(although maybe that should be done withEDIT: nevermind,@
instead of this@
needs a binding on one side, it's not a conjunction of patterns).