Skip to content

Commit bd4e623

Browse files
committed
Use chaining for DiagnosticBuilder construction and emit.
To avoid the use of a mutable local variable, and because it reads more nicely.
1 parent 589591e commit bd4e623

File tree

22 files changed

+193
-183
lines changed

22 files changed

+193
-183
lines changed

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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(

compiler/rustc_codegen_cranelift/src/driver/jit.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,10 @@ fn dep_symbol_lookup_fn(
321321
Linkage::NotLinked | Linkage::IncludedFromDylib => {}
322322
Linkage::Static => {
323323
let name = crate_info.crate_name[&cnum];
324-
let mut err = sess.dcx().struct_err(format!("Can't load static lib {}", name));
325-
err.note("rustc_codegen_cranelift can only load dylibs in JIT mode.");
326-
err.emit();
324+
sess.dcx()
325+
.struct_err(format!("Can't load static lib {}", name))
326+
.note("rustc_codegen_cranelift can only load dylibs in JIT mode.")
327+
.emit();
327328
}
328329
Linkage::Dynamic => {
329330
dylib_paths.push(src.dylib.as_ref().unwrap().0.clone());

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
303303
// This exception needs to be kept in sync with allowing
304304
// `#[target_feature]` on `main` and `start`.
305305
} else if !tcx.features().target_feature_11 {
306-
let mut err = feature_err(
306+
feature_err(
307307
&tcx.sess.parse_sess,
308308
sym::target_feature_11,
309309
attr.span,
310310
"`#[target_feature(..)]` can only be applied to `unsafe` functions",
311-
);
312-
err.span_label(tcx.def_span(did), "not an `unsafe` function");
313-
err.emit();
311+
)
312+
.span_label_mv(tcx.def_span(did), "not an `unsafe` function")
313+
.emit();
314314
} else {
315315
check_target_feature_trait_unsafe(tcx, did, attr.span);
316316
}

compiler/rustc_hir_analysis/src/astconv/generics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,9 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
650650
if position == GenericArgPosition::Value
651651
&& args.num_lifetime_params() != param_counts.lifetimes
652652
{
653-
let mut err = struct_span_err!(tcx.dcx(), span, E0794, "{}", msg);
654-
err.span_note(span_late, note);
655-
err.emit();
653+
struct_span_err!(tcx.dcx(), span, E0794, "{}", msg)
654+
.span_note_mv(span_late, note)
655+
.emit();
656656
} else {
657657
let mut multispan = MultiSpan::from_span(span);
658658
multispan.push_span_label(span_late, note);

compiler/rustc_hir_analysis/src/astconv/object_safety.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
290290

291291
if references_self {
292292
let def_id = i.bottom().0.def_id();
293-
let mut err = struct_span_err!(
293+
struct_span_err!(
294294
tcx.dcx(),
295295
i.bottom().1,
296296
E0038,
297297
"the {} `{}` cannot be made into an object",
298298
tcx.def_descr(def_id),
299299
tcx.item_name(def_id),
300-
);
301-
err.note(
300+
)
301+
.note_mv(
302302
rustc_middle::traits::ObjectSafetyViolation::SupertraitSelf(smallvec![])
303303
.error_msg(),
304-
);
305-
err.emit();
304+
)
305+
.emit();
306306
}
307307

308308
ty::ExistentialTraitRef { def_id: trait_ref.def_id, args }

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,13 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
11401140
let disr_non_unit = def.variants().iter().any(|var| !is_unit(var) && has_disr(var));
11411141

11421142
if disr_non_unit || (disr_units && has_non_units) {
1143-
let err = struct_span_err!(
1143+
struct_span_err!(
11441144
tcx.dcx(),
11451145
tcx.def_span(def_id),
11461146
E0732,
11471147
"`#[repr(inttype)]` must be specified"
1148-
);
1149-
err.emit();
1148+
)
1149+
.emit();
11501150
}
11511151
}
11521152

compiler/rustc_hir_analysis/src/check/intrinsicck.rs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,26 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
153153
};
154154
let Some(asm_ty) = asm_ty else {
155155
let msg = format!("cannot use value of type `{ty}` for inline assembly");
156-
let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
157-
err.note(
158-
"only integers, floats, SIMD vectors, pointers and function pointers \
159-
can be used as arguments for inline assembly",
160-
);
161-
err.emit();
156+
self.tcx
157+
.dcx()
158+
.struct_span_err(expr.span, msg)
159+
.note_mv(
160+
"only integers, floats, SIMD vectors, pointers and function pointers \
161+
can be used as arguments for inline assembly",
162+
)
163+
.emit();
162164
return None;
163165
};
164166

165167
// Check that the type implements Copy. The only case where this can
166168
// possibly fail is for SIMD types which don't #[derive(Copy)].
167169
if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
168170
let msg = "arguments for inline assembly must be copyable";
169-
let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
170-
err.note(format!("`{ty}` does not implement the Copy trait"));
171-
err.emit();
171+
self.tcx
172+
.dcx()
173+
.struct_span_err(expr.span, msg)
174+
.note_mv(format!("`{ty}` does not implement the Copy trait"))
175+
.emit();
172176
}
173177

