Skip to content

Commit ac8d605

Browse files
Auto merge of #146348 - jdonszelmann:eiiv3, r=<try>
[DONT MERGE] externally implementable items
2 parents 35ebdf9 + 05fe89d commit ac8d605

File tree

131 files changed

+3291
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+3291
-52
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: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_abi::ExternAbi;
22
use rustc_ast::visit::AssocCtxt;
33
use rustc_ast::*;
44
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
5-
use rustc_hir::attrs::AttributeKind;
5+
use rustc_hir::attrs::{AttributeKind, EiiDecl};
66
use rustc_hir::def::{DefKind, PerNS, Res};
77
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
88
use rustc_hir::{
@@ -11,6 +11,7 @@ use rustc_hir::{
1111
use rustc_index::{IndexSlice, IndexVec};
1212
use rustc_middle::span_bug;
1313
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
14+
use rustc_span::def_id::DefId;
1415
use rustc_span::edit_distance::find_best_match_for_name;
1516
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
1617
use smallvec::{SmallVec, smallvec};
@@ -133,17 +134,103 @@ impl<'hir> LoweringContext<'_, 'hir> {
133134
}
134135
}
135136

137+
fn generate_extra_attrs_for_item_kind(
138+
&mut self,
139+
id: NodeId,
140+
i: &ItemKind,
141+
) -> Vec<hir::Attribute> {
142+
match i {
143+
ItemKind::Fn(box Fn { eii_impls, .. }) if eii_impls.is_empty() => Vec::new(),
144+
ItemKind::Fn(box Fn { eii_impls, .. }) => {
145+
vec![hir::Attribute::Parsed(AttributeKind::EiiImpls(
146+
eii_impls
147+
.iter()
148+
.flat_map(
149+
|EiiImpl {
150+
node_id,
151+
eii_macro_path,
152+
impl_safety,
153+
span,
154+
inner_span,
155+
is_default,
156+
}| {
157+
self.lower_path_simple_eii(*node_id, eii_macro_path).map(|did| {
158+
hir::attrs::EiiImpl {
159+
eii_macro: did,
160+
span: self.lower_span(*span),
161+
inner_span: self.lower_span(*inner_span),
162+
impl_marked_unsafe: self
163+
.lower_safety(*impl_safety, hir::Safety::Safe)
164+
.is_unsafe(),
165+
is_default: *is_default,
166+
}
167+
})
168+
},
169+
)
170+
.collect(),
171+
))]
172+
}
173+
ItemKind::MacroDef(
174+
_,
175+
MacroDef {
176+
eii_extern_target: Some(EiiExternTarget { extern_item_path, impl_unsafe, span }),
177+
..
178+
},
179+
) => self
180+
.lower_path_simple_eii(id, extern_item_path)
181+
.map(|did| {
182+
vec![hir::Attribute::Parsed(AttributeKind::EiiExternTarget(EiiDecl {
183+
eii_extern_target: did,
184+
impl_unsafe: *impl_unsafe,
185+
span: self.lower_span(*span),
186+
}))]
187+
})
188+
.unwrap_or_default(),
189+
ItemKind::ExternCrate(..)
190+
| ItemKind::Use(..)
191+
| ItemKind::Static(..)
192+
| ItemKind::Const(..)
193+
| ItemKind::Mod(..)
194+
| ItemKind::ForeignMod(..)
195+
| ItemKind::GlobalAsm(..)
196+
| ItemKind::TyAlias(..)
197+
| ItemKind::Enum(..)
198+
| ItemKind::Struct(..)
199+
| ItemKind::Union(..)
200+
| ItemKind::Trait(..)
201+
| ItemKind::TraitAlias(..)
202+
| ItemKind::Impl(..)
203+
| ItemKind::MacCall(..)
204+
| ItemKind::MacroDef(..)
205+
| ItemKind::Delegation(..)
206+
| ItemKind::DelegationMac(..) => Vec::new(),
207+
}
208+
}
209+
136210
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
137211
let vis_span = self.lower_span(i.vis.span);
138212
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
139-
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span, Target::from_ast_item(i));
213+
214+
let extra_hir_attributes = self.generate_extra_attrs_for_item_kind(i.id, &i.kind);
215+
let attrs = self.lower_attrs_with_extra(
216+
hir_id,
217+
&i.attrs,
218+
i.span,
219+
Target::from_ast_item(i),
220+
&extra_hir_attributes,
221+
);
222+
140223
let kind = self.lower_item_kind(i.span, i.id, hir_id, attrs, vis_span, &i.kind);
141224
let item = hir::Item {
142225
owner_id: hir_id.expect_owner(),
143226
kind,
144227
vis_span,
145228
span: self.lower_span(i.span),
146229
has_delayed_lints: !self.delayed_lints.is_empty(),
230+
eii: find_attr!(
231+
attrs,
232+
AttributeKind::EiiImpls(..) | AttributeKind::EiiExternTarget(..)
233+
),
147234
};
148235
self.arena.alloc(item)
149236
}
@@ -432,7 +519,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
432519
);
433520
hir::ItemKind::TraitAlias(constness, ident, generics, bounds)
434521
}
435-
ItemKind::MacroDef(ident, MacroDef { body, macro_rules }) => {
522+
ItemKind::MacroDef(ident, MacroDef { body, macro_rules, eii_extern_target: _ }) => {
436523
let ident = self.lower_ident(*ident);
437524
let body = Box::new(self.lower_delim_args(body));
438525
let def_id = self.local_def_id(id);
@@ -443,7 +530,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
443530
def_kind.descr(def_id.to_def_id())
444531
);
445532
};
446-
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
533+
let macro_def = self.arena.alloc(ast::MacroDef {
534+
body,
535+
macro_rules: *macro_rules,
536+
eii_extern_target: None,
537+
});
447538
hir::ItemKind::Macro(ident, macro_def, macro_kinds)
448539
}
449540
ItemKind::Delegation(box delegation) => {
@@ -462,6 +553,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
462553
}
463554
}
464555

