Skip to content

Commit bee2899

Browse files
authoredJul 15, 2020
Rollup merge of #74344 - estebank:stringly-wobbly, r=eddyb
Remove string comparison and use diagnostic item instead r? @eddyb
2 parents f4bbd0e + c44ca17 commit bee2899

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed
 

‎src/libcore/option.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,7 @@ impl<A, V: FromIterator<A>> FromIterator<Option<A>> for Option<V> {
16811681
/// to allow `x?` (where `x` is an `Option<T>`) to be converted into your error type, you can
16821682
/// implement `impl From<NoneError>` for `YourErrorType`. In that case, `x?` within a function that
16831683
/// returns `Result<_, YourErrorType>` will translate a `None` value into an `Err` result.
1684+
#[rustc_diagnostic_item = "none_error"]
16841685
#[unstable(feature = "try_trait", issue = "42327")]
16851686
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
16861687
pub struct NoneError;

‎src/librustc_span/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ symbols! {
632632
nomem,
633633
non_ascii_idents,
634634
None,
635+
none_error,
635636
non_exhaustive,
636637
no_niche,
637638
non_modrs_mods,

‎src/librustc_trait_selection/traits/error_reporting/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_middle::ty::{
2626
TypeFoldable, WithConstness,
2727
};
2828
use rustc_session::DiagnosticMessageId;
29+
use rustc_span::symbol::sym;
2930
use rustc_span::{ExpnKind, MultiSpan, Span, DUMMY_SP};
3031
use 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

Comments
 (0)
Please sign in to comment.