174178
// Ideally we wouldn't need to do this, but LLVM's register allocator
@@ -183,16 +187,17 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
183187
if let Some((in_expr, Some(in_asm_ty))) = tied_input {
184188
if in_asm_ty != asm_ty {
185189
let msg = "incompatible types for asm inout argument";
186-
let mut err = self.tcx.dcx().struct_span_err(vec![in_expr.span, expr.span], msg);
187-
188190
let in_expr_ty = (self.get_operand_ty)(in_expr);
189-
err.span_label(in_expr.span, format!("type `{in_expr_ty}`"));
190-
err.span_label(expr.span, format!("type `{ty}`"));
191-
err.note(
192-
"asm inout arguments must have the same type, \
191+
self.tcx
192+
.dcx()
193+
.struct_span_err(vec![in_expr.span, expr.span], msg)
194+
.span_label_mv(in_expr.span, format!("type `{in_expr_ty}`"))
195+
.span_label_mv(expr.span, format!("type `{ty}`"))
196+
.note_mv(
197+
"asm inout arguments must have the same type, \
193198
unless they are both pointers or integers of the same size",
194-
);
195-
err.emit();
199+
)
200+
.emit();
196201
}
197202

198203
// All of the later checks have already been done on the input, so
@@ -234,13 +239,15 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
234239
if let Some(feature) = feature {
235240
if !target_features.contains(feature) {
236241
let msg = format!("`{feature}` target feature is not enabled");
237-
let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
238-
err.note(format!(
239-
"this is required to use type `{}` with register class `{}`",
240-
ty,
241-
reg_class.name(),
242-
));
243-
err.emit();
242+
self.tcx
243+
.dcx()
244+
.struct_span_err(expr.span, msg)
245+
.note_mv(format!(
246+
"this is required to use type `{}` with register class `{}`",
247+
ty,
248+
reg_class.name(),
249+
))
250+
.emit();
244251
return Some(asm_ty);
245252
}
246253
}
@@ -449,14 +456,17 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
449456
ty::Never | ty::Error(_) => {}
450457
ty::FnDef(..) => {}
451458
_ => {
452-
let mut err =
453-
self.tcx.dcx().struct_span_err(*op_sp, "invalid `sym` operand");
454-
err.span_label(
455-
self.tcx.def_span(anon_const.def_id),
456-
format!("is {} `{}`", ty.kind().article(), ty),
457-
);
458-
err.help("`sym` operands must refer to either a function or a static");
459-
err.emit();
459+
self.tcx
460+
.dcx()
461+
.struct_span_err(*op_sp, "invalid `sym` operand")
462+
.span_label_mv(
463+
self.tcx.def_span(anon_const.def_id),
464+
format!("is {} `{}`", ty.kind().article(), ty),
465+
)
466+
.help_mv(
467+
"`sym` operands must refer to either a function or a static",
468+
)
469+
.emit();
460470
}
461471
};
462472
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,12 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
197197
let mut res = Ok(());
198198
if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
199199
let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
200-
let mut err =
201-
tcx.dcx().struct_span_err(sp, "impls of auto traits cannot be default");
202-
err.span_labels(impl_.defaultness_span, "default because of this");
203-
err.span_label(sp, "auto trait");
204-
res = Err(err.emit());
200+
res = Err(tcx
201+
.dcx()
202+
.struct_span_err(sp, "impls of auto traits cannot be default")
203+
.span_labels_mv(impl_.defaultness_span, "default because of this")
204+
.span_label_mv(sp, "auto trait")
205+
.emit());
205206
}
206207
// We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
207208
match tcx.impl_polarity(def_id) {
@@ -489,35 +490,33 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
489490

490491
if !unsatisfied_bounds.is_empty() {
491492
let plural = pluralize!(unsatisfied_bounds.len());
492-
let mut err = tcx.dcx().struct_span_err(
493-
gat_item_hir.span,
494-
format!("missing required bound{} on `{}`", plural, gat_item_hir.ident),
495-
);
496-
497493
let suggestion = format!(
498494
"{} {}",
499495
gat_item_hir.generics.add_where_or_trailing_comma(),
500496
unsatisfied_bounds.join(", "),
501497
);
502-
err.span_suggestion(
503-
gat_item_hir.generics.tail_span_for_predicate_suggestion(),
504-
format!("add the required where clause{plural}"),
505-
suggestion,
506-
Applicability::MachineApplicable,
507-
);
508-
509498
let bound =
510499
if unsatisfied_bounds.len() > 1 { "these bounds are" } else { "this bound is" };
511-
err.note(format!(
512-
"{bound} currently required to ensure that impls have maximum flexibility"
513-
));
514-
err.note(
515-
"we are soliciting feedback, see issue #87479 \
500+
tcx.dcx()
501+
.struct_span_err(
502+
gat_item_hir.span,
503+
format!("missing required bound{} on `{}`", plural, gat_item_hir.ident),
504+
)
505+
.span_suggestion_mv(
506+
gat_item_hir.generics.tail_span_for_predicate_suggestion(),
507+
format!("add the required where clause{plural}"),
508+
suggestion,
509+
Applicability::MachineApplicable,
510+
)
511+
.note_mv(format!(
512+
"{bound} currently required to ensure that impls have maximum flexibility"
513+
))
514+
.note_mv(
515+
"we are soliciting feedback, see issue #87479 \
516516
<https://github.com/rust-lang/rust/issues/87479> \
517517
for more information",
518-
);
519-
520-
err.emit();
518+
)
519+
.emit();
521520
}
522521
}
523522
}
@@ -938,8 +937,8 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
938937
};
939938
if may_suggest_feature && tcx.sess.is_nightly_build() {
940939
diag.help(
941-
"add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types",
942-
);
940+
"add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types",
941+
);
943942
}
944943

