Skip to content

Commit 8a47602

Browse files
committed
Tweak alloc_error_handler desugaring
1 parent b54ba21 commit 8a47602

6 files changed

+55
-44
lines changed

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use thin_vec::{thin_vec, ThinVec};
1010

1111
pub fn expand(
1212
ecx: &mut ExtCtxt<'_>,
13-
span: Span,
13+
_span: Span,
1414
meta_item: &ast::MetaItem,
1515
item: Annotatable,
1616
) -> Vec<Annotatable> {
@@ -20,29 +20,29 @@ pub fn expand(
2020

2121
// Allow using `#[alloc_error_handler]` on an item statement
2222
// FIXME - if we get deref patterns, use them to reduce duplication here
23-
let (item, is_stmt, sig_span) =
23+
let (item, is_stmt, sig) =
2424
if let Annotatable::Item(item) = &item
2525
&& let ItemKind::Fn(fn_kind) = &item.kind
2626
{
27-
(item, false, ecx.with_def_site_ctxt(fn_kind.sig.span))
27+
(item, false, &fn_kind.sig)
2828
} else if let Annotatable::Stmt(stmt) = &item
2929
&& let StmtKind::Item(item) = &stmt.kind
3030
&& let ItemKind::Fn(fn_kind) = &item.kind
3131
{
32-
(item, true, ecx.with_def_site_ctxt(fn_kind.sig.span))
32+
(item, true, &fn_kind.sig)
3333
} else {
3434
ecx.sess.parse_sess.span_diagnostic.span_err(item.span(), "alloc_error_handler must be a function");
3535
return vec![orig_item];
3636
};
3737

3838
// Generate a bunch of new items using the AllocFnFactory
39-
let span = ecx.with_def_site_ctxt(span);
39+
let span = ecx.with_def_site_ctxt(item.span);
4040

4141
// Generate item statements for the allocator methods.
42-
let stmts = thin_vec![generate_handler(ecx, item.ident, span, sig_span)];
42+
let stmts = thin_vec![generate_handler(ecx, item.ident, span, sig)];
4343

4444
// Generate anonymous constant serving as container for the allocator methods.
45-
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
45+
let const_ty = ecx.ty(ecx.with_def_site_ctxt(sig.span), TyKind::Tup(ThinVec::new()));
4646
let const_body = ecx.expr_block(ecx.block(span, stmts));
4747
let const_item = ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, const_body);
4848
let const_item = if is_stmt {
@@ -59,27 +59,31 @@ pub fn expand(
5959
// unsafe fn __rg_oom(size: usize, align: usize) -> ! {
6060
// handler(core::alloc::Layout::from_size_align_unchecked(size, align))
6161
// }
62-
fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span) -> Stmt {
62+
fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig: &FnSig) -> Stmt {
6363
let usize = cx.path_ident(span, Ident::new(sym::usize, span));
6464
let ty_usize = cx.ty_path(usize);
6565
let size = Ident::from_str_and_span("size", span);
6666
let align = Ident::from_str_and_span("align", span);
6767

68+
let sig_span = cx.with_def_site_ctxt(sig.span);
69+
let ret_sp = cx.with_def_site_ctxt(sig.decl.output.span());
70+
71+
// core::alloc::Layout::from_size_align_unchecked(size, align)
6872
let layout_new = cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
6973
let layout_new = cx.expr_path(cx.path(span, layout_new));
7074
let layout = cx.expr_call(
71-
span,
75+
cx.with_def_site_ctxt(sig.decl.inputs.get(0).map_or(span, |p| p.span)),
7276
layout_new,
7377
thin_vec![cx.expr_ident(span, size), cx.expr_ident(span, align)],
7478
);
75-
76-
let call = cx.expr_call_ident(sig_span, handler, thin_vec![layout]);
79+
let call =
80+
cx.expr(ret_sp, ast::ExprKind::Call(cx.expr_ident(span, handler), thin_vec![layout]));
7781

7882
let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
7983
let params = thin_vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
8084
let decl = cx.fn_decl(params, never);
8185
let header = FnHeader { unsafety: Unsafe::Yes(span), ..FnHeader::default() };
82-
let sig = FnSig { decl, header, span: span };
86+
let sig = FnSig { decl, header, span };
8387

8488
let body = Some(cx.block_expr(call));
8589
let kind = ItemKind::Fn(Box::new(Fn {

tests/ui/alloc-error/alloc-error-handler-bad-signature-1.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use core::alloc::Layout;
88

9-
#[alloc_error_handler] //~ ERROR mismatched types
10-
fn oom( //~ ERROR mismatched types
11-
info: &Layout,
12-
) -> ()
9+
#[alloc_error_handler]
10+
fn oom(
11+
info: &Layout, //~ ERROR mismatched types
12+
) -> () //~ ERROR mismatched types
1313
{
1414
loop {}
1515
}

tests/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
error[E0308]: mismatched types
2-
--> $DIR/alloc-error-handler-bad-signature-1.rs:9:1
2+
--> $DIR/alloc-error-handler-bad-signature-1.rs:11:5
33
|
44
LL | #[alloc_error_handler]
5-
| ^^^^^^^^^^^^^^^^^^^^^^ expected `&Layout`, found `Layout`
5+
| ---------------------- in this procedural macro expansion
66
LL | / fn oom(
77
LL | | info: &Layout,
8+
| | ^^^^^^^^^^^^^ expected `&Layout`, found `Layout`
89
LL | | ) -> ()
9-
| |_______- arguments to this function are incorrect
10+
LL | | {
11+
LL | | loop {}
12+
LL | | }
13+
| |_- arguments to this function are incorrect
1014
|
1115
note: function defined here
1216
--> $DIR/alloc-error-handler-bad-signature-1.rs:10:4
@@ -18,17 +22,18 @@ LL | info: &Layout,
1822
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
1923

2024
error[E0308]: mismatched types
21-
--> $DIR/alloc-error-handler-bad-signature-1.rs:10:1
25+
--> $DIR/alloc-error-handler-bad-signature-1.rs:12:6
2226
|
2327
LL | #[alloc_error_handler]
24-
| ----------------------
25-
| |
26-
| expected `!` because of return type
27-
| in this procedural macro expansion
28+
| ---------------------- in this procedural macro expansion
2829
LL | / fn oom(
2930
LL | | info: &Layout,
3031
LL | | ) -> ()
31-
| |_______^ expected `!`, found `()`
32+
| | ^^ expected `!`, found `()`
33+
LL | | {
34+
LL | | loop {}
35+
LL | | }
36+
| |_- expected `!` because of return type
3237
|
3338
= note: expected type `!`
3439
found unit type `()`

tests/ui/alloc-error/alloc-error-handler-bad-signature-2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
struct Layout;
88

9-
#[alloc_error_handler] //~ ERROR mismatched types
10-
fn oom( //~ ERROR mismatched types
11-
info: Layout,
12-
) {
9+
#[alloc_error_handler]
10+
fn oom(
11+
info: Layout, //~ ERROR mismatched types
12+
) { //~ ERROR mismatched types
1313
loop {}
1414
}
1515

tests/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
error[E0308]: mismatched types
2-
--> $DIR/alloc-error-handler-bad-signature-2.rs:9:1
2+
--> $DIR/alloc-error-handler-bad-signature-2.rs:11:5
33
|
44
LL | #[alloc_error_handler]
5-
| ^^^^^^^^^^^^^^^^^^^^^^ expected `Layout`, found `core::alloc::Layout`
5+
| ---------------------- in this procedural macro expansion
66
LL | / fn oom(
77
LL | | info: Layout,
8+
| | ^^^^^^^^^^^^ expected `Layout`, found `core::alloc::Layout`
89
LL | | ) {
10+
LL | | loop {}
11+
LL | | }
912
| |_- arguments to this function are incorrect
1013
|
1114
= note: `core::alloc::Layout` and `Layout` have similar names, but are actually distinct types
@@ -26,17 +29,17 @@ LL | info: Layout,
2629
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
2730

2831
error[E0308]: mismatched types
29-
--> $DIR/alloc-error-handler-bad-signature-2.rs:10:1
32+
--> $DIR/alloc-error-handler-bad-signature-2.rs:12:3
3033
|
3134
LL | #[alloc_error_handler]
32-
| ----------------------
33-
| |
34-
| expected `!` because of return type
35-
| in this procedural macro expansion
35+
| ---------------------- in this procedural macro expansion
3636
LL | / fn oom(
3737
LL | | info: Layout,
3838
LL | | ) {
39-
| |_^ expected `!`, found `()`
39+
| | ^ expected `!`, found `()`
40+
LL | | loop {}
41+
LL | | }
42+
| |_- expected `!` because of return type
4043
|
4144
= note: expected type `!`
4245
found unit type `()`

tests/ui/alloc-error/alloc-error-handler-bad-signature-3.stderr

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
error[E0061]: this function takes 0 arguments but 1 argument was supplied
22
--> $DIR/alloc-error-handler-bad-signature-3.rs:10:1
33
|
4-
LL | #[alloc_error_handler]
5-
| ----------------------
6-
| |
7-
| unexpected argument of type `core::alloc::Layout`
8-
| in this procedural macro expansion
9-
LL | fn oom() -> ! {
10-
| ^^^^^^^^^^^^^
4+
LL | #[alloc_error_handler]
5+
| ---------------------- in this procedural macro expansion
6+
LL | / fn oom() -> ! {
7+
LL | | loop {}
8+
LL | | }
9+
| |_^ unexpected argument of type `core::alloc::Layout`
1110
|
1211
note: function defined here
1312
--> $DIR/alloc-error-handler-bad-signature-3.rs:10:4

0 commit comments

Comments
 (0)