Skip to content

Commit eef34a6

Browse files
committed
stop suggesting things inside of macros
1 parent f1836c4 commit eef34a6

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,22 @@ enum InferSourceKind<'tcx> {
532532
},
533533
}
534534

535+
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+
}
548+
}
549+
}
550+
535551
impl<'tcx> InferSourceKind<'tcx> {
536552
fn ty_msg(&self, infcx: &InferCtxt<'_, 'tcx>) -> String {
537553
match *self {
@@ -638,7 +654,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
638654
// The sources are listed in order of preference here.
639655
let tcx = self.infcx.tcx;
640656
let ctx = CostCtxt { tcx };
641-
match source.kind {
657+
let base_cost = match source.kind {
642658
InferSourceKind::LetBinding { ty, .. } => ctx.ty_cost(ty),
643659
InferSourceKind::ClosureArg { ty, .. } => ctx.ty_cost(ty),
644660
InferSourceKind::GenericArg { def_id, generic_args, .. } => {
@@ -655,7 +671,12 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
655671
InferSourceKind::ClosureReturn { ty, should_wrap_expr, .. } => {
656672
30 + ctx.ty_cost(ty) + if should_wrap_expr.is_some() { 10 } else { 0 }
657673
}
658-
}
674+
};
675+
676+
let suggestion_may_apply =
677+
if source.main_insert_span().can_be_used_for_suggestions() { 0 } else { 10000 };
678+
679+
base_cost + suggestion_may_apply
659680
}
660681

661682
/// Uses `fn source_cost` to determine whether this inference source is preferable to

src/test/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
error[E0282]: type annotations needed
2-
--> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:18
1+
error[E0282]: type annotations needed for `(Vec<T>,)`
2+
--> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:9
33
|
44
LL | let (x, ) = (vec![], );
5-
| ^^^^^^ cannot infer type of the type parameter `T` declared on the struct `Vec`
5+
| ^^^^^
66
|
7-
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
8-
help: consider specifying the generic argument
9-
--> $SRC_DIR/alloc/src/macros.rs:LL:COL
7+
help: consider giving this pattern a type, where the type for type parameter `T` is specified
108
|
11-
LL | $crate::__rust_force_expr!($crate::vec::Vec::<T>::new())
12-
| +++++
9+
LL | let (x, ): (Vec<T>,) = (vec![], );
10+
| +++++++++++
1311

1412
error: aborting due to previous error
1513

0 commit comments

Comments
 (0)