@@ -11,6 +11,7 @@ use crate::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
1111use crate :: ty:: { self , Ty , TyCtxt , TypeFoldable } ;
1212use rustc_hir as ast;
1313use rustc_hir:: def_id:: DefId ;
14+ use rustc_span:: DUMMY_SP ;
1415use rustc_target:: spec:: abi;
1516use std:: iter;
1617use std:: rc:: Rc ;
@@ -507,6 +508,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
507508 a : & ' tcx ty:: Const < ' tcx > ,
508509 b : & ' tcx ty:: Const < ' tcx > ,
509510) -> RelateResult < ' tcx , & ' tcx ty:: Const < ' tcx > > {
511+ debug ! ( "{}.super_relate_consts(a = {:?}, b = {:?})" , relation. tag( ) , a, b) ;
510512 let tcx = relation. tcx ( ) ;
511513
512514 let eagerly_eval = |x : & ' tcx ty:: Const < ' tcx > | {
@@ -561,7 +563,7 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
561563 }
562564 }
563565
564- ( a_val @ ConstValue :: Slice { .. } , b_val @ ConstValue :: Slice { .. } ) => {
566+ ( ConstValue :: Slice { .. } , ConstValue :: Slice { .. } ) => {
565567 let a_bytes = get_slice_bytes ( & tcx, a_val) ;
566568 let b_bytes = get_slice_bytes ( & tcx, b_val) ;
567569 if a_bytes == b_bytes {
@@ -571,7 +573,37 @@ pub fn super_relate_consts<R: TypeRelation<'tcx>>(
571573 }
572574 }
573575
574- // FIXME(const_generics): handle `ConstValue::ByRef`.
576+ ( ConstValue :: ByRef { .. } , ConstValue :: ByRef { .. } ) => {
577+ match a. ty . kind {
578+ ty:: Array ( ..) | ty:: Adt ( ..) | ty:: Tuple ( ..) => {
579+ let a_destructured = tcx. destructure_const ( relation. param_env ( ) . and ( a) ) ;
580+ let b_destructured = tcx. destructure_const ( relation. param_env ( ) . and ( b) ) ;
581+
582+ // Both the variant and each field have to be equal.
583+ if a_destructured. variant == b_destructured. variant {
584+ for ( a_field, b_field) in
585+ a_destructured. fields . iter ( ) . zip ( b_destructured. fields . iter ( ) )
586+ {
587+ relation. consts ( a_field, b_field) ?;
588+ }
589+
590+ Ok ( a_val)
591+ } else {
592+ Err ( TypeError :: ConstMismatch ( expected_found ( relation, & a, & b) ) )
593+ }
594+ }
595+ // FIXME(const_generics): There are probably some `TyKind`s
596+ // which should be handled here.
597+ _ => {
598+ tcx. sess . delay_span_bug (
599+ DUMMY_SP ,
600+ & format ! ( "unexpected consts: a: {:?}, b: {:?}" , a, b) ,
601+ ) ;
602+ Err ( TypeError :: ConstMismatch ( expected_found ( relation, & a, & b) ) )
603+ }
604+ }
605+ }
606+
575607 _ => Err ( TypeError :: ConstMismatch ( expected_found ( relation, & a, & b) ) ) ,
576608 } ;
577609
0 commit comments