Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't ICE when using #[global_alloc] on a non-item statement #83486

Merged
merged 1 commit into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions compiler/rustc_builtin_macros/src/global_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/proc-macro/issue-83469-global-alloc-invalid-stmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Regression test for issue #83469
// Ensures that we recover from `#[global_alloc]` on an invalid
// stmt without an ICE

fn outer() {
#[global_allocator]
fn inner() {} //~ ERROR allocators must be statics
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: allocators must be statics
--> $DIR/issue-83469-global-alloc-invalid-stmt.rs:7:5
|
LL | fn inner() {}
| ^^^^^^^^^^^^^

error: aborting due to previous error