-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-langRelevant to the language teamRelevant to the language team
Description
I tried this code:
macro match and echo non const X: Y
pattern is fine.
macro_rules! echo1 {
(pub type $ident:ident<$($gi:ident),*> = $($tt:tt)*) => {
pub type $ident<$($gi),*> = $($tt)*;
};
}
echo1!(pub type Foo1<T, N> = (T, N));
But within current rust desgin, to let declarative macro match lookahead within rust grammer syntax const X: Y
-
or input rust code, and do lookahead simulation
-
or input lookahead free dsl, and output lookahead code
macro_rules! echo2 {
(@derive_foo pub type $ident:ident<$($gi:ident $(lookahead_qualifier=$gq:tt)? $(: $gt:ty)?),*> = $($tt:tt)*) => {
pub type $ident<$($($gq)? $gi $(: $gt)?),*> = $($tt)*;
};
(@derive_bar pub type $ident:ident<$($gi:ident $(lookahead_qualifier=$gq:tt)? $(: $gt:ty)?),*> = $($tt:tt)*) => {
pub type $ident<$($($gq)? $gi $(: $gt)?),*> = $($tt)*;
};
(@lookahead_workaround pub type $ident:ident<$($gi:ident $(lookahead_qualifier=$gq:tt)? $(: $gt:ty)?),*> = $($tt:tt)*) => {
pub type $ident<$($($gq)? $gi $(: $gt)?),*> = $($tt)*;
};
(pub type $ident:ident<$($(const)? $gi:ident $(: $gt:ty)?),*> = $($tt:tt)*) => {
pub type $ident<$($(const)? $gi $(: $gt)?),*> = $($tt)*;
};
}
// // TODO: https://github.com/rust-lang/rust/issues/130928
// echo2!(pub type Foo2<T, const N: usize> = [T; N]);
echo2!(@lookahead_workaround pub type Foo2<T, N lookahead_qualifier=const: usize> = [T; N]);
I expected to see this happen:
- Neat way to match lookahead within rust grammer syntax
const X: Y
Instead, this happened:
-
Need to simulate lookahead within rust grammer syntax
const X: Y
-
Leads to ugly simulation code.
-
Thus I think it's a design bug.
Meta
rustc --version --verbose
:
rustc 1.83.0-nightly (9b72238eb 2024-09-14)
binary: rustc
commit-hash: 9b72238eb813e9d06e9e9d270168512fbffd7ee7
commit-date: 2024-09-14
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
Backtrace
<backtrace>
related
Also found similar issue back to 2021 in pin-project-lite
.
So, I guess there is no way yet.
Metadata
Metadata
Assignees
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.T-langRelevant to the language teamRelevant to the language team