Skip to content

Commit 37e5111

Browse files
committed
finish typechecking
1 parent 2a4495a commit 37e5111

File tree

25 files changed

+164
-85
lines changed

25 files changed

+164
-85
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3620,7 +3620,7 @@ pub enum DistributedSlice {
36203620
#[default]
36213621
None,
36223622
/// This const or static declares a global registry that can be added to
3623-
Declaration(Span),
3623+
Declaration(Span, NodeId),
36243624
/// This const (we never do this to statics) represents an addition to a global registry
36253625
/// declared somewhere else.
36263626
Addition { declaration: Path, id: NodeId },

compiler/rustc_ast/src/visit.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,14 @@ macro_rules! common_visitor_and_walkers {
458458
visit_opt!(vis, visit_expr, expr);
459459
match distributed_slice {
460460
DistributedSlice::None => {}
461-
DistributedSlice::Declaration(span) => try_visit!(visit_span(vis, span)),
462-
DistributedSlice::Addition { declaration, id } => try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?)),
461+
DistributedSlice::Declaration(span, id) => {
462+
try_visit!(visit_span(vis, span));
463+
try_visit!(visit_id(vis, id));
464+
}
465+
DistributedSlice::Addition { declaration, id } => {
466+
try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?));
467+
try_visit!(visit_id(vis, id));
468+
}
463469
}
464470
walk_define_opaques(vis, define_opaque)
465471
}
@@ -629,8 +635,14 @@ macro_rules! common_visitor_and_walkers {
629635

630636
match distributed_slice {
631637
DistributedSlice::None => {}
632-
DistributedSlice::Declaration(span) => try_visit!(visit_span(vis, span)),
633-
DistributedSlice::Addition { declaration, id } => try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?)),
638+
DistributedSlice::Declaration(span, id) => {
639+
try_visit!(visit_span(vis, span));
640+
try_visit!(visit_id(vis, id));
641+
}
642+
DistributedSlice::Addition { declaration, id } => {
643+
try_visit!(vis.visit_path(declaration$(${ignore($lt)}, *id)?));
644+
try_visit!(visit_id(vis, id));
645+
}
634646
}
635647

636648

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
428428
hir::ConstBlock {
429429
def_id,
430430
hir_id: this.lower_node_id(c.id),
431-
body: this.lower_const_body(c.value.span, Some(&c.value)),
431+
body: this.lower_const_body(c.value.span, Some(&c.value), None),
432432
}
433433
})
434434
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
154154
) -> DistributedSlice {
155155
match distributed_slice {
156156
ast::DistributedSlice::None => DistributedSlice::None,
157-
ast::DistributedSlice::Declaration(span) => {
157+
ast::DistributedSlice::Declaration(span, _) => {
158158
DistributedSlice::Declaration(self.lower_span(*span))
159159
}
160160
ast::DistributedSlice::Addition { declaration, id } => {
@@ -544,12 +544,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
544544
impl_trait_position: ImplTraitPosition,
545545
distributed_slice: &ast::DistributedSlice,
546546
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
547+
let distributed_slice_declaration =
548+
if let ast::DistributedSlice::Declaration(_, node_id) = distributed_slice {
549+
Some(*node_id)
550+
} else {
551+
None
552+
};
553+
547554
let ty = self.lower_ty(
548555
ty,
549556
ImplTraitContext::Disallowed(impl_trait_position),
550-
matches!(distributed_slice, ast::DistributedSlice::Declaration(..)),
557+
distributed_slice_declaration.is_some(),
551558
);
552-
(ty, self.lower_const_body(span, body))
559+
(ty, self.lower_const_body(span, body, distributed_slice_declaration))
553560
}
554561

555562
#[instrument(level = "debug", skip(self))]
@@ -866,7 +873,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
866873
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
867874
false,
868875
);
869-
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
876+
let body =
877+
expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x), None));
870878

871879
hir::TraitItemKind::Const(ty, body)
872880
},
@@ -1063,7 +1071,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10631071
ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
10641072
false,
10651073
);
1066-
let body = this.lower_const_body(i.span, expr.as_deref());
1074+
let body = this.lower_const_body(i.span, expr.as_deref(), None);
10671075
this.lower_define_opaque(hir_id, &define_opaque);
10681076
hir::ImplItemKind::Const(ty, body)
10691077
},
@@ -1342,13 +1350,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
13421350
self.lower_fn_body(decl, contract, |this| this.lower_block_expr(body))
13431351
}
13441352

