Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c34424

Browse files
authoredMar 25, 2021
Rollup merge of #83486 - Aaron1011:fix/global-alloc-error, r=petrochenkov
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`
2 parents 85b0449 + 8ecd931 commit 3c34424

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed
 

‎compiler/rustc_builtin_macros/src/global_allocator.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ pub fn expand(
1414
ecx: &mut ExtCtxt<'_>,
1515
_span: Span,
1616
meta_item: &ast::MetaItem,
17-
mut item: Annotatable,
17+
item: Annotatable,
1818
) -> Vec<Annotatable> {
1919
check_builtin_macro_attribute(ecx, meta_item, sym::global_allocator);
2020

21-
let not_static = |item: Annotatable| {
21+
let orig_item = item.clone();
22+
let not_static = || {
2223
ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "allocators must be statics");
23-
vec![item]
24+
vec![orig_item.clone()]
2425
};
25-
let orig_item = item.clone();
26-
let mut is_stmt = false;
2726

2827
// Allow using `#[global_allocator]` on an item statement
29-
if let Annotatable::Stmt(stmt) = &item {
30-
if let StmtKind::Item(item_) = &stmt.kind {
31-
item = Annotatable::Item(item_.clone());
32-
is_stmt = true;
33-
}
34-
}
35-
36-
let item = match item {
28+
// FIXME - if we get deref patterns, use them to reduce duplication here
29+
let (item, is_stmt) = match &item {
3730
Annotatable::Item(item) => match item.kind {
38-
ItemKind::Static(..) => item,
39-
_ => return not_static(Annotatable::Item(item)),
31+
ItemKind::Static(..) => (item, false),
32+
_ => return not_static(),
33+
},
34+
Annotatable::Stmt(stmt) => match &stmt.kind {
35+
StmtKind::Item(item_) => match item_.kind {
36+
ItemKind::Static(..) => (item_, true),
37+
_ => return not_static(),
38+
},
39+
_ => return not_static(),
4040
},
41-
_ => return not_static(item),
41+
_ => return not_static(),
4242
};
4343

4444
// Generate a bunch of new items using the AllocFnFactory
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Regression test for issue #83469
2+
// Ensures that we recover from `#[global_alloc]` on an invalid
3+
// stmt without an ICE
4+
5+
fn outer() {
6+
#[global_allocator]
7+
fn inner() {} //~ ERROR allocators must be statics
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: allocators must be statics
2+
--> $DIR/issue-83469-global-alloc-invalid-stmt.rs:7:5
3+
|
4+
LL | fn inner() {}
5+
| ^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)
Please sign in to comment.