public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macros: add compile_error! macro
@ 2022-06-08 12:25 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:25 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:35570ae4104086a20069f967fea35d7850f0710f
commit 35570ae4104086a20069f967fea35d7850f0710f
Author: liushuyu <liushuyu011@gmail.com>
Date: Sun Apr 3 02:38:49 2022 -0600
macros: add compile_error! macro
addresses #927
Signed-off-by: Zixing Liu <liushuyu011@gmail.com>
Diff:
---
gcc/rust/expand/rust-macro-builtins.cc | 16 ++++++++++++++++
gcc/rust/expand/rust-macro-builtins.h | 3 +++
gcc/rust/util/rust-hir-map.cc | 1 +
.../rust/compile/builtin_macro_compile_error.rs | 12 ++++++++++++
4 files changed, 32 insertions(+)
diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc
index f9b8e302d09..3cd6e43d160 100644
--- a/gcc/rust/expand/rust-macro-builtins.cc
+++ b/gcc/rust/expand/rust-macro-builtins.cc
@@ -236,4 +236,20 @@ MacroBuiltin::include_str (Location invoc_locus, AST::MacroInvocData &invoc)
return AST::ASTFragment ({node});
}
+/* Expand builtin macro compile_error!("error"), which forces a compile error
+ during the compile time. */
+AST::ASTFragment
+MacroBuiltin::compile_error (Location invoc_locus, AST::MacroInvocData &invoc)
+{
+ auto lit_expr
+ = parse_single_string_literal (invoc.get_delim_tok_tree (), invoc_locus);
+ if (lit_expr == nullptr)
+ return AST::ASTFragment::create_error ();
+
+ std::string error_string = lit_expr->as_string ();
+ rust_error_at (invoc_locus, "%s", error_string.c_str ());
+
+ return AST::ASTFragment::create_error ();
+}
+
} // namespace Rust
diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h
index 8b7c016b253..b119466ac66 100644
--- a/gcc/rust/expand/rust-macro-builtins.h
+++ b/gcc/rust/expand/rust-macro-builtins.h
@@ -77,6 +77,9 @@ public:
static AST::ASTFragment include_str (Location invoc_locus,
AST::MacroInvocData &invoc);
+
+ static AST::ASTFragment compile_error (Location invoc_locus,
+ AST::MacroInvocData &invoc);
};
} // namespace Rust
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 7fbdbb0fc6d..47f91751aaf 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -753,6 +753,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
{"column", MacroBuiltin::column},
{"include_bytes", MacroBuiltin::include_bytes},
{"include_str", MacroBuiltin::include_str},
+ {"compile_error", MacroBuiltin::compile_error},
};
auto builtin = builtin_macros.find (macro->get_rule_name ());
diff --git a/gcc/testsuite/rust/compile/builtin_macro_compile_error.rs b/gcc/testsuite/rust/compile/builtin_macro_compile_error.rs
new file mode 100644
index 00000000000..6f9a8cefebc
--- /dev/null
+++ b/gcc/testsuite/rust/compile/builtin_macro_compile_error.rs
@@ -0,0 +1,12 @@
+macro_rules! compile_error {
+ () => {{}};
+}
+
+fn main () {
+ let message = "error message";
+ compile_error! (message); // { dg-error "argument must be a string literal" "" }
+ compile_error! (); // { dg-error "macro takes 1 argument" "" }
+ compile_error! ("a", "b"); // { dg-error "macro takes 1 argument" "" }
+ compile_error! ("expected error message"); // { dg-error "expected error message" }
+ compile_error! ("expected error message",); // { dg-error "expected error message" }
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-08 12:25 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:25 [gcc/devel/rust/master] macros: add compile_error! macro Thomas Schwinge
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).