1345-
pub(super) fn lower_const_body(&mut self, span: Span, expr: Option<&Expr>) -> hir::BodyId {
1353+
pub(super) fn lower_const_body(
1354+
&mut self,
1355+
span: Span,
1356+
expr: Option<&Expr>,
1357+
global_registry_declaration: Option<NodeId>,
1358+
) -> hir::BodyId {
13461359
self.lower_body(|this| {
13471360
(
13481361
&[],
1349-
match expr {
1350-
Some(expr) => this.lower_expr_mut(expr),
1351-
None => this.expr_err(span, this.dcx().span_delayed_bug(span, "no block")),
1362+
match (expr, global_registry_declaration) {
1363+
(Some(expr), None) => this.lower_expr_mut(expr),
1364+
(None, Some(node_id)) => {
1365+
let expr_hir_id = this.lower_node_id(node_id);
1366+
hir::Expr {
1367+
hir_id: expr_hir_id,
1368+
kind: rustc_hir::ExprKind::DistributedSliceDeferredArray,
1369+
span: this.lower_span(span),
1370+
}
1371+
}
1372+
(Some(expr), Some(_)) => panic!("distributed slice with initializer"),
1373+
(None, None) => {
1374+
this.expr_err(span, this.dcx().span_delayed_bug(span, "no block"))
1375+
}
13521376
},
13531377
)
13541378
})

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21562156
self.arena.alloc(hir::AnonConst {
21572157
def_id,
21582158
hir_id,
2159-
body: this.lower_const_body(path_expr.span, Some(&path_expr)),
2159+
body: this.lower_const_body(path_expr.span, Some(&path_expr), None),
21602160
span,
21612161
})
21622162
});
@@ -2223,7 +2223,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22232223
hir::AnonConst {
22242224
def_id,
22252225
hir_id,
2226-
body: this.lower_const_body(c.value.span, Some(&c.value)),
2226+
body: this.lower_const_body(c.value.span, Some(&c.value), None),
22272227
span: this.lower_span(c.value.span),
22282228
}
22292229
}))

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10891089

