Skip to content

Commit 99a8e38

Browse files
committed
improve eii macro by using ecx methods
1 parent 1b9ae9e commit 99a8e38

File tree

1 file changed

+78
-136
lines changed
  • compiler/rustc_builtin_macros/src

1 file changed

+78
-136
lines changed

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 78 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,32 @@ fn eii_(
108108
let mut return_items = Vec::new();
109109

110110
if func.body.is_some() {
111-
return_items.push(Box::new(generate_default_impl(
111+
return_items.push(generate_default_impl(
112112
ecx,
113113
&func,
114114
impl_unsafe,
115115
macro_name,
116116
eii_attr_span,
117117
item_span,
118118
foreign_item_name,
119-
)))
119+
))
120120
}
121121

122-
return_items.push(Box::new(generate_foreign_item(
122+
return_items.push(generate_foreign_item(
123123
ecx,
124124
eii_attr_span,
125125
item_span,
126126
func,
127127
vis,
128128
&attrs_from_decl,
129-
)));
130-
return_items.push(Box::new(generate_attribute_macro_to_implement(
129+
));
130+
return_items.push(generate_attribute_macro_to_implement(
131131
ecx,
132132
eii_attr_span,
133133
macro_name,
134134
foreign_item_name,
135135
impl_unsafe,
136-
)));
136+
));
137137

138138
return_items.into_iter().map(wrap_item).collect()
139139
}
@@ -193,7 +193,7 @@ fn generate_default_impl(
193193
eii_attr_span: Span,
194194
item_span: Span,
195195
foreign_item_name: Ident,
196-
) -> ast::Item {
196+
) -> Box<ast::Item> {
197197
// FIXME: re-add some original attrs
198198
let attrs = ThinVec::new();
199199

@@ -210,124 +210,72 @@ fn generate_default_impl(
210210
span: eii_attr_span,
211211
is_default: true,
212212
known_eii_macro_resolution: Some(ast::EiiExternTarget {
213-
extern_item_path: ast::Path {
214-
span: foreign_item_name.span,
215-
segments: thin_vec![
216-
ast::PathSegment {
217-
ident: Ident::from_str_and_span("super", foreign_item_name.span,),
218-
id: DUMMY_NODE_ID,
219-
args: None
220-
},
221-
ast::PathSegment { ident: foreign_item_name, id: DUMMY_NODE_ID, args: None },
222-
],
223-
tokens: None,
224-
},
213+
extern_item_path: ecx.path(
214+
foreign_item_name.span,
215+
// prefix super to escape the `dflt` module generated below
216+
vec![Ident::from_str_and_span("super", foreign_item_name.span), foreign_item_name],
217+
),
225218
impl_unsafe,
226219
}),
227220
});
228221

