Allow multiple impl Into<{D,Subd}iagMessage> parameters in a function.

The internal diagnostic lint currently only allows one, because that was
all that occurred in practice. But rust-lang/rust-clippy/pull/12453
wants to introduce functions with more than one, and this limitation is
getting in the way.
This commit is contained in:
Nicholas Nethercote 2024-03-11 09:06:08 +11:00
parent c69fda7dc6
commit 47b4b8e8c5
2 changed files with 13 additions and 14 deletions

View File

@ -409,9 +409,8 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
} }
}; };
// Does the callee have a `impl Into<{D,Subd}iagMessage>` parameter? (There should be at // Does the callee have one or more `impl Into<{D,Subd}iagMessage>` parameters?
// most one.) let mut impl_into_diagnostic_message_params = vec![];
let mut impl_into_diagnostic_message_param = None;
let fn_sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder(); let fn_sig = cx.tcx.fn_sig(def_id).instantiate_identity().skip_binder();
let predicates = cx.tcx.predicates_of(def_id).instantiate_identity(cx.tcx).predicates; let predicates = cx.tcx.predicates_of(def_id).instantiate_identity(cx.tcx).predicates;
for (i, &param_ty) in fn_sig.inputs().iter().enumerate() { for (i, &param_ty) in fn_sig.inputs().iter().enumerate() {
@ -425,20 +424,14 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
&& let ty1 = trait_ref.args.type_at(1) && let ty1 = trait_ref.args.type_at(1)
&& is_diag_message(ty1) && is_diag_message(ty1)
{ {
if impl_into_diagnostic_message_param.is_some() { impl_into_diagnostic_message_params.push((i, p.name));
cx.tcx.dcx().span_bug(
span,
"can't handle multiple `impl Into<{D,Sub}iagMessage>` params",
);
}
impl_into_diagnostic_message_param = Some((i, p.name));
} }
} }
} }
} }
// Is the callee interesting? // Is the callee interesting?
if !has_attr && impl_into_diagnostic_message_param.is_none() { if !has_attr && impl_into_diagnostic_message_params.is_empty() {
return; return;
} }
@ -481,7 +474,7 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
// Calls to methods with an `impl Into<{D,Subd}iagMessage>` parameter must be passed an arg // Calls to methods with an `impl Into<{D,Subd}iagMessage>` parameter must be passed an arg
// with type `{D,Subd}iagMessage` or `impl Into<{D,Subd}iagMessage>`. Otherwise, emit an // with type `{D,Subd}iagMessage` or `impl Into<{D,Subd}iagMessage>`. Otherwise, emit an
// `UNTRANSLATABLE_DIAGNOSTIC` lint. // `UNTRANSLATABLE_DIAGNOSTIC` lint.
if let Some((param_i, param_i_p_name)) = impl_into_diagnostic_message_param { for (param_i, param_i_p_name) in impl_into_diagnostic_message_params {
// Is the arg type `{Sub,D}iagMessage`or `impl Into<{Sub,D}iagMessage>`? // Is the arg type `{Sub,D}iagMessage`or `impl Into<{Sub,D}iagMessage>`?
let arg_ty = call_tys[param_i]; let arg_ty = call_tys[param_i];
let is_translatable = is_diag_message(arg_ty) let is_translatable = is_diag_message(arg_ty)

View File

@ -15,7 +15,7 @@
use rustc_errors::{ use rustc_errors::{
Diag, DiagCtxt, DiagInner, DiagMessage, Diagnostic, EmissionGuarantee, Level, LintDiagnostic, Diag, DiagCtxt, DiagInner, DiagMessage, Diagnostic, EmissionGuarantee, Level, LintDiagnostic,
SubdiagMessageOp, Subdiagnostic, SubdiagMessageOp, SubdiagMessage, Subdiagnostic,
}; };
use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_span::Span; use rustc_span::Span;
@ -114,9 +114,15 @@ pub fn make_diagnostics<'a>(dcx: &'a DiagCtxt) {
// Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted for // Check that `rustc_lint_diagnostics`-annotated functions aren't themselves linted for
// `diagnostic_outside_of_impl`. // `diagnostic_outside_of_impl`.
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
pub fn skipped_because_of_annotation<'a>(dcx: &'a DiagCtxt) { pub fn skipped_because_of_annotation<'a>(dcx: &'a DiagCtxt) {
#[allow(rustc::untranslatable_diagnostic)] #[allow(rustc::untranslatable_diagnostic)]
let _diag = dcx.struct_err("untranslatable diagnostic"); // okay! let _diag = dcx.struct_err("untranslatable diagnostic"); // okay!
} }
// Check that multiple translatable params are allowed in a single function (at one point they
// weren't).
fn f(_x: impl Into<DiagMessage>, _y: impl Into<SubdiagMessage>) {}
fn g() {
f(crate::fluent_generated::no_crate_example, crate::fluent_generated::no_crate_example);
}