@@ -154,11 +154,11 @@ pub trait InferCtxtExt<'tcx> {
154154 fn suggest_new_overflow_limit ( & self , err : & mut DiagnosticBuilder < ' _ > ) ;
155155
156156 /// Suggest to await before try: future? => future.await?
157- fn suggest_await_befor_try (
157+ fn suggest_await_before_try (
158158 & self ,
159159 err : & mut DiagnosticBuilder < ' _ > ,
160160 obligation : & PredicateObligation < ' tcx > ,
161- ty : Ty < ' tcx > ,
161+ trait_ref : & ty :: Binder < ty :: TraitRef < ' tcx > > ,
162162 span : Span ,
163163 ) ;
164164}
@@ -1777,21 +1777,23 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
17771777 ) ) ;
17781778 }
17791779
1780- fn suggest_await_befor_try (
1780+ fn suggest_await_before_try (
17811781 & self ,
17821782 err : & mut DiagnosticBuilder < ' _ > ,
17831783 obligation : & PredicateObligation < ' tcx > ,
1784- ty : Ty < ' tcx > ,
1784+ trait_ref : & ty :: Binder < ty :: TraitRef < ' tcx > > ,
17851785 span : Span ,
17861786 ) {
1787- debug ! ( "suggest_await_befor_try: obligation={:?}, span={:?}" , obligation, span) ;
1787+ debug ! (
1788+ "suggest_await_befor_try: obligation={:?}, span={:?}, trait_ref={:?}" ,
1789+ obligation, span, trait_ref
1790+ ) ;
17881791 let body_hir_id = obligation. cause . body_id ;
17891792 let item_id = self . tcx . hir ( ) . get_parent_node ( body_hir_id) ;
1793+
17901794 if let Some ( body_id) = self . tcx . hir ( ) . maybe_body_owned_by ( item_id) {
17911795 let body = self . tcx . hir ( ) . body ( body_id) ;
17921796 if let Some ( hir:: GeneratorKind :: Async ( _) ) = body. generator_kind {
1793- // Check for `Future` implementations by constructing a predicate to
1794- // prove: `<T as Future>::Output == U`
17951797 let future_trait = self . tcx . lang_items ( ) . future_trait ( ) . unwrap ( ) ;
17961798 let item_def_id = self
17971799 . tcx
@@ -1803,14 +1805,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18031805 // `<T as Future>::Output`
18041806 let projection_ty = ty:: ProjectionTy {
18051807 // `T`
1806- substs : self
1807- . tcx
1808- . mk_substs_trait ( ty, self . fresh_substs_for_item ( span, item_def_id) ) ,
1808+ substs : self . tcx . mk_substs_trait (
1809+ trait_ref. self_ty ( ) ,
1810+ self . fresh_substs_for_item ( span, item_def_id) ,
1811+ ) ,
18091812 // `Future::Output`
18101813 item_def_id,
18111814 } ;
18121815
1813- let cause = ObligationCause :: misc ( span, body_hir_id) ;
1816+ // let cause = ObligationCause::misc(span, body_hir_id);
18141817 let mut selcx = SelectionContext :: new ( self ) ;
18151818
18161819 let mut obligations = vec ! [ ] ;
@@ -1824,19 +1827,20 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18241827 ) ;
18251828
18261829 debug ! ( "suggest_await_befor_try: normalized_projection_type {:?}" , normalized_ty) ;
1827- let try_trait_ref_id = self . tcx . lang_items ( ) . try_trait ( ) . unwrap ( ) ;
1828- if let Some ( try_trait_ref) = self . tcx . impl_trait_ref ( try_trait_ref_id) {
1829- let try_predicate = try_trait_ref. without_const ( ) . to_predicate ( ) ;
1830- let try_obligation =
1831- Obligation :: new ( cause, obligation. param_env , try_predicate) ;
1832- debug ! ( "suggest_await_befor_try: try_trait_obligation {:?}" , try_obligation) ;
1833- if self . predicate_may_hold ( & try_obligation) {
1834- debug ! ( "try_obligation holds" ) ;
1835- if let Ok ( snippet) = self . tcx . sess . source_map ( ) . span_to_snippet ( span) {
1830+ let try_obligation = self . mk_obligation_for_def_id (
1831+ trait_ref. def_id ( ) ,
1832+ normalized_ty,
1833+ obligation. cause . clone ( ) ,
1834+ obligation. param_env ,
1835+ ) ;
1836+ debug ! ( "suggest_await_befor_try: try_trait_obligation {:?}" , try_obligation) ;
1837+ if self . predicate_may_hold ( & try_obligation) {
1838+ if let Ok ( snippet) = self . tcx . sess . source_map ( ) . span_to_snippet ( span) {
1839+ if snippet. ends_with ( '?' ) {
18361840 err. span_suggestion (
18371841 span,
18381842 "consider using `.await` here" ,
1839- format ! ( "{}.await" , snippet) ,
1843+ format ! ( "{}.await? " , snippet. trim_end_matches ( '?' ) ) ,
18401844 Applicability :: MaybeIncorrect ,
18411845 ) ;
18421846 }
0 commit comments