@@ -1468,6 +1468,45 @@ crate enum Type {
14681468rustc_data_structures:: static_assert_size!( Type , 72 ) ;
14691469
14701470impl Type {
1471+ /// When comparing types for equality, it can help to ignore `&` wrapping.
1472+ crate fn without_borrowed_ref ( & self ) -> & Type {
1473+ let mut result = self ;
1474+ while let Type :: BorrowedRef { type_, .. } = result {
1475+ result = & * type_;
1476+ }
1477+ result
1478+ }
1479+
1480+ /// Check if two types are "potentially the same".
1481+ /// This is different from `Eq`, because it knows that things like
1482+ /// `Placeholder` are possible matches for everything.
1483+ crate fn is_same ( & self , other : & Self , cache : & Cache ) -> bool {
1484+ match ( self , other) {
1485+ // Recursive cases.
1486+ ( Type :: Tuple ( a) , Type :: Tuple ( b) ) => {
1487+ a. len ( ) == b. len ( ) && a. iter ( ) . zip ( b) . all ( |( a, b) | a. is_same ( & b, cache) )
1488+ }
1489+ ( Type :: Slice ( a) , Type :: Slice ( b) ) => a. is_same ( & b, cache) ,
1490+ ( Type :: Array ( a, al) , Type :: Array ( b, bl) ) => al == bl && a. is_same ( & b, cache) ,
1491+ ( Type :: RawPointer ( mutability, type_) , Type :: RawPointer ( b_mutability, b_type_) ) => {
1492+ mutability == b_mutability && type_. is_same ( & b_type_, cache)
1493+ }
1494+ (
1495+ Type :: BorrowedRef { mutability, type_, .. } ,
1496+ Type :: BorrowedRef { mutability : b_mutability, type_ : b_type_, .. } ,
1497+ ) => mutability == b_mutability && type_. is_same ( & b_type_, cache) ,
1498+ // Placeholders and generics are equal to all other types.
1499+ ( Type :: Infer , _) | ( _, Type :: Infer ) => true ,
1500+ ( Type :: Generic ( _) , _) | ( _, Type :: Generic ( _) ) => true ,
1501+ // Other cases, such as primitives, just use recursion.
1502+ ( a, b) => a
1503+ . def_id ( cache)
1504+ . and_then ( |a| Some ( ( a, b. def_id ( cache) ?) ) )
1505+ . map ( |( a, b) | a == b)
1506+ . unwrap_or ( false ) ,
1507+ }
1508+ }
1509+
14711510 crate fn primitive_type ( & self ) -> Option < PrimitiveType > {
14721511 match * self {
14731512 Primitive ( p) | BorrowedRef { type_ : box Primitive ( p) , .. } => Some ( p) ,
0 commit comments