Skip to content

Commit 55e5a1d

Browse files
committed
Create "AppendConstMessage" enum
This patch creates an enum to replace a nested `Option`.
1 parent d0d40d2 commit 55e5a1d

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
1515
use crate::traits::query::normalize::QueryNormalizeExt as _;
1616
use crate::traits::specialize::to_pretty_impl_header;
1717
use crate::traits::NormalizeExt;
18-
use on_unimplemented::OnUnimplementedNote;
19-
use on_unimplemented::TypeErrCtxtExt as _;
18+
use on_unimplemented::{AppendConstMessage, OnUnimplementedNote, TypeErrCtxtExt as _};
2019
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
2120
use rustc_errors::{
2221
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
@@ -707,7 +706,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
707706
conversion on the error value using the `From` trait"
708707
.to_owned(),
709708
),
710-
Some(None),
709+
Some(AppendConstMessage::Default),
711710
)
712711
} else {
713712
(message, note, append_const_msg)
@@ -1272,7 +1271,7 @@ trait InferCtxtPrivExt<'tcx> {
12721271
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
12731272
message: Option<String>,
12741273
predicate_is_const: bool,
1275-
append_const_msg: Option<Option<rustc_span::Symbol>>,
1274+
append_const_msg: Option<AppendConstMessage>,
12761275
post_message: String,
12771276
) -> String;
12781277

@@ -2682,7 +2681,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
26822681
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
26832682
message: Option<String>,
26842683
predicate_is_const: bool,
2685-
append_const_msg: Option<Option<rustc_span::Symbol>>,
2684+
append_const_msg: Option<AppendConstMessage>,
26862685
post_message: String,
26872686
) -> String {
26882687
message
@@ -2691,17 +2690,19 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
26912690
// do nothing if predicate is not const
26922691
(false, _) => Some(cannot_do_this),
26932692
// suggested using default post message
2694-
(true, Some(None)) => Some(format!("{cannot_do_this} in const contexts")),
2693+
(true, Some(AppendConstMessage::Default)) => {
2694+
Some(format!("{cannot_do_this} in const contexts"))
2695+
}
26952696
// overridden post message
2696-
(true, Some(Some(post_message))) => {
2697-
Some(format!("{cannot_do_this}{post_message}"))
2697+
(true, Some(AppendConstMessage::Custom(custom_msg))) => {
2698+
Some(format!("{cannot_do_this}{custom_msg}"))
26982699
}
26992700
// fallback to generic message
27002701
(true, None) => None,
27012702
}
27022703
})
27032704
.unwrap_or_else(|| {
2704-
format!("the trait bound `{}` is not satisfied{}", trait_predicate, post_message,)
2705+
format!("the trait bound `{}` is not satisfied{}", trait_predicate, post_message)
27052706
})
27062707
}
27072708

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub struct OnUnimplementedDirective {
327327
pub label: Option<OnUnimplementedFormatString>,
328328
pub note: Option<OnUnimplementedFormatString>,
329329
pub parent_label: Option<OnUnimplementedFormatString>,
330-
pub append_const_msg: Option<Option<Symbol>>,
330+
pub append_const_msg: Option<AppendConstMessage>,
331331
}
332332

333333
/// For the `#[rustc_on_unimplemented]` attribute
@@ -337,12 +337,21 @@ pub struct OnUnimplementedNote {
337337
pub label: Option<String>,
338338
pub note: Option<String>,
339339
pub parent_label: Option<String>,
340-
/// Append a message for `~const Trait` errors. `None` means not requested and
341-
/// should fallback to a generic message, `Some(None)` suggests using the default
342-
/// appended message, `Some(Some(s))` suggests use the `s` message instead of the
343-
/// default one..
344-
/// FIXME(bryangarza): Change this to an enum with the 3 variants described above.
345-
pub append_const_msg: Option<Option<Symbol>>,
340+
// If none, should fall back to a generic message
341+
pub append_const_msg: Option<AppendConstMessage>,
342+
}
343+
344+
/// Append a message for `~const Trait` errors.
345+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
346+
pub enum AppendConstMessage {
347+
Default,
348+
Custom(Symbol),
349+
}
350+
351+
impl Default for AppendConstMessage {
352+
fn default() -> Self {
353+
AppendConstMessage::Default
354+
}
346355
}
347356

348357
impl<'tcx> OnUnimplementedDirective {
@@ -420,10 +429,10 @@ impl<'tcx> OnUnimplementedDirective {
420429
}
421430
} else if item.has_name(sym::append_const_msg) && append_const_msg.is_none() {
422431
if let Some(msg) = item.value_str() {
423-
append_const_msg = Some(Some(msg));
432+
append_const_msg = Some(AppendConstMessage::Custom(msg));
424433
continue;
425434
} else if item.is_word() {
426-
append_const_msg = Some(None);
435+
append_const_msg = Some(AppendConstMessage::Default);
427436
continue;
428437
}
429438
}

0 commit comments

Comments
 (0)