aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-05-10 17:17:08 +0000
committerGitHub <noreply@github.com>2022-05-10 17:17:08 +0000
commitc1ff79996acfba45a50f181f9d3be1625cdef8f6 (patch)
treed99b71a754b5c9bc70d7a8ac6b7783f0f535d773
parent03c21a08eb40b71e67da0c8ae83ed2c645fb76a3 (diff)
parent2959ff8e7dd8b06bee76636af84c5aee06971397 (diff)
downloadgcc-c1ff79996acfba45a50f181f9d3be1625cdef8f6.zip
gcc-c1ff79996acfba45a50f181f9d3be1625cdef8f6.tar.gz
gcc-c1ff79996acfba45a50f181f9d3be1625cdef8f6.tar.bz2
Merge #1242
1242: Remove undefined behavior in context vector r=CohenArthur a=CohenArthur 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` Closes #1233 Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
-rw-r--r--gcc/rust/expand/rust-attribute-visitor.cc2
-rw-r--r--gcc/rust/expand/rust-macro-expand.h3
-rw-r--r--gcc/testsuite/rust/compile/macro-issue1233.rs22
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 771f034..c7c6867 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 3c53d8d..a582524 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 0000000..d762bb7
--- /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");