public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: Arthur Cohen <arthur.cohen@embecosm.com>
To: gcc-patches@gcc.gnu.org
Cc: gcc-rust@gcc.gnu.org, dmalcolm@redhat.com
Subject: [PATCH 2/2] Experiment with adding an error code to an error
Date: Wed,  6 Sep 2023 15:53:03 +0200	[thread overview]
Message-ID: <20230906135303.3643659-3-arthur.cohen@embecosm.com> (raw)
In-Reply-To: <20230906135303.3643659-1-arthur.cohen@embecosm.com>

From: David Malcolm <dmalcolm@redhat.com>

---
 gcc/rust/rust-diagnostics.cc                  | 11 +++++++
 gcc/rust/rust-diagnostics.h                   | 18 ++++++++++
 gcc/rust/rust-gcc-diagnostics.cc              | 33 +++++++++++++++++++
 gcc/rust/typecheck/rust-casts.cc              |  2 +-
 .../rust/compile/bad_as_bool_char.rs          |  4 +--
 5 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/rust-diagnostics.cc b/gcc/rust/rust-diagnostics.cc
index 4e5c2ececd4..f29aec67652 100644
--- a/gcc/rust/rust-diagnostics.cc
+++ b/gcc/rust/rust-diagnostics.cc
@@ -166,6 +166,17 @@ rust_error_at (const Location location, const char *fmt, ...)
   va_end (ap);
 }
 
+void
+rust_error_at (const RichLocation &location, const ErrorCode code,
+	       const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start (ap, fmt);
+  rust_be_error_at (location, code, expand_message (fmt, ap));
+  va_end (ap);
+}
+
 void
 rust_warning_at (const Location location, int opt, const char *fmt, ...)
 {
diff --git a/gcc/rust/rust-diagnostics.h b/gcc/rust/rust-diagnostics.h
index 43fee8baf4f..d198bd5736f 100644
--- a/gcc/rust/rust-diagnostics.h
+++ b/gcc/rust/rust-diagnostics.h
@@ -50,6 +50,18 @@
 
 // clang-format off
 // simple location
+
+struct ErrorCode
+{
+  explicit ErrorCode (const char *str) : m_str (str)
+  {
+    gcc_assert (str);
+    gcc_assert (str[0] == 'E');
+  }
+
+  const char *m_str;
+};
+
 extern void
 rust_internal_error_at (const Location, const char *fmt, ...)
   RUST_ATTRIBUTE_GCC_DIAG (2, 3)
@@ -72,6 +84,9 @@ rust_inform (const Location, const char *fmt, ...)
 extern void
 rust_error_at (const RichLocation &, const char *fmt, ...)
   RUST_ATTRIBUTE_GCC_DIAG (2, 3);
+extern void
+rust_error_at (const RichLocation &, const ErrorCode, const char *fmt, ...)
+  RUST_ATTRIBUTE_GCC_DIAG (3, 4);
 // clang-format on
 
 // These interfaces provide a way for the front end to ask for
@@ -97,6 +112,9 @@ rust_be_error_at (const Location, const std::string &errmsg);
 extern void
 rust_be_error_at (const RichLocation &, const std::string &errmsg);
 extern void
+rust_be_error_at (const RichLocation &, const ErrorCode,
+		  const std::string &errmsg);
+extern void
 rust_be_warning_at (const Location, int opt, const std::string &warningmsg);
 extern void
 rust_be_fatal_error (const Location, const std::string &errmsg)
diff --git a/gcc/rust/rust-gcc-diagnostics.cc b/gcc/rust/rust-gcc-diagnostics.cc
index 72d2c068541..58c0a5654ea 100644
--- a/gcc/rust/rust-gcc-diagnostics.cc
+++ b/gcc/rust/rust-gcc-diagnostics.cc
@@ -22,6 +22,7 @@
 #include "rust-diagnostics.h"
 
 #include "options.h"
+#include "diagnostic-metadata.h"
 
 void
 rust_be_internal_error_at (const Location location, const std::string &errmsg)
@@ -70,6 +71,38 @@ rust_be_error_at (const RichLocation &location, const std::string &errmsg)
   error_at (&gcc_loc, "%s", errmsg.c_str ());
 }
 
