mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-26 13:01:27 +03:00
Rollup merge of #155561 - cijiugechu:fix/need-type-info-underscore-wording, r=adwinwhite
Use singular wording for single _ placeholders in type suggestions While looking this part of code, I noticed this FIXME and fixed it :)
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError,
|
||||
SourceKindMultiSuggestion, SourceKindSubdiag,
|
||||
};
|
||||
use crate::infer::InferCtxt;
|
||||
use crate::infer::{InferCtxt, TyOrConstInferVar};
|
||||
|
||||
pub enum TypeAnnotationNeeded {
|
||||
/// ```compile_fail,E0282
|
||||
@@ -82,13 +82,27 @@ fn can_add_more_info(&self) -> bool {
|
||||
!(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"
|
||||
}
|
||||
@@ -555,7 +569,7 @@ pub fn emit_inference_failure_err_with_type_hint(
|
||||
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,
|
||||
@@ -567,7 +581,7 @@ pub fn emit_inference_failure_err_with_type_hint(
|
||||
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,
|
||||
|
||||
@@ -277,7 +277,8 @@ pub(crate) 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",
|
||||
|
||||
@@ -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();
|
||||
| ++++++++++++++
|
||||
|
||||
@@ -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 _;
|
||||
| ++++++++++
|
||||
|
||||
@@ -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),);
|
||||
| ++++++
|
||||
@@ -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),);
|
||||
| +++++++++
|
||||
|
||||
@@ -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();
|
||||
| ++++
|
||||
|
||||
@@ -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() {}
|
||||
@@ -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`.
|
||||
@@ -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 _;
|
||||
| ++++++++++
|
||||
@@ -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 _;
|
||||
| ++++++++
|
||||
|
||||
@@ -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![];
|
||||
| ++++++++
|
||||
@@ -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![];
|
||||
| ++++++++
|
||||
|
||||
@@ -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];
|
||||
| ++++++++
|
||||
|
||||
@@ -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];
|
||||
| +++++++++++++
|
||||
|
||||
@@ -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] = [];
|
||||
| ++++++++
|
||||
|
||||
@@ -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![];
|
||||
| ++++++++
|
||||
|
||||
@@ -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![], );
|
||||
| +++++++++++
|
||||
|
||||
@@ -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] = &[];
|
||||
| +++++++++
|
||||
|
||||
@@ -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;
|
||||
| +++++++++++
|
||||
|
||||
Reference in New Issue
Block a user