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

View File

@ -1,16 +1,15 @@
error[E0282]: type annotations needed for `Result<(), QualifiedError<_>>` error[E0282]: type annotations needed
--> $DIR/cannot-infer-partial-try-return.rs:18:13 --> $DIR/cannot-infer-partial-try-return.rs:20:9
| |
LL | let x = || -> Result<_, QualifiedError<_>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | infallible()?; LL | infallible()?;
| ------------- type must be known at this point | ------------- 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 error: aborting due to previous error

View File

@ -1,8 +1,13 @@
error[E0282]: type annotations needed error[E0282]: type annotations needed
--> $DIR/issue-23041.rs:6:22 --> $DIR/issue-23041.rs:6:7
| |
LL | b.downcast_ref::<fn(_)->_>(); 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 error: aborting due to previous error

View File

@ -1,8 +1,13 @@
error[E0282]: type annotations needed 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))}; 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 error: aborting due to previous error