From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id D5D0F3858D39; Tue, 28 Feb 2023 22:36:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5D0F3858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677623773; bh=5kuieqIsbuJO0tSHJ0C9LQ2mdhCM8RqqVBe22E7tNJY=; h=From:To:Subject:Date:From; b=eJHIr0k2FkOCJ37bKth1weBrnDk9WrGwG7jZilpalC26Z247/FHzldc+W3Gb+rkRi 1MBNaK0nss6B6SbIStc42A9nIq99gjzkwL/TIqNNx5cv+2lhHCfcXNyxSMk8rriT0e KVJ3L7Vza3sZ+rq9KyAZvEVcMmz0rcOUCWj5T1XU= 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] macro_invoc_lexer: Add `split_current_token` implementation X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 53059c7e103baabf76a927e016f8cf79f412327a X-Git-Newrev: cda1d7852d6a82df6eea53deb73aab3d459b4443 Message-Id: <20230228223613.D5D0F3858D39@sourceware.org> Date: Tue, 28 Feb 2023 22:36:13 +0000 (GMT) List-Id: https://gcc.gnu.org/g:cda1d7852d6a82df6eea53deb73aab3d459b4443 commit cda1d7852d6a82df6eea53deb73aab3d459b4443 Author: Arthur Cohen Date: Tue Feb 21 13:51:54 2023 +0100 macro_invoc_lexer: Add `split_current_token` implementation gcc/rust/ChangeLog: * expand/rust-macro-invoc-lexer.cc (MacroInvocLexer::split_current_token): Add proper implementation. gcc/testsuite/ChangeLog: * rust/compile/expand_macro_qual_path_in_type.rs: New test. Diff: --- gcc/rust/expand/rust-macro-invoc-lexer.cc | 21 ++++++-- .../rust/compile/expand_macro_qual_path_in_type.rs | 62 ++++++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/gcc/rust/expand/rust-macro-invoc-lexer.cc b/gcc/rust/expand/rust-macro-invoc-lexer.cc index 321f0f97a76..66a3a6922fc 100644 --- a/gcc/rust/expand/rust-macro-invoc-lexer.cc +++ b/gcc/rust/expand/rust-macro-invoc-lexer.cc @@ -1,4 +1,5 @@ #include "rust-macro-invoc-lexer.h" +#include "rust-token.h" namespace Rust { @@ -19,12 +20,22 @@ MacroInvocLexer::skip_token (int n) } void -MacroInvocLexer::split_current_token (TokenId new_left __attribute__ ((unused)), - TokenId new_right - __attribute__ ((unused))) +MacroInvocLexer::split_current_token (TokenId new_left, TokenId new_right) { - // FIXME - gcc_unreachable (); + auto ¤t_token = token_stream.at (offs); + auto current_pos = token_stream.begin () + offs; + + auto l_tok = Token::make (new_left, current_token->get_locus ()); + auto r_tok = Token::make (new_right, current_token->get_locus ()); + + token_stream.erase (current_pos); + + // `insert` inserts before the specified position, so we insert the right one + // then the left + token_stream.insert (current_pos, + std::unique_ptr (new AST::Token (r_tok))); + token_stream.insert (current_pos, + std::unique_ptr (new AST::Token (l_tok))); } std::vector> diff --git a/gcc/testsuite/rust/compile/expand_macro_qual_path_in_type.rs b/gcc/testsuite/rust/compile/expand_macro_qual_path_in_type.rs new file mode 100644 index 00000000000..2d60197e15c --- /dev/null +++ b/gcc/testsuite/rust/compile/expand_macro_qual_path_in_type.rs @@ -0,0 +1,62 @@ +// this SEGVs in lowering for now +// { dg-additional-options "-frust-compile-until=nameresolution" } + +macro_rules! forward_ref_binop { + (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { + forward_ref_binop!(impl $imp, $method for $t, $u, + #[stable(feature = "rust1", since = "1.0.0")]); + }; + (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => { + #[$attr] + impl<'a> $imp<$u> for &'a $t { + type Output = <$t as $imp<$u>>::Output; + + #[inline] + fn $method(self, other: $u) -> <$t as $imp<$u>>::Output { + $imp::$method(*self, other) + } + } + + #[$attr] + impl<'a> $imp<&'a $u> for $t { + type Output = <$t as $imp<$u>>::Output; + + #[inline] + fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output { + $imp::$method(self, *other) + } + } + + #[$attr] + impl<'a, 'b> $imp<&'a $u> for &'b $t { + type Output = <$t as $imp<$u>>::Output; + + #[inline] + fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output { + $imp::$method(*self, *other) + } + } + } +} + +#[lang = "add"] +pub trait Add { + type Output; + + fn add(self, rhs: RHS) -> Self::Output; +} + +macro_rules! add_impl { + ($($t:ty)*) => ($( + #[stable(feature = "rust1", since = "1.0.0")] + impl Add for $t { + type Output = $t; + + fn add(self, other: $t) -> $t { self + other } + } + + forward_ref_binop! { impl Add, add for $t, $t } + )*) +} + +add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }