Skip to content

Commit 7276f34

Browse files
Rollup merge of #126317 - oli-obk:recursive_rpit4, r=compiler-errors
Avoid a bunch of booleans in favor of Result<(), ErrorGuaranteed> as that more robustly proves that an error has been emitted pulled out of #126316 This PR cannot have any effect on compilation. All it does is shift a `Ty::new_misc_error` to a `span_delayed_bug` and preserve the `ErrorGuaranteed` in all other cases
2 parents 49b28a4 + 6299071 commit 7276f34

File tree

6 files changed

+51
-48
lines changed

6 files changed

+51
-48
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1342,14 +1342,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13421342
Ok(method)
13431343
}
13441344
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-
}
1345+
if segment.ident.name == kw::Empty {
1346+
span_bug!(rcvr.span, "empty method name")
1347+
} else {
1348+
Err(self.report_method_error(expr.hir_id, rcvr_t, error, expected, false))
13511349
}
1352-
Err(())
13531350
}
13541351
};
13551352

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
660660
})
661661
}
662662

663-
pub(crate) fn err_args(&self, len: usize) -> Vec<Ty<'tcx>> {
664-
let ty_error = Ty::new_misc_error(self.tcx);
663+
pub(crate) fn err_args(&self, len: usize, guar: ErrorGuaranteed) -> Vec<Ty<'tcx>> {
664+
let ty_error = Ty::new_error(self.tcx, guar);
665665
vec![ty_error; len]
666666
}
667667

@@ -846,15 +846,13 @@ 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+
);
858856
}
859857

860858
result

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ 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 {
126-
let err_inputs = self.err_args(args_no_rcvr.len());
125+
if let Err(guar) = has_error {
126+
let err_inputs = self.err_args(args_no_rcvr.len(), guar);
127127

128128
let err_inputs = match tuple_arguments {
129129
DontTupleArguments => err_inputs,
@@ -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();
@@ -237,15 +237,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
237237
_ => {
238238
// Otherwise, there's a mismatch, so clear out what we're expecting, and set
239239
// our input types to err_args so we don't blow up the error messages
240-
struct_span_code_err!(
240+
let guar = struct_span_code_err!(
241241
tcx.dcx(),
242242
call_span,
243243
E0059,
244244
"cannot use call notation; the first type parameter \
245245
for the function trait is neither a tuple nor unit"
246246
)
247247
.emit();
248-
(self.err_args(provided_args.len()), None)
248+
(self.err_args(provided_args.len(), guar), None)
249249
}
250250
}
251251
} else {

compiler/rustc_hir_typeck/src/method/suggest.rs

+26-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+
) -> 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,8 +226,8 @@ 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;
229+
if let Err(guar) = rcvr_ty.error_reported() {
230+
return guar;
231231
}
232232

233233
match error {
@@ -265,7 +265,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
265265
&mut sources,
266266
Some(sugg_span),
267267
);
268-
err.emit();
268+
return err.emit();
269269
}
270270

271271
MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
@@ -286,7 +286,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
286286
.unwrap_or_else(|| self.tcx.def_span(def_id));
287287
err.span_label(sp, format!("private {kind} defined here"));
288288
self.suggest_valid_traits(&mut err, item_name, out_of_scope_traits, true);
289-
err.emit();
289+
return err.emit();
290290
}
291291

292292
MethodError::IllegalSizedBound { candidates, needs_mut, bound_span, self_expr } => {
@@ -343,12 +343,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
343343
}
344344
}
345345
}
346-
err.emit();
346+
return err.emit();
347347
}
348348

349349
MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
350350
}
351-
None
352351
}
353352

354353
fn suggest_missing_writer(&self, rcvr_ty: Ty<'tcx>, rcvr_expr: &hir::Expr<'tcx>) -> Diag<'_> {
@@ -564,7 +563,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
564563
}
565564
}
566565

567-
pub fn report_no_match_method_error(
566+
fn report_no_match_method_error(
568567
&self,
569568
mut span: Span,
570569
rcvr_ty: Ty<'tcx>,
@@ -576,7 +575,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
576575
no_match_data: &mut NoMatchData<'tcx>,
577576
expected: Expectation<'tcx>,
578577
trait_missing_method: bool,
579-
) -> Option<Diag<'_>> {
578+
) -> ErrorGuaranteed {
580579
let mode = no_match_data.mode;
581580
let tcx = self.tcx;
582581
let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
@@ -608,14 +607,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
608607

609608
// We could pass the file for long types into these two, but it isn't strictly necessary
610609
// given how targeted they are.
611-
if self.suggest_wrapping_range_with_parens(
610+
if let Err(guar) = self.report_failed_method_call_on_range_end(
612611
tcx,
613612
rcvr_ty,
614613
source,
615614
span,
616615
item_name,
617616
&short_ty_str,
618-
) || self.suggest_constraining_numerical_ty(
617+
) {
618+
return guar;
619+
}
620+
if let Err(guar) = self.report_failed_method_call_on_numerical_infer_var(
619621
tcx,
620622
rcvr_ty,
621623
source,
@@ -624,7 +626,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
624626
item_name,
625627
&short_ty_str,
626628
) {
627-
return None;
629+
return guar;
628630
}
629631
span = item_name.span;
630632

@@ -881,7 +883,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
881883
vec![(span.shrink_to_lo(), format!("into_iter()."))],
882884
Applicability::MaybeIncorrect,
883885
);
884-
return Some(err);
886+
return err.emit();
885887
} else if !unsatisfied_predicates.is_empty() && matches!(rcvr_ty.kind(), ty::Param(_)) {
886888
// We special case the situation where we are looking for `_` in
887889
// `<TypeParam as _>::method` because otherwise the machinery will look for blanket
@@ -1606,7 +1608,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16061608
}
16071609

