Skip to content

Commit c658a76

Browse files
committed
Avoid a bunch of booleans in favor of Result<(), ErrorGuaranteed> as that more robustly proves that an error has been emitted
1 parent 07a646c commit c658a76

File tree

4 files changed

+35
-44
lines changed

4 files changed

+35
-44
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1341,16 +1341,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13411341
self.write_method_call_and_enforce_effects(expr.hir_id, expr.span, method);
13421342
Ok(method)
13431343
}
1344-
Err(error) => {
1345-
if segment.ident.name != kw::Empty {
1346-
if let Some(err) =
1347-
self.report_method_error(expr.hir_id, rcvr_t, error, expected, false)
1348-
{
1349-
err.emit();
1350-
}
1344+
Err(error) => Err(if segment.ident.name == kw::Empty {
1345+
self.dcx().span_delayed_bug(rcvr.span, "empty method name")
1346+
} else {
1347+
match self.report_method_error(expr.hir_id, rcvr_t, error, expected, false) {
1348+
Ok(diag) => diag.emit(),
1349+
Err(guar) => guar,
13511350
}
1352-
Err(())
1353-
}
1351+
}),
13541352
};
13551353

13561354
// Call the generic checker.

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -846,15 +846,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
846846
}
847847

848848
if item_name.name != kw::Empty {
849-
if let Some(e) = self.report_method_error(
849+
self.report_method_error(
850850
hir_id,
851851
ty.normalized,
852852
error,
853853
Expectation::NoExpectation,
854854
trait_missing_method && span.edition().at_least_rust_2021(), // emits missing method for trait only after edition 2021
855-
) {
856-
e.emit();
857-
}
855+
)?
856+
.emit();
858857
}
859858

860859
result

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
113113
&self,
114114
sp: Span,
115115
expr: &'tcx hir::Expr<'tcx>,
116-
method: Result<MethodCallee<'tcx>, ()>,
116+
method: Result<MethodCallee<'tcx>, ErrorGuaranteed>,
117117
args_no_rcvr: &'tcx [hir::Expr<'tcx>],
118118
tuple_arguments: TupleArgumentsFlag,
119119
expected: Expectation<'tcx>,
120120
) -> Ty<'tcx> {
121121
let has_error = match method {
122-
Ok(method) => method.args.references_error() || method.sig.references_error(),
123-
Err(_) => true,
122+
Ok(method) => method.args.error_reported().and(method.sig.error_reported()),
123+
Err(guar) => Err(guar),
124124
};
125-
if has_error {
125+
if let Err(guar) = has_error {
126126
let err_inputs = self.err_args(args_no_rcvr.len());
127127

128128
let err_inputs = match tuple_arguments {
@@ -140,7 +140,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
140140
tuple_arguments,
141141
method.ok().map(|method| method.def_id),
142142
);
143-
return Ty::new_misc_error(self.tcx);
143+
return Ty::new_error(self.tcx, guar);
144144
}
145145

146146
let method = method.unwrap();

compiler/rustc_hir_typeck/src/method/suggest.rs

+20-26
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_middle::ty::IsSuggestable;
3333
use rustc_middle::ty::{self, GenericArgKind, Ty, TyCtxt, TypeVisitableExt};
3434
use rustc_span::def_id::DefIdSet;
3535
use rustc_span::symbol::{kw, sym, Ident};
36-
use rustc_span::{edit_distance, ExpnKind, FileName, MacroKind, Span};
36+
use rustc_span::{edit_distance, ErrorGuaranteed, ExpnKind, FileName, MacroKind, Span};
3737
use rustc_span::{Symbol, DUMMY_SP};
3838
use rustc_trait_selection::infer::InferCtxtExt;
3939
use rustc_trait_selection::traits::error_reporting::on_unimplemented::OnUnimplementedNote;
@@ -192,7 +192,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
192192
error: MethodError<'tcx>,
193193
expected: Expectation<'tcx>,
194194
trait_missing_method: bool,
195-
) -> Option<Diag<'_>> {
195+
) -> Result<Diag<'_>, ErrorGuaranteed> {
196196
let (span, sugg_span, source, item_name, args) = match self.tcx.hir_node(call_id) {
197197
hir::Node::Expr(&hir::Expr {
198198
kind: hir::ExprKind::MethodCall(segment, rcvr, args, _),
@@ -226,9 +226,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
226226
};
227227

228228
// Avoid suggestions when we don't know what's going on.
229-
if rcvr_ty.references_error() {
230-
return None;
231-
}
229+
rcvr_ty.error_reported()?;
232230

233231
match error {
234232
MethodError::NoMatch(mut no_match_data) => {
@@ -265,7 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
265263
&mut sources,
266264
Some(sugg_span),
267265
);
268-
err.emit();
266+
return Err(err.emit());
269267
}
270268

271269
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
@@ -286,7 +284,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
286284
.unwrap_or_else(|| self.tcx.def_span(def_id));
287285
err.span_label(sp, format!("private {kind} defined here"));
288286
self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
289-
err.emit();
287+
return Err(err.emit());
290288
}
291289

292290
MethodError::IllegalSizedBound { candidates, needs_mut, bound_span, self_expr } => {
@@ -343,12 +341,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
343341
}
344342
}
345343
}
346-
err.emit();
344+
return Err(err.emit());
347345
}
348346

