InferSource::GenericArg, check for contains

This commit is contained in:
lcnr 2022-07-01 15:49:05 +02:00
parent 7952d2ed83
commit f475e880a4
5 changed files with 42 additions and 30 deletions

View File

@ -533,18 +533,19 @@ enum InferSourceKind<'tcx> {
}
impl<'tcx> InferSource<'tcx> {
/// Returns the span where we're going to insert our suggestion.
///
/// Used when computing the cost of this infer source to check whether
/// we're inside of a macro expansion.
fn main_insert_span(&self) -> Span {
match self.kind {
InferSourceKind::LetBinding { insert_span, .. } => insert_span,
InferSourceKind::ClosureArg { insert_span, .. } => insert_span,
InferSourceKind::GenericArg { insert_span, .. } => insert_span,
InferSourceKind::FullyQualifiedMethodCall { receiver, .. } => receiver.span,
InferSourceKind::ClosureReturn { data, .. } => data.span(),
}
fn from_expansion(&self) -> bool {
let source_from_expansion = match self.kind {
InferSourceKind::LetBinding { insert_span, .. }
| InferSourceKind::ClosureArg { insert_span, .. }
| InferSourceKind::GenericArg { insert_span, .. } => insert_span.from_expansion(),
InferSourceKind::FullyQualifiedMethodCall { receiver, .. } => {
receiver.span.from_expansion()
}
InferSourceKind::ClosureReturn { data, should_wrap_expr, .. } => {
data.span().from_expansion() || should_wrap_expr.map_or(false, Span::from_expansion)
}
};
source_from_expansion || self.span.from_expansion()
}
}
@ -631,7 +632,7 @@ fn arg_cost(self, arg: GenericArg<'tcx>) -> usize {
}
}
fn ty_cost(self, ty: Ty<'tcx>) -> usize {
match ty.kind() {
match *ty.kind() {
ty::Closure(..) => 1000,
ty::FnDef(..) => 150,
ty::FnPtr(..) => 30,
@ -645,6 +646,7 @@ fn ty_cost(self, ty: Ty<'tcx>) -> usize {
.sum::<usize>()
}
ty::Tuple(args) => 5 + args.iter().map(|arg| self.ty_cost(arg)).sum::<usize>(),
ty::Ref(_, ty, _) => 2 + self.ty_cost(ty),
ty::Infer(..) => 0,
_ => 1,
}
@ -673,8 +675,7 @@ fn ty_cost(self, ty: Ty<'tcx>) -> usize {
}
};
let suggestion_may_apply =
if source.main_insert_span().can_be_used_for_suggestions() { 0 } else { 10000 };
let suggestion_may_apply = if source.from_expansion() { 10000 } else { 0 };
base_cost + suggestion_may_apply
}
@ -1022,8 +1023,10 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
debug!(?args);
let InsertableGenericArgs { insert_span, substs, generics_def_id, def_id } = args;
let generics = tcx.generics_of(generics_def_id);
if let Some(argument_index) =
generics.own_substs(substs).iter().position(|&arg| self.generic_arg_is_target(arg))
if let Some(argument_index) = generics
.own_substs(substs)
.iter()
.position(|&arg| self.generic_arg_contains_target(arg))
{
let substs = self.infcx.resolve_vars_if_possible(substs);
let generic_args = &generics.own_substs_no_defaults(tcx, substs)

View File

@ -16,8 +16,8 @@ fn infallible() -> Result<(), std::convert::Infallible> {
fn main() {
let x = || -> Result<_, QualifiedError<_>> {
//~^ ERROR type annotations needed for `Result<(), QualifiedError<_>>`
infallible()?;
Ok(())
//~^ ERROR type annotations needed
};
}

View File

@ -1,16 +1,15 @@
error[E0282]: type annotations needed for `Result<(), QualifiedError<_>>`
--> $DIR/cannot-infer-partial-try-return.rs:18:13
error[E0282]: type annotations needed
--> $DIR/cannot-infer-partial-try-return.rs:20:9
|
LL | let x = || -> Result<_, QualifiedError<_>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | infallible()?;
| ------------- type must be known at this point
LL | Ok(())
| ^^ cannot infer type of the type parameter `E` declared on the enum `Result`
|
help: try giving this closure an explicit return type
help: consider specifying the generic arguments
|
LL | let x = || -> Result<(), QualifiedError<_>> {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LL | Ok::<(), QualifiedError<_>>(())
| +++++++++++++++++++++++++
error: aborting due to previous error

View File

@ -1,8 +1,13 @@
error[E0282]: type annotations needed
--> $DIR/issue-23041.rs:6:22
--> $DIR/issue-23041.rs:6:7
|
LL | b.downcast_ref::<fn(_)->_>();
| ^^^^^^^^ cannot infer type
| ^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the associated function `downcast_ref`
|
help: consider specifying the generic arguments
|
LL | b.downcast_ref::<fn(_) -> _>();
| ~~~~~~~~~~~~~~
error: aborting due to previous error

View File

@ -1,8 +1,13 @@
error[E0282]: type annotations needed
--> $DIR/issue-24013.rs:5:20
--> $DIR/issue-24013.rs:5:13
|
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
| ^^^^^^ cannot infer type
| ^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `swap`
|
help: consider specifying the generic arguments
|
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
| ~~~~~~~~~~
error: aborting due to previous error