@@ -726,25 +726,62 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
726
726
// Ensure that the parent of the def is an item, not HRTB
727
727
let parent_id = self . tcx . parent_hir_id ( hir_id) ;
728
728
if !parent_id. is_owner ( ) {
729
- struct_span_code_err ! (
730
- self . tcx. dcx( ) ,
731
- lifetime. ident. span,
732
- E0657 ,
733
- "`impl Trait` can only capture lifetimes bound at the fn or impl level"
734
- )
735
- . emit ( ) ;
729
+ // If the lifetime span is the same as the lifetime's declaration,
730
+ // then it's likely we've implicitly captured the lifetime. Give
731
+ // a better span and also note that it's implicitly captured.
732
+ let diag = if lifetime. ident . span == self . tcx . def_span ( def_id) {
733
+ self . tcx
734
+ . dcx ( )
735
+ . struct_span_err (
736
+ self . tcx . def_span ( item_id. owner_id ) ,
737
+ "`impl Trait` can only capture lifetimes bound \
738
+ at the fn or impl level",
739
+ )
740
+ . with_span_label (
741
+ self . tcx . def_span ( item_id. owner_id ) ,
742
+ "`impl Trait` implicitly captures all lifetimes in scope" ,
743
+ )
744
+ } else {
745
+ struct_span_code_err ! (
746
+ self . tcx. dcx( ) ,
747
+ lifetime. ident. span,
748
+ E0657 ,
749
+ "`impl Trait` can only capture lifetimes bound at the fn or impl level"
750
+ )
751
+ } ;
752
+ diag. with_code ( E0657 )
753
+ . with_span_note ( self . tcx . def_span ( def_id) , "lifetime declared here" )
754
+ . emit ( ) ;
736
755
self . uninsert_lifetime_on_error ( lifetime, def. unwrap ( ) ) ;
737
756
}
738
757
if let hir:: Node :: Item ( hir:: Item {
739
758
kind : hir:: ItemKind :: OpaqueTy { .. } , ..
740
759
} ) = self . tcx . hir_node ( parent_id)
741
760
{
742
- self . tcx . dcx ( ) . struct_span_err (
743
- lifetime. ident . span ,
744
- "higher kinded lifetime bounds on nested opaque types are not supported yet" ,
745
- )
746
- . with_span_note ( self . tcx . def_span ( def_id) , "lifetime declared here" )
747
- . emit ( ) ;
761
+ let diag = if lifetime. ident . span == self . tcx . def_span ( def_id) {
762
+ // If the lifetime span is the same as the lifetime's declaration,
763
+ // then it's likely we've implicitly captured the lifetime. Give
764
+ // a better span and also note that it's implicitly captured.
765
+ self . tcx
766
+ . dcx ( )
767
+ . struct_span_err (
768
+ self . tcx . def_span ( item_id. owner_id ) ,
769
+ "`impl Trait` captures higher-ranked lifetime, \
770
+ which is not supported yet",
771
+ )
772
+ . with_span_label (
773
+ self . tcx . def_span ( item_id. owner_id ) ,
774
+ "`impl Trait` implicitly captures all lifetimes in scope" ,
775
+ )
776
+ } else {
777
+ self . tcx . dcx ( ) . struct_span_err (
778
+ lifetime. ident . span ,
779
+ "higher kinded lifetime bounds on nested opaque types are not supported yet" ,
780
+ )
781
+ } ;
782
+ diag. with_code ( E0657 )
783
+ . with_span_note ( self . tcx . def_span ( def_id) , "lifetime declared here" )
784
+ . emit ( ) ;
748
785
self . uninsert_lifetime_on_error ( lifetime, def. unwrap ( ) ) ;
749
786
}
750
787
}
0 commit comments