Skip to content

Commit d2fa85c

Browse files
Rollup merge of #154193 - JonathanBrouwer:external-static, r=jdonszelmann
Implement EII for statics This PR implements EII for statics. I've tried to mirror the implementation for functions in a few places, this causes some duplicate code but I'm also not really sure whether there's a clean way to merge the implementations. This does not implement defaults for static EIIs yet, I will do that in a followup PR
2 parents 148ef32 + 106b16c commit d2fa85c

File tree

75 files changed

+1036
-174
lines changed

Some content is hidden

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

75 files changed

+1036
-174
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3918,6 +3918,13 @@ pub struct StaticItem {
39183918
pub mutability: Mutability,
39193919
pub expr: Option<Box<Expr>>,
39203920
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3921+
3922+
/// This static is an implementation of an externally implementable item (EII).
3923+
/// This means, there was an EII declared somewhere and this static is the
3924+
/// implementation that should be used for the declaration.
3925+
///
3926+
/// For statics, there may be at most one `EiiImpl`, but this is a `ThinVec` to make usages of this field nicer.
3927+
pub eii_impls: ThinVec<EiiImpl>,
39213928
}
39223929

39233930
#[derive(Clone, Encodable, Decodable, Debug, Walkable)]

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,14 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
213213
i: &ItemKind,
214214
) -> Vec<hir::Attribute> {
215215
match i {
216-
ItemKind::Fn(box Fn { eii_impls, .. }) if eii_impls.is_empty() => Vec::new(),
217-
ItemKind::Fn(box Fn { eii_impls, .. }) => {
216+
ItemKind::Fn(box Fn { eii_impls, .. })
217+
| ItemKind::Static(box StaticItem { eii_impls, .. })
218+
if eii_impls.is_empty() =>
219+
{
220+
Vec::new()
221+
}
222+
ItemKind::Fn(box Fn { eii_impls, .. })
223+
| ItemKind::Static(box StaticItem { eii_impls, .. }) => {
218224
vec![hir::Attribute::Parsed(AttributeKind::EiiImpls(
219225
eii_impls.iter().map(|i| self.lower_eii_impl(i)).collect(),
220226
))]
@@ -226,7 +232,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
226232

227233
ItemKind::ExternCrate(..)
228234
| ItemKind::Use(..)
229-
| ItemKind::Static(..)
230235
| ItemKind::Const(..)
231236
| ItemKind::ConstBlock(..)
232237
| ItemKind::Mod(..)
@@ -302,6 +307,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
302307
mutability: m,
303308
expr: e,
304309
define_opaque,
310+
eii_impls: _,
305311
}) => {
306312
let ident = self.lower_ident(*ident);
307313
let ty = self
@@ -826,6 +832,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
826832
expr: _,
827833
safety,
828834
define_opaque,
835+
eii_impls: _,
829836
}) => {
830837
let ty = self
831838
.lower_ty_alloc(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl<'a> State<'a> {
4343
expr,
4444
safety,
4545
define_opaque,
46+
eii_impls,
4647
}) => self.print_item_const(
4748
*ident,
4849
Some(*mutability),
@@ -53,6 +54,7 @@ impl<'a> State<'a> {
5354
*safety,
5455
ast::Defaultness::Implicit,
5556
define_opaque.as_deref(),
57+
eii_impls,
5658
),
5759
ast::ForeignItemKind::TyAlias(box ast::TyAlias {
5860
defaultness,
@@ -93,8 +95,12 @@ impl<'a> State<'a> {
9395
safety: ast::Safety,
9496
defaultness: ast::Defaultness,
9597
define_opaque: Option<&[(ast::NodeId, ast::Path)]>,
98+
eii_impls: &[EiiImpl],
9699
) {
97100
self.print_define_opaques(define_opaque);
101+
for eii_impl in eii_impls {
102+
self.print_eii_impl(eii_impl);
103+
}
98104
let (cb, ib) = self.head("");
99105
self.print_visibility(vis);
100106
self.print_safety(safety);
@@ -191,6 +197,7 @@ impl<'a> State<'a> {
191197
mutability: mutbl,
192198
expr: body,
193199
define_opaque,
200+
eii_impls,
194201
}) => {
195202
self.print_safety(*safety);
196203
self.print_item_const(
@@ -203,6 +210,7 @@ impl<'a> State<'a> {
203210
ast::Safety::Default,
204211
ast::Defaultness::Implicit,
205212
define_opaque.as_deref(),
213+
eii_impls,
206214
);
207215
}
208216
ast::ItemKind::ConstBlock(ast::ConstBlockItem { id: _, span: _, block }) => {
@@ -234,6 +242,7 @@ impl<'a> State<'a> {
234242
ast::Safety::Default,
235243
*defaultness,
236244
define_opaque.as_deref(),
245+
&[],
237246
);
238247
}
239248
ast::ItemKind::Fn(func) => {
@@ -602,6 +611,7 @@ impl<'a> State<'a> {
602611
ast::Safety::Default,
603612
*defaultness,
604613
define_opaque.as_deref(),
614+
&[],
605615
);
606616
}
607617
ast::AssocItemKind::Type(box ast::TyAlias {
@@ -703,18 +713,8 @@ impl<'a> State<'a> {
703713

704714
self.print_define_opaques(define_opaque.as_deref());
705715

706-
for EiiImpl { eii_macro_path, impl_safety, .. } in eii_impls {
707-
self.word("#[");
708-
if let Safety::Unsafe(..) = impl_safety {
709-
self.word("unsafe");
710-
self.popen();
711-
}
712-
self.print_path(eii_macro_path, false, 0);
713-
if let Safety::Unsafe(..) = impl_safety {
714-
self.pclose();
715-
}
716-
self.word("]");
717-
self.hardbreak();
716+
for eii_impl in eii_impls {
717+
self.print_eii_impl(eii_impl);
718718
}
719719

720720
let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
@@ -741,6 +741,20 @@ impl<'a> State<'a> {
741741
}
742742
}
743743

744+
fn print_eii_impl(&mut self, eii: &ast::EiiImpl) {
745+
self.word("#[");
746+
if let Safety::Unsafe(..) = eii.impl_safety {
747+
self.word("unsafe");
748+
self.popen();
749+
}
750+
self.print_path(&eii.eii_macro_path, false, 0);
751+
if let Safety::Unsafe(..) = eii.impl_safety {
752+
self.pclose();
753+
}
754+
self.word("]");
755+
self.hardbreak();
756+
}
757+
744758
fn print_define_opaques(&mut self, define_opaque: Option<&[(ast::NodeId, ast::Path)]>) {
745759
if let Some(define_opaque) = define_opaque {
746760
self.word("#[define_opaque(");

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,8 @@ pub(crate) struct RustcEiiForeignItemParser;
709709
impl<S: Stage> NoArgsAttributeParser<S> for RustcEiiForeignItemParser {
710710
const PATH: &[Symbol] = &[sym::rustc_eii_foreign_item];
711711
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
712-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::ForeignFn)]);
712+
const ALLOWED_TARGETS: AllowedTargets =
713+
AllowedTargets::AllowList(&[Allow(Target::ForeignFn), Allow(Target::ForeignStatic)]);
713714
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::RustcEiiForeignItem;
714715
}
715716

0 commit comments

Comments
 (0)