@@ -154,11 +154,11 @@ pub trait InferCtxtExt<'tcx> {
154
154
fn suggest_new_overflow_limit ( & self , err : & mut DiagnosticBuilder < ' _ > ) ;
155
155
156
156
/// Suggest to await before try: future? => future.await?
157
- fn suggest_await_befor_try (
157
+ fn suggest_await_before_try (
158
158
& self ,
159
159
err : & mut DiagnosticBuilder < ' _ > ,
160
160
obligation : & PredicateObligation < ' tcx > ,
161
- ty : Ty < ' tcx > ,
161
+ trait_ref : & ty :: Binder < ty :: TraitRef < ' tcx > > ,
162
162
span : Span ,
163
163
) ;
164
164
}
@@ -1777,21 +1777,23 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1777
1777
) ) ;
1778
1778
}
1779
1779
1780
- fn suggest_await_befor_try (
1780
+ fn suggest_await_before_try (
1781
1781
& self ,
1782
1782
err : & mut DiagnosticBuilder < ' _ > ,
1783
1783
obligation : & PredicateObligation < ' tcx > ,
1784
- ty : Ty < ' tcx > ,
1784
+ trait_ref : & ty :: Binder < ty :: TraitRef < ' tcx > > ,
1785
1785
span : Span ,
1786
1786
) {
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
+ ) ;
1788
1791
let body_hir_id = obligation. cause . body_id ;
1789
1792
let item_id = self . tcx . hir ( ) . get_parent_node ( body_hir_id) ;
1793
+
1790
1794
if let Some ( body_id) = self . tcx . hir ( ) . maybe_body_owned_by ( item_id) {
1791
1795
let body = self . tcx . hir ( ) . body ( body_id) ;
1792
1796
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`
1795
1797
let future_trait = self . tcx . lang_items ( ) . future_trait ( ) . unwrap ( ) ;
1796
1798
let item_def_id = self
1797
1799
. tcx
@@ -1803,14 +1805,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1803
1805
// `<T as Future>::Output`
1804
1806
let projection_ty = ty:: ProjectionTy {
1805
1807
// `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
+ ) ,
1809
1812
// `Future::Output`
1810
1813
item_def_id,
1811
1814
} ;
1812
1815
1813
- let cause = ObligationCause :: misc ( span, body_hir_id) ;
1816
+ // let cause = ObligationCause::misc(span, body_hir_id);
1814
1817
let mut selcx = SelectionContext :: new ( self ) ;
1815
1818
1816
1819
let mut obligations = vec ! [ ] ;
@@ -1824,19 +1827,20 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
1824
1827
) ;
1825
1828
1826
1829
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 ( '?' ) {
1836
1840
err. span_suggestion (
1837
1841
span,
1838
1842
"consider using `.await` here" ,
1839
- format ! ( "{}.await" , snippet) ,
1843
+ format ! ( "{}.await? " , snippet. trim_end_matches ( '?' ) ) ,
1840
1844
Applicability :: MaybeIncorrect ,
1841
1845
) ;
1842
1846
}
0 commit comments