public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] macro: Add assertion to avoid undefined behavior Date: Wed, 8 Jun 2022 12:45:50 +0000 (GMT) [thread overview] Message-ID: <20220608124550.628D3381D494@sourceware.org> (raw) https://gcc.gnu.org/g:2959ff8e7dd8b06bee76636af84c5aee06971397 commit 2959ff8e7dd8b06bee76636af84c5aee06971397 Author: Arthur Cohen <arthur.cohen@embecosm.com> Date: Tue May 10 15:10:59 2022 +0200 macro: Add assertion to avoid undefined behavior This also fixes the undefined behavior. Once again we are hurt by `std::vector<T>::back()` returning references and not pointers/`std::optional<T>`s! The cause of the bug was some overzealous popping from the context vector in block expressions. The amount of calls to `pop_context` is now the same as the amount of calls to `push_context` Diff: --- gcc/rust/expand/rust-attribute-visitor.cc | 2 -- gcc/rust/expand/rust-macro-expand.h | 3 +++ gcc/testsuite/rust/compile/macro-issue1233.rs | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc index 771f034a5bb..c7c6867dd10 100644 --- a/gcc/rust/expand/rust-attribute-visitor.cc +++ b/gcc/rust/expand/rust-attribute-visitor.cc @@ -1193,7 +1193,6 @@ AttrVisitor::visit (AST::BlockExpr &expr) if (expander.fails_cfg_with_expand (expr.get_outer_attrs ())) { expr.mark_for_strip (); - expander.pop_context (); return; } @@ -1203,7 +1202,6 @@ AttrVisitor::visit (AST::BlockExpr &expr) if (expander.fails_cfg_with_expand (expr.get_inner_attrs ())) { expr.mark_for_strip (); - expander.pop_context (); return; } diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h index 3c53d8d099f..a5825249f33 100644 --- a/gcc/rust/expand/rust-macro-expand.h +++ b/gcc/rust/expand/rust-macro-expand.h @@ -275,8 +275,11 @@ struct MacroExpander ContextType pop_context () { + rust_assert (!context.empty ()); + ContextType t = context.back (); context.pop_back (); + return t; } diff --git a/gcc/testsuite/rust/compile/macro-issue1233.rs b/gcc/testsuite/rust/compile/macro-issue1233.rs new file mode 100644 index 00000000000..d762bb7fb9e --- /dev/null +++ b/gcc/testsuite/rust/compile/macro-issue1233.rs @@ -0,0 +1,22 @@ +// { dg-additional-options "-w" } + +macro_rules! impl_uint { + ($($ty:ident = $lang:literal),*) => { + $( + impl $ty { + pub fn to_le(self) -> Self { + #[cfg(not(target_endian = "little"))] + { + self + } + #[cfg(target_endian = "little")] + { + self + } + } + } + )* + } +} + +impl_uint!(u8 = "u8", u16 = "u16", u32 = "u32");
reply other threads:[~2022-06-08 12:45 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220608124550.628D3381D494@sourceware.org \ --to=tschwinge@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).