349347
MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
350348
}
351-
None
352349
}
353350

354351
fn suggest_missing_writer(&self, rcvr_ty: Ty<'tcx>, rcvr_expr: &hir::Expr<'tcx>) -> Diag<'_> {
@@ -576,7 +573,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
576573
no_match_data: &mut NoMatchData<'tcx>,
577574
expected: Expectation<'tcx>,
578575
trait_missing_method: bool,
579-
) -> Option<Diag<'_>> {
576+
) -> Result<Diag<'_>, ErrorGuaranteed> {
580577
let mode = no_match_data.mode;
581578
let tcx = self.tcx;
582579
let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
@@ -608,24 +605,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
608605

609606
// We could pass the file for long types into these two, but it isn't strictly necessary
610607
// given how targeted they are.
611-
if self.suggest_wrapping_range_with_parens(
608+
self.suggest_wrapping_range_with_parens(
612609
tcx,
613610
rcvr_ty,
614611
source,
615612
span,
616613
item_name,
617614
&short_ty_str,
618-
) || self.suggest_constraining_numerical_ty(
615+
)?;
616+
self.suggest_constraining_numerical_ty(
619617
tcx,
620618
rcvr_ty,
621619
source,
622620
span,
623621
item_kind,
624622
item_name,
625623
&short_ty_str,
626-
) {
627-
return None;
628-
}
624+
)?;
629625
span = item_name.span;
630626

631627
// Don't show generic arguments when the method can't be found in any implementation (#81576).
@@ -881,7 +877,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
881877
vec![(span.shrink_to_lo(), format!("into_iter()."))],
882878
Applicability::MaybeIncorrect,
883879
);
884-
return Some(err);
880+
return Ok(err);
885881
} else if !unsatisfied_predicates.is_empty() && matches!(rcvr_ty.kind(), ty::Param(_)) {
886882
// We special case the situation where we are looking for `_` in
887883
// `<TypeParam as _>::method` because otherwise the machinery will look for blanket
@@ -1606,7 +1602,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16061602
}
16071603

16081604
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
1609-
Some(err)
1605+
Ok(err)
16101606
}
16111607

16121608
/// If an appropriate error source is not found, check method chain for possible candidates
@@ -2259,7 +2255,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22592255
span: Span,
22602256
item_name: Ident,
22612257
ty_str: &str,
2262-
) -> bool {
2258+
) -> Result<(), ErrorGuaranteed> {
22632259
if let SelfSource::MethodCall(expr) = source {
22642260
for (_, parent) in tcx.hir().parent_iter(expr.hir_id).take(5) {
22652261
if let Node::Expr(parent_expr) = parent {
@@ -2316,7 +2312,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23162312
);
23172313
if pick.is_ok() {
23182314
let range_span = parent_expr.span.with_hi(expr.span.hi());
2319-
tcx.dcx().emit_err(errors::MissingParenthesesInRange {
2315+
return Err(tcx.dcx().emit_err(errors::MissingParenthesesInRange {
23202316
span,
23212317
ty_str: ty_str.to_string(),
23222318
method_name: item_name.as_str().to_string(),
@@ -2325,13 +2321,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23252321
left: range_span.shrink_to_lo(),
23262322
right: range_span.shrink_to_hi(),
23272323
}),
2328-
});
2329-
return true;
2324+
}));
23302325
}
23312326
}
23322327
}
23332328
}
2334-
false
2329+
Ok(())
23352330
}
23362331

23372332
fn suggest_constraining_numerical_ty(
@@ -2343,7 +2338,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23432338
item_kind: &str,
23442339
item_name: Ident,
23452340
ty_str: &str,
2346-
) -> bool {
2341+
) -> Result<(), ErrorGuaranteed> {
23472342
let found_candidate = all_traits(self.tcx)
23482343
.into_iter()
23492344
.any(|info| self.associated_value(info.def_id, item_name).is_some());
@@ -2447,10 +2442,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24472442
}
24482443
_ => {}
24492444
}
2450-
err.emit();
2451-
return true;
2445+
return Err(err.emit());
24522446
}
2453-
false
2447+
Ok(())
24542448
}
24552449

24562450
/// For code `rect::area(...)`,

0 commit comments

Comments
 (0)