diff --git a/src/libcore/ops/try.rs b/src/libcore/ops/try.rs index ef748bcee6e2..996a01d413cb 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 2ffa77031f33..a503e3534e3b 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 272827cfef83..c74870306869 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 d0b39d6016af..b28f0001cd97 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>, 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>, 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: `::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> { // `::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, ); }