From 55e5a1d20668f4384b647842007095dbf5868851 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Fri, 21 Apr 2023 14:04:22 -0700 Subject: [PATCH] Create "AppendConstMessage" enum This patch creates an enum to replace a nested `Option`. --- .../src/traits/error_reporting/mod.rs | 19 ++++++------- .../error_reporting/on_unimplemented.rs | 27 ++++++++++++------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index 87b6092bd5d..4cc986774b2 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -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, predicate_is_const: bool, - append_const_msg: Option>, + append_const_msg: Option, post_message: String, ) -> String; @@ -2682,7 +2681,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> { trait_predicate: &ty::PolyTraitPredicate<'tcx>, message: Option, predicate_is_const: bool, - append_const_msg: Option>, + append_const_msg: Option, 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) }) } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs index 193378d7633..88525e1b720 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs @@ -327,7 +327,7 @@ pub struct OnUnimplementedDirective { pub label: Option, pub note: Option, pub parent_label: Option, - pub append_const_msg: Option>, + pub append_const_msg: Option, } /// For the `#[rustc_on_unimplemented]` attribute @@ -337,12 +337,21 @@ pub struct OnUnimplementedNote { pub label: Option, pub note: Option, pub parent_label: Option, - /// 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>, + // If none, should fall back to a generic message + pub append_const_msg: Option, +} + +/// 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; } }