From f475e880a4c6b2359ebaef88c844daede9c88fc9 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 1 Jul 2022 15:49:05 +0200 Subject: [PATCH] `InferSource::GenericArg`, check for contains --- .../infer/error_reporting/need_type_info.rs | 37 ++++++++++--------- .../cannot-infer-partial-try-return.rs | 2 +- .../cannot-infer-partial-try-return.stderr | 15 ++++---- src/test/ui/issues/issue-23041.stderr | 9 ++++- src/test/ui/issues/issue-24013.stderr | 9 ++++- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index 036692c43dc..4d29fc46946 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -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::() } ty::Tuple(args) => 5 + args.iter().map(|arg| self.ty_cost(arg)).sum::(), + 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) diff --git a/src/test/ui/inference/cannot-infer-partial-try-return.rs b/src/test/ui/inference/cannot-infer-partial-try-return.rs index 976827a4478..b555697dc34 100644 --- a/src/test/ui/inference/cannot-infer-partial-try-return.rs +++ b/src/test/ui/inference/cannot-infer-partial-try-return.rs @@ -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 }; } diff --git a/src/test/ui/inference/cannot-infer-partial-try-return.stderr b/src/test/ui/inference/cannot-infer-partial-try-return.stderr index c1e43f0b721..2a56aaa44fe 100644 --- a/src/test/ui/inference/cannot-infer-partial-try-return.stderr +++ b/src/test/ui/inference/cannot-infer-partial-try-return.stderr @@ -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 diff --git a/src/test/ui/issues/issue-23041.stderr b/src/test/ui/issues/issue-23041.stderr index 401086b2044..7b9a1634a0d 100644 --- a/src/test/ui/issues/issue-23041.stderr +++ b/src/test/ui/issues/issue-23041.stderr @@ -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::_>(); - | ^^^^^^^^ 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:: _>(); + | ~~~~~~~~~~~~~~ error: aborting due to previous error diff --git a/src/test/ui/issues/issue-24013.stderr b/src/test/ui/issues/issue-24013.stderr index 4e3cb88297d..863993f4509 100644 --- a/src/test/ui/issues/issue-24013.stderr +++ b/src/test/ui/issues/issue-24013.stderr @@ -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