about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2023-11-14 01:31:03 +0000
committerEsteban Küber <esteban@kuber.com.ar>2023-11-19 17:50:47 +0000
commita519c9b6b7ab7a1245cae8a9053b666e379f9d53 (patch)
tree27f9d1625149ea1760a255305689c6519e49f00f
parent4d16171f563761e19798c220ee415f84d62df26c (diff)
downloadrust-a519c9b6b7ab7a1245cae8a9053b666e379f9d53.tar.gz
rust-a519c9b6b7ab7a1245cae8a9053b666e379f9d53.zip
review comments
-rw-r--r--compiler/rustc_hir_typeck/src/expr.rs32
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs35
2 files changed, 24 insertions, 43 deletions
diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs
index 4c177dbab79..ab567edd062 100644
--- a/compiler/rustc_hir_typeck/src/expr.rs
+++ b/compiler/rustc_hir_typeck/src/expr.rs
@@ -2121,27 +2121,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     if !self.can_eq(self.param_env, ret_ty, adt_ty) {
                         return None;
                     }
-                    // Check for `-> Self`
                     let input_len = fn_sig.inputs().skip_binder().len();
-                    if def.did() == def_id {
-                        let order = if item.name.as_str().starts_with("new") { 0 } else { 1 };
-                        Some((order, item.name, input_len))
-                    } else {
-                        None
-                    }
+                    let order = !item.name.as_str().starts_with("new");
+                    Some((order, item.name, input_len))
                 })
                 .collect::<Vec<_>>();
             items.sort_by_key(|(order, _, _)| *order);
+            let suggestion = |name, args| {
+                format!(
+                    "::{name}({})",
+                    std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
+                )
+            };
             match &items[..] {
                 [] => {}
                 [(_, name, args)] => {
                     err.span_suggestion_verbose(
                         span.shrink_to_hi().with_hi(expr_span.hi()),
-                        format!("you might have meant to use the `{name}` associated function",),
-                        format!(
-                            "::{name}({})",
-                            std::iter::repeat("_").take(*args).collect::<Vec<_>>().join(", ")
-                        ),
+                        format!("you might have meant to use the `{name}` associated function"),
+                        suggestion(name, *args),
                         Applicability::MaybeIncorrect,
                     );
                 }
@@ -2151,15 +2149,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                         "you might have meant to use an associated function to build this type",
                         items
                             .iter()
-                            .map(|(_, name, args)| {
-                                format!(
-                                    "::{name}({})",
-                                    std::iter::repeat("_")
-                                        .take(*args)
-                                        .collect::<Vec<_>>()
-                                        .join(", ")
-                                )
-                            })
+                            .map(|(_, name, args)| suggestion(name, *args))
                             .collect::<Vec<String>>(),
                         Applicability::MaybeIncorrect,
                     );
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index b0bb9a383db..a4f2446cc74 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -1732,6 +1732,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
         args: &[P<Expr>],
     ) {
         if def_id.is_local() {
+            // Doing analysis on local `DefId`s would cause infinite recursion.
             return;
         }
         // Look at all the associated functions without receivers in the type's
@@ -1752,23 +1753,21 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
                 let ty::Adt(def, _args) = ret_ty.kind() else {
                     return None;
                 };
-                // Check for `-> Self`
                 let input_len = fn_sig.inputs().skip_binder().len();
-                if def.did() == def_id {
-                    let order = if item.name.as_str().starts_with("new") {
-                        0
-                    } else if input_len == args.len() {
-                        2
-                    } else {
-                        3
-                    };
-                    Some((order, item.name, input_len))
-                } else {
-                    None
+                if def.did() != def_id {
+                    return None;
                 }
+                let order = !item.name.as_str().starts_with("new");
+                Some((order, item.name, input_len))
             })
             .collect::<Vec<_>>();
         items.sort_by_key(|(order, _, _)| *order);
+        let suggestion = |name, args| {
+            format!(
+                "::{name}({})",
+                std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
+            )
+        };
         match &items[..] {
             [] => {}
             [(_, name, len)] if *len == args.len() => {
@@ -1783,10 +1782,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
                 err.span_suggestion_verbose(
                     path_span.shrink_to_hi().with_hi(call_span.hi()),
                     format!("you might have meant to use the `{name}` associated function",),
-                    format!(
-                        "::{name}({})",
-                        std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
-                    ),
+                    suggestion(name, *len),
                     Applicability::MaybeIncorrect,
                 );
             }
@@ -1796,12 +1792,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
                     "you might have meant to use an associated function to build this type",
                     items
                         .iter()
-                        .map(|(_, name, len)| {
-                            format!(
-                                "::{name}({})",
-                                std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
-                            )
-                        })
+                        .map(|(_, name, len)| suggestion(name, *len))
                         .collect::<Vec<String>>(),
                     Applicability::MaybeIncorrect,
                     SuggestionStyle::ShowAlways,