Skip to content

Commit 8f2efa0

Browse files
committed
EII ast changes
1 parent bf80165 commit 8f2efa0

File tree

14 files changed

+110
-11
lines changed

14 files changed

+110
-11
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,19 @@ pub struct MacroDef {
20782078
pub body: Box<DelimArgs>,
20792079
/// `true` if macro was defined with `macro_rules`.
20802080
pub macro_rules: bool,
2081+
2082+
/// If this is a macro used for externally implementable items,
2083+
/// it refers to an extern item which is its "target". This requires
2084+
/// name resolution so can't just be an attribute, so we store it in this field.
2085+
pub eii_extern_target: Option<EiiExternTarget>,
2086+
}
2087+
2088+
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic, Walkable)]
2089+
pub struct EiiExternTarget {
2090+
/// path to the extern item we're targetting
2091+
pub extern_item_path: Path,
2092+
pub impl_unsafe: bool,
2093+
pub span: Span,
20812094
}
20822095

20832096
#[derive(Clone, Encodable, Decodable, Debug, Copy, Hash, Eq, PartialEq)]
@@ -3719,6 +3732,21 @@ pub struct Fn {
37193732
pub contract: Option<Box<FnContract>>,
37203733
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
37213734
pub body: Option<Box<Block>>,
3735+
3736+
/// This function is an implementation of an externally implementable item (EII).
3737+
/// This means, there was an EII declared somewhere and this function is the
3738+
/// implementation that should be run when the declaration is called.
3739+
pub eii_impls: ThinVec<EiiImpl>,
3740+
}
3741+
3742+
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
3743+
pub struct EiiImpl {
3744+
pub node_id: NodeId,
3745+
pub eii_macro_path: Path,
3746+
pub impl_safety: Safety,
3747+
pub span: Span,
3748+
pub inner_span: Span,
3749+
pub is_default: bool,
37223750
}
37233751