10901090
// declarations of global registries have no body deliberately - items are added
10911091
// later using global registry additions
1092-
if expr.is_none() && !matches!(distributed_slice, DistributedSlice::Declaration(_))
1092+
if expr.is_none() && !matches!(distributed_slice, DistributedSlice::Declaration(..))
10931093
{
10941094
self.dcx().emit_err(errors::ConstWithoutBody {
10951095
span: item.span,
@@ -1104,9 +1104,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11041104
self.dcx().emit_err(errors::UnsafeStatic { span: item.span });
11051105
}
11061106

1107-
// declarations of global registries have no body deliberately - items are added
1108-
// later using global registry additions
1109-
if expr.is_none() && !matches!(distributed_slice, DistributedSlice::Declaration(_))
1107+
// declarations of distributed slices have no body deliberately - items are added
1108+
// later using `distributed_slice_element`
1109+
if expr.is_none() && !matches!(distributed_slice, DistributedSlice::Declaration(..))
11101110
{
11111111
self.dcx().emit_err(errors::StaticWithoutBody {
11121112
span: item.span,

compiler/rustc_builtin_macros/src/distributed_slice.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ pub(crate) fn distributed_slice(
3333

3434
match &mut item.kind {
3535
ItemKind::Static(static_item) => {
36-
static_item.distributed_slice = DistributedSlice::Declaration(span);
36+
static_item.distributed_slice = DistributedSlice::Declaration(span, DUMMY_NODE_ID);
3737
}
3838
ItemKind::Const(const_item) => {
39-
const_item.distributed_slice = DistributedSlice::Declaration(span);
39+
const_item.distributed_slice = DistributedSlice::Declaration(span, DUMMY_NODE_ID);
4040
}
4141
other => {
4242
panic!(
@@ -49,14 +49,14 @@ pub(crate) fn distributed_slice(
4949
}
5050

5151
fn parse_element(mut p: Parser<'_>) -> PResult<'_, (Path, P<Expr>)> {
52-
let ident = p.parse_path(PathStyle::Expr)?;
52+
let path = p.parse_path(PathStyle::Expr)?;
5353
p.expect(exp![Comma])?;
5454
let expr = p.parse_expr()?;
5555

5656
// optional trailing comma
5757
let _ = p.eat(exp![Comma]);
5858

59-
Ok((ident, expr))
59+
Ok((path, expr))
6060
}
6161

6262
/// ```rust

compiler/rustc_hir/src/hir.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,6 +2329,7 @@ impl Expr<'_> {
23292329
| ExprKind::Type(..)
23302330
| ExprKind::UnsafeBinderCast(..)
23312331
| ExprKind::Use(..)
2332+
| ExprKind::DistributedSliceDeferredArray
23322333
| ExprKind::Err(_) => ExprPrecedence::Unambiguous,
23332334

23342335
ExprKind::DropTemps(expr, ..) => expr.precedence(),
@@ -2402,6 +2403,7 @@ impl Expr<'_> {
24022403
| ExprKind::Yield(..)
24032404
| ExprKind::Cast(..)
24042405
| ExprKind::DropTemps(..)
2406+
| ExprKind::DistributedSliceDeferredArray
24052407
| ExprKind::Err(_) => false,
24062408
}
24072409
}
@@ -2449,9 +2451,11 @@ impl Expr<'_> {
24492451

24502452
pub fn can_have_side_effects(&self) -> bool {
24512453
match self.peel_drop_temps().kind {
2452-
ExprKind::Path(_) | ExprKind::Lit(_) | ExprKind::OffsetOf(..) | ExprKind::Use(..) => {
2453-
false
2454-
}
2454+
ExprKind::Path(_)
2455+
| ExprKind::Lit(_)
2456+
| ExprKind::OffsetOf(..)
2457+
| ExprKind::Use(..)
2458+
| ExprKind::DistributedSliceDeferredArray => false,
24552459
ExprKind::Type(base, _)
24562460
| ExprKind::Unary(_, base)
24572461
| ExprKind::Field(base, _)
@@ -2818,6 +2822,10 @@ pub enum ExprKind<'hir> {
28182822
/// e.g. `unsafe<'a> &'a i32` <=> `&i32`.
28192823
UnsafeBinderCast(UnsafeBinderCastKind, &'hir Expr<'hir>, Option<&'hir Ty<'hir>>),
28202824

2825+
/// Built from elements throughout the crate, when first accessed in const-eval
2826+
/// Mostly acts as a literal whose value we defer to create
2827+
DistributedSliceDeferredArray,
2828+
28212829
/// A placeholder for an expression that wasn't syntactically well formed in some way.
28222830
Err(rustc_span::ErrorGuaranteed),
28232831
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
920920
visit_opt!(visitor, visit_ty_unambig, ty);
921921
}
922922
ExprKind::Lit(lit) => try_visit!(visitor.visit_lit(expression.hir_id, lit, false)),
923+
ExprKind::DistributedSliceDeferredArray => {}
923924
ExprKind::Err(_) => {}
924925
}
925926
V::Result::output()

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamNa
4242
use rustc_trait_selection::infer::InferCtxtExt;
4343
use rustc_trait_selection::traits::{ObligationCtxt, hir_ty_lowering_dyn_compatibility_violations};
4444
use tracing::{debug, instrument};
45+
pub use type_of::type_of_distributed_slice;
4546

4647
use crate::errors;
4748
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer, RegionInferReason};

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ fn const_arg_anon_type_of<'tcx>(icx: &ItemCtxt<'tcx>, arg_hir_id: HirId, span: S
113113
}
114114
}
115115

116-
fn type_of_distributed_slice<'tcx>(
116+
pub fn type_of_distributed_slice<'tcx>(
117117
tcx: TyCtxt<'tcx>,
118-
icx: &ItemCtxt<'tcx>,
118+
lowerer: &dyn HirTyLowerer<'tcx>,
119119
ty: &rustc_hir::Ty<'tcx>,
120120
def_id: LocalDefId,
121121
) -> Ty<'tcx> {
122122
use rustc_hir::*;
123123
use rustc_middle::ty::Ty;
124124

125-
let TyKind::Array(ty, len) = ty.kind else {
125+
let TyKind::Array(element_ty, len) = ty.kind else {
126126
panic!("gr should be an array");
127127
};
128128

@@ -133,11 +133,13 @@ fn type_of_distributed_slice<'tcx>(
133133

134134
let res: Option<&Vec<LocalDefId>> = tcx.distributed_slice_elements(()).get(&def_id.to_def_id());
135135

136-
Ty::new_array_with_const_len(
136+
let res = Ty::new_array_with_const_len(
137137
tcx,
138-
icx.lower_ty(ty),
138+
lowerer.lower_ty(element_ty),
139139
ty::Const::from_target_usize(tcx, res.map(|i| i.len()).unwrap_or(0) as u64),
140-
)
140+
);
141+
142+
res
141143
}
142144

143145
pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, Ty<'_>> {
@@ -235,7 +237,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
235237
Node::Item(item) => match item.kind {
236238
ItemKind::Static(_, ident, ty, body_id, distributed_slice) => {
237239
if let DistributedSlice::Declaration(_) = distributed_slice {
238-
type_of_distributed_slice(tcx, &icx, ty, def_id)
240+
type_of_distributed_slice(tcx, icx.lowerer(), ty, def_id)
239241
} else if ty.is_suggestable_infer_ty() {
240242
infer_placeholder_type(
241243
icx.lowerer(),
@@ -252,10 +254,16 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
252254
ItemKind::Const(ident, _, ty, body_id, distributed_slice) => {
253255
if let DistributedSlice::Declaration(_) = distributed_slice {
254256
type_of_distributed_slice(tcx, &icx, ty, def_id)
255-
} else if let DistributedSlice::Addition(_) = distributed_slice {
256-
// if it's a global registration addition, then the type can be infered from the declaration
257-
// during typeck
258-
icx.lower_ty(ty)
257+
} else if let DistributedSlice::Addition(declaration_def_id) = distributed_slice {
258+
// we reject generic const items (`#![feature(generic_const_items)]`) in `#[distributed_slice(crate)]`
259+
let array_ty = tcx.type_of(declaration_def_id).instantiate_identity();
260+
261+
let ty = match array_ty.kind() {
262+
ty::Array(element_ty, _) => *element_ty,
263+
_ => panic!("not an array"),
264+
};
265+
266+
ty
259267
} else if ty.is_suggestable_infer_ty() {
260268
infer_placeholder_type(
261269
icx.lowerer(),

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use rustc_span::symbol::sym;
103103
use rustc_span::{ErrorGuaranteed, Span};
104104
use rustc_trait_selection::traits;
105105

106-
pub use crate::collect::suggest_impl_trait;
106+
pub use crate::collect::{suggest_impl_trait, type_of_distributed_slice};
107107
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer};
108108

109109
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,6 +1671,9 @@ impl<'a> State<'a> {
16711671
self.word_space("yield");
16721672
self.print_expr_cond_paren(expr, expr.precedence() < ExprPrecedence::Jump);
16731673
}
1674+
hir::ExprKind::DistributedSliceDeferredArray => {
1675+
// literally nothing to print.
1676+
}
16741677
hir::ExprKind::Err(_) => {
16751678
self.popen();
16761679
self.word("/*ERROR*/");

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
384384
ExprKind::ConstBlock(_)
385385
| ExprKind::Loop(_, _, _, _)
386386
| ExprKind::Lit(_)
387+
| ExprKind::DistributedSliceDeferredArray
387388
| ExprKind::Path(_)
388389
| ExprKind::Continue(_)
389390
| ExprKind::OffsetOf(_, _)
@@ -505,6 +506,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
505506

506507
let tcx = self.tcx;
507508
match expr.kind {
509+
ExprKind::DistributedSliceDeferredArray => self.next_ty_var(expr.span),
508510
ExprKind::Lit(ref lit) => self.check_expr_lit(lit, expected),
509511
ExprKind::Binary(op, lhs, rhs) => self.check_expr_binop(expr, op, lhs, rhs, expected),
510512
ExprKind::Assign(lhs, rhs, span) => {

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
508508

509509
hir::ExprKind::Continue(..)
510510
| hir::ExprKind::Lit(..)
511+
| hir::ExprKind::DistributedSliceDeferredArray
511512
| hir::ExprKind::ConstBlock(..)
512513
| hir::ExprKind::OffsetOf(..)
513514
| hir::ExprKind::Err(_) => {}
@@ -1437,6 +1438,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
14371438
| hir::ExprKind::InlineAsm(..)
14381439
| hir::ExprKind::OffsetOf(..)
14391440
| hir::ExprKind::UnsafeBinderCast(UnsafeBinderCastKind::Wrap, ..)
1441+
| hir::ExprKind::DistributedSliceDeferredArray
14401442
| hir::ExprKind::Err(_) => Ok(self.cat_rvalue(expr.hir_id, expr_ty)),
14411443
}
14421444
}

0 commit comments

Comments
 (0)