Skip to content

Commit 1ea4851

Browse files
committed
Auto merge of rust-lang#93285 - JulianKnodt:const_eq_2, r=oli-obk
Continue work on associated const equality This actually implements some more complex logic for assigning associated consts to values. Inside of projection candidates, it now defers to a separate function for either consts or types. To reduce amount of code, projections are now generic over T, where T is either a Type or a Const. I can add some comments back later, but this was the fastest way to implement it. It also now finds the correct type of consts in type_of. --- The current main TODO is finding the const of the def id for the LeafDef. Right now it works if the function isn't called, but once you use the trait impl with the bound it fails inside projection. I was hoping to get some help in getting the `&'tcx ty::Const<'tcx>`, in addition to a bunch of other `todo!()`s which I think may not be hit. r? `@oli-obk` Updates rust-lang#92827
2 parents 2681f25 + 78fb74a commit 1ea4851

File tree

19 files changed

+379
-181
lines changed

19 files changed

+379
-181
lines changed

compiler/rustc_hir/src/hir.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,12 @@ impl TypeBinding<'_> {
22032203
_ => panic!("expected equality type binding for parenthesized generic args"),
22042204
}
22052205
}
2206+
pub fn opt_const(&self) -> Option<&'_ AnonConst> {
2207+
match self.kind {
2208+
TypeBindingKind::Equality { term: Term::Const(ref c) } => Some(c),
2209+
_ => None,
2210+
}
2211+
}
22062212
}
22072213

22082214
#[derive(Debug)]

compiler/rustc_infer/src/infer/at.rs

+20
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,26 @@ impl<'tcx> ToTrace<'tcx> for &'tcx Const<'tcx> {
286286
}
287287
}
288288

289+
impl<'tcx> ToTrace<'tcx> for ty::Term<'tcx> {
290+
fn to_trace(
291+
tcx: TyCtxt<'tcx>,
292+
cause: &ObligationCause<'tcx>,
293+
a_is_expected: bool,
294+
a: Self,
295+
b: Self,
296+
) -> TypeTrace<'tcx> {
297+
match (a, b) {
298+
(ty::Term::Ty(a), ty::Term::Ty(b)) => {
299+
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
300+
}
301+
(ty::Term::Const(a), ty::Term::Const(b)) => {
302+
ToTrace::to_trace(tcx, cause, a_is_expected, a, b)
303+
}
304+
(_, _) => todo!(),
305+
}
306+
}
307+
}
308+
289309
impl<'tcx> ToTrace<'tcx> for ty::TraitRef<'tcx> {
290310
fn to_trace(
291311
_: TyCtxt<'tcx>,

compiler/rustc_infer/src/traits/project.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub enum ProjectionCacheEntry<'tcx> {
9393
Recur,
9494
Error,
9595
NormalizedTy {
96-
ty: NormalizedTy<'tcx>,
96+
ty: Normalized<'tcx, ty::Term<'tcx>>,
9797
/// If we were able to successfully evaluate the
9898
/// corresponding cache entry key during predicate
9999
/// evaluation, then this field stores the final
@@ -174,7 +174,11 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {
174174
}
175175

176176
/// Indicates that `key` was normalized to `value`.
177-
pub fn insert_ty(&mut self, key: ProjectionCacheKey<'tcx>, value: NormalizedTy<'tcx>) {
177+
pub fn insert_term(
178+
&mut self,
179+
key: ProjectionCacheKey<'tcx>,
180+
value: Normalized<'tcx, ty::Term<'tcx>>,
181+
) {
178182
debug!(
179183
"ProjectionCacheEntry::insert_ty: adding cache entry: key={:?}, value={:?}",
180184
key, value

compiler/rustc_middle/src/ty/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ impl<'tcx> Term<'tcx> {
867867
pub fn ty(&self) -> Option<Ty<'tcx>> {
868868
if let Term::Ty(ty) = self { Some(ty) } else { None }
869869
}
870+
pub fn ct(&self) -> Option<&'tcx Const<'tcx>> {
871+
if let Term::Const(c) = self { Some(c) } else { None }
872+
}
870873
}
871874

872875
/// This kind of predicate has no *direct* correspondent in the

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -1373,19 +1373,31 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
13731373
| ObligationCauseCode::ObjectCastObligation(_)
13741374
| ObligationCauseCode::OpaqueType
13751375
);
1376-
// FIXME(associated_const_equality): Handle Consts here
1377-
let data_ty = data.term.ty().unwrap();
13781376
if let Err(error) = self.at(&obligation.cause, obligation.param_env).eq_exp(
13791377
is_normalized_ty_expected,
13801378
normalized_ty,
1381-
data_ty,
1379+
data.term,
13821380
) {
1383-
values = Some(infer::ValuePairs::Types(ExpectedFound::new(
1384-
is_normalized_ty_expected,
1385-
normalized_ty,
1386-
data_ty,
1387-
)));
1388-
1381+
values = Some(match (normalized_ty, data.term) {
1382+
(ty::Term::Ty(normalized_ty), ty::Term::Ty(ty)) => {
1383+
infer::ValuePairs::Types(ExpectedFound::new(
1384+
is_normalized_ty_expected,
1385+
normalized_ty,
1386+
ty,
1387+
))
1388+
}
1389+
(ty::Term::Const(normalized_ct), ty::Term::Const(ct)) => {
1390+
infer::ValuePairs::Consts(ExpectedFound::new(
1391+
is_normalized_ty_expected,
1392+
normalized_ct,
1393+
ct,
1394+
))
1395+
}
1396+
(_, _) => span_bug!(
1397+
obligation.cause.span,
1398+
"found const or type where other expected"
1399+
),
1400+
});
13891401
err_buf = error;
13901402
err = &err_buf;
13911403
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2496,7 +2496,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
24962496
let try_obligation = self.mk_trait_obligation_with_new_self_ty(
24972497
obligation.param_env,
24982498
trait_pred,
2499-
normalized_ty,
2499+
normalized_ty.ty().unwrap(),
25002500
);
25012501
debug!("suggest_await_before_try: try_trait_obligation {:?}", try_obligation);
25022502
if self.predicate_may_hold(&try_obligation)

compiler/rustc_trait_selection/src/traits/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
200200

201201
debug!(?normalized_ty);
202202

203-
normalized_ty
203+
normalized_ty.ty().unwrap()
204204
}
205205

206206
fn register_predicate_obligation(

0 commit comments

Comments
 (0)