1
Fork 0

Use chaining for DiagnosticBuilder construction and emit.

To avoid the use of a mutable local variable, and because it reads more
nicely.
This commit is contained in:
Nicholas Nethercote 2024-01-03 17:03:10 +11:00
parent 589591efde
commit bd4e623485
22 changed files with 193 additions and 183 deletions

View file

@ -650,9 +650,9 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
if position == GenericArgPosition::Value
&& args.num_lifetime_params() != param_counts.lifetimes
{
let mut err = struct_span_err!(tcx.dcx(), span, E0794, "{}", msg);
err.span_note(span_late, note);
err.emit();
struct_span_err!(tcx.dcx(), span, E0794, "{}", msg)
.span_note_mv(span_late, note)
.emit();
} else {
let mut multispan = MultiSpan::from_span(span);
multispan.push_span_label(span_late, note);

View file

@ -290,19 +290,19 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if references_self {
let def_id = i.bottom().0.def_id();
let mut err = struct_span_err!(
struct_span_err!(
tcx.dcx(),
i.bottom().1,
E0038,
"the {} `{}` cannot be made into an object",
tcx.def_descr(def_id),
tcx.item_name(def_id),
);
err.note(
)
.note_mv(
rustc_middle::traits::ObjectSafetyViolation::SupertraitSelf(smallvec![])
.error_msg(),
);
err.emit();
)
.emit();
}
ty::ExistentialTraitRef { def_id: trait_ref.def_id, args }

View file

@ -1140,13 +1140,13 @@ fn check_enum(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let disr_non_unit = def.variants().iter().any(|var| !is_unit(var) && has_disr(var));
if disr_non_unit || (disr_units && has_non_units) {
let err = struct_span_err!(
struct_span_err!(
tcx.dcx(),
tcx.def_span(def_id),
E0732,
"`#[repr(inttype)]` must be specified"
);
err.emit();
)
.emit();
}
}

View file

@ -153,12 +153,14 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
};
let Some(asm_ty) = asm_ty else {
let msg = format!("cannot use value of type `{ty}` for inline assembly");
let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
err.note(
"only integers, floats, SIMD vectors, pointers and function pointers \
can be used as arguments for inline assembly",
);
err.emit();
self.tcx
.dcx()
.struct_span_err(expr.span, msg)
.note_mv(
"only integers, floats, SIMD vectors, pointers and function pointers \
can be used as arguments for inline assembly",
)
.emit();
return None;
};
@ -166,9 +168,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
// possibly fail is for SIMD types which don't #[derive(Copy)].
if !ty.is_copy_modulo_regions(self.tcx, self.param_env) {
let msg = "arguments for inline assembly must be copyable";
let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
err.note(format!("`{ty}` does not implement the Copy trait"));
err.emit();
self.tcx
.dcx()
.struct_span_err(expr.span, msg)
.note_mv(format!("`{ty}` does not implement the Copy trait"))
.emit();
}
// Ideally we wouldn't need to do this, but LLVM's register allocator
@ -183,16 +187,17 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
if let Some((in_expr, Some(in_asm_ty))) = tied_input {
if in_asm_ty != asm_ty {
let msg = "incompatible types for asm inout argument";
let mut err = self.tcx.dcx().struct_span_err(vec![in_expr.span, expr.span], msg);
let in_expr_ty = (self.get_operand_ty)(in_expr);
err.span_label(in_expr.span, format!("type `{in_expr_ty}`"));
err.span_label(expr.span, format!("type `{ty}`"));
err.note(
"asm inout arguments must have the same type, \
self.tcx
.dcx()
.struct_span_err(vec![in_expr.span, expr.span], msg)
.span_label_mv(in_expr.span, format!("type `{in_expr_ty}`"))
.span_label_mv(expr.span, format!("type `{ty}`"))
.note_mv(
"asm inout arguments must have the same type, \
unless they are both pointers or integers of the same size",
);
err.emit();
)
.emit();
}
// All of the later checks have already been done on the input, so
@ -234,13 +239,15 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
if let Some(feature) = feature {
if !target_features.contains(feature) {
let msg = format!("`{feature}` target feature is not enabled");
let mut err = self.tcx.dcx().struct_span_err(expr.span, msg);
err.note(format!(
"this is required to use type `{}` with register class `{}`",
ty,
reg_class.name(),
));
err.emit();
self.tcx
.dcx()
.struct_span_err(expr.span, msg)
.note_mv(format!(
"this is required to use type `{}` with register class `{}`",
ty,
reg_class.name(),
))
.emit();
return Some(asm_ty);
}
}
@ -449,14 +456,17 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
ty::Never | ty::Error(_) => {}
ty::FnDef(..) => {}
_ => {
let mut err =
self.tcx.dcx().struct_span_err(*op_sp, "invalid `sym` operand");
err.span_label(
self.tcx.def_span(anon_const.def_id),
format!("is {} `{}`", ty.kind().article(), ty),
);
err.help("`sym` operands must refer to either a function or a static");
err.emit();
self.tcx
.dcx()
.struct_span_err(*op_sp, "invalid `sym` operand")
.span_label_mv(
self.tcx.def_span(anon_const.def_id),
format!("is {} `{}`", ty.kind().article(), ty),
)
.help_mv(
"`sym` operands must refer to either a function or a static",
)
.emit();
}
};
}

