]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/alloc_error_handler.rs
Rollup merge of #104404 - GuillaumeGomez:fix-missing-minification, r=notriddle
[rust.git] / compiler / rustc_builtin_macros / src / alloc_error_handler.rs
1 use crate::util::check_builtin_macro_attribute;
2
3 use rustc_ast::ptr::P;
4 use rustc_ast::{self as ast, FnHeader, FnSig, Generics, StmtKind};
5 use rustc_ast::{Fn, ItemKind, Stmt, TyKind, Unsafe};
6 use rustc_expand::base::{Annotatable, ExtCtxt};
7 use rustc_span::symbol::{kw, sym, Ident};
8 use rustc_span::Span;
9 use thin_vec::thin_vec;
10
11 pub fn expand(
12     ecx: &mut ExtCtxt<'_>,
13     _span: Span,
14     meta_item: &ast::MetaItem,
15     item: Annotatable,
16 ) -> Vec<Annotatable> {
17     check_builtin_macro_attribute(ecx, meta_item, sym::alloc_error_handler);
18
19     let orig_item = item.clone();
20     let not_function = || {
21         ecx.sess
22             .parse_sess
23             .span_diagnostic
24             .span_err(item.span(), "alloc_error_handler must be a function");
25         vec![orig_item.clone()]
26     };
27
28     // Allow using `#[alloc_error_handler]` on an item statement
29     // FIXME - if we get deref patterns, use them to reduce duplication here
30     let (item, is_stmt, sig_span) = match &item {
31         Annotatable::Item(item) => match item.kind {
32             ItemKind::Fn(ref fn_kind) => (item, false, ecx.with_def_site_ctxt(fn_kind.sig.span)),
33             _ => return not_function(),
34         },
35         Annotatable::Stmt(stmt) => match &stmt.kind {
36             StmtKind::Item(item_) => match item_.kind {
37                 ItemKind::Fn(ref fn_kind) => {
38                     (item_, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
39                 }
40                 _ => return not_function(),
41             },
42             _ => return not_function(),
43         },
44         _ => return not_function(),
45     };
46
47     // Generate a bunch of new items using the AllocFnFactory
48     let span = ecx.with_def_site_ctxt(item.span);
49
50     // Generate item statements for the allocator methods.
51     let stmts = vec![generate_handler(ecx, item.ident, span, sig_span)];
52
53     // Generate anonymous constant serving as container for the allocator methods.
54     let const_ty = ecx.ty(sig_span, TyKind::Tup(Vec::new()));
55     let const_body = ecx.expr_block(ecx.block(span, stmts));
56     let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
57     let const_item = if is_stmt {
58         Annotatable::Stmt(P(ecx.stmt_item(span, const_item)))
59     } else {
60         Annotatable::Item(const_item)
61     };
62
63     // Return the original item and the new methods.
64     vec![orig_item, const_item]
65 }
66
67 // #[rustc_std_internal_symbol]
68 // unsafe fn __rg_oom(size: usize, align: usize) -> ! {
69 //     handler(core::alloc::Layout::from_size_align_unchecked(size, align))
70 // }
71 fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span) -> Stmt {
72     let usize = cx.path_ident(span, Ident::new(sym::usize, span));
73     let ty_usize = cx.ty_path(usize);
74     let size = Ident::from_str_and_span("size", span);
75     let align = Ident::from_str_and_span("align", span);
76
77     let layout_new = cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
78     let layout_new = cx.expr_path(cx.path(span, layout_new));
79     let layout =
80         cx.expr_call(span, layout_new, vec![cx.expr_ident(span, size), cx.expr_ident(span, align)]);
81
82     let call = cx.expr_call_ident(sig_span, handler, vec![layout]);
83
84     let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
85     let params = vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
86     let decl = cx.fn_decl(params, never);
87     let header = FnHeader { unsafety: Unsafe::Yes(span), ..FnHeader::default() };
88     let sig = FnSig { decl, header, span: span };
89
90     let body = Some(cx.block_expr(call));
91     let kind = ItemKind::Fn(Box::new(Fn {
92         defaultness: ast::Defaultness::Final,
93         sig,
94         generics: Generics::default(),
95         body,
96     }));
97
98     let special = sym::rustc_std_internal_symbol;
99     let special = cx.meta_word(span, special);
100     let attrs = thin_vec![cx.attribute(special)];
101
102     let item = cx.item(span, Ident::from_str_and_span("__rg_oom", span), attrs, kind);
103     cx.stmt_item(sig_span, item)
104 }