@@ -1669,6 +1669,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1669
1669
) -> bool {
1670
1670
let ( traits:: ExprItemObligation ( def_id, hir_id, idx) | traits:: ExprBindingObligation ( def_id, _, hir_id, idx) )
1671
1671
= * error. obligation . cause . code ( ) . peel_derives ( ) else { return false ; } ;
1672
+ let hir = self . tcx . hir ( ) ;
1673
+ let hir:: Node :: Expr ( expr) = hir. get ( hir_id) else { return false ; } ;
1672
1674
1673
1675
// Skip over mentioning async lang item
1674
1676
if Some ( def_id) == self . tcx . lang_items ( ) . from_generator_fn ( )
@@ -1677,15 +1679,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1677
1679
{
1678
1680
return false ;
1679
1681
}
1680
- // Skip over closure arg mismatch, which has a better heuristic
1681
- // to determine what span to point at.
1682
- if let traits:: FulfillmentErrorCode :: CodeSelectionError (
1683
- traits:: SelectionError :: OutputTypeParameterMismatch ( _, expected, _) ,
1684
- ) = error. code
1685
- && let ty:: Closure ( ..) | ty:: Generator ( ..) = expected. skip_binder ( ) . self_ty ( ) . kind ( )
1686
- {
1687
- return false ;
1688
- }
1689
1682
1690
1683
let Some ( unsubstituted_pred) =
1691
1684
self . tcx . predicates_of ( def_id) . instantiate_identity ( self . tcx ) . predicates . into_iter ( ) . nth ( idx)
@@ -1741,16 +1734,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1741
1734
self . find_ambiguous_parameter_in ( def_id, error. root_obligation . predicate ) ;
1742
1735
}
1743
1736
1744
- let hir = self . tcx . hir ( ) ;
1745
- match hir. get ( hir_id) {
1746
- hir:: Node :: Expr ( hir:: Expr { kind : hir:: ExprKind :: Path ( qpath) , hir_id, .. } ) => {
1737
+ if self . closure_span_overlaps_error ( error, expr. span ) {
1738
+ return false ;
1739
+ }
1740
+
1741
+ match & expr. kind {
1742
+ hir:: ExprKind :: Path ( qpath) => {
1747
1743
if let hir:: Node :: Expr ( hir:: Expr {
1748
1744
kind : hir:: ExprKind :: Call ( callee, args) ,
1749
1745
hir_id : call_hir_id,
1746
+ span : call_span,
1750
1747
..
1751
- } ) = hir. get ( hir. get_parent_node ( * hir_id) )
1752
- && callee. hir_id == * hir_id
1748
+ } ) = hir. get ( hir. get_parent_node ( expr . hir_id ) )
1749
+ && callee. hir_id == expr . hir_id
1753
1750
{
1751
+ if self . closure_span_overlaps_error ( error, * call_span) {
1752
+ return false ;
1753
+ }
1754
+
1754
1755
for param in
1755
1756
[ param_to_point_at, fallback_param_to_point_at, self_param_to_point_at]
1756
1757
. into_iter ( )
@@ -1780,10 +1781,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1780
1781
}
1781
1782
}
1782
1783
}
1783
- hir:: Node :: Expr ( hir:: Expr {
1784
- kind : hir:: ExprKind :: MethodCall ( segment, args, ..) ,
1785
- ..
1786
- } ) => {
1784
+ hir:: ExprKind :: MethodCall ( segment, args, ..) => {
1787
1785
for param in [ param_to_point_at, fallback_param_to_point_at, self_param_to_point_at]
1788
1786
. into_iter ( )
1789
1787
. flatten ( )
@@ -1805,9 +1803,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1805
1803
return true ;
1806
1804
}
1807
1805
}
1808
- hir:: Node :: Expr ( hir:: Expr {
1809
- kind : hir:: ExprKind :: Struct ( qpath, fields, ..) , ..
1810
- } ) => {
1806
+ hir:: ExprKind :: Struct ( qpath, fields, ..) => {
1811
1807
if let Res :: Def ( DefKind :: Struct | DefKind :: Variant , variant_def_id) =
1812
1808
self . typeck_results . borrow ( ) . qpath_res ( qpath, hir_id)
1813
1809
{
@@ -1839,6 +1835,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1839
1835
false
1840
1836
}
1841
1837
1838
+ fn closure_span_overlaps_error (
1839
+ & self ,
1840
+ error : & traits:: FulfillmentError < ' tcx > ,
1841
+ span : Span ,
1842
+ ) -> bool {
1843
+ if let traits:: FulfillmentErrorCode :: CodeSelectionError (
1844
+ traits:: SelectionError :: OutputTypeParameterMismatch ( _, expected, _) ,
1845
+ ) = error. code
1846
+ && let ty:: Closure ( def_id, _) | ty:: Generator ( def_id, ..) = expected. skip_binder ( ) . self_ty ( ) . kind ( )
1847
+ && span. overlaps ( self . tcx . def_span ( * def_id) )
1848
+ {
1849
+ true
1850
+ } else {
1851
+ false
1852
+ }
1853
+ }
1854
+
1842
1855
fn point_at_arg_if_possible (
1843
1856
& self ,
1844
1857
error : & mut traits:: FulfillmentError < ' tcx > ,
0 commit comments