Skip to content

Commit 025342e

Browse files
Rollup merge of rust-lang#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 ecd2b10 + 106b16c commit 025342e

75 files changed

Lines changed: 1036 additions & 174 deletions

File tree

Some content is hidden

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

compiler/rustc_ast/src/ast.rs

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

39283935
#[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) => {
@@ -605,6 +614,7 @@ impl<'a> State<'a> {
605614
ast::Safety::Default,
606615
*defaultness,
607616
define_opaque.as_deref(),
617+
&[],
608618
);
609619
}
610620
ast::AssocItemKind::Type(box ast::TyAlias {
@@ -709,18 +719,8 @@ impl<'a> State<'a> {
709719

710720
self.print_define_opaques(define_opaque.as_deref());
711721

712-
for EiiImpl { eii_macro_path, impl_safety, .. } in eii_impls {
713-
self.word("#[");
714-
if let Safety::Unsafe(..) = impl_safety {
715-
self.word("unsafe");
716-
self.popen();
717-
}
718-
self.print_path(eii_macro_path, false, 0);
719-
if let Safety::Unsafe(..) = impl_safety {
720-
self.pclose();
721-
}
722-
self.word("]");
723-
self.hardbreak();
722+
for eii_impl in eii_impls {
723+
self.print_eii_impl(eii_impl);
724724
}
725725

726726
let body_cb_ib = body.as_ref().map(|body| (body, self.head("")));
@@ -747,6 +747,20 @@ impl<'a> State<'a> {
747747
}
748748
}
749749

750+
fn print_eii_impl(&mut self, eii: &ast::EiiImpl) {
751+
self.word("#[");
752+
if let Safety::Unsafe(..) = eii.impl_safety {
753+
self.word("unsafe");
754+
self.popen();
755+
}
756+
self.print_path(&eii.eii_macro_path, false, 0);
757+
if let Safety::Unsafe(..) = eii.impl_safety {
758+
self.pclose();
759+
}
760+
self.word("]");
761+
self.hardbreak();
762+
}
763+
750764
fn print_define_opaques(&mut self, define_opaque: Option<&[(ast::NodeId, ast::Path)]>) {
751765
if let Some(define_opaque) = define_opaque {
752766
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)