From: Enes Cevik <[email protected]>

This patch registers the 'rustc_conversion_suggestion' attribute. This
attribute is needed for compiling the alloc crate, so we register it as
a stub and the compiler will emit a warning when it encounters this
attribute.

gcc/rust/ChangeLog:

        * checks/errors/rust-builtin-attribute-checker.cc
        (attribute_checking_handlers): Add rustc_conversion_suggestion
        attribute to map.
        (check_valid_attribute_for_item): Add warning for new attribute.
        * util/rust-attribute-values.h (class Attributes): Add
        RUSTC_CONVERSION_SUGGESTION constexpr.
        * util/rust-attributes.cc (__definitions): Add
        BuiltinAttrDefinition for new attribute.

gcc/testsuite/ChangeLog:

        * rust/compile/rustc_conversion_suggestion.rs: New test.

Signed-off-by: Enes Cevik <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/970ba600872a6f4c431a4b59ce639456360b9bb4

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4695

 .../errors/rust-builtin-attribute-checker.cc       | 14 ++++++++++++++
 gcc/rust/util/rust-attribute-values.h              |  3 +++
 gcc/rust/util/rust-attributes.cc                   |  3 ++-
 .../rust/compile/rustc_conversion_suggestion.rs    | 14 ++++++++++++++
 4 files changed, 33 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/rust/compile/rustc_conversion_suggestion.rs

diff --git a/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc 
b/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc
index c1ab0df5e..d8a9bb0fb 100644
--- a/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc
+++ b/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc
@@ -389,6 +389,7 @@ const std::unordered_map<std::string, std::function<void 
(AST::Attribute &)>>
     {Attrs::RUSTC_ALLOCATOR, handlers::expect_no_input},
     {Attrs::RUSTC_ALLOCATOR_NOUNWIND, handlers::expect_no_input},
     {Attrs::GLOBAL_ALLOCATOR, handlers::expect_no_input},
+    {Attrs::RUSTC_CONVERSION_SUGGESTION, handlers::expect_no_input},
 };
 
 tl::optional<std::function<void (AST::Attribute &)>>
@@ -450,6 +451,19 @@ check_valid_attribute_for_item (const AST::Attribute &attr,
                     "to static items",
                     attr.get_path ().as_string ().c_str ());
     }
+  else if (attr.get_path () == Values::Attributes::RUSTC_CONVERSION_SUGGESTION)
+    {
+      auto attr_path = attr.get_path ().as_string ();
+      rust_warning_at (attr.get_locus (), 0,
+                      "%<#[%s]%> is not implemented yet and has no effect",
+                      attr_path.c_str ());
+      if (item.get_item_kind () != AST::Item::Kind::Function)
+       {
+         rust_error_at (item.get_locus (),
+                        "%<#[%s]%> can only be applied to functions",
+                        attr_path.c_str ());
+       }
+    }
 }
 
 BuiltinAttributeChecker::BuiltinAttributeChecker () {}
diff --git a/gcc/rust/util/rust-attribute-values.h 
b/gcc/rust/util/rust-attribute-values.h
index 4f8860747..3ab91d35f 100644
--- a/gcc/rust/util/rust-attribute-values.h
+++ b/gcc/rust/util/rust-attribute-values.h
@@ -115,6 +115,9 @@ public:
 
   static constexpr auto &RUSTC_ALLOCATOR = "rustc_allocator";
   static constexpr auto &RUSTC_ALLOCATOR_NOUNWIND = "rustc_allocator_nounwind";
+
+  static constexpr auto &RUSTC_CONVERSION_SUGGESTION
+    = "rustc_conversion_suggestion";
 };
 } // namespace Values
 } // namespace Rust
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 97f9edeea..2ae917fdb 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -98,7 +98,8 @@ static const BuiltinAttrDefinition __definitions[]
      {Attrs::NEEDS_ALLOCATOR, CODE_GENERATION},
      {Attrs::RUSTC_ALLOCATOR, CODE_GENERATION},
      {Attrs::RUSTC_ALLOCATOR_NOUNWIND, CODE_GENERATION},
-     {Attrs::GLOBAL_ALLOCATOR, CODE_GENERATION}};
+     {Attrs::GLOBAL_ALLOCATOR, CODE_GENERATION},
+     {Attrs::RUSTC_CONVERSION_SUGGESTION, TYPE_CHECK}};
 
 static const std::set<std::string> __outer_attributes
   = {Attrs::INLINE,
diff --git a/gcc/testsuite/rust/compile/rustc_conversion_suggestion.rs 
b/gcc/testsuite/rust/compile/rustc_conversion_suggestion.rs
new file mode 100644
index 000000000..e601ccf06
--- /dev/null
+++ b/gcc/testsuite/rust/compile/rustc_conversion_suggestion.rs
@@ -0,0 +1,14 @@
+#![feature(no_core, rustc_attrs)]
+#![no_core]
+
+#[rustc_conversion_suggestion] // { dg-warning 
"...rustc_conversion_suggestion.. is not implemented yet and has no effect" }
+pub fn to_string() {}
+
+#[rustc_conversion_suggestion] // { dg-warning 
"...rustc_conversion_suggestion.. is not implemented yet and has no effect" }
+const TO_STRING : i32 = 0; // { dg-error "...rustc_conversion_suggestion.. can 
only be applied to functions" } 
+
+#[rustc_conversion_suggestion] // { dg-warning 
"...rustc_conversion_suggestion.. is not implemented yet and has no effect" }
+static TO_STRING2 : i32 = 0; // { dg-error "...rustc_conversion_suggestion.. 
can only be applied to functions" }
+
+#[rustc_conversion_suggestion] // { dg-warning 
"...rustc_conversion_suggestion.. is not implemented yet and has no effect" }
+struct TO_STRING3; // { dg-error "...rustc_conversion_suggestion.. can only be 
applied to functions" }

base-commit: 770bccda8a9ebe771e6d6ecd21deff9d656433a5
-- 
2.54.0

Reply via email to