From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 5D3DB385B509 for ; Tue, 31 Jan 2023 13:20:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D3DB385B509 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x432.google.com with SMTP id a3so7488728wrt.6 for ; Tue, 31 Jan 2023 05:20:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=GGxZWozWwqq2AhQyevyDtMPe32b7ExQ+RdpNtNc8z+8=; b=N4XZOzUUGf0JQ+gj6Sp16GwYAvMiX5oGYN0rEccpcK3tR7Bi6HLaOU92egKcMDnqGw ACTAW0TLkj0xmKR76PO4+kB7Wnhehfa8fMyoGrAuZaLlI2Eo7IMPAUGYii5m9huE9L3h XdbUeyMWvXu1ra83mJWlWdMMJiTiBXeLwCA/ZPh5+b0sSQirteEqbOgEXIyOSL7wP7ps HX/jkaetFcbRHfcebhYieAIr3EpLdxF2edVGahC33ifSXMK94C++yhCPMqeB83HaG6Jn 3oPCZU0lOfh9MCdFdH/jAYp8pcxVr6F5tyRY2dEkbB+G0pTJ5ViMHpt5JeKGkfGuIvCM 2rtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=GGxZWozWwqq2AhQyevyDtMPe32b7ExQ+RdpNtNc8z+8=; b=rAqnIG2/f3fW/N66IVL42g1wjyryVAlJ4G8Hvkel8tkKOk1uJNCuZW/XL+Vk3/BPm6 6Dl102B0iZPYh+6XNXWF55Akilu0IK/2FYQU6HA4EE9eiycLnwyYTFcYERuXrsU3/TBR XO0QtEkoSoPgv50Fv9jBs7MYeRl3o7DbNYtL9XTbqjaKsBTv5GB8Xs1ovBbdzjo4DtOJ QAcnqvBrKiwpFQH8BachZjUod88hUmZlHaTruc4rhAIFPmaHterBo2SCQPYBTzFFa/ix CuWjTHoBTJLWOcd2/wPb3m06Khvi0oIKZ9PYQEddf4bSJMOtF8YXaS03scDHU5VmarNu Q/zg== X-Gm-Message-State: AO0yUKX/Sx+vGcS4DiazGAdwHhhtYc7Wj7ZVZ9PTh/v1B/QFlE+PkCID UK3a7qu3DB5tPTjBt/XFB/KB X-Google-Smtp-Source: AK7set/Tj9DQTKTS927OsLCSmbJpwuYS6/eSNomK5FS4JO0Phnd8s3MbVsLxdicASVXb3d87zJKTaQ== X-Received: by 2002:a05:6000:54a:b0:2bf:ec0f:af79 with SMTP id b10-20020a056000054a00b002bfec0faf79mr7051382wrf.63.1675171256030; Tue, 31 Jan 2023 05:20:56 -0800 (PST) Received: from platypus.lan ([2001:861:5e4c:3bb0:6424:328a:1734:3249]) by smtp.gmail.com with ESMTPSA id c17-20020adffb11000000b002bc8130cca7sm14812569wrr.23.2023.01.31.05.20.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Jan 2023 05:20:55 -0800 (PST) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [COMMITTED] gccrs: macros: Handle matchers properly in repetitions Date: Tue, 31 Jan 2023 14:24:41 +0100 Message-Id: <20230131132441.661608-1-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-15.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: gcc/rust/ChangeLog: * expand/rust-macro-expand.cc (MacroExpander::match_matcher): Handle fragments differently based on whether or not we are currently trying to match a matcher in a repetition context. (MacroExpander::match_n_matches): Use new `in_repetition` argument properly when calling `match_matcher`. * expand/rust-macro-expand.h (MacroExpander::match_matcher): Allow passing extra `in_repetition` bool argument gcc/testsuite/ChangeLog: * rust/compile/macro43.rs: New test. Tested on x86_64-pc-linux-gnu, committed on master. --- 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(-) create mode 100644 gcc/testsuite/rust/compile/macro43.rs diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index a214ca906bf..df258bd96ec 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 97a02692d1f..bef140236b3 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); +} -- 2.39.1