2023-04-25 00:08:35 +02:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
|
use rustc_ast::{
|
|
|
|
|
self as ast, Fn, FnHeader, FnSig, Generics, ItemKind, Safety, Stmt, StmtKind, TyKind,
|
2024-05-17 14:17:48 -03:00
|
|
|
};
|
2023-04-25 00:08:35 +02:00
|
|
|
use rustc_expand::base::{Annotatable, ExtCtxt};
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{Ident, Span, kw, sym};
|
2023-04-25 00:08:35 +02:00
|
|
|
use thin_vec::{ThinVec, thin_vec};
|
|
|
|
|
|
|
|
|
|
use crate::errors;
|
|
|
|
|
use crate::util::check_builtin_macro_attribute;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2024-04-26 07:56:48 +10:00
|
|
|
pub(crate) fn expand(
|
2023-04-25 00:08:35 +02:00
|
|
|
ecx: &mut ExtCtxt<'_>,
|
|
|
|
|
_span: Span,
|
|
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
|
item: Annotatable,
|
|
|
|
|
) -> Vec<Annotatable> {
|
|
|
|
|
check_builtin_macro_attribute(ecx, meta_item, sym::alloc_error_handler);
|
|
|
|
|
|
|
|
|
|
let orig_item = item.clone();
|
|
|
|
|
|
|
|
|
|
// Allow using `#[alloc_error_handler]` on an item statement
|
|
|
|
|
// FIXME - if we get deref patterns, use them to reduce duplication here
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-21 09:47:43 +11:00
|
|
|
let (item, ident, is_stmt, sig_span) = if let Annotatable::Item(item) = &item
|
2023-04-25 00:08:35 +02:00
|
|
|
&& let ItemKind::Fn(fn_kind) = &item.kind
|
|
|
|
|
{
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-21 09:47:43 +11:00
|
|
|
(item, fn_kind.ident, false, ecx.with_def_site_ctxt(fn_kind.sig.span))
|
2023-04-25 00:08:35 +02:00
|
|
|
} else if let Annotatable::Stmt(stmt) = &item
|
|
|
|
|
&& let StmtKind::Item(item) = &stmt.kind
|
|
|
|
|
&& let ItemKind::Fn(fn_kind) = &item.kind
|
|
|
|
|
{
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-21 09:47:43 +11:00
|
|
|
(item, fn_kind.ident, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
|
2023-04-25 00:08:35 +02:00
|
|
|
} else {
|
2023-12-18 20:54:03 +11:00
|
|
|
ecx.dcx().emit_err(errors::AllocErrorMustBeFn { span: item.span() });
|
2023-04-25 00:08:35 +02:00
|
|
|
return vec![orig_item];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Generate a bunch of new items using the AllocFnFactory
|
|
|
|
|
let span = ecx.with_def_site_ctxt(item.span);
|
|
|
|
|
|
|
|
|
|
// Generate item statements for the allocator methods.
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-21 09:47:43 +11:00
|
|
|
let stmts = thin_vec![generate_handler(ecx, ident, span, sig_span)];
|
2023-04-25 00:08:35 +02:00
|
|
|
|
|
|
|
|
// Generate anonymous constant serving as container for the allocator methods.
|
|
|
|
|
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
|
|
|
|
|
let const_body = ecx.expr_block(ecx.block(span, stmts));
|
|
|
|
|
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
|
|
|
|
|
let const_item = if is_stmt {
|
|
|
|
|
Annotatable::Stmt(P(ecx.stmt_item(span, const_item)))
|
|
|
|
|
} else {
|
|
|
|
|
Annotatable::Item(const_item)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Return the original item and the new methods.
|
|
|
|
|
vec![orig_item, const_item]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// #[rustc_std_internal_symbol]
|
|
|
|
|
// unsafe fn __rg_oom(size: usize, align: usize) -> ! {
|
|
|
|
|
// handler(core::alloc::Layout::from_size_align_unchecked(size, align))
|
|
|
|
|
// }
|
|
|
|
|
fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span) -> Stmt {
|
|
|
|
|
let usize = cx.path_ident(span, Ident::new(sym::usize, span));
|
|
|
|
|
let ty_usize = cx.ty_path(usize);
|
|
|
|
|
let size = Ident::from_str_and_span("size", span);
|
|
|
|
|
let align = Ident::from_str_and_span("align", span);
|
|
|
|
|
|
|
|
|
|
let layout_new = cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
|
|
|
|
|
let layout_new = cx.expr_path(cx.path(span, layout_new));
|
|
|
|
|
let layout = cx.expr_call(
|
|
|
|
|
span,
|
|
|
|
|
layout_new,
|
|
|
|
|
thin_vec![cx.expr_ident(span, size), cx.expr_ident(span, align)],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let call = cx.expr_call_ident(sig_span, handler, thin_vec![layout]);
|
|
|
|
|
|
|
|
|
|
let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
|
|
|
|
|
let params = thin_vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
|
|
|
|
|
let decl = cx.fn_decl(params, never);
|
2024-05-17 14:17:48 -03:00
|
|
|
let header = FnHeader { safety: Safety::Unsafe(span), ..FnHeader::default() };
|
2024-08-02 12:16:52 -04:00
|
|
|
let sig = FnSig { decl, header, span };
|
2023-04-25 00:08:35 +02:00
|
|
|
|
|
|
|
|
let body = Some(cx.block_expr(call));
|
|
|
|
|
let kind = ItemKind::Fn(Box::new(Fn {
|
|
|
|
|
defaultness: ast::Defaultness::Final,
|
|
|
|
|
sig,
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-21 09:47:43 +11:00
|
|
|
ident: Ident::from_str_and_span("__rg_oom", span),
|
2023-04-25 00:08:35 +02:00
|
|
|
generics: Generics::default(),
|
2025-01-08 16:38:25 -08:00
|
|
|
contract: None,
|
2023-04-25 00:08:35 +02:00
|
|
|
body,
|
2024-07-26 10:04:02 +00:00
|
|
|
define_opaque: None,
|
2023-04-25 00:08:35 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];
|
|
|
|
|
|
Move `ast::Item::ident` into `ast::ItemKind`.
`ast::Item` has an `ident` field.
- It's always non-empty for these item kinds: `ExternCrate`, `Static`,
`Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`,
`Trait`, `TraitAlias`, `MacroDef`, `Delegation`.
- It's always empty for these item kinds: `Use`, `ForeignMod`,
`GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`.
There is a similar story for `AssocItemKind` and `ForeignItemKind`.
Some sites that handle items check for an empty ident, some don't. This
is a very C-like way of doing things, but this is Rust, we have sum
types, we can do this properly and never forget to check for the
exceptional case and never YOLO possibly empty identifiers (or possibly
dummy spans) around and hope that things will work out.
The commit is large but it's mostly obvious plumbing work. Some notable
things.
- `ast::Item` got 8 bytes bigger. This could be avoided by boxing the
fields within some of the `ast::ItemKind` variants (specifically:
`Struct`, `Union`, `Enum`). I might do that in a follow-up; this
commit is big enough already.
- For the visitors: `FnKind` no longer needs an `ident` field because
the `Fn` within how has one.
- In the parser, the `ItemInfo` typedef is no longer needed. It was used
in various places to return an `Ident` alongside an `ItemKind`, but
now the `Ident` (if present) is within the `ItemKind`.
- In a few places I renamed identifier variables called `name` (or
`foo_name`) as `ident` (or `foo_ident`), to better match the type, and
because `name` is normally used for `Symbol`s. It's confusing to see
something like `foo_name.name`.
2025-03-21 09:47:43 +11:00
|
|
|
let item = cx.item(span, attrs, kind);
|
2023-04-25 00:08:35 +02:00
|
|
|
cx.stmt_item(sig_span, item)
|
|
|
|
|
}
|