https://gcc.gnu.org/g:f75da34295b85deb0d219b1ef9e2565e149053e9
commit r16-7776-gf75da34295b85deb0d219b1ef9e2565e149053e9 Author: Jayant Chauhan <[email protected]> Date: Wed Jan 14 02:47:00 2026 +0530 gccrs: util/attributes: Check that #[target_feature] is only used on unsafe functions 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]> Diff: --- 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(-) diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc index bf7f3d9827d6..a58336ef9e3d 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 000000000000..bb60e35ace64 --- /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 c87902fcd5f1..e6657a6d6cbf 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(); } }
