Skip to content

Commit fceb5cc

Browse files
authored
Unrolled build for #158375
Rollup merge of #158375 - khyperia:inline-consts-in-type-system, r=BoxyUwU Support `DefKind::InlineConst` in `ConstKind::Unevaluated` fixes rust-lang/project-const-generics#101 required for rust-lang/project-const-generics#108 consider: `Struct<{ (some, stuff, const { hi }) }>`. The following is very pseudocode-y, the important parts are whether it says AnonConst or InlineConst, not the Tuple stuff - On stable, we represent this with: `AnonConst(Tuple(some, stuff, InlineConst(hi)))` - Under mGCA, with "direct" arguments, before this PR, it was `Tuple(some, stuff, AnonConst(hi))`. The inner InlineConst got intercepted in the def_collector with hacks (`ConstArgContext`) and converted into an AnonConst, even though it has inline const syntax. It would be nice to keep it as an InlineConst under mGCA, i.e. `Tuple(some, stuff, InlineConst(hi))`, and have the type system support passing around InlineConsts in `ConstKind::Unevaluated` (soon to be renamed `ConstKind::Alias`). This would allow the def collector to not need to know if we are in a "direct" or "regular/anon" context, which it turns out is extremely useful for implementing rust-lang/project-const-generics#108. Supporting InlineConsts in the type system are also useful for other things, for example, mentioned in rust-lang/project-const-generics#101 is arg position const generics experiments. This PR does two things: - support InlineConsts in the type system (i.e. in `ConstKind::Unevaluated`) - exercise that support, by no longer intercepting mGCA "direct" argument inline consts to be anon consts r? @BoxyUwU
2 parents 0966944 + f31f522 commit fceb5cc

19 files changed

Lines changed: 151 additions & 158 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2791,7 +2791,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
27912791
}
27922792
ExprKind::ConstBlock(anon_const) => {
27932793
let def_id = self.local_def_id(anon_const.id);
2794-
assert_eq!(DefKind::AnonConst, self.tcx.def_kind(def_id));
2794+
assert_eq!(DefKind::InlineConst, self.tcx.def_kind(def_id));
27952795
self.lower_anon_const_to_const_arg(anon_const, span)
27962796
}
27972797
_ => overly_complex_const(self),

compiler/rustc_borrowck/src/universal_regions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
615615

