Skip to content

[DO NOT MERGE] Crate local distributed slice #141830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3812,6 +3812,8 @@ name = "rustc_hir_analysis"
version = "0.0.0"
dependencies = [
"itertools",
"rand 0.9.1",
"rand_xoshiro",
"rustc_abi",
"rustc_arena",
"rustc_ast",
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3614,6 +3614,24 @@ pub struct DelegationMac {
pub body: Option<P<Block>>,
}

#[derive(Clone, Encodable, Decodable, Debug, Default)]
pub enum DistributedSlice {
/// This const or static has nothing to do with global registration whatsoever
#[default]
None,
/// This const or static declares a global registry that can be added to
Declaration(Span, NodeId),
/// This const (we never do this to statics) represents an addition to a global registry
/// declared somewhere else.
Addition { declaration: Path, id: NodeId },
/// This const (we never do this to statics) represents an addition of an array
/// to a global registry declared somewhere else. All elements are added, though not necessarily
/// in the same order as in the original slice.
AdditionMany { declaration: Path, id: NodeId },
/// Applied to an invalid item, error guaranteed to have be emitted
Err(ErrorGuaranteed),
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct StaticItem {
pub ident: Ident,
Expand All @@ -3622,6 +3640,7 @@ pub struct StaticItem {
pub mutability: Mutability,
pub expr: Option<P<Expr>>,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
pub distributed_slice: DistributedSlice,
}

#[derive(Clone, Encodable, Decodable, Debug)]
Expand All @@ -3632,6 +3651,7 @@ pub struct ConstItem {
pub ty: P<Ty>,
pub expr: Option<P<Expr>>,
pub define_opaque: Option<ThinVec<(NodeId, Path)>>,
pub distributed_slice: DistributedSlice,
}

// Adding a new variant? Please update `test_item` in `tests/ui/macros/stringify.rs`.
Expand Down
39 changes: 38 additions & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,27 @@ macro_rules! common_visitor_and_walkers {
mutability: _,
expr,
define_opaque,
distributed_slice,
}) => {
try_visit!(vis.visit_ident(ident));
try_visit!(vis.visit_ty(ty));
visit_opt!(vis, visit_expr, expr);
match distributed_slice {
DistributedSlice::None => {}
DistributedSlice::Err(..) => {}
DistributedSlice::Declaration(span, id) => {
try_visit!(visit_span(vis, span));
try_visit!(visit_id(vis, id));
}
DistributedSlice::Addition { declaration, id } => {
try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?));
try_visit!(visit_id(vis, id));
}
DistributedSlice::AdditionMany { declaration, id } => {
try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?));
try_visit!(visit_id(vis, id));
}
}
walk_define_opaques(vis, define_opaque)
}
ItemKind::Const(item) => {
Expand Down Expand Up @@ -614,12 +631,31 @@ macro_rules! common_visitor_and_walkers {
}

fn walk_const_item<$($lt,)? V: $Visitor$(<$lt>)?>(vis: &mut V, item: &$($lt)? $($mut)? ConstItem) $(-> <V as Visitor<$lt>>::Result)? {
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque } = item;
let ConstItem { defaultness, ident, generics, ty, expr, define_opaque, distributed_slice } = item;
try_visit!(visit_defaultness(vis, defaultness));
try_visit!(vis.visit_ident(ident));
try_visit!(vis.visit_generics(generics));
try_visit!(vis.visit_ty(ty));
visit_opt!(vis, visit_expr, expr);

match distributed_slice {
DistributedSlice::None => {}
DistributedSlice::Err(..) => {}
DistributedSlice::Declaration(span, id) => {
try_visit!(visit_span(vis, span));
try_visit!(visit_id(vis, id));
}
DistributedSlice::Addition { declaration, id } => {
try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?));
try_visit!(visit_id(vis, id));
}
DistributedSlice::AdditionMany { declaration, id } => {
try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?));
try_visit!(visit_id(vis, id));
}
}


