1
Fork 0

Make the non-const part swappable in the diagnostic

This commit is contained in:
Michael Goulet 2024-12-23 21:17:36 +00:00
parent b321cd5573
commit 5a9f0be0bd
3 changed files with 63 additions and 41 deletions

View file

@ -130,7 +130,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
let ConstCx { tcx, typing_env, .. } = *ccx;
let caller = ccx.def_id();
let diag_trait = |err, self_ty: Ty<'_>, trait_id| {
let note_trait_if_possible = |err, self_ty: Ty<'_>, trait_id| {
let trait_ref = TraitRef::from_method(tcx, trait_id, args);
match self_ty.kind() {
@ -183,6 +183,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
ty: self_ty,
kind: ccx.const_kind(),
non: "non",
})
};
}
@ -226,9 +227,10 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
kind: ccx.const_kind(),
note,
non: "non",
});
diag_trait(&mut err, self_ty, fn_trait_id);
note_trait_if_possible(&mut err, self_ty, fn_trait_id);
err
}
CallKind::Operator { trait_id, self_ty, .. } => {
@ -237,6 +239,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
kind: ccx.const_kind(),
ty: self_ty,
non: "non",
})
} else {
let mut sugg = None;
@ -282,10 +285,11 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
span,
kind: ccx.const_kind(),
sugg,
non: "non",
})
};
diag_trait(&mut err, self_ty, trait_id);
note_trait_if_possible(&mut err, self_ty, trait_id);
err
}
CallKind::DerefCoercion { deref_target, deref_target_ty, self_ty } => {
@ -302,19 +306,21 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
kind: ccx.const_kind(),
target_ty: deref_target_ty,
deref_target: target,
non: "non",
});
diag_trait(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span)));
note_trait_if_possible(&mut err, self_ty, tcx.require_lang_item(LangItem::Deref, Some(span)));
err
}
_ if tcx.opt_parent(callee) == tcx.get_diagnostic_item(sym::ArgumentMethods) => {
ccx.dcx().create_err(errors::NonConstFmtMacroCall { span, kind: ccx.const_kind() })
ccx.dcx().create_err(errors::NonConstFmtMacroCall { span, kind: ccx.const_kind(), non: "non" })
}
_ => ccx.dcx().create_err(errors::NonConstFnCall {
span,
def_descr: ccx.tcx.def_descr(callee),
def_path_str: ccx.tcx.def_path_str_with_args(callee, args),
kind: ccx.const_kind(),
non: "non",
}),
};

View file

@ -174,6 +174,7 @@ pub(crate) struct NonConstFmtMacroCall {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
@ -194,6 +195,7 @@ pub(crate) struct NonConstFnCall {
pub def_path_str: String,
pub def_descr: &'static str,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
@ -293,68 +295,75 @@ pub struct RawBytesNote {
// FIXME(fee1-dead) do not use stringly typed `ConstContext`
#[derive(Diagnostic)]
#[diag(const_eval_match_eq_non_const, code = E0015)]
#[diag(const_eval_non_const_match_eq, code = E0015)]
#[note]
pub struct NonConstMatchEq<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
#[diag(const_eval_for_loop_into_iter_non_const, code = E0015)]
#[diag(const_eval_non_const_for_loop_into_iter, code = E0015)]
pub struct NonConstForLoopIntoIter<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
#[diag(const_eval_question_branch_non_const, code = E0015)]
#[diag(const_eval_non_const_question_branch, code = E0015)]
pub struct NonConstQuestionBranch<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
#[diag(const_eval_question_from_residual_non_const, code = E0015)]
#[diag(const_eval_non_const_question_from_residual, code = E0015)]
pub struct NonConstQuestionFromResidual<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
#[diag(const_eval_try_block_from_output_non_const, code = E0015)]
#[diag(const_eval_non_const_try_block_from_output, code = E0015)]
pub struct NonConstTryBlockFromOutput<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
#[diag(const_eval_await_non_const, code = E0015)]
#[diag(const_eval_non_const_await, code = E0015)]
pub struct NonConstAwait<'tcx> {
#[primary_span]
pub span: Span,
pub ty: Ty<'tcx>,
pub kind: ConstContext,
pub non: &'static str,
}
#[derive(Diagnostic)]
#[diag(const_eval_closure_non_const, code = E0015)]
#[diag(const_eval_non_const_closure, code = E0015)]
pub struct NonConstClosure {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
#[subdiagnostic]
pub note: Option<NonConstClosureNote>,
pub non: &'static str,
}
#[derive(Subdiagnostic)]
@ -381,17 +390,18 @@ pub struct ConsiderDereferencing {
}
#[derive(Diagnostic)]
#[diag(const_eval_operator_non_const, code = E0015)]
#[diag(const_eval_non_const_operator, code = E0015)]
pub struct NonConstOperator {
#[primary_span]
pub span: Span,
pub kind: ConstContext,
#[subdiagnostic]
pub sugg: Option<ConsiderDereferencing>,
pub non: &'static str,
}
#[derive(Diagnostic)]
#[diag(const_eval_deref_coercion_non_const, code = E0015)]
#[diag(const_eval_non_const_deref_coercion, code = E0015)]
#[note]
pub struct NonConstDerefCoercion<'tcx> {
#[primary_span]
@ -401,6 +411,7 @@ pub struct NonConstDerefCoercion<'tcx> {
pub target_ty: Ty<'tcx>,
#[note(const_eval_target_note)]
pub deref_target: Option<Span>,
pub non: &'static str,
}
#[derive(Diagnostic)]