Skip to content

Commit c7e64f5

Browse files
committed
remove try_trait lang item
1 parent 627f473 commit c7e64f5

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

src/libcore/ops/try.rs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
)
2626
)]
2727
#[doc(alias = "?")]
28-
#[cfg_attr(not(bootstrap), lang = "try_trait")]
2928
pub trait Try {
3029
/// The type of this value when viewed as successful.
3130
#[unstable(feature = "try_trait", issue = "42327")]

src/librustc_hir/lang_items.rs

-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,6 @@ language_item_table! {
194194
ShrAssignTraitLangItem, "shr_assign", shr_assign_trait, Target::Trait;
195195
IndexTraitLangItem, "index", index_trait, Target::Trait;
196196
IndexMutTraitLangItem, "index_mut", index_mut_trait, Target::Trait;
197-
TryTraitLangItem, "try_trait", try_trait, Target::Trait;
198-
199197
UnsafeCellTypeLangItem, "unsafe_cell", unsafe_cell_type, Target::Struct;
200198
VaListTypeLangItem, "va_list", va_list, Target::Struct;
201199

src/librustc_trait_selection/traits/error_reporting/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -400,17 +400,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
400400
self.suggest_remove_reference(&obligation, &mut err, &trait_ref);
401401
self.suggest_semicolon_removal(&obligation, &mut err, span, &trait_ref);
402402
self.note_version_mismatch(&mut err, &trait_ref);
403-
//self.sugggest_await_before_try(&mut err, &obligation, &trait_ref);
404-
debug!(
405-
"suggest_await_befor_try: trait_predicate={:?} obligation={:?}, trait_ref={:?}",
406-
trait_predicate, obligation, trait_ref
407-
);
408-
self.suggest_await_befor_try(
409-
&mut err,
410-
&obligation,
411-
trait_ref.self_ty(),
412-
span,
413-
);
403+
self.suggest_await_before_try(&mut err, &obligation, &trait_ref, span);
414404
if self.suggest_impl_trait(&mut err, span, &obligation, &trait_ref) {
415405
err.emit();
416406
return;

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)