about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2023-06-05 15:28:50 -0700
committerMichael Howell <michael@notriddle.com>2023-06-05 19:05:15 -0700
commit467bc9ffd56dc31d6db79820bd07dbdd5f653783 (patch)
tree15d5b3ecb2b2de52a2ee3b45578b66137225907c /compiler
parent408bbd040613f6776e0a7d05d582c81f4ddc189a (diff)
downloadrust-467bc9ffd56dc31d6db79820bd07dbdd5f653783.tar.gz
rust-467bc9ffd56dc31d6db79820bd07dbdd5f653783.zip
diagnostics: do not suggest type name tweaks on type-inferred closure args
Fixes #111932
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir_typeck/src/check.rs14
-rw-r--r--compiler/rustc_hir_typeck/src/gather_locals.rs12
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs4
3 files changed, 26 insertions, 4 deletions
diff --git a/compiler/rustc_hir_typeck/src/check.rs b/compiler/rustc_hir_typeck/src/check.rs
index bfabd44bb57..69ccbf0b58f 100644
--- a/compiler/rustc_hir_typeck/src/check.rs
+++ b/compiler/rustc_hir_typeck/src/check.rs
@@ -96,7 +96,19 @@ pub(super) fn check_fn<'a, 'tcx>(
         // for simple cases like `fn foo(x: Trait)`,
         // where we would error once on the parameter as a whole, and once on the binding `x`.
         if param.pat.simple_ident().is_none() && !params_can_be_unsized {
-            fcx.require_type_is_sized(param_ty, param.pat.span, traits::SizedArgumentType(ty_span));
+            fcx.require_type_is_sized(
+                param_ty,
+                param.pat.span,
+                // ty_span == binding_span iff this is a closure parameter with no type ascription,
+                // or if it's an implicit `self` parameter
+                traits::SizedArgumentType(
+                    if ty_span == Some(param.span) && tcx.is_closure(fn_def_id.into()) {
+                        None
+                    } else {
+                        ty_span
+                    },
+                ),
+            );
         }
 
         fcx.write_ty(param.hir_id, param_ty);
diff --git a/compiler/rustc_hir_typeck/src/gather_locals.rs b/compiler/rustc_hir_typeck/src/gather_locals.rs
index 38445f28440..d9b9b34ba58 100644
--- a/compiler/rustc_hir_typeck/src/gather_locals.rs
+++ b/compiler/rustc_hir_typeck/src/gather_locals.rs
@@ -129,7 +129,17 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
                     self.fcx.require_type_is_sized(
                         var_ty,
                         p.span,
-                        traits::SizedArgumentType(Some(ty_span)),
+                        // ty_span == ident.span iff this is a closure parameter with no type
+                        // ascription, or if it's an implicit `self` parameter
+                        traits::SizedArgumentType(
+                            if ty_span == ident.span
+                                && self.fcx.tcx.is_closure(self.fcx.body_id.into())
+                            {
+                                None
+                            } else {
+                                Some(ty_span)
+                            },
+                        ),
                     );
                 }
             } else {
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
index 42038dbc3d8..01f2782fd28 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
@@ -2807,8 +2807,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
                     err.help("unsized locals are gated as an unstable feature");
                 }
             }
-            ObligationCauseCode::SizedArgumentType(sp) => {
-                if let Some(span) = sp {
+            ObligationCauseCode::SizedArgumentType(ty_span) => {
+                if let Some(span) = ty_span {
                     if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder()
                         && let ty::Clause::Trait(trait_pred) = clause
                         && let ty::Dynamic(..) = trait_pred.self_ty().kind()