From ff9fd36aa4ed90051424560268108370bbb3749b Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 28 Jun 2022 22:09:59 -0700 Subject: [PATCH] Implement IntoDiagnosticArg for hir::ConstContext --- Cargo.lock | 1 + compiler/rustc_errors/Cargo.toml | 1 + compiler/rustc_errors/src/diagnostic.rs | 11 +++++++++++ compiler/rustc_hir/src/hir.rs | 3 +++ 4 files changed, 16 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 86936b25d8a..268af59aa89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3813,6 +3813,7 @@ dependencies = [ "atty", "rustc_data_structures", "rustc_error_messages", + "rustc_hir", "rustc_lint_defs", "rustc_macros", "rustc_serialize", diff --git a/compiler/rustc_errors/Cargo.toml b/compiler/rustc_errors/Cargo.toml index 89557626057..7d7e92c5229 100644 --- a/compiler/rustc_errors/Cargo.toml +++ b/compiler/rustc_errors/Cargo.toml @@ -13,6 +13,7 @@ rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } rustc_macros = { path = "../rustc_macros" } rustc_data_structures = { path = "../rustc_data_structures" } +rustc_hir = { path = "../rustc_hir" } rustc_lint_defs = { path = "../rustc_lint_defs" } unicode-width = "0.1.4" atty = "0.2" diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 0d1d017d874..9429ad1a897 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -5,6 +5,7 @@ use crate::{ }; use rustc_data_structures::stable_map::FxHashMap; use rustc_error_messages::FluentValue; +use rustc_hir as hir; use rustc_lint_defs::{Applicability, LintExpectationId}; use rustc_span::edition::LATEST_STABLE_EDITION; use rustc_span::symbol::{Ident, Symbol}; @@ -160,6 +161,16 @@ impl<'source> Into> for DiagnosticArgValue<'source> { } } +impl IntoDiagnosticArg for hir::ConstContext { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(Cow::Borrowed(match self { + hir::ConstContext::ConstFn => "constant function", + hir::ConstContext::Static(_) => "static", + hir::ConstContext::Const => "constant", + })) + } +} + /// Trait implemented by error types. This should not be implemented manually. Instead, use /// `#[derive(SessionSubdiagnostic)]` -- see [rustc_macros::SessionSubdiagnostic]. #[rustc_diagnostic_item = "AddSubdiagnostic"] diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index a2ef158ce8d..acd77f5d2ee 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -1595,6 +1595,9 @@ impl fmt::Display for ConstContext { } } +// NOTE: `IntoDiagnosticArg` impl for `ConstContext` lives in `rustc_errors` +// due to a cyclical dependency between hir that crate. + /// A literal. pub type Lit = Spanned;