Create "AppendConstMessage" enum

This patch creates an enum to replace a nested `Option`.
This commit is contained in:
Bryan Garza 2023-04-21 14:04:22 -07:00
parent d0d40d2a40
commit 55e5a1d206
2 changed files with 28 additions and 18 deletions

View File

@ -15,8 +15,7 @@ use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::query::normalize::QueryNormalizeExt as _;
use crate::traits::specialize::to_pretty_impl_header;
use crate::traits::NormalizeExt;
use on_unimplemented::OnUnimplementedNote;
use on_unimplemented::TypeErrCtxtExt as _;
use on_unimplemented::{AppendConstMessage, OnUnimplementedNote, TypeErrCtxtExt as _};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_errors::{
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
@ -707,7 +706,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
conversion on the error value using the `From` trait"
.to_owned(),
),
Some(None),
Some(AppendConstMessage::Default),
)
} else {
(message, note, append_const_msg)
@ -1272,7 +1271,7 @@ trait InferCtxtPrivExt<'tcx> {
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
message: Option<String>,
predicate_is_const: bool,
append_const_msg: Option<Option<rustc_span::Symbol>>,
append_const_msg: Option<AppendConstMessage>,
post_message: String,
) -> String;
@ -2682,7 +2681,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
message: Option<String>,
predicate_is_const: bool,
append_const_msg: Option<Option<rustc_span::Symbol>>,
append_const_msg: Option<AppendConstMessage>,
post_message: String,
) -> String {
message
@ -2691,17 +2690,19 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// do nothing if predicate is not const
(false, _) => Some(cannot_do_this),
// suggested using default post message
(true, Some(None)) => Some(format!("{cannot_do_this} in const contexts")),
(true, Some(AppendConstMessage::Default)) => {
Some(format!("{cannot_do_this} in const contexts"))
}
// overridden post message
(true, Some(Some(post_message))) => {
Some(format!("{cannot_do_this}{post_message}"))
(true, Some(AppendConstMessage::Custom(custom_msg))) => {
Some(format!("{cannot_do_this}{custom_msg}"))
}
// fallback to generic message
(true, None) => None,
}
})
.unwrap_or_else(|| {
format!("the trait bound `{}` is not satisfied{}", trait_predicate, post_message,)
format!("the trait bound `{}` is not satisfied{}", trait_predicate, post_message)
})
}

View File

@ -327,7 +327,7 @@ pub struct OnUnimplementedDirective {
pub label: Option<OnUnimplementedFormatString>,
pub note: Option<OnUnimplementedFormatString>,
pub parent_label: Option<OnUnimplementedFormatString>,
pub append_const_msg: Option<Option<Symbol>>,
pub append_const_msg: Option<AppendConstMessage>,
}
/// For the `#[rustc_on_unimplemented]` attribute
@ -337,12 +337,21 @@ pub struct OnUnimplementedNote {
pub label: Option<String>,
pub note: Option<String>,
pub parent_label: Option<String>,
/// Append a message for `~const Trait` errors. `None` means not requested and
/// should fallback to a generic message, `Some(None)` suggests using the default
/// appended message, `Some(Some(s))` suggests use the `s` message instead of the
/// default one..
/// FIXME(bryangarza): Change this to an enum with the 3 variants described above.
pub append_const_msg: Option<Option<Symbol>>,
// If none, should fall back to a generic message
pub append_const_msg: Option<AppendConstMessage>,
}
/// Append a message for `~const Trait` errors.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum AppendConstMessage {
Default,
Custom(Symbol),
}
impl Default for AppendConstMessage {
fn default() -> Self {
AppendConstMessage::Default
}
}
impl<'tcx> OnUnimplementedDirective {
@ -420,10 +429,10 @@ impl<'tcx> OnUnimplementedDirective {
}
} else if item.has_name(sym::append_const_msg) && append_const_msg.is_none() {
if let Some(msg) = item.value_str() {
append_const_msg = Some(Some(msg));
append_const_msg = Some(AppendConstMessage::Custom(msg));
continue;
} else if item.is_word() {
append_const_msg = Some(None);
append_const_msg = Some(AppendConstMessage::Default);
continue;
}
}