walk_define_opaques(vis, define_opaque)
}

Expand Down Expand Up @@ -739,6 +775,7 @@ macro_rules! common_visitor_and_walkers {
expr,
safety: _,
define_opaque,
distributed_slice: _,
}) => {
try_visit!(vis.visit_ident(ident));
try_visit!(vis.visit_ty(ty));
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ ast_lowering_coroutine_too_many_parameters =
ast_lowering_default_field_in_tuple = default fields are not supported in tuple structs
.label = default fields are only supported on structs

ast_lowering_distributed_slice_elements_wrong_expr =
`distributed_slice_elements!()` only accepts a path or array literal
.note = arbitrary expressions are not supported
ast_lowering_distributed_slice_with_initializer =
distributed slice elements are added with `distributed_slice_element!(...)`
ast_lowering_does_not_support_modifiers =
the `{$class_name}` register class does not support template modifiers

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Let statements are allowed to have impl trait in bindings.
let super_ = l.super_;
let ty = l.ty.as_ref().map(|t| {
self.lower_ty(t, self.impl_trait_in_bindings_ctxt(ImplTraitPosition::Variable))
self.lower_ty(t, self.impl_trait_in_bindings_ctxt(ImplTraitPosition::Variable), false)
});
let init = l.kind.init().map(|init| self.lower_expr(init));
let hir_id = self.lower_node_id(l.id);
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,18 @@ pub(crate) struct UseConstGenericArg {
#[suggestion_part(code = "{other_args}")]
pub call_args: Span,
}

#[derive(Diagnostic)]
#[diag(ast_lowering_distributed_slice_with_initializer)]
pub(crate) struct DistributedSliceWithInitializer {
#[primary_span]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(ast_lowering_distributed_slice_elements_wrong_expr)]
#[note]
pub(crate) struct DistributedSliceElementsWrongExpr {
#[primary_span]
pub span: Span,
}
25 changes: 18 additions & 7 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
ExprKind::ConstBlock(c) => hir::ExprKind::ConstBlock(self.lower_const_block(c)),
ExprKind::Repeat(expr, count) => {
let expr = self.lower_expr(expr);
let count = self.lower_array_length_to_const_arg(count);
let count = self.lower_array_length_to_const_arg(count, false);
hir::ExprKind::Repeat(expr, count)
}
ExprKind::Tup(elts) => hir::ExprKind::Tup(self.lower_exprs(elts)),
Expand Down Expand Up @@ -154,14 +154,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
ExprKind::Cast(expr, ty) => {
let expr = self.lower_expr(expr);
let ty =
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
let ty = self.lower_ty(
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::Cast),
false,
);
hir::ExprKind::Cast(expr, ty)
}
ExprKind::Type(expr, ty) => {
let expr = self.lower_expr(expr);
let ty =
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast));
let ty = self.lower_ty(
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::Cast),
false,
);
hir::ExprKind::Type(expr, ty)
}
ExprKind::AddrOf(k, m, ohs) => {
Expand Down Expand Up @@ -329,6 +335,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.lower_ty(
container,
ImplTraitContext::Disallowed(ImplTraitPosition::OffsetOf),
false,
),
self.arena.alloc_from_iter(fields.iter().map(|&ident| self.lower_ident(ident))),
),
Expand Down Expand Up @@ -360,7 +367,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
*kind,
self.lower_expr(expr),
ty.as_ref().map(|ty| {
self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Cast))
self.lower_ty(
ty,
ImplTraitContext::Disallowed(ImplTraitPosition::Cast),
false,
)
}),
),

Expand Down Expand Up @@ -417,7 +428,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ConstBlock {
def_id,
hir_id: this.lower_node_id(c.id),
body: this.lower_const_body(c.value.span, Some(&c.value)),
body: this.lower_const_body(c.value.span, Some(&c.value), None),
}
})
}
Expand Down
Loading
Loading