Skip to content

Commit e2df1d9

Browse files
committed
add registry macro
1 parent 2cff157 commit e2df1d9

File tree

19 files changed

+123
-4
lines changed

19 files changed

+123
-4
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3601,6 +3601,18 @@ pub struct DelegationMac {
36013601
pub body: Option<P<Block>>,
36023602
}
36033603

3604+
#[derive(Clone, Encodable, Decodable, Debug, Default)]
3605+
pub enum DistributedSlice {
3606+
/// This const or static has nothing to do with global registration whatsoever
3607+
#[default]
3608+
None,
3609+
/// This const or static declares a global registry that can be added to
3610+
Declaration(Span),
3611+
/// This const (we never do this to statics) represents an addition to a global registry
3612+
/// declared somewhere else.
3613+
Addition { declaration: Path },
3614+
}
3615+
36043616
#[derive(Clone, Encodable, Decodable, Debug)]
36053617
pub struct StaticItem {
36063618
pub ident: Ident,
@@ -3609,6 +3621,7 @@ pub struct StaticItem {
36093621
pub mutability: Mutability,
36103622
pub expr: Option<P<Expr>>,
36113623
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3624+
pub distributed_slice: DistributedSlice,
36123625
}
36133626

36143627
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3619,6 +3632,7 @@ pub struct ConstItem {
36193632
pub ty: P<Ty>,
36203633
pub expr: Option<P<Expr>>,
36213634
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3635+
pub distributed_slice: DistributedSlice,
36223636
}
36233637

36243638
// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.

compiler/rustc_ast/src/visit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ macro_rules! common_visitor_and_walkers {
451451
mutability: _,
452452
expr,
453453
define_opaque,
454+
distributed_slice: _,
454455
}) => {
455456
try_visit!(vis.visit_ident(ident));
456457
try_visit!(vis.visit_ty(ty));
@@ -614,7 +615,7 @@ macro_rules! common_visitor_and_walkers {
614615
}
615616

616617
fn walk_const_item<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, item: &$($lt)? $($mut)? ConstItem) $(-> <V as Visitor<$lt>>::Result)? {
617-
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque } = item;
618+
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque, distributed_slice: _ } = item;
618619
try_visit!(visit_defaultness(vis, defaultness));
619620
try_visit!(vis.visit_ident(ident));
620621
try_visit!(vis.visit_generics(generics));
@@ -739,6 +740,7 @@ macro_rules! common_visitor_and_walkers {
739740
expr,
740741
safety: _,
741742
define_opaque,
743+
distributed_slice: _,
742744
}) => {
743745
try_visit!(vis.visit_ident(ident));
744746
try_visit!(vis.visit_ty(ty));

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
175175
mutability: m,
176176
expr: e,
177177
define_opaque,
178+
distributed_slice,
178179
}) => {
179180
let ident = self.lower_ident(*ident);
180181
let (ty, body_id) =
@@ -660,6 +661,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
660661
expr: _,
661662
safety,
662663
define_opaque,
664+
distributed_slice,
663665
}) => {
664666
let ty =
665667
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::StaticTy));

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ impl<'a> State<'a> {
4545
expr,
4646
safety,
4747
define_opaque,
48+
// FIXME(gr) pretty print global registry
49+
distributed_slice: _,
4850
}) => self.print_item_const(
4951
*ident,
5052
Some(*mutability),
@@ -195,7 +197,9 @@ impl<'a> State<'a> {
195197
mutability: mutbl,
196198
expr: body,
197199
define_opaque,
200+
distributed_slice: _,
198201
}) => {
202+
// FIXME(gr): pretty print global registry
199203
self.print_safety(*safety);
200204
self.print_item_const(
201205
*ident,
@@ -216,7 +220,9 @@ impl<'a> State<'a> {
216220
ty,
217221
expr,
218222
define_opaque,
223+
distributed_slice: _,
219224
}) => {
225+
// FIXME(gr): pretty print global registry
220226
self.print_item_const(
221227
*ident,
222228
None,
@@ -565,6 +571,8 @@ impl<'a> State<'a> {
565571
ty,
566572
expr,
567573
define_opaque,
574+
// FIXME(gr) pretty print global registry
575+
distributed_slice: _,
568576
}) => {
569577
self.print_item_const(
570578
*ident,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use rustc_ast::{DistributedSlice, ItemKind, ast};
2+
use rustc_expand::base::{Annotatable, ExtCtxt};
3+
use rustc_span::Span;
4+
5+
pub(crate) fn distributed_slice(
6+
_ecx: &mut ExtCtxt<'_>,
7+
span: Span,
8+
_meta_item: &ast::MetaItem,
9+
mut orig_item: Annotatable,
10+
) -> Vec<Annotatable> {
11+
// TODO: FIXME(gr)
12+
// FIXME(gr): check item
13+
14+
let Annotatable::Item(item) = &mut orig_item else {
15+
panic!("expected `#[distributed_slice]` on an item")
16+
};
17+
18+
match &mut item.kind {
19+
ItemKind::Static(static_item) => {
20+
static_item.distributed_slice = DistributedSlice::Declaration(span);
21+
}
22+
ItemKind::Const(const_item) => {
23+
const_item.distributed_slice = DistributedSlice::Declaration(span);
24+
}
25+
other => {
26+
panic!("expected `#[distributed_slice]` on a const or static item, not {other:?}");
27+
}
28+
}
29+
30+
vec![orig_item]
31+
}

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod concat_idents;
4343
mod define_opaque;
4444
mod derive;
4545
mod deriving;
46+
mod distributed_slice;
4647
mod edition_panic;
4748
mod env;
4849
mod errors;
@@ -122,6 +123,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
122123
global_allocator: global_allocator::expand,
123124
test: test::expand_test,
124125
test_case: test::expand_test_case,
126+
distributed_slice: distributed_slice::distributed_slice,
125127
}
126128

127129
register_derive! {

compiler/rustc_builtin_macros/src/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ pub(crate) fn expand_test_or_bench(
377377
],
378378
), // }
379379
),
380+
// FIXME(gr) tests should be GR
381+
distributed_slice: Default::default(),
380382
}
381383
.into(),
382384
),

