Skip to content

Commit 43c060f

Browse files
m-ou-sejdonszelmann
authored andcommitted
Add #[unsafe_eii].
1 parent 83f28ba commit 43c060f

File tree

5 files changed

+54
-45
lines changed

5 files changed

+54
-45
lines changed

compiler/rustc_builtin_macros/src/eii.rs

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,49 @@ use rustc_ast_pretty::pprust::path_to_string;
66
use rustc_expand::base::{Annotatable, ExtCtxt};
77
use rustc_span::{Ident, Span, kw, sym};
88

9-
/* ```rust
10-
11-
#[eii]
12-
fn panic_handler();
13-
14-
#[eii(panic_handler)]
15-
fn panic_handler();
16-
17-
#[eii(panic_handler, unsafe)]
18-
fn panic_handler();
19-
20-
// expansion:
21-
22-
extern "Rust" {
23-
fn panic_handler();
9+
// ```rust
10+
// #[eii]
11+
// fn panic_handler();
12+
//
13+
// // or:
14+
//
15+
// #[eii(panic_handler)]
16+
// fn panic_handler();
17+
//
18+
// // expansion:
19+
//
20+
// extern "Rust" {
21+
// fn panic_handler();
22+
// }
23+
//
24+
// #[rustc_builtin_macro(eii_macro)]
25+
// #[eii_macro_for(panic_handler)]
26+
// macro panic_handler() {}
27+
// ```
28+
pub(crate) fn eii(
29+
ecx: &mut ExtCtxt<'_>,
30+
span: Span,
31+
meta_item: &ast::MetaItem,
32+
item: Annotatable,
33+
) -> Vec<Annotatable> {
34+
eii_(ecx, span, meta_item, item, false)
2435
}
2536

26-
#[rustc_builtin_macro(eii_macro)] // eii_macro_for: panic_handler
27-
macro panic_handler() {}
37+
pub(crate) fn unsafe_eii(
38+
ecx: &mut ExtCtxt<'_>,
39+
span: Span,
40+
meta_item: &ast::MetaItem,
41+
item: Annotatable,
42+
) -> Vec<Annotatable> {
43+
eii_(ecx, span, meta_item, item, true)
44+
}
2845

29-
``` */
30-
pub(crate) fn eii(
46+
fn eii_(
3147
ecx: &mut ExtCtxt<'_>,
3248
span: Span,
3349
meta_item: &ast::MetaItem,
3450
item: Annotatable,
51+
impl_unsafe: bool,
3552
) -> Vec<Annotatable> {
3653
let span = ecx.with_def_site_ctxt(span);
3754

@@ -74,8 +91,6 @@ pub(crate) fn eii(
7491
return vec![Annotatable::Item(orig_item)];
7592
};
7693

77-
let impl_unsafe = false; // TODO
78-
7994
let abi = match func.sig.header.ext {
8095
// extern "X" fn => extern "X" {}
8196
ast::Extern::Explicit(lit, _) => Some(lit),
@@ -123,28 +138,6 @@ pub(crate) fn eii(
123138

124139
let macro_def = Annotatable::Item(P(ast::Item {
125140
attrs: ast::AttrVec::from_iter([
126-
// #[eii_macro_for(func.ident)]
127-
ast::Attribute {
128-
kind: ast::AttrKind::Normal(P(ast::NormalAttr {
129-
item: ast::AttrItem {
130-
unsafety: ast::Safety::Default,
131-
path: ast::Path::from_ident(Ident::new(sym::eii_macro_for, span)),
132-
args: ast::AttrArgs::Delimited(ast::DelimArgs {
133-
dspan: DelimSpan::from_single(span),
134-
delim: Delimiter::Parenthesis,
135-
tokens: TokenStream::new(vec![tokenstream::TokenTree::Token(
136-
token::Token::from_ast_ident(func.ident),
137-
Spacing::Alone,
138-
)]),
139-
}),
140-
tokens: None,
141-
},
142-
tokens: None,
143-
})),
144-
id: ecx.sess.psess.attr_id_generator.mk_attr_id(),
145-
style: ast::AttrStyle::Outer,
146-
span,
147-
},
148141
// #[builtin_macro(eii_macro)]
149142
ast::Attribute {
150143
kind: ast::AttrKind::Normal(P(ast::NormalAttr {
@@ -197,7 +190,11 @@ pub(crate) fn eii(
197190
]),
198191
}),
199192
macro_rules: false,
200-
eii_macro_for: None,
193+
// #[eii_macro_for(func.ident)]
194+
eii_macro_for: Some(ast::EIIMacroFor {
195+
extern_item_path: ast::Path::from_ident(func.ident),
196+
impl_unsafe,
197+
}),
201198
}
202199
),
203200
tokens: None,

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
124124
test: test::expand_test,
125125
test_case: test::expand_test_case,
126126
eii: eii::eii,
127+
unsafe_eii: eii::unsafe_eii,
127128
eii_macro_for: eii::eii_macro_for,
128129
eii_macro: eii::eii_macro,
129130
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,6 +2218,7 @@ symbols! {
22182218
unsafe_block_in_unsafe_fn,
22192219
unsafe_cell,
22202220
unsafe_cell_raw_get,
2221+
unsafe_eii,
22212222
unsafe_extern_blocks,
22222223
unsafe_fields,
22232224
unsafe_no_drop_flag,

library/core/src/macros/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,16 @@ pub(crate) mod builtin {
17911791
/* compiler built-in */
17921792
}
17931793

1794+
/// Unsafely Externally Implementable Item: Defines an unsafe attribute macro that can override
1795+
/// the item this is applied to.
1796+
#[cfg(not(bootstrap))]
1797+
#[unstable(feature = "eii", issue = "none")]
1798+
#[rustc_builtin_macro]
1799+
#[allow_internal_unstable(eii_internals, decl_macro, rustc_attrs)]
1800+
pub macro unsafe_eii($item:item) {
1801+
/* compiler built-in */
1802+
}
1803+
17941804
/// Impl detail of EII
17951805
#[cfg(not(bootstrap))]
17961806
#[unstable(feature = "eii_internals", issue = "none")]

library/core/src/prelude/v1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub use crate::macros::builtin::define_opaque;
121121

122122
#[unstable(feature = "eii", issue = "none")]
123123
#[cfg(not(bootstrap))]
124-
pub use crate::macros::builtin::eii;
124+
pub use crate::macros::builtin::{eii, unsafe_eii};
125125

126126
#[unstable(feature = "eii_internals", issue = "none")]
127127
#[cfg(not(bootstrap))]

0 commit comments

Comments
 (0)