Skip to content

Commit 68c5571

Browse files
committed
allow eii marked items to have a body which becomes a default
1 parent 29ccc83 commit 68c5571

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]
@@ -90,6 +91,75 @@ fn eii_(
9091
return vec![Annotatable::Item(P(item))];
9192
};
9293

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

202-
vec![extern_block, macro_def]
272+
return_items.push(extern_block);
273+
return_items.push(macro_def);
274+
275+
return_items
203276
}
204277

205278
use crate::errors::{

0 commit comments

Comments
 (0)