From 0ecc128ccb43d0302288011a6919989e91da3bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 29 Dec 2018 10:54:32 -0800 Subject: [PATCH] Use `same_type` instead of duplicating logic --- src/librustc/infer/error_reporting/mod.rs | 31 +++++++++-------------- src/librustc/ty/util.rs | 28 ++++++++++---------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index c9fc896b1b2d..e34bb9f4f7bd 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -1005,26 +1005,19 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { { let mut show_suggestion = true; for (exp_ty, found_ty) in exp_substs.types().zip(found_substs.types()) { - if let TyKind::Ref(_, exp_ty, _) = exp_ty.sty { - match (&exp_ty.sty, &found_ty.sty) { - (TyKind::Adt(exp_did, _), TyKind::Adt(found_did, _)) - if exp_did == found_did => {} - (TyKind::Bool, TyKind::Bool) | - (TyKind::Char, TyKind::Char) | - (TyKind::Str, TyKind::Str) | - (_, TyKind::Param(_)) | - (_, TyKind::Infer(_)) | - (TyKind::Param(_), _) | - (TyKind::Infer(_), _) => {} - (TyKind::Int(x), TyKind::Int(y)) if x == y => {} - (TyKind::Uint(x), TyKind::Uint(y)) if x == y => {} - (TyKind::Int(x), TyKind::Int(y)) if x == y => {} - (TyKind::Uint(x), TyKind::Uint(y)) if x == y => {} - (TyKind::Float(x), TyKind::Float(y)) if x == y => {} - _ => show_suggestion = false, + match exp_ty.sty { + TyKind::Ref(_, exp_ty, _) => { + match (&exp_ty.sty, &found_ty.sty) { + (_, TyKind::Param(_)) | + (_, TyKind::Infer(_)) | + (TyKind::Param(_), _) | + (TyKind::Infer(_), _) => {} + _ if ty::TyS::same_type(exp_ty, found_ty) => {} + _ => show_suggestion = false, + }; } - } else { - show_suggestion = false; + TyKind::Param(_) | TyKind::Infer(_) => {} + _ => show_suggestion = false, } } if let (Ok(snippet), true) = ( diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index b2e2955619e0..1d30ccb87b5d 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -658,6 +658,19 @@ impl<'a, 'tcx> ty::TyS<'tcx> { tcx.needs_drop_raw(param_env.and(self)) } + pub fn same_type(a: Ty<'tcx>, b: Ty<'tcx>) -> bool { + match (&a.sty, &b.sty) { + (&Adt(did_a, substs_a), &Adt(did_b, substs_b)) => { + if did_a != did_b { + return false; + } + + substs_a.types().zip(substs_b.types()).all(|(a, b)| Self::same_type(a, b)) + } + _ => a == b, + } + } + /// Check whether a type is representable. This means it cannot contain unboxed /// structural recursion. This check is needed for structs and enums. pub fn is_representable(&'tcx self, @@ -730,19 +743,6 @@ impl<'a, 'tcx> ty::TyS<'tcx> { } } - fn same_type<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool { - match (&a.sty, &b.sty) { - (&Adt(did_a, substs_a), &Adt(did_b, substs_b)) => { - if did_a != did_b { - return false; - } - - substs_a.types().zip(substs_b.types()).all(|(a, b)| same_type(a, b)) - } - _ => a == b, - } - } - // Does the type `ty` directly (without indirection through a pointer) // contain any types on stack `seen`? fn is_type_structurally_recursive<'a, 'tcx>( @@ -807,7 +807,7 @@ impl<'a, 'tcx> ty::TyS<'tcx> { // struct Foo { Option> } for &seen_type in iter { - if same_type(ty, seen_type) { + if ty::TyS::same_type(ty, seen_type) { debug!("ContainsRecursive: {:?} contains {:?}", seen_type, ty);