aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2023-09-07 11:33:39 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2023-09-07 11:37:40 +0200
commit1aee5d2ace9ea8f35baf5b18e36caf44fe018ea3 (patch)
tree3a0edf68fbf8d07d0140d5280eeea4b25e4978a1 /gcc/rust
parent1ad5ae5a45f2e3fc6948b35a3b052fdd48453704 (diff)
downloadgcc-1aee5d2ace9ea8f35baf5b18e36caf44fe018ea3.zip
gcc-1aee5d2ace9ea8f35baf5b18e36caf44fe018ea3.tar.gz
gcc-1aee5d2ace9ea8f35baf5b18e36caf44fe018ea3.tar.bz2
gccrs: Experiment with adding an error code to an error
gcc/rust/ChangeLog: * rust-diagnostics.cc (rust_error_at): New overload. * rust-diagnostics.h (struct ErrorCode): New struct. (rust_error_at): New. (rust_be_error_at): Likewise. * rust-gcc-diagnostics.cc (class rust_error_code_rule): New class. (rust_be_error_at): New function. * typecheck/rust-casts.cc (TypeCastRules::emit_cast_error): Emit E0054 when reporting invalid cast error. gcc/testsuite/ChangeLog: * rust/compile/bad_as_bool_char.rs: Add error code to testcase. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/rust-diagnostics.cc11
-rw-r--r--gcc/rust/rust-diagnostics.h18
-rw-r--r--gcc/rust/rust-gcc-diagnostics.cc33
-rw-r--r--gcc/rust/typecheck/rust-casts.cc2
4 files changed, 63 insertions, 1 deletions
diff --git a/gcc/rust/rust-diagnostics.cc b/gcc/rust/rust-diagnostics.cc
index 4e5c2ec..f29aec6 100644
--- a/gcc/rust/rust-diagnostics.cc
+++ b/gcc/rust/rust-diagnostics.cc
@@ -167,6 +167,17 @@ rust_error_at (const Location location, const char *fmt, ...)
}
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, ...)
{
va_list ap;
diff --git a/gcc/rust/rust-diagnostics.h b/gcc/rust/rust-diagnostics.h
index 43fee8b..d198bd5 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 72d2c06..58c0a56 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 e379216b..987542e 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 ());
}