556+
fn lower_path_simple_eii(&mut self, id: NodeId, path: &Path) -> Option<DefId> {
557+
let res = self.resolver.get_partial_res(id)?;
558+
let Some(did) = res.expect_full_res().opt_def_id() else {
559+
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
560+
return None;
561+
};
562+
563+
Some(did)
564+
}
565+
465566
fn lower_const_item(
466567
&mut self,
467568
ty: &Ty,
@@ -581,6 +682,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
581682
vis_span,
582683
span: this.lower_span(use_tree.span),
583684
has_delayed_lints: !this.delayed_lints.is_empty(),
685+
eii: find_attr!(
686+
attrs,
687+
AttributeKind::EiiImpls(..) | AttributeKind::EiiExternTarget(..)
688+
),
584689
};
585690
hir::OwnerNode::Item(this.arena.alloc(item))
586691
});

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -956,11 +956,23 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
956956
target_span: Span,
957957
target: Target,
958958
) -> &'hir [hir::Attribute] {
959-
if attrs.is_empty() {
959+
self.lower_attrs_with_extra(id, attrs, target_span, target, &[])
960+
}
961+
962+
fn lower_attrs_with_extra(
963+
&mut self,
964+
id: HirId,
965+
attrs: &[Attribute],
966+
target_span: Span,
967+
target: Target,
968+
extra_hir_attributes: &[hir::Attribute],
969+
) -> &'hir [hir::Attribute] {
970+
if attrs.is_empty() && extra_hir_attributes.is_empty() {
960971
&[]
961972
} else {
962-
let lowered_attrs =
973+
let mut lowered_attrs =
963974
self.lower_attrs_vec(attrs, self.lower_span(target_span), id, target);
975+
lowered_attrs.extend(extra_hir_attributes.iter().cloned());
964976

965977
assert_eq!(id.owner, self.current_hir_id_owner);
966978
let ret = self.arena.alloc_from_iter(lowered_attrs);

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, 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/messages.ftl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ builtin_macros_derive_path_args_value = traits in `#[derive(...)]` don't accept
154154
155155
builtin_macros_duplicate_macro_attribute = duplicated attribute
156156
157+
builtin_macros_eii_extern_target_expected_list = `#[eii_extern_target(...)]` expects a list of one or two elements
158+
builtin_macros_eii_extern_target_expected_macro = `#[eii_extern_target(...)]` is only valid on macros
159+
builtin_macros_eii_extern_target_expected_unsafe = expected this argument to be "unsafe"
160+
.note = the second argument is optional
161+
162+
builtin_macros_eii_only_once = `#[{$name}]` can only be specified once
163+
.note = specified again here
164+
165+
builtin_macros_eii_shared_macro_expected_function = `#[{$name}]` is only valid on functions
166+
builtin_macros_eii_shared_macro_expected_max_one_argument = `#[{$name}]` expected no arguments or a single argument: `#[{$name}(default)]`
167+
157168
builtin_macros_env_not_defined = environment variable `{$var}` not defined at compile time
158169
.cargo = Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead
159170
.custom = use `std::env::var({$var_expr})` to read the variable at run time

0 commit comments

Comments
 (0)