Skip to content

Commit 0ecc128

Browse files
committed
Use same_type instead of duplicating logic
1 parent 8da6727 commit 0ecc128

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

src/librustc/infer/error_reporting/mod.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -1005,26 +1005,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10051005
{
10061006
let mut show_suggestion = true;
10071007
for (exp_ty, found_ty) in exp_substs.types().zip(found_substs.types()) {
1008-
if let TyKind::Ref(_, exp_ty, _) = exp_ty.sty {
1009-
match (&exp_ty.sty, &found_ty.sty) {
1010-
(TyKind::Adt(exp_did, _), TyKind::Adt(found_did, _))
1011-
if exp_did == found_did => {}
1012-
(TyKind::Bool, TyKind::Bool) |
1013-
(TyKind::Char, TyKind::Char) |
1014-
(TyKind::Str, TyKind::Str) |
1015-
(_, TyKind::Param(_)) |
1016-
(_, TyKind::Infer(_)) |
1017-
(TyKind::Param(_), _) |
1018-
(TyKind::Infer(_), _) => {}
1019-
(TyKind::Int(x), TyKind::Int(y)) if x == y => {}
1020-
(TyKind::Uint(x), TyKind::Uint(y)) if x == y => {}
1021-
(TyKind::Int(x), TyKind::Int(y)) if x == y => {}
1022-
(TyKind::Uint(x), TyKind::Uint(y)) if x == y => {}
1023-
(TyKind::Float(x), TyKind::Float(y)) if x == y => {}
1024-
_ => show_suggestion = false,
1008+
match exp_ty.sty {
1009+
TyKind::Ref(_, exp_ty, _) => {
1010+
match (&exp_ty.sty, &found_ty.sty) {
1011+
(_, TyKind::Param(_)) |
1012+
(_, TyKind::Infer(_)) |
1013+
(TyKind::Param(_), _) |
1014+
(TyKind::Infer(_), _) => {}
1015+
_ if ty::TyS::same_type(exp_ty, found_ty) => {}
1016+
_ => show_suggestion = false,
1017+
};
10251018
}
1026-
} else {
1027-
show_suggestion = false;
1019+
TyKind::Param(_) | TyKind::Infer(_) => {}
1020+
_ => show_suggestion = false,
10281021
}
10291022
}
10301023
if let (Ok(snippet), true) = (

src/librustc/ty/util.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,19 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
658658
tcx.needs_drop_raw(param_env.and(self))
659659
}
660660

661+
pub fn same_type(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
662+
match (&a.sty, &b.sty) {
663+
(&Adt(did_a, substs_a), &Adt(did_b, substs_b)) => {
664+
if did_a != did_b {
665+
return false;
666+
}
667+
668+
substs_a.types().zip(substs_b.types()).all(|(a, b)| Self::same_type(a, b))
669+
}
670+
_ => a == b,
671+
}
672+
}
673+
661674
/// Check whether a type is representable. This means it cannot contain unboxed
662675
/// structural recursion. This check is needed for structs and enums.
663676
pub fn is_representable(&'tcx self,
@@ -730,19 +743,6 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
730743
}
731744
}
732745

733-
fn same_type<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
734-
match (&a.sty, &b.sty) {
735-
(&Adt(did_a, substs_a), &Adt(did_b, substs_b)) => {
736-
if did_a != did_b {
737-
return false;
738-
}
739-
740-
substs_a.types().zip(substs_b.types()).all(|(a, b)| same_type(a, b))
741-
}
742-
_ => a == b,
743-
}
744-
}
745-
746746
// Does the type `ty` directly (without indirection through a pointer)
747747
// contain any types on stack `seen`?
748748
fn is_type_structurally_recursive<'a, 'tcx>(
@@ -807,7 +807,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> {
807807
// struct Foo { Option<Option<Foo>> }
808808

809809
for &seen_type in iter {
810-
if same_type(ty, seen_type) {
810+
if ty::TyS::same_type(ty, seen_type) {
811811
debug!("ContainsRecursive: {:?} contains {:?}",
812812
seen_type,
813813
ty);

0 commit comments

Comments
 (0)