Skip to content

Commit f475e88

Browse files
committed
InferSource::GenericArg, check for contains
1 parent 7952d2e commit f475e88

File tree

5 files changed

+42
-30
lines changed

5 files changed

+42
-30
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -533,18 +533,19 @@ enum InferSourceKind<'tcx> {
533533
}
534534

535535
impl<'tcx> InferSource<'tcx> {
536-
/// Returns the span where we're going to insert our suggestion.
537-
///
538-
/// Used when computing the cost of this infer source to check whether
539-
/// we're inside of a macro expansion.
540-
fn main_insert_span(&self) -> Span {
541-
match self.kind {
542-
InferSourceKind::LetBinding { insert_span, .. } => insert_span,
543-
InferSourceKind::ClosureArg { insert_span, .. } => insert_span,
544-
InferSourceKind::GenericArg { insert_span, .. } => insert_span,
545-
InferSourceKind::FullyQualifiedMethodCall { receiver, .. } => receiver.span,
546-
InferSourceKind::ClosureReturn { data, .. } => data.span(),
547-
}
536+
fn from_expansion(&self) -> bool {
537+
let source_from_expansion = match self.kind {
538+
InferSourceKind::LetBinding { insert_span, .. }
539+
| InferSourceKind::ClosureArg { insert_span, .. }
540+
| InferSourceKind::GenericArg { insert_span, .. } => insert_span.from_expansion(),
541+
InferSourceKind::FullyQualifiedMethodCall { receiver, .. } => {
542+
receiver.span.from_expansion()
543+
}
544+
InferSourceKind::ClosureReturn { data, should_wrap_expr, .. } => {
545+
data.span().from_expansion() || should_wrap_expr.map_or(false, Span::from_expansion)
546+
}
547+
};
548+
source_from_expansion || self.span.from_expansion()
548549
}
549550
}
550551

@@ -631,7 +632,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
631632
}
632633
}
633634
fn ty_cost(self, ty: Ty<'tcx>) -> usize {
634-
match ty.kind() {
635+
match *ty.kind() {
635636
ty::Closure(..) => 1000,
636637
ty::FnDef(..) => 150,
637638
ty::FnPtr(..) => 30,
@@ -645,6 +646,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
645646
.sum::<usize>()
646647
}
647648
ty::Tuple(args) => 5 + args.iter().map(|arg| self.ty_cost(arg)).sum::<usize>(),
649+
ty::Ref(_, ty, _) => 2 + self.ty_cost(ty),
648650
ty::Infer(..) => 0,
649651
_ => 1,
650652
}
@@ -673,8 +675,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
673675
}
674676
};
675677

676-
let suggestion_may_apply =
677-
if source.main_insert_span().can_be_used_for_suggestions() { 0 } else { 10000 };
678+
let suggestion_may_apply = if source.from_expansion() { 10000 } else { 0 };
678679

679680
base_cost + suggestion_may_apply
680681
}
@@ -1022,8 +1023,10 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
10221023
debug!(?args);
10231024
let InsertableGenericArgs { insert_span, substs, generics_def_id, def_id } = args;
10241025
let generics = tcx.generics_of(generics_def_id);
1025-
if let Some(argument_index) =
1026-
generics.own_substs(substs).iter().position(|&arg| self.generic_arg_is_target(arg))
1026+
if let Some(argument_index) = generics
1027+
.own_substs(substs)
1028+
.iter()
1029+
.position(|&arg| self.generic_arg_contains_target(arg))
10271030
{
10281031
let substs = self.infcx.resolve_vars_if_possible(substs);
10291032
let generic_args = &generics.own_substs_no_defaults(tcx, substs)

src/test/ui/inference/cannot-infer-partial-try-return.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ fn infallible() -> Result<(), std::convert::Infallible> {
1616

1717
fn main() {
1818
let x = || -> Result<_, QualifiedError<_>> {
19-
//~^ ERROR type annotations needed for `Result<(), QualifiedError<_>>`
2019
infallible()?;
2120
Ok(())
21+
//~^ ERROR type annotations needed
2222
};
2323
}

src/test/ui/inference/cannot-infer-partial-try-return.stderr

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
error[E0282]: type annotations needed for `Result<(), QualifiedError<_>>`
2-
--> $DIR/cannot-infer-partial-try-return.rs:18:13
1+
error[E0282]: type annotations needed
2+
--> $DIR/cannot-infer-partial-try-return.rs:20:9
33
|
4-
LL | let x = || -> Result<_, QualifiedError<_>> {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
LL |
74
LL | infallible()?;
85
| ------------- type must be known at this point
6+
LL | Ok(())
7+
| ^^ cannot infer type of the type parameter `E` declared on the enum `Result`
98
|
10-
help: try giving this closure an explicit return type
9+
help: consider specifying the generic arguments
1110
|
12-
LL | let x = || -> Result<(), QualifiedError<_>> {
13-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
LL | Ok::<(), QualifiedError<_>>(())
12+
| +++++++++++++++++++++++++
1413

1514
error: aborting due to previous error
1615

src/test/ui/issues/issue-23041.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/issue-23041.rs:6:22
2+
--> $DIR/issue-23041.rs:6:7
33
|
44
LL | b.downcast_ref::<fn(_)->_>();
5-
| ^^^^^^^^ cannot infer type
5+
| ^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the associated function `downcast_ref`
6+
|
7+
help: consider specifying the generic arguments
8+
|
9+
LL | b.downcast_ref::<fn(_) -> _>();
10+
| ~~~~~~~~~~~~~~
611

712
error: aborting due to previous error
813

src/test/ui/issues/issue-24013.stderr

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/issue-24013.rs:5:20
2+
--> $DIR/issue-24013.rs:5:13
33
|
44
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
5-
| ^^^^^^ cannot infer type
5+
| ^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `swap`
6+
|
7+
help: consider specifying the generic arguments
8+
|
9+
LL | unsafe {swap::<&mut _>(transmute(&a), transmute(&b))};
10+
| ~~~~~~~~~~
611

712
error: aborting due to previous error
813

0 commit comments

Comments
 (0)