Skip to content

Commit 6eaf59d

Browse files
committed
use diagnostic_item and modify wording
1 parent c775927 commit 6eaf59d

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/libcore/option.rs

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ use crate::{
151151

152152
/// The `Option` type. See [the module level documentation](index.html) for more.
153153
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
154+
#[rustc_diagnostic_item = "option_type"]
154155
#[stable(feature = "rust1", since = "1.0.0")]
155156
pub enum Option<T> {
156157
/// No value

src/libcore/result.rs

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ use crate::ops::{self, Deref, DerefMut};
242242
/// [`Err`]: enum.Result.html#variant.Err
243243
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
244244
#[must_use = "this `Result` may be an `Err` variant, which should be handled"]
245+
#[rustc_diagnostic_item = "result_type"]
245246
#[stable(feature = "rust1", since = "1.0.0")]
246247
pub enum Result<T, E> {
247248
/// Contains the success value

src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc::mir::*;
22
use rustc::ty;
33
use rustc_errors::{Applicability, DiagnosticBuilder};
44
use rustc_span::source_map::DesugaringKind;
5-
use rustc_span::Span;
5+
use rustc_span::{Span, Symbol};
66

77
use crate::borrow_check::diagnostics::UseSpans;
88
use crate::borrow_check::prefixes::PrefixSet;
@@ -384,10 +384,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
384384
}
385385
}
386386
};
387-
let move_ty = format!("{:?}", move_place.ty(*self.body, self.infcx.tcx).ty,);
388387
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
389-
let is_option = move_ty.starts_with("std::option::Option");
390-
let is_result = move_ty.starts_with("std::result::Result");
388+
let def_id = match move_place.ty(*self.body, self.infcx.tcx).ty.kind {
389+
ty::Adt(self_def, _) => self_def.did,
390+
ty::Foreign(def_id)
391+
| ty::FnDef(def_id, _)
392+
| ty::Closure(def_id, _)
393+
| ty::Generator(def_id, ..)
394+
| ty::Opaque(def_id, _) => def_id,
395+
_ => return err,
396+
};
397+
let is_option =
398+
self.infcx.tcx.is_diagnostic_item(Symbol::intern("option_type"), def_id);
399+
let is_result =
400+
self.infcx.tcx.is_diagnostic_item(Symbol::intern("result_type"), def_id);
391401
if (is_option || is_result) && use_spans.map_or(true, |v| !v.for_closure()) {
392402
err.span_suggestion(
393403
span,
@@ -399,12 +409,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
399409
Applicability::MaybeIncorrect,
400410
);
401411
} else if span.is_desugaring(DesugaringKind::ForLoop)
402-
&& move_ty.starts_with("std::vec::Vec")
412+
&& self.infcx.tcx.is_diagnostic_item(Symbol::intern("vec_type"), def_id)
403413
{
404414
// FIXME: suggest for anything that implements `IntoIterator`.
405415
err.span_suggestion(
406416
span,
407-
"consider iterating over a slice of the `Vec`'s content",
417+
"consider iterating over a slice of the `Vec<_>`'s content",
408418
format!("&{}", snippet),
409419
Applicability::MaybeIncorrect,
410420
);

src/test/ui/suggestions/for-i-in-vec.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | for _ in self.v {
55
| ^^^^^^
66
| |
77
| move occurs because `self.v` has type `std::vec::Vec<u32>`, which does not implement the `Copy` trait
8-
| help: consider iterating over a slice of the `Vec`'s content: `&self.v`
8+
| help: consider iterating over a slice of the `Vec<_>`'s content: `&self.v`
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)