229-
ast::Item {
230-
attrs: ThinVec::new(),
231-
id: ast::DUMMY_NODE_ID,
232-
span: eii_attr_span,
233-
vis: ast::Visibility {
234-
span: eii_attr_span,
235-
kind: ast::VisibilityKind::Inherited,
236-
tokens: None,
237-
},
238-
kind: ast::ItemKind::Const(Box::new(ast::ConstItem {
239-
ident: Ident { name: kw::Underscore, span: eii_attr_span },
240-
defaultness: ast::Defaultness::Final,
241-
generics: ast::Generics::default(),
242-
ty: Box::new(ast::Ty {
243-
id: DUMMY_NODE_ID,
244-
kind: ast::TyKind::Tup(ThinVec::new()),
245-
span: eii_attr_span,
246-
tokens: None,
247-
}),
248-
rhs: Some(ast::ConstItemRhs::Body(Box::new(ast::Expr {
249-
id: DUMMY_NODE_ID,
250-
kind: ast::ExprKind::Block(
251-
Box::new(ast::Block {
252-
stmts: thin_vec![ast::Stmt {
253-
id: DUMMY_NODE_ID,
254-
kind: ast::StmtKind::Item(Box::new(ast::Item {
255-
attrs: ThinVec::new(),
256-
id: DUMMY_NODE_ID,
257-
span: item_span,
258-
vis: ast::Visibility {
259-
span: item_span,
260-
kind: ast::VisibilityKind::Inherited,
261-
tokens: None
262-
},
263-
kind: ItemKind::Mod(
264-
ast::Safety::Default,
265-
Ident::from_str_and_span("dflt", item_span),
266-
ast::ModKind::Loaded(
267-
thin_vec![
268-
Box::new(ast::Item {
269-
attrs: thin_vec![ecx.attr_nested_word(
270-
sym::allow,
271-
sym::unused_imports,
272-
item_span
273-
),],
274-
id: DUMMY_NODE_ID,
275-
span: item_span,
276-
vis: ast::Visibility {
277-
span: eii_attr_span,
278-
kind: ast::VisibilityKind::Inherited,
279-
tokens: None
280-
},
281-
kind: ItemKind::Use(ast::UseTree {
282-
prefix: ast::Path::from_ident(
283-
Ident::from_str_and_span(
284-
"super", item_span,
285-
)
286-
),
287-
kind: ast::UseTreeKind::Glob,
288-
span: item_span,
289-
}),
290-
tokens: None,
291-
}),
292-
Box::new(ast::Item {
293-
attrs,
294-
id: DUMMY_NODE_ID,
295-
span: item_span,
296-
vis: ast::Visibility {
297-
span: eii_attr_span,
298-
kind: ast::VisibilityKind::Inherited,
299-
tokens: None
300-
},
301-
kind: ItemKind::Fn(Box::new(default_func)),
302-
tokens: None,
303-
}),
304-
],
305-
ast::Inline::Yes,
306-
ast::ModSpans {
307-
inner_span: item_span,
308-
inject_use_span: item_span,
309-
}
310-
)
311-
),
312-
tokens: None,
313-
})),
314-
span: eii_attr_span,
315-
}],
316-
id: DUMMY_NODE_ID,
317-
rules: ast::BlockCheckMode::Default,
318-
span: eii_attr_span,
319-
tokens: None,
320-
}),
321-
None,
222+
let item_mod = |span: Span, name: Ident, items: ThinVec<Box<ast::Item>>| {
223+
ecx.item(
224+
item_span,
225+
ThinVec::new(),
226+
ItemKind::Mod(
227+
ast::Safety::Default,
228+
name,
229+
ast::ModKind::Loaded(
230+
items,
231+
ast::Inline::Yes,
232+
ast::ModSpans { inner_span: span, inject_use_span: span },
322233
),
323-
span: eii_attr_span,
324-
attrs: ThinVec::new(),
325-
tokens: None,
326-
}))),
327-
define_opaque: None,
328-
})),
329-
tokens: None,
330-
}
234+
),
235+
)
236+
};
237+
238+
let anon_mod = |span: Span, stmts: ThinVec<ast::Stmt>| {
239+
let unit = ecx.ty(item_span, ast::TyKind::Tup(ThinVec::new()));
240+
let underscore = Ident::new(kw::Underscore, item_span);
241+
ecx.item_const(
242+
span,
243+
underscore,
244+
unit,
245+
ast::ConstItemRhs::Body(ecx.expr_block(ecx.block(span, stmts))),
246+
)
247+
};
248+
249+
// const _: () = {
250+
// mod dflt {
251+
// use super::*;
252+
// <orig fn>
253+
// }
254+
// }
255+
anon_mod(
256+
item_span,
257+
thin_vec![ecx.stmt_item(
258+
item_span,
259+
item_mod(
260+
item_span,
261+
Ident::from_str_and_span("dflt", item_span),
262+
thin_vec![
263+
ecx.item(
264+
item_span,
265+
thin_vec![ecx.attr_nested_word(sym::allow, sym::unused_imports, item_span)],
266+
ItemKind::Use(ast::UseTree {
267+
prefix: ast::Path::from_ident(Ident::from_str_and_span(
268+
"super", item_span,
269+
)),
270+
kind: ast::UseTreeKind::Glob,
271+
span: item_span,
272+
})
273+
),
274+
ecx.item(item_span, attrs, ItemKind::Fn(Box::new(default_func)))
275+
]
276+
)
277+
),],
278+
)
331279
}
332280

333281
/// Generates a foreign item, like
@@ -342,7 +290,7 @@ fn generate_foreign_item(
342290
mut func: ast::Fn,
343291
vis: Visibility,
344292
attrs_from_decl: &[Attribute],
345-
) -> ast::Item {
293+
) -> Box<ast::Item> {
346294
let mut foreign_item_attrs = ThinVec::new();
347295
foreign_item_attrs.extend_from_slice(attrs_from_decl);
348296

@@ -374,16 +322,10 @@ fn generate_foreign_item(
374322
func.sig.header.safety = ast::Safety::Safe(func.sig.span);
375323
}
376324

377-
ast::Item {
378-
attrs: ast::AttrVec::default(),
379-
id: ast::DUMMY_NODE_ID,
380-
span: eii_attr_span,
381-
vis: ast::Visibility {
382-
span: eii_attr_span,
383-
kind: ast::VisibilityKind::Inherited,
384-
tokens: None,
385-
},
386-
kind: ast::ItemKind::ForeignMod(ast::ForeignMod {
325+
ecx.item(
326+
eii_attr_span,
327+
ThinVec::new(),
328+
ast::ItemKind::ForeignMod(ast::ForeignMod {
387329
extern_span: eii_attr_span,
388330
safety: ast::Safety::Unsafe(eii_attr_span),
389331
abi,
@@ -396,8 +338,7 @@ fn generate_foreign_item(
396338
tokens: None,
397339
})]),
398340
}),
399-
tokens: None,
400-
}
341+
)
401342
}
402343

403344
/// Generate a stub macro (a bit like in core) that will roughly look like:
@@ -416,13 +357,14 @@ fn generate_attribute_macro_to_implement(
416357
macro_name: Ident,
417358
foreign_item_name: Ident,
418359
impl_unsafe: bool,
419-
) -> ast::Item {
360+
) -> Box<ast::Item> {
420361
let mut macro_attrs = ThinVec::new();
421362

422363
// #[builtin_macro(eii_shared_macro)]
423364
macro_attrs.push(ecx.attr_nested_word(sym::rustc_builtin_macro, sym::eii_shared_macro, span));
424365

425-
ast::Item {
366+
// cant use ecx methods here to construct item since we need it to be public
367+
Box::new(ast::Item {
426368
attrs: macro_attrs,
427369
id: ast::DUMMY_NODE_ID,
428370
span,
@@ -461,7 +403,7 @@ fn generate_attribute_macro_to_implement(
461403
},
462404
),
463405
tokens: None,
464-
}
406+
})
465407
}
466408

467409
pub(crate) fn eii_extern_target(

0 commit comments

Comments
 (0)