Skip to content

Commit a7bbac8

Browse files
committed
finish typechecking
1 parent 5d1ab0b commit a7bbac8

File tree

26 files changed

+168
-86
lines changed

26 files changed

+168
-86
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3607,7 +3607,7 @@ pub enum DistributedSlice {
36073607
#[default]
36083608
None,
36093609
/// This const or static declares a global registry that can be added to
3610-
Declaration(Span),
3610+
Declaration(Span, NodeId),
36113611
/// This const (we never do this to statics) represents an addition to a global registry
36123612
/// declared somewhere else.
36133613
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};

0 commit comments

Comments
 (0)