Suggest collect
ing into Vec<_>
This commit is contained in:
parent
ed620cf969
commit
9d5e7d3c04
@ -20,7 +20,7 @@
|
||||
use rustc_middle::ty::{self, DefIdTree, InferConst};
|
||||
use rustc_middle::ty::{GenericArg, GenericArgKind, SubstsRef};
|
||||
use rustc_middle::ty::{IsSuggestable, Ty, TyCtxt, TypeckResults};
|
||||
use rustc_span::symbol::{kw, Ident};
|
||||
use rustc_span::symbol::{kw, sym, Ident};
|
||||
use rustc_span::{BytePos, Span};
|
||||
use std::borrow::Cow;
|
||||
use std::iter;
|
||||
@ -78,12 +78,12 @@ fn can_add_more_info(&self) -> bool {
|
||||
}
|
||||
|
||||
fn where_x_is_kind(&self, in_type: Ty<'_>) -> &'static str {
|
||||
if in_type.is_ty_infer() {
|
||||
"empty"
|
||||
} else if self.name == "_" {
|
||||
if self.name == "_" {
|
||||
// FIXME: Consider specializing this message if there is a single `_`
|
||||
// in the type.
|
||||
"underscore"
|
||||
} else if in_type.is_ty_infer() {
|
||||
"empty"
|
||||
} else {
|
||||
"has_name"
|
||||
}
|
||||
@ -368,6 +368,7 @@ fn bad_inference_failure_err(
|
||||
}
|
||||
|
||||
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||
#[instrument(level = "debug", skip(self, error_code))]
|
||||
pub fn emit_inference_failure_err(
|
||||
&self,
|
||||
body_id: Option<hir::BodyId>,
|
||||
@ -406,16 +407,20 @@ pub fn emit_inference_failure_err(
|
||||
let mut infer_subdiags = Vec::new();
|
||||
let mut multi_suggestions = Vec::new();
|
||||
match kind {
|
||||
InferSourceKind::LetBinding { insert_span, pattern_name, ty } => {
|
||||
InferSourceKind::LetBinding { insert_span, pattern_name, ty, is_collect } => {
|
||||
infer_subdiags.push(SourceKindSubdiag::LetLike {
|
||||
span: insert_span,
|
||||
name: pattern_name.map(|name| name.to_string()).unwrap_or_else(String::new),
|
||||
x_kind: arg_data.where_x_is_kind(ty),
|
||||
x_kind: if is_collect { "empty" } else { arg_data.where_x_is_kind(ty) },
|
||||
prefix_kind: arg_data.kind.clone(),
|
||||
prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
|
||||
arg_name: arg_data.name,
|
||||
kind: if pattern_name.is_some() { "with_pattern" } else { "other" },
|
||||
type_name: ty_to_string(self, ty),
|
||||
type_name: if is_collect {
|
||||
"Vec<_>".to_string()
|
||||
} else {
|
||||
ty_to_string(self, ty)
|
||||
},
|
||||
});
|
||||
}
|
||||
InferSourceKind::ClosureArg { insert_span, ty } => {
|
||||
@ -608,6 +613,7 @@ enum InferSourceKind<'tcx> {
|
||||
insert_span: Span,
|
||||
pattern_name: Option<Ident>,
|
||||
ty: Ty<'tcx>,
|
||||
is_collect: bool,
|
||||
},
|
||||
ClosureArg {
|
||||
insert_span: Span,
|
||||
@ -788,10 +794,19 @@ fn ty_cost(self, ty: Ty<'tcx>) -> usize {
|
||||
/// Uses `fn source_cost` to determine whether this inference source is preferable to
|
||||
/// previous sources. We generally prefer earlier sources.
|
||||
#[instrument(level = "debug", skip(self))]
|
||||
fn update_infer_source(&mut self, new_source: InferSource<'tcx>) {
|
||||
fn update_infer_source(&mut self, mut new_source: InferSource<'tcx>) {
|
||||
let cost = self.source_cost(&new_source) + self.attempt;
|
||||
debug!(?cost);
|
||||
self.attempt += 1;
|
||||
if let Some(InferSource { kind: InferSourceKind::GenericArg { def_id, ..}, .. }) = self.infer_source
|
||||
&& self.infcx.tcx.get_diagnostic_item(sym::iterator_collect_fn) == Some(def_id)
|
||||
&& let InferSourceKind::LetBinding { ref ty, ref mut is_collect, ..} = new_source.kind
|
||||
&& ty.is_ty_infer()
|
||||
{
|
||||
// Customize the output so we talk about `let x: Vec<_> = iter.collect();` instead of
|
||||
// `let x: _ = iter.collect();`, as this is a very common case.
|
||||
*is_collect = true;
|
||||
}
|
||||
if cost < self.infer_source_cost {
|
||||
self.infer_source_cost = cost;
|
||||
self.infer_source = Some(new_source);
|
||||
@ -1089,6 +1104,7 @@ fn visit_local(&mut self, local: &'tcx Local<'tcx>) {
|
||||
insert_span: local.pat.span.shrink_to_hi(),
|
||||
pattern_name: local.pat.simple_ident(),
|
||||
ty,
|
||||
is_collect: false,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
@ -827,6 +827,7 @@
|
||||
item_like_imports,
|
||||
iter,
|
||||
iter_repeat,
|
||||
iterator_collect_fn,
|
||||
kcfi,
|
||||
keyword,
|
||||
kind,
|
||||
|
@ -1829,6 +1829,7 @@ fn by_ref(&mut self) -> &mut Self
|
||||
#[inline]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
|
||||
#[cfg_attr(not(test), rustc_diagnostic_item = "iterator_collect_fn")]
|
||||
fn collect<B: FromIterator<Self::Item>>(self) -> B
|
||||
where
|
||||
Self: Sized,
|
||||
|
@ -4,7 +4,7 @@ error[E0282]: type annotations needed
|
||||
LL | let [_, _] = a.into();
|
||||
| ^^^^^^
|
||||
|
|
||||
help: consider giving this pattern a type
|
||||
help: consider giving this pattern a type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let [_, _]: _ = a.into();
|
||||
| +++
|
||||
|
@ -10,7 +10,7 @@ error[E0282]: type annotations needed
|
||||
LL | [(); &(&'static: loop { |x| {}; }) as *const _ as usize]
|
||||
| ^
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | [(); &(&'static: loop { |x: _| {}; }) as *const _ as usize]
|
||||
| +++
|
||||
|
@ -6,8 +6,8 @@ LL | let x = "hello".chars().rev().collect();
|
||||
|
|
||||
help: consider giving `x` an explicit type
|
||||
|
|
||||
LL | let x: _ = "hello".chars().rev().collect();
|
||||
| +++
|
||||
LL | let x: Vec<_> = "hello".chars().rev().collect();
|
||||
| ++++++++
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
@ -18,7 +18,7 @@ error[E0282]: type annotations needed
|
||||
LL | |_| true
|
||||
| ^
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |_: _| true
|
||||
| +++
|
||||
|
@ -30,7 +30,7 @@ error[E0282]: type annotations needed
|
||||
LL | |x| String::from("x".as_ref());
|
||||
| ^
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |x: _| String::from("x".as_ref());
|
||||
| +++
|
||||
|
@ -4,7 +4,7 @@ error[E0282]: type annotations needed
|
||||
LL | let x;
|
||||
| ^
|
||||
|
|
||||
help: consider giving `x` an explicit type
|
||||
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let x: _;
|
||||
| +++
|
||||
|
@ -6,7 +6,7 @@ LL | let x = panic!();
|
||||
LL | x.clone();
|
||||
| - type must be known at this point
|
||||
|
|
||||
help: consider giving `x` an explicit type
|
||||
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let x: _ = panic!();
|
||||
| +++
|
||||
|
@ -17,7 +17,7 @@ error[E0282]: type annotations needed
|
||||
LL | 1 => |c| c + 1,
|
||||
| ^
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | 1 => |c: _| c + 1,
|
||||
| +++
|
||||
|
@ -4,7 +4,7 @@ error[E0282]: type annotations needed
|
||||
LL | |s| s.len()
|
||||
| ^ - type must be known at this point
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |s: _| s.len()
|
||||
| +++
|
||||
@ -15,7 +15,7 @@ error[E0282]: type annotations needed
|
||||
LL | |s| s.len()
|
||||
| ^ - type must be known at this point
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |s: _| s.len()
|
||||
| +++
|
||||
@ -26,7 +26,7 @@ error[E0282]: type annotations needed
|
||||
LL | |s| s.len()
|
||||
| ^ - type must be known at this point
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |s: _| s.len()
|
||||
| +++
|
||||
@ -37,7 +37,7 @@ error[E0282]: type annotations needed
|
||||
LL | |s| s.len()
|
||||
| ^ - type must be known at this point
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |s: _| s.len()
|
||||
| +++
|
||||
|
@ -4,7 +4,7 @@ error[E0282]: type annotations needed
|
||||
LL | let x = match () {
|
||||
| ^
|
||||
|
|
||||
help: consider giving `x` an explicit type
|
||||
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let x: _ = match () {
|
||||
| +++
|
||||
|
@ -7,7 +7,7 @@ LL | let x;
|
||||
LL | (..) => {}
|
||||
| ---- type must be known at this point
|
||||
|
|
||||
help: consider giving `x` an explicit type
|
||||
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let x: _;
|
||||
| +++
|
||||
|
@ -191,7 +191,7 @@ error[E0282]: type annotations needed
|
||||
LL | let x @ ..;
|
||||
| ^^^^^^
|
||||
|
|
||||
help: consider giving this pattern a type
|
||||
help: consider giving this pattern a type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let x @ ..: _;
|
||||
| +++
|
||||
|
@ -19,7 +19,7 @@ error[E0282]: type annotations needed
|
||||
LL | let mut N;
|
||||
| ^^^^^
|
||||
|
|
||||
help: consider giving `N` an explicit type
|
||||
help: consider giving `N` an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let mut N: _;
|
||||
| +++
|
||||
|
@ -7,7 +7,7 @@ LL |
|
||||
LL | x.0;
|
||||
| - type must be known at this point
|
||||
|
|
||||
help: consider giving `x` an explicit type
|
||||
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let mut x: _ = Default::default();
|
||||
| +++
|
||||
@ -21,7 +21,7 @@ LL |
|
||||
LL | x[0];
|
||||
| - type must be known at this point
|
||||
|
|
||||
help: consider giving `x` an explicit type
|
||||
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let mut x: _ = Default::default();
|
||||
| +++
|
||||
|
@ -4,7 +4,7 @@ error[E0282]: type annotations needed
|
||||
LL | |x| x.len()
|
||||
| ^ - type must be known at this point
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |x: _| x.len()
|
||||
| +++
|
||||
@ -15,7 +15,7 @@ error[E0282]: type annotations needed
|
||||
LL | |x| x.len()
|
||||
| ^ - type must be known at this point
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | |x: _| x.len()
|
||||
| +++
|
||||
|
@ -10,7 +10,7 @@ error[E0282]: type annotations needed
|
||||
LL | let x = |_| {};
|
||||
| ^
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let x = |_: _| {};
|
||||
| +++
|
||||
|
@ -28,7 +28,7 @@ error[E0282]: type annotations needed
|
||||
LL | let _ = |a, b: _| -> _ { 0 };
|
||||
| ^
|
||||
|
|
||||
help: consider giving this closure parameter an explicit type
|
||||
help: consider giving this closure parameter an explicit type, where the placeholders `_` are specified
|
||||
|
|
||||
LL | let _ = |a: _, b: _| -> _ { 0 };
|
||||
| +++
|
||||
|
Loading…
Reference in New Issue
Block a user