Skip to content

Commit 8e1c167

Browse files
authored
Rollup merge of rust-lang#150906 - eii-ecx-mehods, r=Kivooeo
Simplify `#[eii]` macro using methods on ecx
2 parents bd30062 + d993bd1 commit 8e1c167

1 file changed

Lines changed: 78 additions & 136 deletions

File tree

  • compiler/rustc_builtin_macros/src

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 78 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,33 @@ 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,
136136
&attrs_from_decl,
137-
)));
137+
));
138138

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

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

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

334282
/// Generates a foreign item, like
@@ -343,7 +291,7 @@ fn generate_foreign_item(
343291
mut func: ast::Fn,
344292
vis: Visibility,
345293
attrs_from_decl: &[Attribute],
346-
) -> ast::Item {
294+
) -> Box<ast::Item> {
347295
let mut foreign_item_attrs = ThinVec::new();
348296
foreign_item_attrs.extend_from_slice(attrs_from_decl);
349297

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

378-
ast::Item {
379-
attrs: ast::AttrVec::default(),
380-
id: ast::DUMMY_NODE_ID,
381-
span: eii_attr_span,
382-
vis: ast::Visibility {
383-
span: eii_attr_span,
384-
kind: ast::VisibilityKind::Inherited,
385-
tokens: None,
386-
},
387-
kind: ast::ItemKind::ForeignMod(ast::ForeignMod {
326+
ecx.item(
327+
eii_attr_span,
328+
ThinVec::new(),
329+
ast::ItemKind::ForeignMod(ast::ForeignMod {
388330
extern_span: eii_attr_span,
389331
safety: ast::Safety::Unsafe(eii_attr_span),
390332
abi,
@@ -397,8 +339,7 @@ fn generate_foreign_item(
397339
tokens: None,
398340
})]),
399341
}),
400-
tokens: None,
401-
}
342+
)
402343
}
403344

404345
/// Generate a stub macro (a bit like in core) that will roughly look like:
@@ -418,7 +359,7 @@ fn generate_attribute_macro_to_implement(
418359
foreign_item_name: Ident,
419360
impl_unsafe: bool,
420361
attrs_from_decl: &[Attribute],
421-
) -> ast::Item {
362+
) -> Box<ast::Item> {
422363
let mut macro_attrs = ThinVec::new();
423364

424365
// To avoid e.g. `error: attribute macro has missing stability attribute`
@@ -428,7 +369,8 @@ fn generate_attribute_macro_to_implement(
428369
// #[builtin_macro(eii_shared_macro)]
429370
macro_attrs.push(ecx.attr_nested_word(sym::rustc_builtin_macro, sym::eii_shared_macro, span));
430371

431-
ast::Item {
372+
// cant use ecx methods here to construct item since we need it to be public
373+
Box::new(ast::Item {
432374
attrs: macro_attrs,
433375
id: ast::DUMMY_NODE_ID,
434376
span,
@@ -467,7 +409,7 @@ fn generate_attribute_macro_to_implement(
467409
},
468410
),
469411
tokens: None,
470-
}
412+
})
471413
}
472414

473415
pub(crate) fn eii_extern_target(

0 commit comments

Comments
 (0)