Skip to content

Commit

Permalink
Use chaining in DiagnosticBuilder construction.
Browse files Browse the repository at this point in the history
To avoid the use of a mutable local variable, and because it reads more
nicely.
  • Loading branch information
nnethercote committed Jan 8, 2024
1 parent b1b9278 commit 589591e
Show file tree
Hide file tree
Showing 22 changed files with 223 additions and 369 deletions.
13 changes: 6 additions & 7 deletions compiler/rustc_attr/src/session_diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ pub(crate) struct UnknownMetaItem<'a> {
impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnknownMetaItem<'_> {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
let mut diag = DiagnosticBuilder::new(dcx, level, fluent::attr_unknown_meta_item);
diag.span(self.span);
diag.code(error_code!(E0541));
diag.arg("item", self.item);
diag.arg("expected", expected.join(", "));
diag.span_label(self.span, fluent::attr_label);
diag
DiagnosticBuilder::new(dcx, level, fluent::attr_unknown_meta_item)
.span_mv(self.span)
.code_mv(error_code!(E0541))
.arg_mv("item", self.item)
.arg_mv("expected", expected.join(", "))
.span_label_mv(self.span, fluent::attr_label)
}
}

Expand Down
70 changes: 29 additions & 41 deletions compiler/rustc_borrowck/src/borrowck_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
borrow_span: Span,
borrow_desc: &str,
) -> DiagnosticBuilder<'tcx> {
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
span,
E0503,
"cannot use {} because it was mutably borrowed",
desc,
);

err.span_label(borrow_span, format!("{borrow_desc} is borrowed here"));
err.span_label(span, format!("use of borrowed {borrow_desc}"));
err
)
.span_label_mv(borrow_span, format!("{borrow_desc} is borrowed here"))
.span_label_mv(span, format!("use of borrowed {borrow_desc}"))
}

pub(crate) fn cannot_mutably_borrow_multiply(
Expand Down Expand Up @@ -238,17 +236,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
borrow_span: Span,
desc: &str,
) -> DiagnosticBuilder<'cx> {
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
span,
E0506,
"cannot assign to {} because it is borrowed",
desc,
);

err.span_label(borrow_span, format!("{desc} is borrowed here"));
err.span_label(span, format!("{desc} is assigned to here but it was already borrowed"));
err
)
.span_label_mv(borrow_span, format!("{desc} is borrowed here"))
.span_label_mv(span, format!("{desc} is assigned to here but it was already borrowed"))
}

pub(crate) fn cannot_reassign_immutable(
Expand Down Expand Up @@ -287,32 +283,30 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
(&ty::Slice(_), _) => "slice",
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
};
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
move_from_span,
E0508,
"cannot move out of type `{}`, a non-copy {}",
ty,
type_name,
);
err.span_label(move_from_span, "cannot move out of here");
err
)
.span_label_mv(move_from_span, "cannot move out of here")
}

pub(crate) fn cannot_move_out_of_interior_of_drop(
&self,
move_from_span: Span,
container_ty: Ty<'_>,
) -> DiagnosticBuilder<'cx> {
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
move_from_span,
E0509,
"cannot move out of type `{}`, which implements the `Drop` trait",
container_ty,
);
err.span_label(move_from_span, "cannot move out of here");
err
)
.span_label_mv(move_from_span, "cannot move out of here")
}

pub(crate) fn cannot_act_on_moved_value(
Expand Down Expand Up @@ -352,18 +346,17 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
immutable_section: &str,
action: &str,
) -> DiagnosticBuilder<'tcx> {
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
mutate_span,
E0510,
"cannot {} {} in {}",
action,
immutable_place,
immutable_section,
);
err.span_label(mutate_span, format!("cannot {action}"));
err.span_label(immutable_span, format!("value is immutable in {immutable_section}"));
err
)
.span_label_mv(mutate_span, format!("cannot {action}"))
.span_label_mv(immutable_span, format!("value is immutable in {immutable_section}"))
}

pub(crate) fn cannot_borrow_across_coroutine_yield(
Expand All @@ -372,14 +365,13 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
yield_span: Span,
) -> DiagnosticBuilder<'tcx> {
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
span,
E0626,
"borrow may still be in use when {coroutine_kind:#} yields",
);
err.span_label(yield_span, "possible yield occurs here");
err
)
.span_label_mv(yield_span, "possible yield occurs here")
}

pub(crate) fn cannot_borrow_across_destructor(
Expand Down Expand Up @@ -409,22 +401,19 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
reference_desc: &str,
path_desc: &str,
) -> DiagnosticBuilder<'tcx> {
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
span,
E0515,
"cannot {RETURN} {REFERENCE} {LOCAL}",
RETURN = return_kind,
REFERENCE = reference_desc,
LOCAL = path_desc,
);

err.span_label(
)
.span_label_mv(
span,
format!("{return_kind}s a {reference_desc} data owned by the current function"),
);

err
)
}

