Skip to content

Commit 589591e

Browse files
committedJan 8, 2024
Use chaining in DiagnosticBuilder construction.
To avoid the use of a mutable local variable, and because it reads more nicely.
1 parent b1b9278 commit 589591e

File tree

22 files changed

+223
-369
lines changed

22 files changed

+223
-369
lines changed
 

‎compiler/rustc_attr/src/session_diagnostics.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,12 @@ pub(crate) struct UnknownMetaItem<'a> {
5454
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnknownMetaItem<'_> {
5555
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
5656
let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
57-
let mut diag = DiagnosticBuilder::new(dcx, level, fluent::attr_unknown_meta_item);
58-
diag.span(self.span);
59-
diag.code(error_code!(E0541));
60-
diag.arg("item", self.item);
61-
diag.arg("expected", expected.join(", "));
62-
diag.span_label(self.span, fluent::attr_label);
63-
diag
57+
DiagnosticBuilder::new(dcx, level, fluent::attr_unknown_meta_item)
58+
.span_mv(self.span)
59+
.code_mv(error_code!(E0541))
60+
.arg_mv("item", self.item)
61+
.arg_mv("expected", expected.join(", "))
62+
.span_label_mv(self.span, fluent::attr_label)
6463
}
6564
}
6665

‎compiler/rustc_borrowck/src/borrowck_errors.rs

+29-41
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
3131
borrow_span: Span,
3232
borrow_desc: &str,
3333
) -> DiagnosticBuilder<'tcx> {
34-
let mut err = struct_span_err!(
34+
struct_span_err!(
3535
self.dcx(),
3636
span,
3737
E0503,
3838
"cannot use {} because it was mutably borrowed",
3939
desc,
40-
);
41-
42-
err.span_label(borrow_span, format!("{borrow_desc} is borrowed here"));
43-
err.span_label(span, format!("use of borrowed {borrow_desc}"));
44-
err
40+
)
41+
.span_label_mv(borrow_span, format!("{borrow_desc} is borrowed here"))
42+
.span_label_mv(span, format!("use of borrowed {borrow_desc}"))
4543
}
4644

4745
pub(crate) fn cannot_mutably_borrow_multiply(
@@ -238,17 +236,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
238236
borrow_span: Span,
239237
desc: &str,
240238
) -> DiagnosticBuilder<'cx> {
241-
let mut err = struct_span_err!(
239+
struct_span_err!(
242240
self.dcx(),
243241
span,
244242
E0506,
245243
"cannot assign to {} because it is borrowed",
246244
desc,
247-
);
248-
249-
err.span_label(borrow_span, format!("{desc} is borrowed here"));
250-
err.span_label(span, format!("{desc} is assigned to here but it was already borrowed"));
251-
err
245+
)
246+
.span_label_mv(borrow_span, format!("{desc} is borrowed here"))
247+
.span_label_mv(span, format!("{desc} is assigned to here but it was already borrowed"))
252248
}
253249

254250
pub(crate) fn cannot_reassign_immutable(
@@ -287,32 +283,30 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
287283
(&ty::Slice(_), _) => "slice",
288284
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
289285
};
290-
let mut err = struct_span_err!(
286+
struct_span_err!(
291287
self.dcx(),
292288
move_from_span,
293289
E0508,
294290
"cannot move out of type `{}`, a non-copy {}",
295291
ty,
296292
type_name,
297-
);
298-
err.span_label(move_from_span, "cannot move out of here");
299-
err
293+
)
294+
.span_label_mv(move_from_span, "cannot move out of here")
300295
}
301296

302297
pub(crate) fn cannot_move_out_of_interior_of_drop(
303298
&self,
304299
move_from_span: Span,
305300
container_ty: Ty<'_>,
306301
) -> DiagnosticBuilder<'cx> {
307-
let mut err = struct_span_err!(
302+
struct_span_err!(
308303
self.dcx(),
309304
move_from_span,
310305
E0509,
311306
"cannot move out of type `{}`, which implements the `Drop` trait",
312307
container_ty,
313-
);
314-
err.span_label(move_from_span, "cannot move out of here");
315-
err
308+
)
309+
.span_label_mv(move_from_span, "cannot move out of here")
316310
}
317311

