@@ -26,6 +26,7 @@ use rustc_middle::ty::{
2626 TypeFoldable , WithConstness ,
2727} ;
2828use rustc_session:: DiagnosticMessageId ;
29+ use rustc_span:: symbol:: sym;
2930use rustc_span:: { ExpnKind , MultiSpan , Span , DUMMY_SP } ;
3031use std:: fmt;
3132
@@ -283,8 +284,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
283284 . span_to_snippet ( span)
284285 . map ( |s| & s == "?" )
285286 . 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 ( ) ) ;
288289 let is_unsize =
289290 { Some ( trait_ref. def_id ( ) ) == self . tcx . lang_items ( ) . unsize_trait ( ) } ;
290291 let ( message, note) = if is_try && is_from {
@@ -315,12 +316,15 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
315316 ) )
316317 ) ;
317318
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<" ) ;
323319 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;
324328 if should_convert_option_to_result {
325329 err. span_suggestion_verbose (
326330 span. shrink_to_lo ( ) ,
0 commit comments