Skip to content

Commit ca663b0

Browse files
committed
Auto merge of rust-lang#119606 - nnethercote:consuming-emit, r=oli-obk
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`
2 parents 0ee9cfd + db09eb2 commit ca663b0

File tree

112 files changed

+952
-1275
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+952
-1275
lines changed

Diff for: compiler/rustc_ast_passes/src/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! gate {
2323
($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{
2424
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
2525
feature_err(&$visitor.sess.parse_sess, sym::$feature, $span, $explain)
26-
.help($help)
26+
.help_mv($help)
2727
.emit();
2828
}
2929
}};

Diff for: 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

Diff for: 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(

Diff for: 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))]

Diff for: 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);

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
348348
let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
349349
let named_key = self.regioncx.name_regions(self.infcx.tcx, key);
350350
let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
351-
let mut diag = unexpected_hidden_region_diagnostic(
351+
let diag = unexpected_hidden_region_diagnostic(
352352
self.infcx.tcx,
353353
span,
354354
named_ty,

Diff for: compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn check_opaque_type_parameter_valid(
421421
return Err(tcx
422422
.dcx()
423423
.struct_span_err(span, "non-defining opaque type use in defining scope")
424-
.span_note(spans, format!("{descr} used multiple times"))
424+
.span_note_mv(spans, format!("{descr} used multiple times"))
425425
.emit());
426426
}
427427
}

Diff for: compiler/rustc_builtin_macros/src/asm.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
458458
match expr_to_spanned_string(ecx, template_expr, msg) {
459459
Ok(template_part) => template_part,
460460
Err(err) => {
461-
if let Some((mut err, _)) = err {
461+
if let Some((err, _)) = err {
462462
err.emit();
463463
}
464464
return None;
@@ -693,13 +693,14 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
693693
0 => {}
694694
1 => {
695695
let (sp, msg) = unused_operands.into_iter().next().unwrap();
696-
let mut err = ecx.dcx().struct_span_err(sp, msg);
697-
err.span_label(sp, msg);
698-
err.help(format!(
699-
"if this argument is intentionally unused, \
700-
consider using it in an asm comment: `\"/*{help_str} */\"`"
701-
));
702-
err.emit();
696+
ecx.dcx()
697+
.struct_span_err(sp, msg)
698+
.span_label_mv(sp, msg)
699+
.help_mv(format!(
700+
"if this argument is intentionally unused, \
701+
consider using it in an asm comment: `\"/*{help_str} */\"`"
702+
))
703+
.emit();
703704
}
704705
_ => {
705706
let mut err = ecx.dcx().struct_span_err(
@@ -747,7 +748,7 @@ pub(super) fn expand_asm<'cx>(
747748
};
748749
MacEager::expr(expr)
749750
}
750-
Err(mut err) => {
751+
Err(err) => {
751752
err.emit();
752753
DummyResult::any(sp)
753754
}
@@ -779,7 +780,7 @@ pub(super) fn expand_global_asm<'cx>(
779780
DummyResult::any(sp)
780781
}
781782
}
782-
Err(mut err) => {
783+
Err(err) => {
783784
err.emit();
784785
DummyResult::any(sp)
785786
}

Diff for: compiler/rustc_builtin_macros/src/assert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn expand_assert<'cx>(
2222
) -> Box<dyn MacResult + 'cx> {
2323
let Assert { cond_expr, custom_message } = match parse_assert(cx, span, tts) {
2424
Ok(assert) => assert,
25-
Err(mut err) => {
25+
Err(err) => {
2626
err.emit();
2727
return DummyResult::any(span);
2828
}

Diff for: compiler/rustc_builtin_macros/src/cfg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn expand_cfg(
2828
);
2929
MacEager::expr(cx.expr_bool(sp, matches_cfg))
3030
}
31-
Err(mut err) => {
31+
Err(err) => {
3232
err.emit();
3333
DummyResult::any(sp)
3434
}

Diff for: compiler/rustc_builtin_macros/src/cfg_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl CfgEval<'_, '_> {
200200
parser.capture_cfg = true;
201201
match parse_annotatable_with(&mut parser) {
202202
Ok(a) => annotatable = a,
203-
Err(mut err) => {
203+
Err(err) => {
204204
err.emit();
205205
return Some(annotatable);
206206
}

Diff for: compiler/rustc_builtin_macros/src/cmdline_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn inject(krate: &mut ast::Crate, parse_sess: &ParseSess, attrs: &[String])
1818
let start_span = parser.token.span;
1919
let AttrItem { path, args, tokens: _ } = match parser.parse_attr_item(false) {
2020
Ok(ai) => ai,
21-
Err(mut err) => {
21+
Err(err) => {
2222
err.emit();
2323
continue;
2424
}

0 commit comments

Comments
 (0)