318312
pub(crate) fn cannot_act_on_moved_value(
@@ -352,18 +346,17 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
352346
immutable_section: &str,
353347
action: &str,
354348
) -> DiagnosticBuilder<'tcx> {
355-
let mut err = struct_span_err!(
349+
struct_span_err!(
356350
self.dcx(),
357351
mutate_span,
358352
E0510,
359353
"cannot {} {} in {}",
360354
action,
361355
immutable_place,
362356
immutable_section,
363-
);
364-
err.span_label(mutate_span, format!("cannot {action}"));
365-
err.span_label(immutable_span, format!("value is immutable in {immutable_section}"));
366-
err
357+
)
358+
.span_label_mv(mutate_span, format!("cannot {action}"))
359+
.span_label_mv(immutable_span, format!("value is immutable in {immutable_section}"))
367360
}
368361

369362
pub(crate) fn cannot_borrow_across_coroutine_yield(
@@ -372,14 +365,13 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
372365
yield_span: Span,
373366
) -> DiagnosticBuilder<'tcx> {
374367
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
375-
let mut err = struct_span_err!(
368+
struct_span_err!(
376369
self.dcx(),
377370
span,
378371
E0626,
379372
"borrow may still be in use when {coroutine_kind:#} yields",
380-
);
381-
err.span_label(yield_span, "possible yield occurs here");
382-
err
373+
)
374+
.span_label_mv(yield_span, "possible yield occurs here")
383375
}
384376

385377
pub(crate) fn cannot_borrow_across_destructor(
@@ -409,22 +401,19 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
409401
reference_desc: &str,
410402
path_desc: &str,
411403
) -> DiagnosticBuilder<'tcx> {
412-
let mut err = struct_span_err!(
404+
struct_span_err!(
413405
self.dcx(),
414406
span,
415407
E0515,
416408
"cannot {RETURN} {REFERENCE} {LOCAL}",
417409
RETURN = return_kind,
418410
REFERENCE = reference_desc,
419411
LOCAL = path_desc,
420-
);
421-
422-
err.span_label(
412+
)
413+
.span_label_mv(
423414
span,
424415
format!("{return_kind}s a {reference_desc} data owned by the current function"),
425-
);
426-
427-
err
416+
)
428417
}
429418

430419
pub(crate) fn cannot_capture_in_long_lived_closure(
@@ -435,16 +424,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
435424
capture_span: Span,
436425
scope: &str,
437426
) -> DiagnosticBuilder<'tcx> {
438-
let mut err = struct_span_err!(
427+
struct_span_err!(
439428
self.dcx(),
440429
closure_span,
441430
E0373,
442431
"{closure_kind} may outlive the current {scope}, but it borrows {borrowed_path}, \
443432
which is owned by the current {scope}",
444-
);
445-
err.span_label(capture_span, format!("{borrowed_path} is borrowed here"))
446-
.span_label(closure_span, format!("may outlive borrowed value {borrowed_path}"));
447-
err
433+
)
434+
.span_label_mv(capture_span, format!("{borrowed_path} is borrowed here"))
435+
.span_label_mv(closure_span, format!("may outlive borrowed value {borrowed_path}"))
448436
}
449437

450438
pub(crate) fn thread_local_value_does_not_live_long_enough(

‎compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -2218,15 +2218,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22182218
drop_span, borrow_span
22192219
);
22202220

2221-
let mut err = self.thread_local_value_does_not_live_long_enough(borrow_span);
2222-
2223-
err.span_label(
2224-
borrow_span,
2225-
"thread-local variables cannot be borrowed beyond the end of the function",
2226-
);
2227-
err.span_label(drop_span, "end of enclosing function is here");
2228-
2229-
err
2221+
self.thread_local_value_does_not_live_long_enough(borrow_span)
2222+
.span_label_mv(
2223+
borrow_span,
2224+
"thread-local variables cannot be borrowed beyond the end of the function",
2225+
)
2226+
.span_label_mv(drop_span, "end of enclosing function is here")
22302227
}
22312228

