about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcsmoe <csmoe@msn.com>2020-05-10 22:34:01 +0800
committercsmoe <csmoe@msn.com>2020-05-10 22:40:11 +0800
commitc7e64f54c86a05ddd09fc3da4e98a8d748658337 (patch)
tree1f2329ed5d4c1cd029e2a03cdf7bf56e1a0069ea
parent627f473dd426497972cce58ba64e8b0ff2409078 (diff)
downloadrust-c7e64f54c86a05ddd09fc3da4e98a8d748658337.tar.gz
rust-c7e64f54c86a05ddd09fc3da4e98a8d748658337.zip
remove try_trait lang item
-rw-r--r--src/libcore/ops/try.rs1
-rw-r--r--src/librustc_hir/lang_items.rs2
-rw-r--r--src/librustc_trait_selection/traits/error_reporting/mod.rs12
-rw-r--r--src/librustc_trait_selection/traits/error_reporting/suggestions.rs46
4 files changed, 26 insertions, 35 deletions
diff --git a/src/libcore/ops/try.rs b/src/libcore/ops/try.rs
index ef748bcee6e..996a01d413c 100644
--- a/src/libcore/ops/try.rs
+++ b/src/libcore/ops/try.rs
@@ -25,7 +25,6 @@
     )
 )]
 #[doc(alias = "?")]
-#[cfg_attr(not(bootstrap), lang = "try_trait")]
 pub trait Try {
     /// The type of this value when viewed as successful.
     #[unstable(feature = "try_trait", issue = "42327")]
diff --git a/src/librustc_hir/lang_items.rs b/src/librustc_hir/lang_items.rs
index 2ffa77031f3..a503e3534e3 100644
--- a/src/librustc_hir/lang_items.rs
+++ b/src/librustc_hir/lang_items.rs
@@ -194,8 +194,6 @@ language_item_table! {
     ShrAssignTraitLangItem,      "shr_assign",         shr_assign_trait,        Target::Trait;
     IndexTraitLangItem,          "index",              index_trait,             Target::Trait;
     IndexMutTraitLangItem,       "index_mut",          index_mut_trait,         Target::Trait;
-    TryTraitLangItem,            "try_trait",                try_trait,               Target::Trait;
-
     UnsafeCellTypeLangItem,      "unsafe_cell",        unsafe_cell_type,        Target::Struct;
     VaListTypeLangItem,          "va_list",            va_list,                 Target::Struct;
 
diff --git a/src/librustc_trait_selection/traits/error_reporting/mod.rs b/src/librustc_trait_selection/traits/error_reporting/mod.rs
index 272827cfef8..c7487030686 100644
--- a/src/librustc_trait_selection/traits/error_reporting/mod.rs
+++ b/src/librustc_trait_selection/traits/error_reporting/mod.rs
@@ -400,17 +400,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
                         self.suggest_remove_reference(&obligation, &mut err, &trait_ref);
                         self.suggest_semicolon_removal(&obligation, &mut err, span, &trait_ref);
                         self.note_version_mismatch(&mut err, &trait_ref);
-                        //self.sugggest_await_before_try(&mut err, &obligation, &trait_ref);
-                        debug!(
-                            "suggest_await_befor_try: trait_predicate={:?} obligation={:?}, trait_ref={:?}",
-                            trait_predicate, obligation, trait_ref
-                        );
-                        self.suggest_await_befor_try(
-                            &mut err,
-                            &obligation,
-                            trait_ref.self_ty(),
-                            span,
-                        );
+                        self.suggest_await_before_try(&mut err, &obligation, &trait_ref, span);
                         if self.suggest_impl_trait(&mut err, span, &obligation, &trait_ref) {
                             err.emit();
                             return;
diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
index d0b39d6016a..b28f0001cd9 100644
--- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
+++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs
@@ -154,11 +154,11 @@ pub trait InferCtxtExt<'tcx> {
     fn suggest_new_overflow_limit(&self, err: &mut DiagnosticBuilder<'_>);
 
     /// Suggest to await before try: future? => future.await?
-    fn suggest_await_befor_try(
+    fn suggest_await_before_try(
         &self,
         err: &mut DiagnosticBuilder<'_>,
         obligation: &PredicateObligation<'tcx>,
-        ty: Ty<'tcx>,
+        trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
         span: Span,
     );
 }
@@ -1777,21 +1777,23 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
         ));
     }
 
-    fn suggest_await_befor_try(
+    fn suggest_await_before_try(
         &self,
         err: &mut DiagnosticBuilder<'_>,
         obligation: &PredicateObligation<'tcx>,
-        ty: Ty<'tcx>,
+        trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
         span: Span,
     ) {
-        debug!("suggest_await_befor_try: obligation={:?}, span={:?}", obligation, span);
+        debug!(
+            "suggest_await_befor_try: obligation={:?}, span={:?}, trait_ref={:?}",
+            obligation, span, trait_ref
+        );
         let body_hir_id = obligation.cause.body_id;
         let item_id = self.tcx.hir().get_parent_node(body_hir_id);
+
         if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(item_id) {
             let body = self.tcx.hir().body(body_id);
             if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind {
-                // Check for `Future` implementations by constructing a predicate to
-                // prove: `<T as Future>::Output == U`
                 let future_trait = self.tcx.lang_items().future_trait().unwrap();
                 let item_def_id = self
                     .tcx
@@ -1803,14 +1805,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
                 // `<T as Future>::Output`
                 let projection_ty = ty::ProjectionTy {
                     // `T`
-                    substs: self
-                        .tcx
-                        .mk_substs_trait(ty, self.fresh_substs_for_item(span, item_def_id)),
+                    substs: self.tcx.mk_substs_trait(
+                        trait_ref.self_ty(),
+                        self.fresh_substs_for_item(span, item_def_id),
+                    ),
                     // `Future::Output`
                     item_def_id,
                 };
 
-                let cause = ObligationCause::misc(span, body_hir_id);
+                //let cause = ObligationCause::misc(span, body_hir_id);
                 let mut selcx = SelectionContext::new(self);
 
                 let mut obligations = vec![];
@@ -1824,19 +1827,20 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
                 );
 
                 debug!("suggest_await_befor_try: normalized_projection_type {:?}", normalized_ty);
-                let try_trait_ref_id = self.tcx.lang_items().try_trait().unwrap();
-                if let Some(try_trait_ref) = self.tcx.impl_trait_ref(try_trait_ref_id) {
-                    let try_predicate = try_trait_ref.without_const().to_predicate();
-                    let try_obligation =
-                        Obligation::new(cause, obligation.param_env, try_predicate);
-                    debug!("suggest_await_befor_try: try_trait_obligation {:?}", try_obligation);
-                    if self.predicate_may_hold(&try_obligation) {
-                        debug!("try_obligation holds");
-                        if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
+                let try_obligation = self.mk_obligation_for_def_id(
+                    trait_ref.def_id(),
+                    normalized_ty,
+                    obligation.cause.clone(),
+                    obligation.param_env,
+                );
+                debug!("suggest_await_befor_try: try_trait_obligation {:?}", try_obligation);
+                if self.predicate_may_hold(&try_obligation) {
+                    if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
+                        if snippet.ends_with('?') {
                             err.span_suggestion(
                                 span,
                                 "consider using `.await` here",
-                                format!("{}.await", snippet),
+                                format!("{}.await?", snippet.trim_end_matches('?')),
                                 Applicability::MaybeIncorrect,
                             );
                         }