Skip to content

Commit 4c952eb

Browse files
committed
Refactor and cleanup FnCtxt::report_no_match_method_error
Currently this method is quiet long and complex, this commit improves its readability, refactor and cleanup few things
1 parent ae2c070 commit 4c952eb

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10501050

10511051
fn report_no_match_method_error(
10521052
&self,
1053-
mut span: Span,
1053+
span: Span,
10541054
rcvr_ty: Ty<'tcx>,
10551055
item_ident: Ident,
10561056
expr_id: hir::HirId,
@@ -1062,13 +1062,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10621062
trait_missing_method: bool,
10631063
within_macro_span: Option<Span>,
10641064
) -> ErrorGuaranteed {
1065-
let mode = no_match_data.mode;
10661065
let tcx = self.tcx;
10671066
let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
1067+
1068+
if let Err(guar) = rcvr_ty.error_reported() {
1069+
return guar;
1070+
}
1071+
1072+
// We could pass the file for long types into these two, but it isn't strictly necessary
1073+
// given how targeted they are.
1074+
if let Err(guar) =
1075+
self.report_failed_method_call_on_range_end(tcx, rcvr_ty, source, span, item_ident)
1076+
{
1077+
return guar;
1078+
}
1079+
10681080
let mut ty_file = None;
1081+
let mode = no_match_data.mode;
10691082
let is_method = mode == Mode::MethodCall;
1070-
let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
1071-
let similar_candidate = no_match_data.similar_candidate;
10721083
let item_kind = if is_method {
10731084
"method"
10741085
} else if rcvr_ty.is_enum() {
@@ -1082,13 +1093,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10821093
}
10831094
};
10841095

1085-
// We could pass the file for long types into these two, but it isn't strictly necessary
1086-
// given how targeted they are.
1087-
if let Err(guar) =
1088-
self.report_failed_method_call_on_range_end(tcx, rcvr_ty, source, span, item_ident)
1089-
{
1090-
return guar;
1091-
}
10921096
if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var(
10931097
tcx,
10941098
rcvr_ty,
@@ -1100,8 +1104,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11001104
) {
11011105
return guar;
11021106
}
1103-
span = item_ident.span;
11041107

1108+
let unsatisfied_predicates = &no_match_data.unsatisfied_predicates;
11051109
let is_write = sugg_span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
11061110
tcx.is_diagnostic_item(sym::write_macro, def_id)
11071111
|| tcx.is_diagnostic_item(sym::writeln_macro, def_id)
@@ -1120,29 +1124,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11201124
unsatisfied_predicates,
11211125
)
11221126
};
1123-
if rcvr_ty.references_error() {
1124-
err.downgrade_to_delayed_bug();
1125-
}
11261127

11271128
self.set_label_for_method_error(
11281129
&mut err,
11291130
source,
11301131
rcvr_ty,
11311132
item_ident,
11321133
expr_id,
1133-
span,
1134+
item_ident.span,
11341135
sugg_span,
11351136
within_macro_span,
11361137
args,
11371138
);
11381139

11391140
self.suggest_method_call_annotation(
1140-
&mut err, span, rcvr_ty, item_ident, mode, source, expected,
1141+
&mut err,
1142+
item_ident.span,
1143+
rcvr_ty,
1144+
item_ident,
1145+
mode,
1146+
source,
1147+
expected,
11411148
);
11421149

11431150
let static_candidates = self.suggest_static_method_candidates(
11441151
&mut err,
1145-
span,
1152+
item_ident.span,
11461153
rcvr_ty,
11471154
item_ident,
11481155
source,
@@ -1159,7 +1166,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11591166
bound_spans,
11601167
)) = self.suggest_unsatisfied_ty_or_trait(
11611168
&mut err,
1162-
span,
1169+
item_ident.span,
11631170
rcvr_ty,
11641171
item_ident,
11651172
item_kind,
@@ -1171,9 +1178,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11711178
return err.emit();
11721179
};
11731180

1181+
let similar_candidate = no_match_data.similar_candidate;
11741182
let should_label_not_found = self.suggest_surround_method_call(
11751183
&mut err,
1176-
span,
1184+
item_ident.span,
11771185
rcvr_ty,
11781186
item_ident,
11791187
source,
@@ -1182,7 +1190,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11821190

11831191
self.find_possible_candidates_for_method(
11841192
&mut err,
1185-
span,
1193+
item_ident.span,
11861194
rcvr_ty,
11871195
item_ident,
11881196
item_kind,
@@ -1201,7 +1209,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12011209
} else {
12021210
self.suggest_traits_to_import(
12031211
&mut err,
1204-
span,
1212+
item_ident.span,
12051213
rcvr_ty,
12061214
item_ident,
12071215
args.map(|args| args.len() + 1),
@@ -1218,14 +1226,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12181226
&mut err,
12191227
rcvr_ty,
12201228
item_ident,
1221-
span,
1229+
item_ident.span,
12221230
source,
12231231
unsatisfied_predicates,
12241232
);
12251233

12261234
self.suggest_confusable_or_similarly_named_method(
12271235
&mut err,
1228-
span,
1236+
item_ident.span,
12291237
rcvr_ty,
12301238
item_ident,
12311239
mode,

0 commit comments

Comments
 (0)