View file

@ -197,11 +197,12 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
let mut res = Ok(());
if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
let mut err =
tcx.dcx().struct_span_err(sp, "impls of auto traits cannot be default");
err.span_labels(impl_.defaultness_span, "default because of this");
err.span_label(sp, "auto trait");
res = Err(err.emit());
res = Err(tcx
.dcx()
.struct_span_err(sp, "impls of auto traits cannot be default")
.span_labels_mv(impl_.defaultness_span, "default because of this")
.span_label_mv(sp, "auto trait")
.emit());
}
// We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
match tcx.impl_polarity(def_id) {
@ -489,35 +490,33 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
if !unsatisfied_bounds.is_empty() {
let plural = pluralize!(unsatisfied_bounds.len());
let mut err = tcx.dcx().struct_span_err(
gat_item_hir.span,
format!("missing required bound{} on `{}`", plural, gat_item_hir.ident),
);
let suggestion = format!(
"{} {}",
gat_item_hir.generics.add_where_or_trailing_comma(),
unsatisfied_bounds.join(", "),
);
err.span_suggestion(
gat_item_hir.generics.tail_span_for_predicate_suggestion(),
format!("add the required where clause{plural}"),
suggestion,
Applicability::MachineApplicable,
);
let bound =
if unsatisfied_bounds.len() > 1 { "these bounds are" } else { "this bound is" };
err.note(format!(
"{bound} currently required to ensure that impls have maximum flexibility"
));
err.note(
"we are soliciting feedback, see issue #87479 \
tcx.dcx()
.struct_span_err(
gat_item_hir.span,
format!("missing required bound{} on `{}`", plural, gat_item_hir.ident),
)
.span_suggestion_mv(
gat_item_hir.generics.tail_span_for_predicate_suggestion(),
format!("add the required where clause{plural}"),
suggestion,
Applicability::MachineApplicable,
)
.note_mv(format!(
"{bound} currently required to ensure that impls have maximum flexibility"
))
.note_mv(
"we are soliciting feedback, see issue #87479 \
<https://github.com/rust-lang/rust/issues/87479> \
for more information",
);
err.emit();
)
.emit();
}
}
}
@ -938,8 +937,8 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
};
if may_suggest_feature && tcx.sess.is_nightly_build() {
diag.help(
"add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types",
);
"add `#![feature(adt_const_params)]` to the crate attributes to enable more complex and user defined types",
);
}
Err(diag.emit())

View file

@ -70,17 +70,16 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
match seen_items.entry(norm_ident) {
Entry::Occupied(entry) => {
let former = entry.get();
let mut err = struct_span_err!(
struct_span_err!(
self.tcx.dcx(),
span,
E0592,
"duplicate definitions with name `{}`",
ident,
);
err.span_label(span, format!("duplicate definitions for `{ident}`"));
err.span_label(*former, format!("other definition for `{ident}`"));
err.emit();
)
.span_label_mv(span, format!("duplicate definitions for `{ident}`"))
.span_label_mv(*former, format!("other definition for `{ident}`"))
.emit();
}
Entry::Vacant(entry) => {
entry.insert(span);

View file

@ -750,12 +750,12 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
kind: hir::ItemKind::OpaqueTy { .. }, ..
}) = self.tcx.hir_node(parent_id)
{
let mut err = self.tcx.dcx().struct_span_err(
self.tcx.dcx().struct_span_err(
lifetime.ident.span,
"higher kinded lifetime bounds on nested opaque types are not supported yet",
);
err.span_note(self.tcx.def_span(def_id), "lifetime declared here");
err.emit();
)
.span_note_mv(self.tcx.def_span(def_id), "lifetime declared here")
.emit();
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
}
}