@@ -1811,6 +1811,41 @@ impl Expr<'_> {
1811
1811
}
1812
1812
}
1813
1813
1814
+ /// Whether this and the `other` expression are the same for purposes of an indexing operation.
1815
+ ///
1816
+ /// This is only used for diagnostics to see if we have things like `foo[i]` where `foo` is
1817
+ /// borrowed multiple times with `i`.
1818
+ pub fn equals ( & self , other : & Expr < ' _ > ) -> bool {
1819
+ match ( self . kind , other. kind ) {
1820
+ ( ExprKind :: Lit ( lit1) , ExprKind :: Lit ( lit2) ) => lit1. node == lit2. node ,
1821
+ (
1822
+ ExprKind :: Path ( QPath :: LangItem ( item1, _) ) ,
1823
+ ExprKind :: Path ( QPath :: LangItem ( item2, _) ) ,
1824
+ ) => item1 == item2,
1825
+ (
1826
+ ExprKind :: Path ( QPath :: Resolved ( None , path1) ) ,
1827
+ ExprKind :: Path ( QPath :: Resolved ( None , path2) ) ,
1828
+ ) => path1. res == path2. res ,
1829
+ (
1830
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: RangeTo , _) , [ val1] , None ) ,
1831
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: RangeTo , _) , [ val2] , None ) ,
1832
+ )
1833
+ | (
1834
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: RangeToInclusive , _) , [ val1] , None ) ,
1835
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: RangeToInclusive , _) , [ val2] , None ) ,
1836
+ )
1837
+ | (
1838
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: RangeFrom , _) , [ val1] , None ) ,
1839
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: RangeFrom , _) , [ val2] , None ) ,
1840
+ ) => val1. expr . equals ( val2. expr ) ,
1841
+ (
1842
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: Range , _) , [ val1, val3] , None ) ,
1843
+ ExprKind :: Struct ( QPath :: LangItem ( LangItem :: Range , _) , [ val2, val4] , None ) ,
1844
+ ) => val1. expr . equals ( val2. expr ) && val3. expr . equals ( val4. expr ) ,
1845
+ _ => false ,
1846
+ }
1847
+ }
1848
+
1814
1849
pub fn method_ident ( & self ) -> Option < Ident > {
1815
1850
match self . kind {
1816
1851
ExprKind :: MethodCall ( receiver_method, ..) => Some ( receiver_method. ident ) ,
0 commit comments