Skip to content

Commit 9f66c46

Browse files
committed
add registry macro
1 parent 2a1ce93 commit 9f66c46

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
@@ -3614,6 +3614,18 @@ pub struct DelegationMac {
36143614
pub body: Option<P<Block>>,
36153615
}
36163616

3617+
#[derive(Clone, Encodable, Decodable, Debug, Default)]
3618+
pub enum DistributedSlice {
3619+
/// This const or static has nothing to do with global registration whatsoever
3620+
#[default]
3621+
None,
3622+
/// This const or static declares a global registry that can be added to
3623+
Declaration(Span),
3624+
/// This const (we never do this to statics) represents an addition to a global registry
3625+
/// declared somewhere else.
3626+
Addition { declaration: Path },
3627+
}
3628+
36173629
#[derive(Clone, Encodable, Decodable, Debug)]
36183630
pub struct StaticItem {
36193631
pub ident: Ident,
@@ -3622,6 +3634,7 @@ pub struct StaticItem {
36223634
pub mutability: Mutability,
36233635
pub expr: Option<P<Expr>>,
36243636
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3637+
pub distributed_slice: DistributedSlice,
36253638
}
36263639

36273640
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -3632,6 +3645,7 @@ pub struct ConstItem {
36323645
pub ty: P<Ty>,
36333646
pub expr: Option<P<Expr>>,
36343647
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
3648+
pub distributed_slice: DistributedSlice,
36353649
}
36363650

36373651
// 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
@@ -41,6 +41,7 @@ mod concat_idents;
4141
mod define_opaque;
4242
mod derive;
4343
mod deriving;
44+
mod distributed_slice;
4445
mod edition_panic;
4546
mod env;
4647
mod errors;
@@ -121,6 +122,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
121122
global_allocator: global_allocator::expand,
122123
test: test::expand_test,
123124
test_case: test::expand_test_case,
125+
distributed_slice: distributed_slice::distributed_slice,
124126
}
125127

126128
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

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
246246
expr: _,
247247
safety,
248248
define_opaque: _,
249+
distributed_slice: _,
249250
}) => {
250251
let safety = match safety {
251252
ast::Safety::Unsafe(_) | ast::Safety::Default => hir::Safety::Unsafe,

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,7 @@ symbols! {
833833
discriminant_value,
834834
disjoint_bitor,
835835
dispatch_from_dyn,
836+
distributed_slice,
836837
div,
837838
div_assign,
838839
diverging_block_default,

library/core/src/macros/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,4 +1804,14 @@ pub(crate) mod builtin {
18041804
pub macro deref($pat:pat) {
18051805
builtin # deref($pat)
18061806
}
1807+
1808+
/// Create a global registry.
1809+
///
1810+
// FIXME(gr): docs
1811+
#[unstable(feature = "crate_local_distributed_slice", issue = "125119")]
1812+
#[rustc_builtin_macro]
1813+
#[cfg(not(bootstrap))]
1814+
pub macro distributed_slice($item:item) {
1815+
/* compiler built-in */
1816+
}
18071817
}

library/core/src/prelude/v1.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ pub use crate::macros::builtin::{
8181
alloc_error_handler, bench, derive, global_allocator, test, test_case,
8282
};
8383

84+
#[unstable(feature = "crate_local_distributed_slice", issue = "125119")]
85+
#[cfg(not(bootstrap))]
86+
pub use crate::macros::builtin::distributed_slice;
87+
8488
#[unstable(feature = "derive_const", issue = "none")]
8589
pub use crate::macros::builtin::derive_const;
8690

library/std/src/prelude/v1.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ pub use core::prelude::v1::{
6868
alloc_error_handler, bench, derive, global_allocator, test, test_case,
6969
};
7070

71+
#[unstable(feature = "crate_local_distributed_slice", issue = "125119")]
72+
#[cfg(not(bootstrap))]
73+
pub use core::prelude::v1::distributed_slice;
74+
7175
#[unstable(feature = "derive_const", issue = "none")]
7276
pub use core::prelude::v1::derive_const;
7377

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(crate_local_distributed_slice)]
2+
// @build-pass
3+
4+
#[distributed_slice(crate)]
5+
const MEOWS: [&str; _];
6+
7+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: free constant item without body
2+
--> $DIR/create_slice.rs:5:1
3+
|
4+
LL | const MEOWS: [&str; _];
5+
| ^^^^^^^^^^^^^^^^^^^^^^-
6+
| |
7+
| help: provide a definition for the constant: `= <expr>;`
8+
9+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
10+
--> $DIR/create_slice.rs:5:21
11+
|
12+
LL | const MEOWS: [&str; _];
13+
| ^ not allowed in type signatures
14+
15+
error: aborting due to 2 previous errors
16+
17+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)