Skip to content

Commit a519c9b

Browse files
committed
review comments
1 parent 4d16171 commit a519c9b

File tree

2 files changed

+24
-43
lines changed

2 files changed

+24
-43
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+11-21
Original file line numberDiff line numberDiff line change
@@ -2121,27 +2121,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21212121
if !self.can_eq(self.param_env, ret_ty, adt_ty) {
21222122
return None;
21232123
}
2124-
// Check for `-> Self`
21252124
let input_len = fn_sig.inputs().skip_binder().len();
2126-
if def.did() == def_id {
2127-
let order = if item.name.as_str().starts_with("new") { 0 } else { 1 };
2128-
Some((order, item.name, input_len))
2129-
} else {
2130-
None
2131-
}
2125+
let order = !item.name.as_str().starts_with("new");
2126+
Some((order, item.name, input_len))
21322127
})
21332128
.collect::<Vec<_>>();
21342129
items.sort_by_key(|(order, _, _)| *order);
2130+
let suggestion = |name, args| {
2131+
format!(
2132+
"::{name}({})",
2133+
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
2134+
)
2135+
};
21352136
match &items[..] {
21362137
[] => {}
21372138
[(_, name, args)] => {
21382139
err.span_suggestion_verbose(
21392140
span.shrink_to_hi().with_hi(expr_span.hi()),
2140-
format!("you might have meant to use the `{name}` associated function",),
2141-
format!(
2142-
"::{name}({})",
2143-
std::iter::repeat("_").take(*args).collect::<Vec<_>>().join(", ")
2144-
),
2141+
format!("you might have meant to use the `{name}` associated function"),
2142+
suggestion(name, *args),
21452143
Applicability::MaybeIncorrect,
21462144
);
21472145
}
@@ -2151,15 +2149,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21512149
"you might have meant to use an associated function to build this type",
21522150
items
21532151
.iter()
2154-
.map(|(_, name, args)| {
2155-
format!(
2156-
"::{name}({})",
2157-
std::iter::repeat("_")
2158-
.take(*args)
2159-
.collect::<Vec<_>>()
2160-
.join(", ")
2161-
)
2162-
})
2152+
.map(|(_, name, args)| suggestion(name, *args))
21632153
.collect::<Vec<String>>(),
21642154
Applicability::MaybeIncorrect,
21652155
);

compiler/rustc_resolve/src/late/diagnostics.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17321732
args: &[P<Expr>],
17331733
) {
17341734
if def_id.is_local() {
1735+
// Doing analysis on local `DefId`s would cause infinite recursion.
17351736
return;
17361737
}
17371738
// 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> {
17521753
let ty::Adt(def, _args) = ret_ty.kind() else {
17531754
return None;
17541755
};
1755-
// Check for `-> Self`
17561756
let input_len = fn_sig.inputs().skip_binder().len();
1757-
if def.did() == def_id {
1758-
let order = if item.name.as_str().starts_with("new") {
1759-
0
1760-
} else if input_len == args.len() {
1761-
2
1762-
} else {
1763-
3
1764-
};
1765-
Some((order, item.name, input_len))
1766-
} else {
1767-
None
1757+
if def.did() != def_id {
1758+
return None;
17681759
}
1760+
let order = !item.name.as_str().starts_with("new");
1761+
Some((order, item.name, input_len))
17691762
})
17701763
.collect::<Vec<_>>();
17711764
items.sort_by_key(|(order, _, _)| *order);
1765+
let suggestion = |name, args| {
1766+
format!(
1767+
"::{name}({})",
1768+
std::iter::repeat("_").take(args).collect::<Vec<_>>().join(", ")
1769+
)
1770+
};
17721771
match &items[..] {
17731772
[] => {}
17741773
[(_, name, len)] if *len == args.len() => {
@@ -1783,10 +1782,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17831782
err.span_suggestion_verbose(
17841783
path_span.shrink_to_hi().with_hi(call_span.hi()),
17851784
format!("you might have meant to use the `{name}` associated function",),
1786-
format!(
1787-
"::{name}({})",
1788-
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
1789-
),
1785+
suggestion(name, *len),
17901786
Applicability::MaybeIncorrect,
17911787
);
17921788
}
@@ -1796,12 +1792,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17961792
"you might have meant to use an associated function to build this type",
17971793
items
17981794
.iter()
1799-
.map(|(_, name, len)| {
1800-
format!(
1801-
"::{name}({})",
1802-
std::iter::repeat("_").take(*len).collect::<Vec<_>>().join(", ")
1803-
)
1804-
})
1795+
.map(|(_, name, len)| suggestion(name, *len))
18051796
.collect::<Vec<String>>(),
18061797
Applicability::MaybeIncorrect,
18071798
SuggestionStyle::ShowAlways,

0 commit comments

Comments
 (0)