about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros/src/global_allocator.rs
diff options
context:
space:
mode:
authorAaron Hill <aa1ronham@gmail.com>2021-03-25 15:40:50 -0400
committerAaron Hill <aa1ronham@gmail.com>2021-03-25 15:41:31 -0400
commit8ecd931a8eb0bb9bde2e7d35e72553b512d01c80 (patch)
treeb564308e942c85f29774402b8a1c9fad2d157db2 /compiler/rustc_builtin_macros/src/global_allocator.rs
parent6e17a5c5fd086ebe6f57216fb3ce5d1d8d6c83e5 (diff)
downloadrust-8ecd931a8eb0bb9bde2e7d35e72553b512d01c80.tar.gz
rust-8ecd931a8eb0bb9bde2e7d35e72553b512d01c80.zip
Don't ICE when using `#[global_alloc]` on a non-item statement
Fixes #83469

We need to return an `Annotatable::Stmt` if we were passed an
`Annotatable::Stmt`
Diffstat (limited to 'compiler/rustc_builtin_macros/src/global_allocator.rs')
-rw-r--r--compiler/rustc_builtin_macros/src/global_allocator.rs32
1 files changed, 16 insertions, 16 deletions
diff --git a/compiler/rustc_builtin_macros/src/global_allocator.rs b/compiler/rustc_builtin_macros/src/global_allocator.rs
index 9b43c11f0f3..a97cac7e514 100644
--- a/compiler/rustc_builtin_macros/src/global_allocator.rs
+++ b/compiler/rustc_builtin_macros/src/global_allocator.rs
@@ -14,31 +14,31 @@ pub fn expand(
     ecx: &mut ExtCtxt<'_>,
     _span: Span,
     meta_item: &ast::MetaItem,
-    mut item: Annotatable,
+    item: Annotatable,
 ) -> Vec<Annotatable> {
     check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
 
-    let not_static = |item: Annotatable| {
+    let orig_item = item.clone();
+    let not_static = || {
         ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics");
-        vec![item]
+        vec![orig_item.clone()]
     };
-    let orig_item = item.clone();
-    let mut is_stmt = false;
 
     // Allow using `#[global_allocator]` on an item statement
-    if let Annotatable::Stmt(stmt) = &item {
-        if let StmtKind::Item(item_) = &stmt.kind {
-            item = Annotatable::Item(item_.clone());
-            is_stmt = true;
-        }
-    }
-
-    let item = match item {
+    // FIXME - if we get deref patterns, use them to reduce duplication here
+    let (item, is_stmt) = match &item {
         Annotatable::Item(item) => match item.kind {
-            ItemKind::Static(..) => item,
-            _ => return not_static(Annotatable::Item(item)),
+            ItemKind::Static(..) => (item, false),
+            _ => return not_static(),
+        },
+        Annotatable::Stmt(stmt) => match &stmt.kind {
+            StmtKind::Item(item_) => match item_.kind {
+                ItemKind::Static(..) => (item_, true),
+                _ => return not_static(),
+            },
+            _ => return not_static(),
         },
-        _ => return not_static(item),
+        _ => return not_static(),
     };
 
     // Generate a bunch of new items using the AllocFnFactory