945944
Err(diag.emit())

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,16 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
7070
match seen_items.entry(norm_ident) {
7171
Entry::Occupied(entry) => {
7272
let former = entry.get();
73-
let mut err = struct_span_err!(
73+
struct_span_err!(
7474
self.tcx.dcx(),
7575
span,
7676
E0592,
7777
"duplicate definitions with name `{}`",
7878
ident,
79-
);
80-
err.span_label(span, format!("duplicate definitions for `{ident}`"));
81-
err.span_label(*former, format!("other definition for `{ident}`"));
82-
83-
err.emit();
79+
)
80+
.span_label_mv(span, format!("duplicate definitions for `{ident}`"))
81+
.span_label_mv(*former, format!("other definition for `{ident}`"))
82+
.emit();
8483
}
8584
Entry::Vacant(entry) => {
8685
entry.insert(span);

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,12 +750,12 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
750750
kind: hir::ItemKind::OpaqueTy { .. }, ..
751751
}) = self.tcx.hir_node(parent_id)
752752
{
753-
let mut err = self.tcx.dcx().struct_span_err(
753+
self.tcx.dcx().struct_span_err(
754754
lifetime.ident.span,
755755
"higher kinded lifetime bounds on nested opaque types are not supported yet",
756-
);
757-
err.span_note(self.tcx.def_span(def_id), "lifetime declared here");
758-
err.emit();
756+
)
757+
.span_note_mv(self.tcx.def_span(def_id), "lifetime declared here")
758+
.emit();
759759
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
760760
}
761761
}

0 commit comments

Comments
 (0)