pub(crate) fn cannot_capture_in_long_lived_closure(
Expand All @@ -435,16 +424,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
capture_span: Span,
scope: &str,
) -> DiagnosticBuilder<'tcx> {
let mut err = struct_span_err!(
struct_span_err!(
self.dcx(),
closure_span,
E0373,
"{closure_kind} may outlive the current {scope}, but it borrows {borrowed_path}, \
which is owned by the current {scope}",
);
err.span_label(capture_span, format!("{borrowed_path} is borrowed here"))
.span_label(closure_span, format!("may outlive borrowed value {borrowed_path}"));
err
)
.span_label_mv(capture_span, format!("{borrowed_path} is borrowed here"))
.span_label_mv(closure_span, format!("may outlive borrowed value {borrowed_path}"))
}

pub(crate) fn thread_local_value_does_not_live_long_enough(
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,15 +2218,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
drop_span, borrow_span
);

let mut err = self.thread_local_value_does_not_live_long_enough(borrow_span);

err.span_label(
borrow_span,
"thread-local variables cannot be borrowed beyond the end of the function",
);
err.span_label(drop_span, "end of enclosing function is here");

err
self.thread_local_value_does_not_live_long_enough(borrow_span)
.span_label_mv(
borrow_span,
"thread-local variables cannot be borrowed beyond the end of the function",
)
.span_label_mv(drop_span, "end of enclosing function is here")
}

#[instrument(level = "debug", skip(self))]
Expand Down
31 changes: 14 additions & 17 deletions compiler/rustc_borrowck/src/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if let PlaceRef { local, projection: [] } = deref_base {
let decl = &self.body.local_decls[local];
if decl.is_ref_for_guard() {
let mut err = self.cannot_move_out_of(
span,
&format!("`{}` in pattern guard", self.local_names[local].unwrap()),
);
err.note(
"variables bound in patterns cannot be moved from \
return self
.cannot_move_out_of(
span,
&format!("`{}` in pattern guard", self.local_names[local].unwrap()),
)
.note_mv(
"variables bound in patterns cannot be moved from \
until after the end of the pattern guard",
);
return err;
);
} else if decl.is_ref_to_static() {
return self.report_cannot_move_from_static(move_place, span);
}
Expand Down Expand Up @@ -381,15 +381,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
closure_kind_ty, closure_kind, place_description,
);

let mut diag = self.cannot_move_out_of(span, &place_description);

diag.span_label(upvar_span, "captured outer variable");
diag.span_label(
self.infcx.tcx.def_span(def_id),
format!("captured by this `{closure_kind}` closure"),
);

diag
self.cannot_move_out_of(span, &place_description)
.span_label_mv(upvar_span, "captured outer variable")
.span_label_mv(
self.infcx.tcx.def_span(def_id),
format!("captured by this `{closure_kind}` closure"),
)
}
_ => {
let source = self.borrowed_content_source(deref_base);
Expand Down
17 changes: 8 additions & 9 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,24 +803,23 @@ pub(crate) struct AsmClobberNoReg {

impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for AsmClobberNoReg {
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
let mut diag = DiagnosticBuilder::new(
dcx,
level,
crate::fluent_generated::builtin_macros_asm_clobber_no_reg,
);
diag.span(self.spans.clone());
// eager translation as `span_labels` takes `AsRef<str>`
let lbl1 = dcx.eagerly_translate_to_string(
crate::fluent_generated::builtin_macros_asm_clobber_abi,
[].into_iter(),
);
diag.span_labels(self.clobbers, &lbl1);
let lbl2 = dcx.eagerly_translate_to_string(
crate::fluent_generated::builtin_macros_asm_clobber_outputs,
[].into_iter(),
);
diag.span_labels(self.spans, &lbl2);
diag
DiagnosticBuilder::new(
dcx,
level,
crate::fluent_generated::builtin_macros_asm_clobber_no_reg,
)
.span_mv(self.spans.clone())
.span_labels_mv(self.clobbers, &lbl1)
.span_labels_mv(self.spans, &lbl2)
}
}

Expand Down
14 changes: 6 additions & 8 deletions compiler/rustc_codegen_llvm/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for ParseTargetMachineConfig<'_
let (message, _) = diag.messages().first().expect("`LlvmError` with no message");
let message = dcx.eagerly_translate_to_string(message.clone(), diag.args());

let mut diag =
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config);
diag.arg("error", message);
diag
DiagnosticBuilder::new(dcx, level, fluent::codegen_llvm_parse_target_machine_config)
.arg_mv("error", message)
}
}

Expand Down Expand Up @@ -204,10 +202,10 @@ impl<G: EmissionGuarantee> IntoDiagnostic<'_, G> for WithLlvmError<'_> {
PrepareThinLtoModule => fluent::codegen_llvm_prepare_thin_lto_module_with_llvm_err,
ParseBitcode => fluent::codegen_llvm_parse_bitcode_with_llvm_err,
};
let mut diag = self.0.into_diagnostic(dcx, level);
diag.primary_message(msg_with_llvm_err);
diag.arg("llvm_err", self.1);
diag
self.0
.into_diagnostic(dcx, level)
.primary_message_mv(msg_with_llvm_err)
.arg_mv("llvm_err", self.1)
}
}

Expand Down
Loading

0 comments on commit 589591e

Please sign in to comment.