Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::errors::{
AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError,
SourceKindMultiSuggestion, SourceKindSubdiag,
};
use crate::infer::InferCtxt;
use crate::infer::{InferCtxt, TyOrConstInferVar};

pub enum TypeAnnotationNeeded {
/// ```compile_fail,E0282
Expand Down Expand Up @@ -81,13 +81,27 @@ impl InferenceDiagnosticsData {
!(self.name == "_" && matches!(self.kind, UnderspecifiedArgKind::Type { .. }))
}

fn where_x_is_kind(&self, in_type: Ty<'_>) -> &'static str {
fn where_x_is_kind<'tcx>(&self, infcx: &InferCtxt<'tcx>, in_type: Ty<'tcx>) -> &'static str {
if in_type.is_ty_or_numeric_infer() {
""
} else if self.name == "_" {
// FIXME: Consider specializing this message if there is a single `_`
// in the type.
"underscore"
let displayed_ty = infcx
.resolve_vars_if_possible(in_type)
.fold_with(&mut ClosureEraser { infcx, depth: 0 });
if displayed_ty.is_ty_or_numeric_infer() {
""
} else {
match displayed_ty
.walk()
.filter_map(TyOrConstInferVar::maybe_from_generic_arg)
.take(2)
.count()
{
0 => "",
1 => "underscore_single",
_ => "underscore_multiple",
}
}
} else {
"has_name"
}
Expand Down Expand Up @@ -554,7 +568,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
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: arg_data.where_x_is_kind(self.infcx, ty),
prefix_kind: arg_data.kind.clone(),
prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
arg_name: arg_data.name,
Expand All @@ -566,7 +580,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
infer_subdiags.push(SourceKindSubdiag::LetLike {
span: insert_span,
name: String::new(),
x_kind: arg_data.where_x_is_kind(ty),
x_kind: arg_data.where_x_is_kind(self.infcx, ty),
prefix_kind: arg_data.kind.clone(),
prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
arg_name: arg_data.name,
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_trait_selection/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ pub enum SourceKindSubdiag<'a> {
[const_with_param] value of const parameter
[const] value of the constant
} `{$arg_name}` is specified
[underscore] , where the placeholders `_` are specified
[underscore_single] , where the placeholder `_` is specified
[underscore_multiple] , where the placeholders `_` are specified
*[empty] {\"\"}
}",
style = "verbose",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let var_fn = Value::wrap();
LL | let _ = var_fn.clone();
| ------ type must be known at this point
|
help: consider giving `var_fn` an explicit type, where the placeholders `_` are specified
help: consider giving `var_fn` an explicit type, where the placeholder `_` is specified
|
LL | let var_fn: Value<Rc<_>> = Value::wrap();
| ++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/editions/edition-raw-pointer-method-2018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL |
LL | let _ = y.is_null();
| ------- cannot call a method on a raw pointer with an unknown pointee type
|
help: consider giving `y` an explicit type, where the placeholders `_` are specified
help: consider giving `y` an explicit type, where the placeholder `_` is specified
|
LL | let y: *const _ = &x as *const _;
| ++++++++++
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/recursive-in-exhaustiveness.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0282]: type annotations needed for `(_,)`
LL | let (x,) = (build(x),);
| ^^^^
|
help: consider giving this pattern a type, where the placeholders `_` are specified
help: consider giving this pattern a type, where the placeholder `_` is specified
|
LL | let (x,): (_,) = (build(x),);
| ++++++
Expand All @@ -15,7 +15,7 @@ error[E0282]: type annotations needed for `((_,),)`
LL | let (x,) = (build2(x),);
| ^^^^
|
help: consider giving this pattern a type, where the placeholders `_` are specified
help: consider giving this pattern a type, where the placeholder `_` is specified
|
LL | let (x,): ((_,),) = (build2(x),);
| +++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/indexing/ambiguity-after-deref-step.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL |
LL | x[1];
| - type must be known at this point
|
help: consider giving `x` an explicit type, where the placeholders `_` are specified
help: consider giving `x` an explicit type, where the placeholder `_` is specified
|
LL | let x: &_ = &Default::default();
| ++++
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Check that `need_type_info` distinguishes between a single placeholder and
// multiple placeholders when suggesting an explicit type.

fn singular() {
let v = &[];
//~^ ERROR type annotations needed
let _ = v.iter();
}

fn plural() {
let x = (vec![], vec![]);
//~^ ERROR type annotations needed
let _ = x;
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0282]: type annotations needed for `&[_; 0]`
--> $DIR/underscore-placeholder-wording.rs:5:9
|
LL | let v = &[];
| ^ --- type must be known at this point
|
help: consider giving `v` an explicit type, where the placeholder `_` is specified
|
LL | let v: &[_; 0] = &[];
| +++++++++

error[E0282]: type annotations needed for `(Vec<_>, Vec<_>)`
--> $DIR/underscore-placeholder-wording.rs:11:9
|
LL | let x = (vec![], vec![]);
| ^ ---------------- type must be known at this point
|
help: consider giving `x` an explicit type, where the placeholders `_` are specified
|
LL | let x: (Vec<_>, Vec<_>) = (vec![], vec![]);
| ++++++++++++++++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0282`.
4 changes: 2 additions & 2 deletions tests/ui/methods/call_method_unknown_pointee.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL |
LL | let _b: u8 = b.read();
| ---- cannot call a method on a raw pointer with an unknown pointee type
|
help: consider giving `b` an explicit type, where the placeholders `_` are specified
help: consider giving `b` an explicit type, where the placeholder `_` is specified
|
LL | let b: *const _ = ptr as *const _;
| ++++++++++
Expand All @@ -37,7 +37,7 @@ LL |
LL | let _d: u8 = d.read();
| ---- cannot call a method on a raw pointer with an unknown pointee type
|
help: consider giving `d` an explicit type, where the placeholders `_` are specified
help: consider giving `d` an explicit type, where the placeholder `_` is specified
|
LL | let d: *mut _ = ptr as *mut _;
| ++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ error[E0282]: type annotations needed for `Vec<_>`
LL | let v : Vec<(u32,_) = vec![];
| ^ ------ type must be known at this point
|
help: consider giving `v` an explicit type, where the placeholders `_` are specified
help: consider giving `v` an explicit type, where the placeholder `_` is specified
|
LL | let v: Vec<_> : Vec<(u32,_) = vec![];
| ++++++++
Expand All @@ -54,7 +54,7 @@ error[E0282]: type annotations needed for `Vec<_>`
LL | let v : Vec<'a = vec![];
| ^ ------ type must be known at this point
|
help: consider giving `v` an explicit type, where the placeholders `_` are specified
help: consider giving `v` an explicit type, where the placeholder `_` is specified
|
LL | let v: Vec<_> : Vec<'a = vec![];
| ++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/pattern/slice-patterns-irrefutable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL |
LL | [a, b] = Default::default();
| - type must be known at this point
|
help: consider giving `b` an explicit type, where the placeholders `_` are specified
help: consider giving `b` an explicit type, where the placeholder `_` is specified
|
LL | let b: [_; 3];
| ++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL |
LL | extract(x).max(2);
| ---------- type must be known at this point
|
help: consider giving `x` an explicit type, where the placeholders `_` are specified
help: consider giving `x` an explicit type, where the placeholder `_` is specified
|
LL | let x: [Foo<T>; 2] = [Foo(PhantomData); 2];
| +++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0282]: type annotations needed for `[_; 0]`
LL | let x = [];
| ^ -- type must be known at this point
|
help: consider giving `x` an explicit type, where the placeholders `_` are specified
help: consider giving `x` an explicit type, where the placeholder `_` is specified
|
LL | let x: [_; 0] = [];
| ++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/type/type-check/cannot_infer_local_or_vec.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0282]: type annotations needed for `Vec<_>`
LL | let x = vec![];
| ^ ------ type must be known at this point
|
help: consider giving `x` an explicit type, where the placeholders `_` are specified
help: consider giving `x` an explicit type, where the placeholder `_` is specified
|
LL | let x: Vec<_> = vec![];
| ++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0282]: type annotations needed for `(Vec<_>,)`
LL | let (x, ) = (vec![], );
| ^^^^^ ---------- type must be known at this point
|
help: consider giving this pattern a type, where the placeholders `_` are specified
help: consider giving this pattern a type, where the placeholder `_` is specified
|
LL | let (x, ): (Vec<_>,) = (vec![], );
| +++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/typeck/issue-7813.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0282]: type annotations needed for `&[_; 0]`
LL | let v = &[];
| ^ --- type must be known at this point
|
help: consider giving `v` an explicit type, where the placeholders `_` are specified
help: consider giving `v` an explicit type, where the placeholder `_` is specified
|
LL | let v: &[_; 0] = &[];
| +++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let mut closure0 = None;
LL | return c();
| - type must be known at this point
|
help: consider giving `closure0` an explicit type, where the placeholders `_` are specified
help: consider giving `closure0` an explicit type, where the placeholder `_` is specified
|
LL | let mut closure0: Option<T> = None;
| +++++++++++
Expand Down
Loading