From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id C50F0383906D; Tue, 30 Aug 2022 13:42:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C50F0383906D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661866977; bh=X2unhpUVEoYfzEyLRhfgmGpmWBagPSRPwYUEqT0VT7M=; h=From:To:Subject:Date:From; b=mU8zCed463doC7ew+iKGNikoSJLHHfNOx2P1NeSxX7A622Nfwo0yJ8FQyzHEl4/3b 2JirvJsmHNDxtBqr2r9ie4dz0cwS63Cyh+qbH4XRDlHZfIw3oi8jQcYkVo8rTDqn4Y tQ4bhuRxhR8IQh1ZTK+evhtYPkZNR9sh4/eF2wTk= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] macros: Handle matchers properly in repetitions X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 64c7a90934f03630356c76d7aa9ba6978ee4e9e6 X-Git-Newrev: 32be5443e30189c9236c62a864e6549804860977 Message-Id: <20220830134257.C50F0383906D@sourceware.org> Date: Tue, 30 Aug 2022 13:42:57 +0000 (GMT) List-Id: https://gcc.gnu.org/g:32be5443e30189c9236c62a864e6549804860977 commit 32be5443e30189c9236c62a864e6549804860977 Author: Arthur Cohen Date: Mon Aug 29 12:51:33 2022 +0200 macros: Handle matchers properly in repetitions Diff: --- gcc/rust/expand/rust-macro-expand.cc | 14 +++++--- gcc/rust/expand/rust-macro-expand.h | 2 +- gcc/testsuite/rust/compile/macro43.rs | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index 1d57e394220..a372e81affe 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -435,7 +435,7 @@ MacroExpander::match_fragment (Parser &parser, bool MacroExpander::match_matcher (Parser &parser, - AST::MacroMatcher &matcher) + AST::MacroMatcher &matcher, bool in_repetition) { if (depth_exceeds_recursion_limit ()) { @@ -485,8 +485,12 @@ MacroExpander::match_matcher (Parser &parser, // matched fragment get the offset in the token stream size_t offs_end = source.get_offs (); - sub_stack.insert_metavar ( - MatchedFragment (fragment->get_ident (), offs_begin, offs_end)); + if (in_repetition) + sub_stack.append_fragment ( + MatchedFragment (fragment->get_ident (), offs_begin, offs_end)); + else + sub_stack.insert_metavar ( + MatchedFragment (fragment->get_ident (), offs_begin, offs_end)); } break; @@ -509,7 +513,7 @@ MacroExpander::match_matcher (Parser &parser, AST::MacroMatcher *m = static_cast (match.get ()); expansion_depth++; - if (!match_matcher (parser, *m)) + if (!match_matcher (parser, *m, in_repetition)) { expansion_depth--; return false; @@ -619,7 +623,7 @@ MacroExpander::match_n_matches (Parser &parser, case AST::MacroMatch::MacroMatchType::Matcher: { AST::MacroMatcher *m = static_cast (match.get ()); - valid_current_match = match_matcher (parser, *m); + valid_current_match = match_matcher (parser, *m, true); } break; } diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 94d6702ecb8..c324c0ee019 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -273,7 +273,7 @@ struct MacroExpander AST::MacroMatchRepetition &rep); bool match_matcher (Parser &parser, - AST::MacroMatcher &matcher); + AST::MacroMatcher &matcher, bool in_repetition = false); /** * Match any amount of matches diff --git a/gcc/testsuite/rust/compile/macro43.rs b/gcc/testsuite/rust/compile/macro43.rs new file mode 100644 index 00000000000..c7bf50a030e --- /dev/null +++ b/gcc/testsuite/rust/compile/macro43.rs @@ -0,0 +1,64 @@ +macro_rules! nonzero_integers { + ( $( $Ty: ident($Int: ty); )+ ) => { + $( + /// An integer that is known not to equal zero. + /// + /// This enables some memory layout optimization. + /// For example, `Option` is the same size as `u32`: + /// + /// ```rust + /// use std::mem::size_of; + /// assert_eq!(size_of::>(), size_of::()); + /// ``` + #[stable(feature = "nonzero", since = "1.28.0")] + #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] + #[repr(transparent)] + pub struct $Ty(NonZero<$Int>); + + impl $Ty { + /// Create a non-zero without checking the value. + /// + /// # Safety + /// + /// The value must not be zero. + #[stable(feature = "nonzero", since = "1.28.0")] + #[inline] + pub const unsafe fn new_unchecked(n: $Int) -> Self { + $Ty(NonZero(n)) + } + + /// Create a non-zero if the given value is not zero. + #[stable(feature = "nonzero", since = "1.28.0")] + #[inline] + pub fn new(n: $Int) -> Option { + if n != 0 { + Some($Ty(NonZero(n))) + } else { + None + } + } + + /// Returns the value as a primitive type. + #[stable(feature = "nonzero", since = "1.28.0")] + #[inline] + pub fn get(self) -> $Int { + self.0 .0 + } + + } + + impl_nonzero_fmt! { // { dg-error "unknown macro" } + (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty + } + )+ + } +} + +nonzero_integers! { + NonZeroU8(u8); + NonZeroU16(u16); + NonZeroU32(u32); + NonZeroU64(u64); + NonZeroU128(u128); + NonZeroUsize(usize); +}