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]>
---
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/4d60acdace07b0cbed73a86a17e14bca6bef67cd
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4356
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 bf7f3d982..a58336ef9 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 000000000..bb60e35ac
--- /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 c87902fcd..e6657a6d6 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(); }
}
base-commit: 92888487add96854d5ed0033c455d252b5dba991
--
2.52.0