From: Jayant Chauhan <[email protected]>
The #[target_feature] attribute allows code generation that may not be
supported by the runtime hardware, making it inherently unsafe. This
patch adds a check to ensure it is only applied to functions declared
as 'unsafe', matching rustc behavior (E0658).
Fixes Rust-GCC#4234
gcc/rust/ChangeLog:
* util/rust-attributes.cc (AttributeChecker::visit):
Reject #[target_feature] on non-unsafe functions.
gcc/testsuite/ChangeLog:
* rust/compile/issue-4234.rs: New test.
* rust/compile/unsafe11.rs: Mark function as unsafe to
to satisfy new #[target_feature] restriction.
Signed-off-by: Jayant Chauhan <[email protected]>
---
gcc/rust/util/rust-attributes.cc | 7 +++++++
gcc/testsuite/rust/compile/issue-4234.rs | 3 +++
gcc/testsuite/rust/compile/unsafe11.rs | 5 +++--
3 files changed, 13 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/rust/compile/issue-4234.rs
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index bf7f3d9827d..a58336ef9e3 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -885,6 +885,13 @@ AttributeChecker::visit (AST::Function &fun)
"must be of the form: %<#[target_feature(enable = "
"\"name\")]%>");
}
+ else if (!fun.get_qualifiers ().is_unsafe ())
+ {
+ rust_error_at (
+ attribute.get_locus (),
+ "the %<#[target_feature]%> attribute can only be applied "
+ "to %<unsafe%> functions");
+ }
}
else if (result.name == "no_mangle")
{
diff --git a/gcc/testsuite/rust/compile/issue-4234.rs
b/gcc/testsuite/rust/compile/issue-4234.rs
new file mode 100644
index 00000000000..bb60e35ace6
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4234.rs
@@ -0,0 +1,3 @@
+// { dg-options "-w" }
+#[target_feature(sse)] // { dg-error "attribute can only be applied" }
+fn foo() {}
\ No newline at end of file
diff --git a/gcc/testsuite/rust/compile/unsafe11.rs
b/gcc/testsuite/rust/compile/unsafe11.rs
index c87902fcd5f..e6657a6d6cb 100644
--- a/gcc/testsuite/rust/compile/unsafe11.rs
+++ b/gcc/testsuite/rust/compile/unsafe11.rs
@@ -1,8 +1,9 @@
#[target_feature(sse)]
-fn foo() {
+unsafe fn foo() {
let a: usize = 0;
}
fn main() {
- foo() // { dg-error "requires unsafe function or block" }
+ foo(); // { dg-error "requires unsafe function or block" }
+ unsafe { foo(); }
}
--
2.50.1