Skip to content

Commit

Permalink
Auto merge of #119606 - nnethercote:consuming-emit, r=oli-obk
Browse files Browse the repository at this point in the history
Consuming `emit`

This PR makes `DiagnosticBuilder::emit` consuming, i.e. take `self` instead of `&mut self`. This is good because it doesn't make sense to emit a diagnostic twice.

This requires some changes to `DiagnosticBuilder` method changing -- every existing non-consuming chaining method gets a new consuming partner with a `_mv` suffix -- but permits a host of beneficial follow-up changes: more concise code through more chaining, removal of redundant diagnostic construction API methods, and removal of machinery to track the possibility of a diagnostic being emitted multiple times.

r? `@compiler-errors`
  • Loading branch information
bors committed Jan 8, 2024
2 parents 0ee9cfd + db09eb2 commit ca663b0
Show file tree
Hide file tree
Showing 112 changed files with 952 additions and 1,275 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ macro_rules! gate {
($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
feature_err(&$visitor.sess.parse_sess, sym::$feature, $span, $explain)
.help($help)
.help_mv($help)
.emit();
}
}};
Expand Down
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
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
let named_key = self.regioncx.name_regions(self.infcx.tcx, key);
let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
let mut diag = unexpected_hidden_region_diagnostic(
let diag = unexpected_hidden_region_diagnostic(
self.infcx.tcx,
span,
named_ty,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ fn check_opaque_type_parameter_valid(
return Err(tcx
.dcx()
.struct_span_err(span, "non-defining opaque type use in defining scope")
.span_note(spans, format!("{descr} used multiple times"))
.span_note_mv(spans, format!("{descr} used multiple times"))
.emit());
}
}
Expand Down
21 changes: 11 additions & 10 deletions compiler/rustc_builtin_macros/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
match expr_to_spanned_string(ecx, template_expr, msg) {
Ok(template_part) => template_part,
Err(err) => {
if let Some((mut err, _)) = err {
if let Some((err, _)) = err {
err.emit();
}
return None;
Expand Down Expand Up @@ -693,13 +693,14 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
0 => {}
1 => {
let (sp, msg) = unused_operands.into_iter().next().unwrap();
let mut err = ecx.dcx().struct_span_err(sp, msg);
err.span_label(sp, msg);
err.help(format!(
"if this argument is intentionally unused, \
consider using it in an asm comment: `\"/*{help_str} */\"`"
));
err.emit();
ecx.dcx()
.struct_span_err(sp, msg)
.span_label_mv(sp, msg)
.help_mv(format!(
"if this argument is intentionally unused, \
consider using it in an asm comment: `\"/*{help_str} */\"`"
))
.emit();
}
_ => {
let mut err = ecx.dcx().struct_span_err(
Expand Down Expand Up @@ -747,7 +748,7 @@ pub(super) fn expand_asm<'cx>(
};
MacEager::expr(expr)
}
Err(mut err) => {
Err(err) => {
err.emit();
DummyResult::any(sp)
}
Expand Down Expand Up @@ -779,7 +780,7 @@ pub(super) fn expand_global_asm<'cx>(
DummyResult::any(sp)
}
}
Err(mut err) => {
Err(err) => {
err.emit();
DummyResult::any(sp)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn expand_assert<'cx>(
) -> Box<dyn MacResult + 'cx> {
let Assert { cond_expr, custom_message } = match parse_assert(cx, span, tts) {
Ok(assert) => assert,
Err(mut err) => {
Err(err) => {
err.emit();
return DummyResult::any(span);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn expand_cfg(
);
MacEager::expr(cx.expr_bool(sp, matches_cfg))
}
Err(mut err) => {
Err(err) => {
err.emit();
DummyResult::any(sp)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl CfgEval<'_, '_> {
parser.capture_cfg = true;
match parse_annotatable_with(&mut parser) {
Ok(a) => annotatable = a,
Err(mut err) => {
Err(err) => {
err.emit();
return Some(annotatable);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/cmdline_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn inject(krate: &mut ast::Crate, parse_sess: &ParseSess, attrs: &[String])
let start_span = parser.token.span;
let AttrItem { path, args, tokens: _ } = match parser.parse_attr_item(false) {
Ok(ai) => ai,
Err(mut err) => {
Err(err) => {
err.emit();
continue;
}
Expand Down
Loading

0 comments on commit ca663b0

Please sign in to comment.