+class rust_error_code_rule : public diagnostic_metadata::rule
+{
+public:
+  rust_error_code_rule (const ErrorCode code) : m_code (code) {}
+
+  char *make_description () const final override
+  {
+    return xstrdup (m_code.m_str);
+  }
+
+  char *make_url () const final override
+  {
+    return concat ("https://doc.rust-lang.org/error-index.html#", m_code.m_str,
+		   NULL);
+  }
+
+private:
+  const ErrorCode m_code;
+};
+
+void
+rust_be_error_at (const RichLocation &location, const ErrorCode code,
+		  const std::string &errmsg)
+{
+  /* TODO: 'error_at' would like a non-'const' 'rich_location *'.  */
+  rich_location &gcc_loc = const_cast<rich_location &> (location.get ());
+  diagnostic_metadata m;
+  rust_error_code_rule rule (code);
+  m.add_rule (rule);
+  error_meta (&gcc_loc, m, "%s", errmsg.c_str ());
+}
+
 void
 rust_be_get_quotechars (const char **open_qu, const char **close_qu)
 {
diff --git a/gcc/rust/typecheck/rust-casts.cc b/gcc/rust/typecheck/rust-casts.cc
index e379216b948..987542e59c4 100644
--- a/gcc/rust/typecheck/rust-casts.cc
+++ b/gcc/rust/typecheck/rust-casts.cc
@@ -283,7 +283,7 @@ TypeCastRules::emit_cast_error () const
   RichLocation r (locus);
   r.add_range (from.get_locus ());
   r.add_range (to.get_locus ());
-  rust_error_at (r, "invalid cast %<%s%> to %<%s%>",
+  rust_error_at (r, ErrorCode ("E0054"), "invalid cast %<%s%> to %<%s%>",
 		 from.get_ty ()->get_name ().c_str (),
 		 to.get_ty ()->get_name ().c_str ());
 }
diff --git a/gcc/testsuite/rust/compile/bad_as_bool_char.rs b/gcc/testsuite/rust/compile/bad_as_bool_char.rs
index 91a28eebe00..9652915fe11 100644
--- a/gcc/testsuite/rust/compile/bad_as_bool_char.rs
+++ b/gcc/testsuite/rust/compile/bad_as_bool_char.rs
@@ -5,13 +5,13 @@ pub fn main ()
   let fone = t as f32;   // { dg-error "invalid cast" }
   let fzero = f as f64;  // { dg-error "invalid cast" }
 
-  let nb = 0u8 as bool;  // { dg-error "invalid cast" }
+  let nb = 0u8 as bool;  // { dg-error "invalid cast .u8. to .bool. \\\[E0054\\\]" }
   let nc = true as char; // { dg-error "invalid cast" }
 
   let a = 'a';
   let b = 'b';
   let fa = a as f32;     // { dg-error "invalid cast" }
-  let bb = b as bool;    // { dg-error "invalid cast" }
+  let bb = b as bool;    // { dg-error "invalid cast .char. to .bool. \\\[E0054\\\]" }
 
   let t32: u32 = 33;
   let ab = t32 as char;  // { dg-error "invalid cast" }
-- 
2.40.0


  parent reply	other threads:[~2023-09-06 13:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-06 13:53 [PATCHSET] Add error metadata to diagnostics Arthur Cohen
2023-09-06 13:53 ` [PATCH 1/2] diagnostics: add error_meta Arthur Cohen
2023-09-06 14:12   ` David Malcolm
2023-09-06 14:24     ` Arthur Cohen
2023-09-06 13:53 ` Arthur Cohen [this message]
2023-09-06 14:19   ` [PATCH 2/2] Experiment with adding an error code to an error David Malcolm
2023-09-07  9:39 ` [PATCHSET] Add error metadata to diagnostics Arthur Cohen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230906135303.3643659-3-arthur.cohen@embecosm.com \
    --to=arthur.cohen@embecosm.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc-rust@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).