Skip to content

Commit 9c29713

Browse files
committed
Set groundwork for proper const normalization
1 parent 53e3907 commit 9c29713

File tree

18 files changed

+343
-210
lines changed

18 files changed

+343
-210
lines changed
+17-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use rustc_middle::traits::ObligationCause;
2-
use rustc_middle::ty::{self, Ty};
2+
use rustc_middle::ty;
33

44
use super::InferCtxt;
5+
use crate::infer::Term;
56
use crate::traits::{Obligation, PredicateObligations};
67

78
impl<'tcx> InferCtxt<'tcx> {
@@ -11,24 +12,32 @@ impl<'tcx> InferCtxt<'tcx> {
1112
/// of the given projection. This allows us to proceed with projections
1213
/// while they cannot be resolved yet due to missing information or
1314
/// simply due to the lack of access to the trait resolution machinery.
14-
pub fn projection_ty_to_infer(
15+
pub fn projection_term_to_infer(
1516
&self,
1617
param_env: ty::ParamEnv<'tcx>,
17-
projection_ty: ty::AliasTy<'tcx>,
18+
alias_term: ty::AliasTerm<'tcx>,
1819
cause: ObligationCause<'tcx>,
1920
recursion_depth: usize,
2021
obligations: &mut PredicateObligations<'tcx>,
21-
) -> Ty<'tcx> {
22+
) -> Term<'tcx> {
2223
debug_assert!(!self.next_trait_solver());
23-
let ty_var = self.next_ty_var(self.tcx.def_span(projection_ty.def_id));
24+
25+
let span = self.tcx.def_span(alias_term.def_id);
26+
let infer_var = if alias_term.kind(self.tcx).is_type() {
27+
self.next_ty_var(span).into()
28+
} else {
29+
self.next_const_var(span).into()
30+
};
31+
2432
let projection =
2533
ty::PredicateKind::Clause(ty::ClauseKind::Projection(ty::ProjectionPredicate {
26-
projection_term: projection_ty.into(),
27-
term: ty_var.into(),
34+
projection_term: alias_term,
35+
term: infer_var,
2836
}));
2937
let obligation =
3038
Obligation::with_depth(self.tcx, cause, recursion_depth, param_env, projection);
3139
obligations.push(obligation);
32-
ty_var
40+
41+
infer_var
3342
}
3443
}

compiler/rustc_middle/src/ty/context.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,18 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
243243
ty::AliasTermKind::ProjectionTy
244244
}
245245
}
246+
DefKind::AssocConst => {
247+
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
248+
{
249+
ty::AliasTermKind::InherentConst
250+
} else {
251+
ty::AliasTermKind::ProjectionConst
252+
}
253+
}
246254
DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy,
247255
DefKind::TyAlias => ty::AliasTermKind::FreeTy,
248-
DefKind::AssocConst => ty::AliasTermKind::ProjectionConst,
249-
DefKind::AnonConst | DefKind::Const | DefKind::Ctor(_, CtorKind::Const) => {
256+
DefKind::Const => ty::AliasTermKind::FreeConst,
257+
DefKind::AnonConst | DefKind::Ctor(_, CtorKind::Const) => {
250258
ty::AliasTermKind::UnevaluatedConst
251259
}
252260
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),

compiler/rustc_middle/src/ty/print/pretty.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3195,7 +3195,7 @@ define_print! {
31953195

31963196
ty::AliasTerm<'tcx> {
31973197
match self.kind(cx.tcx()) {
3198-
ty::AliasTermKind::InherentTy => p!(pretty_print_inherent_projection(*self)),
3198+
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => p!(pretty_print_inherent_projection(*self)),
31993199
ty::AliasTermKind::ProjectionTy => {
32003200
if !(cx.should_print_verbose() || with_reduced_queries())
32013201
&& cx.tcx().is_impl_trait_in_trait(self.def_id)
@@ -3205,7 +3205,8 @@ define_print! {
32053205
p!(print_def_path(self.def_id, self.args));
32063206
}
32073207
}
3208-
| ty::AliasTermKind::FreeTy
3208+
ty::AliasTermKind::FreeTy
3209+
| ty::AliasTermKind::FreeConst
32093210
| ty::AliasTermKind::OpaqueTy
32103211
| ty::AliasTermKind::UnevaluatedConst
32113212
| ty::AliasTermKind::ProjectionConst => {

compiler/rustc_middle/src/ty/util.rs

+2
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,9 @@ impl<'tcx> TyCtxt<'tcx> {
966966
}
967967
ty::AliasTermKind::OpaqueTy => Some(self.variances_of(def_id)),
968968
ty::AliasTermKind::InherentTy
969+
| ty::AliasTermKind::InherentConst
969970
| ty::AliasTermKind::FreeTy
971+
| ty::AliasTermKind::FreeConst
970972
| ty::AliasTermKind::UnevaluatedConst
971973
| ty::AliasTermKind::ProjectionConst => None,
972974
}

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
568568

569569
// Lower the named constant to a THIR pattern.
570570
let args = self.typeck_results.node_args(id);
571+
// FIXME(mgca): we will need to special case IACs here to have type system compatible
572+
// generic args, instead of how we represent them in body expressions.
571573
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
572574
let mut pattern = self.const_to_pat(c, ty, id, span);
573575

compiler/rustc_next_trait_solver/src/solve/normalizes_to/free_alias.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ where
1919
goal: Goal<I, ty::NormalizesTo<I>>,
2020
) -> QueryResult<I> {
2121
let cx = self.cx();
22-
let free_ty = goal.predicate.alias;
22+
let free_alias = goal.predicate.alias;
2323

2424
// Check where clauses
2525
self.add_goals(
2626
GoalSource::Misc,
27-
cx.predicates_of(free_ty.def_id)
28-
.iter_instantiated(cx, free_ty.args)
27+
cx.predicates_of(free_alias.def_id)
28+
.iter_instantiated(cx, free_alias.args)
2929
.map(|pred| goal.with(cx, pred)),
3030
);
3131

32-
let actual = cx.type_of(free_ty.def_id).instantiate(cx, free_ty.args);
33-
self.instantiate_normalizes_to_term(goal, actual.into());
32+
let actual = if free_alias.kind(cx).is_type() {
33+
cx.type_of(free_alias.def_id).instantiate(cx, free_alias.args)
34+
} else {
35+
// FIXME(mgca): once const items are actual aliases defined as equal to type system consts
36+
// this should instead return that.
37+
panic!("normalizing free const aliases in the type system is unsupported");
38+
};
3439

40+
self.instantiate_normalizes_to_term(goal, actual.into());
3541
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
3642
}
3743
}

