@@ -26,6 +26,7 @@ use rustc_middle::ty::{
26
26
TypeFoldable, WithConstness,
27
27
};
28
28
use rustc_session::DiagnosticMessageId;
29
+ use rustc_span::symbol::sym;
29
30
use rustc_span::{ExpnKind, MultiSpan, Span, DUMMY_SP};
30
31
use std::fmt;
31
32
@@ -283,8 +284,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
283
284
.span_to_snippet(span)
284
285
.map(|s| &s == "?")
285
286
.unwrap_or(false);
286
- let is_from = format!("{}", trait_ref.print_only_trait_path() )
287
- .starts_with("std::convert::From<" );
287
+ let is_from = self.tcx.get_diagnostic_item(sym::from_trait )
288
+ == Some(trait_ref.def_id() );
288
289
let is_unsize =
289
290
{ Some(trait_ref.def_id()) == self.tcx.lang_items().unsize_trait() };
290
291
let (message, note) = if is_try && is_from {
@@ -315,12 +316,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
315
316
))
316
317
);
317
318
318
- let should_convert_option_to_result =
319
- format!("{}", trait_ref.print_only_trait_path())
320
- .starts_with("std::convert::From<std::option::NoneError");
321
- let should_convert_result_to_option = format!("{}", trait_ref)
322
- .starts_with("<std::option::NoneError as std::convert::From<");
323
319
if is_try && is_from {
320
+ let none_error = self
321
+ .tcx
322
+ .get_diagnostic_item(sym::none_error)
323
+ .map(|def_id| tcx.type_of(def_id));
324
+ let should_convert_option_to_result =
325
+ Some(trait_ref.skip_binder().substs.type_at(1)) == none_error;
326
+ let should_convert_result_to_option =
327
+ Some(trait_ref.self_ty().skip_binder()) == none_error;
324
328
if should_convert_option_to_result {
325
329
err.span_suggestion_verbose(
326
330
span.shrink_to_lo(),
0 commit comments