compiler/rustc_expand/src/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ impl<'a> ExtCtxt<'a> {
700700
mutability,
701701
expr: Some(expr),
702702
define_opaque: None,
703+
distributed_slice: Default::default(),
703704
}
704705
.into(),
705706
),
@@ -726,6 +727,7 @@ impl<'a> ExtCtxt<'a> {
726727
ty,
727728
expr: Some(expr),
728729
define_opaque: None,
730+
distributed_slice: Default::default(),
729731
}
730732
.into(),
731733
),

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,8 @@ declare_features! (
454454
/// Allows function attribute `#[coverage(on/off)]`, to control coverage
455455
/// instrumentation of that function.
456456
(unstable, coverage_attribute, "1.74.0", Some(84605)),
457+
/// Allows the creation of crate local distributed slices, slices where the elements can be added all throughout the crate
458+
(unstable, crate_local_distributed_slice, "CURRENT_RUSTC_VERSION", Some(125119)),
457459
/// Allows non-builtin attributes in inner attribute position.
458460
(unstable, custom_inner_attributes, "1.30.0", Some(54726)),
459461
/// Allows custom test frameworks with `#![test_runner]` and `#[test_case]`.
@@ -526,8 +528,6 @@ declare_features! (
526528
(incomplete, generic_const_parameter_types, "1.87.0", Some(137626)),
527529
/// Allows any generic constants being used as pattern type range ends
528530
(incomplete, generic_pattern_types, "1.86.0", Some(136574)),
529-
/// Allows the creation of crate local distributed slices, slices where the elements can be added all throughout the crate
530-
(unstable, crate_local_distributed_slice, "CURRENT_RUSTC_VERSION", Some(125119)),
531531
/// Allows using guards in patterns.
532532
(incomplete, guard_patterns, "1.85.0", Some(129967)),
533533
/// Allows using `..=X` as a patterns in slices.

compiler/rustc_parse/src/parser/item.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ impl<'a> Parser<'a> {
260260
ty,
261261
expr,
262262
define_opaque: None,
263+
distributed_slice: Default::default(),
263264
}))
264265
}
265266
} else if self.check_keyword(exp!(Trait)) || self.check_auto_or_unsafe_trait_item() {
@@ -966,6 +967,7 @@ impl<'a> Parser<'a> {
966967
mutability: _,
967968
expr,
968969
define_opaque,
970+
distributed_slice,
969971
}) => {
970972
self.dcx().emit_err(errors::AssociatedStaticItemNotAllowed { span });
971973
AssocItemKind::Const(Box::new(ConstItem {
@@ -975,6 +977,7 @@ impl<'a> Parser<'a> {
975977
ty,
976978
expr,
977979
define_opaque,
980+
distributed_slice,
978981
}))
979982
}
980983
_ => return self.error_bad_item_kind(span, &kind, "`trait`s or `impl`s"),
@@ -1241,6 +1244,7 @@ impl<'a> Parser<'a> {
12411244
expr,
12421245
safety: Safety::Default,
12431246
define_opaque: None,
1247+
distributed_slice: Default::default(),
12441248
}))
12451249
}
12461250
_ => return self.error_bad_item_kind(span, &kind, "`extern` blocks"),
@@ -1398,7 +1402,15 @@ impl<'a> Parser<'a> {
13981402

13991403
self.expect_semi()?;
14001404

1401-
let item = StaticItem { ident, ty, safety, mutability, expr, define_opaque: None };
1405+
let item = StaticItem {
1406+
ident,
1407+
ty,
1408+
safety,
1409+
mutability,
1410+
expr,
1411+
define_opaque: None,
1412+
distributed_slice: Default::default(),
1413+
};
14021414
Ok(ItemKind::Static(Box::new(item)))
14031415
}
14041416

0 commit comments

Comments
 (0)