22322229
#[instrument(level = "debug", skip(self))]

‎compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
329329
if let PlaceRef { local, projection: [] } = deref_base {
330330
let decl = &self.body.local_decls[local];
331331
if decl.is_ref_for_guard() {
332-
let mut err = self.cannot_move_out_of(
333-
span,
334-
&format!("`{}` in pattern guard", self.local_names[local].unwrap()),
335-
);
336-
err.note(
337-
"variables bound in patterns cannot be moved from \
332+
return self
333+
.cannot_move_out_of(
334+
span,
335+
&format!("`{}` in pattern guard", self.local_names[local].unwrap()),
336+
)
337+
.note_mv(
338+
"variables bound in patterns cannot be moved from \
338339
until after the end of the pattern guard",
339-
);
340-
return err;
340+
);
341341
} else if decl.is_ref_to_static() {
342342
return self.report_cannot_move_from_static(move_place, span);
343343
}
@@ -381,15 +381,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
381381
closure_kind_ty, closure_kind, place_description,
382382
);
383383

384-
let mut diag = self.cannot_move_out_of(span, &place_description);
385-
386-
diag.span_label(upvar_span, "captured outer variable");
387-
diag.span_label(
388-
self.infcx.tcx.def_span(def_id),
389-
format!("captured by this `{closure_kind}` closure"),
390-
);
391-
392-
diag
384+
self.cannot_move_out_of(span, &place_description)
385+
.span_label_mv(upvar_span, "captured outer variable")
386+
.span_label_mv(
387+
self.infcx.tcx.def_span(def_id),
388+
format!("captured by this `{closure_kind}` closure"),
389+
)
393390
}
394391
_ => {
395392
let source = self.borrowed_content_source(deref_base);

‎compiler/rustc_builtin_macros/src/errors.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -803,24 +803,23 @@ pub(crate) struct AsmClobberNoReg {
803803

804804
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for AsmClobberNoReg {
805805
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
806-
let mut diag = DiagnosticBuilder::new(
807-
dcx,
808-
level,
809-
crate::fluent_generated::builtin_macros_asm_clobber_no_reg,
810-
);
811-
diag.span(self.spans.clone());
812806
// eager translation as `span_labels` takes `AsRef<str>`
813807
let lbl1 = dcx.eagerly_translate_to_string(
814808
crate::fluent_generated::builtin_macros_asm_clobber_abi,
815809
[].into_iter(),
816810
);
817-
diag.span_labels(self.clobbers, &lbl1);
818811
let lbl2 = dcx.eagerly_translate_to_string(
819812
crate::fluent_generated::builtin_macros_asm_clobber_outputs,
820813
[].into_iter(),
821814
);
822-
diag.span_labels(self.spans, &lbl2);
823-
diag
815+
DiagnosticBuilder::new(
816+
dcx,
817+
level,
818+
crate::fluent_generated::builtin_macros_asm_clobber_no_reg,
819+
)
820+
.span_mv(self.spans.clone())
821+
.span_labels_mv(self.clobbers, &lbl1)
822+
.span_labels_mv(self.spans, &lbl2)
824823
}
825824
}
826825

‎compiler/rustc_codegen_llvm/src/errors.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_
105105
let (message, _) = diag.messages().first().expect("`LlvmError` with no message");
106106
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args());
107107

108-
let mut diag =
109-
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config);
110-
diag.arg("error", message);
111-
diag
108+
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
109+
.arg_mv("error", message)
112110
}
113111
}
114112

@@ -204,10 +202,10 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for WithLlvmError<'_> {
204202
PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
205203
ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
206204
};
207-
let mut diag = self.0.into_diagnostic(dcx, level);
208-
diag.primary_message(msg_with_llvm_err);
209-
diag.arg("llvm_err", self.1);
210-
diag
205+
self.0
206+
.into_diagnostic(dcx, level)
207+
.primary_message_mv(msg_with_llvm_err)
208+
.arg_mv("llvm_err", self.1)
211209
}
212210
}
213211

0 commit comments

Comments
 (0)