616616
BodyOwnerKind::Const { .. } | BodyOwnerKind::Static(..) => {
617617
match tcx.def_kind(self.mir_def) {
618-
DefKind::InlineConst => {
618+
DefKind::InlineConst if !tcx.is_type_system_inline_const(self.mir_def) => {
619619
// This is required for `AscribeUserType` canonical query, which will call
620620
// `type_of(inline_const_def_id)`. That `type_of` would inject erased lifetimes
621621
// into borrowck, which is ICE #78174.

compiler/rustc_hir/src/def.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -448,43 +448,6 @@ impl DefKind {
448448
| DefKind::ExternCrate => false,
449449
}
450450
}
451-
452-
/// Returns `true` if `self` is a kind of definition that does not have its own
453-
/// type-checking context, i.e. closure, coroutine or inline const.
454-
#[inline]
455-
pub fn is_typeck_child(self) -> bool {
456-
match self {
457-
DefKind::Closure | DefKind::InlineConst | DefKind::SyntheticCoroutineBody => true,
458-
DefKind::Mod
459-
| DefKind::Struct
460-
| DefKind::Union
461-
| DefKind::Enum
462-
| DefKind::Variant
463-
| DefKind::Trait
464-
| DefKind::TyAlias
465-
| DefKind::ForeignTy
466-
| DefKind::TraitAlias
467-
| DefKind::AssocTy
468-
| DefKind::TyParam
469-
| DefKind::Fn
470-
| DefKind::Const { .. }
471-
| DefKind::ConstParam
472-
| DefKind::Static { .. }
473-
| DefKind::Ctor(_, _)
474-
| DefKind::AssocFn
475-
| DefKind::AssocConst { .. }
476-
| DefKind::Macro(_)
477-
| DefKind::ExternCrate
478-
| DefKind::Use
479-
| DefKind::ForeignMod
480-
| DefKind::AnonConst
481-
| DefKind::OpaqueTy
482-
| DefKind::Field
483-
| DefKind::LifetimeParam
484-
| DefKind::GlobalAsm
485-
| DefKind::Impl { .. } => false,
486-
}
487-
}
488451
}
489452

490453
/// The resolution of a path or export.

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! crate as a kind of pass. This should eventually be factored away.
1616
1717
use std::cell::Cell;
18-
use std::{assert_matches, iter};
18+
use std::{assert_matches, debug_assert_matches, iter};
1919

2020
use rustc_abi::{ExternAbi, Size};
2121
use rustc_ast::Recovered;
@@ -1635,10 +1635,12 @@ fn const_param_default<'tcx>(
16351635
}
16361636

16371637
fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKind {
1638+
debug_assert_matches!(tcx.def_kind(def), DefKind::AnonConst | DefKind::InlineConst);
16381639
let hir_id = tcx.local_def_id_to_hir_id(def);
16391640
let const_arg_id = tcx.parent_hir_id(hir_id);
16401641
match tcx.hir_node(const_arg_id) {
1641-
hir::Node::ConstArg(_) => {
1642+
hir::Node::ConstArg(const_arg) => {
1643+
debug_assert_matches!(const_arg.kind, hir::ConstArgKind::Anon(hir::AnonConst { def_id, .. }) if *def_id == def);
16421644
let parent_hir_node = tcx.hir_node(tcx.parent_hir_id(const_arg_id));
16431645
if tcx.features().generic_const_exprs() {
16441646
ty::AnonConstKind::GCE

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
483483
let tcx = self.tcx();
484484
let parent_def_id = self.item_def_id();
485485
if let Res::Def(DefKind::ConstParam, _) = res
486-
&& tcx.def_kind(parent_def_id) == DefKind::AnonConst
486+
&& matches!(tcx.def_kind(parent_def_id), DefKind::AnonConst | DefKind::InlineConst)
487487
&& let ty::AnonConstKind::MCG = tcx.anon_const_kind(parent_def_id)
488488
{
489489
let folder = ForbidParamUsesFolder {
@@ -512,15 +512,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
512512
// Inline consts and closures can be nested inside anon consts that forbid generic
513513
// params (e.g. an enum discriminant). Walk up the def parent chain to find the
514514
// nearest enclosing AnonConst and use that to determine the context.
515+
let parent_def_id = tcx.typeck_root_def_id(parent_def_id.into());
516+
515517
let anon_const_def_id = match tcx.def_kind(parent_def_id) {
516518
DefKind::AnonConst => parent_def_id,
517-
DefKind::InlineConst | DefKind::Closure => {
518-
let root = tcx.typeck_root_def_id(parent_def_id.into());
519-
match tcx.def_kind(root) {
520-
DefKind::AnonConst => root.expect_local(),
521-
_ => return None,
522-
}
523-
}
519+
DefKind::InlineConst if tcx.is_type_system_inline_const(parent_def_id) => parent_def_id,
524520
_ => return None,
525521
};
526522

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
186186
}
187187
_ => (),
188188
}
189-
// Skip `AnonConst`s because we feed their `type_of`.
189+
// Skip `AnonConst`s and type system `InlineConst`s because we feed their `type_of` in
190+
// `feed_anon_const_type`.
190191
// Also skip items for which typeck forwards to parent typeck.
191-
if !(matches!(def_kind, DefKind::AnonConst) || def_kind.is_typeck_child()) {
192+
if !(def_kind == DefKind::AnonConst
193+
|| def_kind == DefKind::InlineConst && tcx.is_type_system_inline_const(item_def_id)
194+
|| tcx.is_typeck_child(item_def_id.to_def_id()))
195+
{
192196
tcx.ensure_ok().typeck(item_def_id);
193197
}
194198
// Ensure we generate the new `DefId` before finishing `check_crate`.

compiler/rustc_hir_typeck/src/loops.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ pub(crate) fn check<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir
8484
CheckLoopVisitor { tcx, cx_stack: vec![Normal], block_breaks: Default::default() };
8585
let cx = match tcx.def_kind(def_id) {
8686
DefKind::AnonConst => AnonConst,
87+
DefKind::InlineConst => {
88+
// only type system inline consts are typeck roots
89+
debug_assert!(tcx.is_type_system_inline_const(def_id));
90+
ConstBlock
91+
}
8792
_ => Fn,
8893
};
8994
check.with_context(cx, |v| v.visit_body(body));

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16241624
<- tcx.explicit_implied_const_bounds(def_id).skip_binder());
16251625
}
16261626
}
1627-
if let DefKind::AnonConst = def_kind {
1627+
if let DefKind::AnonConst | DefKind::InlineConst = def_kind {
16281628
record!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id));
16291629
}
16301630
if should_encode_const_of_item(self.tcx, def_id, def_kind) {

compiler/rustc_middle/src/hir/map.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, kw, with_metavar_spans};
2222
use crate::hir::{ModuleItems, ProjectedMaybeOwner, nested_filter};
2323
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
2424
use crate::query::{IntoQueryKey, LocalCrate};
25-
use crate::ty::TyCtxt;
25+
use crate::ty::{self, TyCtxt};
2626

2727
/// An iterator that walks up the ancestor tree of a given `HirId`.
2828
/// Constructed using `tcx.hir_parent_iter(hir_id)`.
@@ -1115,6 +1115,12 @@ impl<'tcx> TyCtxt<'tcx> {
11151115
}
11161116
}
11171117

1118+
pub fn is_type_system_inline_const(self, def_id: impl IntoQueryKey<DefId>) -> bool {
1119+
let def_id = def_id.into_query_key();
1120+
debug_assert_eq!(self.def_kind(def_id), DefKind::InlineConst);
1121+
self.anon_const_kind(def_id) != ty::AnonConstKind::NonTypeSystem
1122+
}
1123+
11181124
pub fn hir_maybe_get_struct_pattern_shorthand_field(self, expr: &Expr<'_>) -> Option<Symbol> {
11191125
let local = match expr {
11201126
Expr {

compiler/rustc_middle/src/ty/context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,14 @@ impl<'tcx> TyCtxt<'tcx> {
601601
/// effect. However, we do not want this as a general capability, so this interface restricts
602602
/// to the only allowed case.
603603
pub fn feed_anon_const_type(self, key: LocalDefId, value: ty::EarlyBinder<'tcx, Ty<'tcx>>) {
604-
debug_assert_eq!(self.def_kind(key), DefKind::AnonConst);
604+
if cfg!(debug_assertions) {
605+
match self.def_kind(key) {
606+
DefKind::AnonConst => (),
607+
DefKind::InlineConst => assert!(self.is_type_system_inline_const(key)),
608+
def_kind => bug!("unexpected DefKind in feed_anon_const_type: {def_kind:?}"),
609+
}
610+
}
611+
605612
TyCtxtFeed { tcx: self, key }.type_of(value)
606613
}
607614

0 commit comments

Comments
 (0)