Skip to content

Commit 65c042e

Browse files
committed
allow eii marked items to have a body which becomes a default
1 parent de5f9bc commit 65c042e

File tree

1 file changed

+74
-1
lines changed
  • compiler/rustc_builtin_macros/src

1 file changed

+74
-1
lines changed

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_ast::{DUMMY_NODE_ID, EIIImpl, EIIMacroFor, ItemKind, ast, token, token
55
use rustc_ast_pretty::pprust::path_to_string;
66
use rustc_expand::base::{Annotatable, ExtCtxt};
77
use rustc_span::{Ident, Span, kw, sym};
8+
use thin_vec::{ThinVec, thin_vec};
89

910
// ```rust
1011
// #[eii]
@@ -85,6 +86,75 @@ fn eii_(
8586
return vec![Annotatable::Item(orig_item)];
8687
};
8788

89+
let mut return_items = Vec::new();
90+
91+
if func.body.is_some() {
92+
let mut default_func = func.clone();
93+
func.body = None;
94+
default_func.eii_impl.push(ast::EIIImpl {
95+
node_id: DUMMY_NODE_ID,
96+
eii_macro_path: ast::Path::from_ident(macro_name),
97+
impl_safety: if impl_unsafe { ast::Safety::Unsafe(span) } else { ast::Safety::Default },
98+
span,
99+
inner_span: macro_name.span,
100+
is_default: true, // important!
101+
});
102+
103+
return_items.push(Annotatable::Item(P(ast::Item {
104+
attrs: ThinVec::new(),
105+
id: ast::DUMMY_NODE_ID,
106+
span,
107+
vis: ast::Visibility { span, kind: ast::VisibilityKind::Inherited, tokens: None },
108+
ident: Ident { name: kw::Underscore, span },
109+
kind: ast::ItemKind::Const(Box::new(ast::ConstItem {
110+
defaultness: ast::Defaultness::Final,
111+
generics: ast::Generics::default(),
112+
ty: P(ast::Ty {
113+
id: DUMMY_NODE_ID,
114+
kind: ast::TyKind::Tup(ThinVec::new()),
115+
span,
116+
tokens: None,
117+
}),
118+
expr: Some(P(ast::Expr {
119+
id: DUMMY_NODE_ID,
120+
kind: ast::ExprKind::Block(
121+
P(ast::Block {
122+
stmts: thin_vec![ast::Stmt {
123+
id: DUMMY_NODE_ID,
124+
kind: ast::StmtKind::Item(P(ast::Item {
125+
attrs: thin_vec![], // TODO: re-add some original attrs
126+
id: DUMMY_NODE_ID,
127+
span: item_span,
128+
vis: ast::Visibility {
129+
span,
130+
kind: ast::VisibilityKind::Inherited,
131+
tokens: None
132+
},
133+
ident: item_name,
134+
kind: ItemKind::Fn(default_func),
135+
tokens: None,
136+
})),
137+
span
138+
}],
139+
id: DUMMY_NODE_ID,
140+
rules: ast::BlockCheckMode::Default,
141+
span,
142+
tokens: None,
143+
could_be_bare_literal: false,
144+
}),
145+
None,
146+
),
147+
span,
148+
attrs: ThinVec::new(),
149+
tokens: None,
150+
})),
151+
})),
152+
tokens: None,
153+
})))
154+
}
155+
156+
let decl_span = span.to(func.sig.span);
157+
88158
let abi = match func.sig.header.ext {
89159
// extern "X" fn => extern "X" {}
90160
ast::Extern::Explicit(lit, _) => Some(lit),
@@ -194,7 +264,10 @@ fn eii_(
194264
tokens: None,
195265
}));
196266

197-
vec![extern_block, macro_def]
267+
return_items.push(extern_block);
268+
return_items.push(macro_def);
269+
270+
return_items
198271
}
199272

200273
use crate::errors::{

0 commit comments

Comments
 (0)