16081610
self.note_derefed_ty_has_method(&mut err, source, rcvr_ty, item_name, expected);
1609-
Some(err)
1611+
err.emit()
16101612
}
16111613

16121614
/// If an appropriate error source is not found, check method chain for possible candidates
@@ -2251,15 +2253,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22512253

22522254
/// Suggest possible range with adding parentheses, for example:
22532255
/// when encountering `0..1.map(|i| i + 1)` suggest `(0..1).map(|i| i + 1)`.
2254-
fn suggest_wrapping_range_with_parens(
2256+
fn report_failed_method_call_on_range_end(
22552257
&self,
22562258
tcx: TyCtxt<'tcx>,
22572259
actual: Ty<'tcx>,
22582260
source: SelfSource<'tcx>,
22592261
span: Span,
22602262
item_name: Ident,
22612263
ty_str: &str,
2262-
) -> bool {
2264+
) -> Result<(), ErrorGuaranteed> {
22632265
if let SelfSource::MethodCall(expr) = source {
22642266
for (_, parent) in tcx.hir().parent_iter(expr.hir_id).take(5) {
22652267
if let Node::Expr(parent_expr) = parent {
@@ -2316,7 +2318,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23162318
);
23172319
if pick.is_ok() {
23182320
let range_span = parent_expr.span.with_hi(expr.span.hi());
2319-
tcx.dcx().emit_err(errors::MissingParenthesesInRange {
2321+
return Err(tcx.dcx().emit_err(errors::MissingParenthesesInRange {
23202322
span,
23212323
ty_str: ty_str.to_string(),
23222324
method_name: item_name.as_str().to_string(),
@@ -2325,16 +2327,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23252327
left: range_span.shrink_to_lo(),
23262328
right: range_span.shrink_to_hi(),
23272329
}),
2328-
});
2329-
return true;
2330+
}));
23302331
}
23312332
}
23322333
}
23332334
}
2334-
false
2335+
Ok(())
23352336
}
23362337

2337-
fn suggest_constraining_numerical_ty(
2338+
fn report_failed_method_call_on_numerical_infer_var(
23382339
&self,
23392340
tcx: TyCtxt<'tcx>,
23402341
actual: Ty<'tcx>,
@@ -2343,7 +2344,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23432344
item_kind: &str,
23442345
item_name: Ident,
23452346
ty_str: &str,
2346-
) -> bool {
2347+
) -> Result<(), ErrorGuaranteed> {
23472348
let found_candidate = all_traits(self.tcx)
23482349
.into_iter()
23492350
.any(|info| self.associated_value(info.def_id, item_name).is_some());
@@ -2447,10 +2448,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24472448
}
24482449
_ => {}
24492450
}
2450-
err.emit();
2451-
return true;
2451+
return Err(err.emit());
24522452
}
2453-
false
2453+
Ok(())
24542454
}
24552455

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

compiler/rustc_log/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
tracing = "0.1.28"
99
tracing-core = "=0.1.30" # FIXME(Nilstrieb) tracing has a deadlock: https://github.com/tokio-rs/tracing/issues/2635
1010
tracing-subscriber = { version = "0.3.3", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
11-
tracing-tree = "0.3.0"
11+
tracing-tree = "0.3.1"
1212
# tidy-alphabetical-end
1313

1414
[dev-dependencies]

compiler/rustc_log/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub struct LoggerConfig {
5858
pub verbose_thread_ids: Result<String, VarError>,
5959
pub backtrace: Result<String, VarError>,
6060
pub wraptree: Result<String, VarError>,
61+
pub lines: Result<String, VarError>,
6162
}
6263

6364
impl LoggerConfig {
@@ -69,6 +70,7 @@ impl LoggerConfig {
6970
verbose_thread_ids: env::var(format!("{env}_THREAD_IDS")),
7071
backtrace: env::var(format!("{env}_BACKTRACE")),
7172
wraptree: env::var(format!("{env}_WRAPTREE")),
73+
lines: env::var(format!("{env}_LINES")),
7274
}
7375
}
7476
}
@@ -101,13 +103,19 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
101103
Err(_) => false,
102104
};
103105

106+
let lines = match cfg.lines {
107+
Ok(v) => &v == "1",
108+
Err(_) => false,
109+
};
110+
104111
let mut layer = tracing_tree::HierarchicalLayer::default()
105112
.with_writer(io::stderr)
106113
.with_ansi(color_logs)
107114
.with_targets(true)
108115
.with_verbose_exit(verbose_entry_exit)
109116
.with_verbose_entry(verbose_entry_exit)
110117
.with_indent_amount(2)
118+
.with_indent_lines(lines)
111119
.with_thread_ids(verbose_thread_ids)
112120
.with_thread_names(verbose_thread_ids);
113121

0 commit comments

Comments
 (0)