37243752
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]
@@ -4066,7 +4094,7 @@ mod size_asserts {
40664094
static_assert_size!(Block, 32);
40674095
static_assert_size!(Expr, 72);
40684096
static_assert_size!(ExprKind, 40);
4069-
static_assert_size!(Fn, 184);
4097+
static_assert_size!(Fn, 192);
40704098
static_assert_size!(ForeignItem, 80);
40714099
static_assert_size!(ForeignItemKind, 16);
40724100
static_assert_size!(GenericArg, 24);

compiler/rustc_ast/src/visit.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ macro_rules! common_visitor_and_walkers {
393393
ThinVec<Pat>,
394394
ThinVec<Box<Ty>>,
395395
ThinVec<TyPat>,
396+
ThinVec<EiiImpl>,
396397
);
397398

398399
// This macro generates `impl Visitable` and `impl MutVisitable` that forward to `Walkable`
@@ -484,6 +485,8 @@ macro_rules! common_visitor_and_walkers {
484485
WhereEqPredicate,
485486
WhereRegionPredicate,
486487
YieldKind,
488+
EiiExternTarget,
489+
EiiImpl,
487490
);
488491

489492
/// Each method of this trait is a hook to be potentially
@@ -913,13 +916,13 @@ macro_rules! common_visitor_and_walkers {
913916
_ctxt,
914917
// Visibility is visited as a part of the item.
915918
_vis,
916-
Fn { defaultness, ident, sig, generics, contract, body, define_opaque },
919+
Fn { defaultness, ident, sig, generics, contract, body, define_opaque, eii_impls },
917920
) => {
918921
let FnSig { header, decl, span } = sig;
919922
visit_visitable!($($mut)? vis,
920923
defaultness, ident, header, generics, decl,
921-
contract, body, span, define_opaque
922-
)
924+
contract, body, span, define_opaque, eii_impls
925+
);
923926
}
924927
FnKind::Closure(binder, coroutine_kind, decl, body) =>
925928
visit_visitable!($($mut)? vis, binder, coroutine_kind, decl, body),

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
432432
);
433433
hir::ItemKind::TraitAlias(constness, ident, generics, bounds)
434434
}
435-
ItemKind::MacroDef(ident, MacroDef { body, macro_rules }) => {
435+
ItemKind::MacroDef(ident, MacroDef { body, macro_rules, eii_extern_target: _ }) => {
436436
let ident = self.lower_ident(*ident);
437437
let body = Box::new(self.lower_delim_args(body));
438438
let def_id = self.local_def_id(id);
@@ -443,7 +443,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
443443
def_kind.descr(def_id.to_def_id())
444444
);
445445
};
446-
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
446+
let macro_def = self.arena.alloc(ast::MacroDef {
447+
body,
448+
macro_rules: *macro_rules,
449+
eii_extern_target: None,
450+
});
447451
hir::ItemKind::Macro(ident, macro_def, macro_kinds)
448452
}
449453
ItemKind::Delegation(box delegation) => {

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,16 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10741074
contract: _,
10751075
body,
10761076
define_opaque: _,
1077+
eii_impls,
10771078
},
10781079
) => {
10791080
self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
10801081
self.check_defaultness(item.span, *defaultness);
10811082

1083+
for EiiImpl { eii_macro_path, .. } in eii_impls {
1084+
self.visit_path(eii_macro_path);
1085+
}
1086+
10821087
let is_intrinsic = item.attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic));
10831088
if body.is_none() && !is_intrinsic && !self.is_sdylib_interface {
10841089
self.dcx().emit_err(errors::FnWithoutBody {

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
852852
sp: Span,
853853
print_visibility: impl FnOnce(&mut Self),
854854
) {
855+
if let Some(eii_extern_target) = &macro_def.eii_extern_target {
856+
self.word("#[eii_extern_target(");
857+
self.print_path(&eii_extern_target.extern_item_path, false, 0);
858+
if eii_extern_target.impl_unsafe {
859+
self.word(",");
860+
self.space();
861+
self.word("unsafe");
862+
}
863+
self.word(")]");
864+
self.hardbreak();
865+
}
855866
let (kw, has_bang) = if macro_def.macro_rules {
856867
("macro_rules", true)
857868
} else {
@@ -2147,6 +2158,15 @@ impl<'a> State<'a> {
21472158

21482159
fn print_meta_item(&mut self, item: &ast::MetaItem) {
21492160
let ib = self.ibox(INDENT_UNIT);
2161+
2162+
match item.unsafety {
2163+
ast::Safety::Unsafe(_) => {
2164+
self.word("unsafe");
2165+
self.popen();
2166+
}
2167+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2168+
}
2169+
21502170
match &item.kind {
21512171
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
21522172
ast::MetaItemKind::NameValue(value) => {
@@ -2162,6 +2182,12 @@ impl<'a> State<'a> {
21622182
self.pclose();
21632183
}
21642184
}
2185+
2186+
match item.unsafety {
2187+
ast::Safety::Unsafe(_) => self.pclose(),
2188+
ast::Safety::Default | ast::Safety::Safe(_) => {}
2189+
}
2190+
21652191
self.end(ib);
21662192
}
21672193

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ast::StaticItem;
22
use itertools::{Itertools, Position};
3-
use rustc_ast::{self as ast, ModKind, TraitAlias};
3+
use rustc_ast::{self as ast, EiiImpl, ModKind, ModKind, Safety, TraitAlias};
44
use rustc_span::Ident;
55

66
use crate::pp::BoxMarker;
@@ -675,10 +675,25 @@ impl<'a> State<'a> {
675675
}
676676

677677
fn print_fn_full(&mut self, vis: &ast::Visibility, attrs: &[ast::Attribute], func: &ast::Fn) {
678-
let ast::Fn { defaultness, ident, generics, sig, contract, body, define_opaque } = func;
678+
let ast::Fn { defaultness, ident, generics, sig, contract, body, define_opaque, eii_impls } =
679+
func;
679680

680681
self.print_define_opaques(define_opaque.as_deref());
681682

683+
for EiiImpl { eii_macro_path, impl_safety, .. } in eii_impls {
684+
self.word("#[");
685+
if let Safety::Unsafe(..) = impl_safety {
686+
self.word("unsafe");
687+
self.popen();
688+
}
689+
self.print_path(eii_macro_path, false, 0);
690+
if let Safety::Unsafe(..) = impl_safety {
691+
self.pclose();
692+
}
693+
self.word("]");
694+
self.hardbreak();
695+
}
696+
682697
let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
683698

684699
self.print_visibility(vis);

compiler/rustc_builtin_macros/src/alloc_error_handler.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
9090
contract: None,
9191
body,
9292
define_opaque: None,
93+
eii_impls: ThinVec::new(),
9394
}));
9495

9596
let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];

compiler/rustc_builtin_macros/src/autodiff.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ mod llvm_enzyme {
346346
contract: None,
347347
body: Some(d_body),
348348
define_opaque: None,
349+
eii_impls: ThinVec::new(),
349350
});
350351
let mut rustc_ad_attr =
351352
Box::new(ast::NormalAttr::from_ident(Ident::with_dummy_span(sym::rustc_autodiff)));

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,7 @@ impl<'a> MethodDef<'a> {
10861086
contract: None,
10871087
body: Some(body_block),
10881088
define_opaque: None,
1089+
eii_impls: ThinVec::new(),
10891090
})),
10901091
tokens: None,
10911092
})

compiler/rustc_builtin_macros/src/global_allocator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ impl AllocFnFactory<'_, '_> {
8484
contract: None,
8585
body,
8686
define_opaque: None,
87+
eii_impls: ThinVec::new(),
8788
}));
8889
let item = self.cx.item(self.span, self.attrs(method), kind);
8990
self.cx.stmt_item(self.ty_span, item)

0 commit comments

Comments
 (0)