compiler/rustc_next_trait_solver/src/solve/normalizes_to/inherent.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ where
1515
D: SolverDelegate<Interner = I>,
1616
I: Interner,
1717
{
18-
pub(super) fn normalize_inherent_associated_type(
18+
pub(super) fn normalize_inherent_associated_term(
1919
&mut self,
2020
goal: Goal<I, ty::NormalizesTo<I>>,
2121
) -> QueryResult<I> {
2222
let cx = self.cx();
23-
let inherent = goal.predicate.alias.expect_ty(cx);
23+
let inherent = goal.predicate.alias;
2424

2525
let impl_def_id = cx.parent(inherent.def_id);
2626
let impl_args = self.fresh_args_for_item(impl_def_id);
@@ -48,8 +48,13 @@ where
4848
.map(|pred| goal.with(cx, pred)),
4949
);
5050

51-
let normalized = cx.type_of(inherent.def_id).instantiate(cx, inherent_args);
52-
self.instantiate_normalizes_to_term(goal, normalized.into());
51+
let normalized = if inherent.kind(cx).is_type() {
52+
cx.type_of(inherent.def_id).instantiate(cx, inherent_args).into()
53+
} else {
54+
// FIXME(mgca): Properly handle IACs in the type system
55+
panic!("normalizing inherent associated consts in the type system is unsupported");
56+
};
57+
self.instantiate_normalizes_to_term(goal, normalized);
5358
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
5459
}
5560
}

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@ where
4848
})
4949
})
5050
}
51-
ty::AliasTermKind::InherentTy => self.normalize_inherent_associated_type(goal),
51+
ty::AliasTermKind::InherentTy | ty::AliasTermKind::InherentConst => {
52+
self.normalize_inherent_associated_term(goal)
53+
}
5254
ty::AliasTermKind::OpaqueTy => self.normalize_opaque_type(goal),
53-
ty::AliasTermKind::FreeTy => self.normalize_free_alias(goal),
55+
ty::AliasTermKind::FreeTy | ty::AliasTermKind::FreeConst => {
56+
self.normalize_free_alias(goal)
57+
}
5458
ty::AliasTermKind::UnevaluatedConst => self.normalize_anon_const(goal),
5559
}
5660
}
@@ -333,6 +337,8 @@ where
333337
cx.type_of(target_item_def_id).map_bound(|ty| ty.into())
334338
}
335339
ty::AliasTermKind::ProjectionConst => {
340+
// FIXME(mgca): once const items are actual aliases defined as equal to type system consts
341+
// this should instead return that.
336342
if cx.features().associated_const_equality() {
337343
panic!("associated const projection is not supported yet")
338344
} else {

compiler/rustc_trait_selection/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub use self::dyn_compatibility::{
5151
pub use self::engine::{ObligationCtxt, TraitEngineExt};
5252
pub use self::fulfill::{FulfillmentContext, OldSolverError, PendingPredicateObligation};
5353
pub use self::normalize::NormalizeExt;
54-
pub use self::project::{normalize_inherent_projection, normalize_projection_ty};
54+
pub use self::project::{normalize_inherent_projection, normalize_projection_term};
5555
pub use self::select::{
5656
EvaluationCache, EvaluationResult, IntercrateAmbiguityCause, OverflowError, SelectionCache,
5757
SelectionContext,

0 commit comments

Comments
 (0)