Skip to content

Commit 23c54b8

Browse files
committed
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 #4234 gcc/rust/ChangeLog: * util/rust-attributes.cc (AttributeChecker::visit): modified. gcc/testsuite/ChangeLog: * rust/compile/issue-4234.rs: New test. * rust/compile/unsafe11.rs: Mark function as unsafe to match new restriction. Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
1 parent 17231c8 commit 23c54b8

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

gcc/rust/util/rust-attributes.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,16 @@ AttributeChecker::visit (AST::Function &fun)
898898
else
899899
check_no_mangle_function (attribute, fun);
900900
}
901+
else if (result.name == Attrs::TARGET_FEATURE)
902+
{
903+
if (!fun.get_qualifiers ().is_unsafe ())
904+
{
905+
rust_error_at (
906+
attribute.get_locus (),
907+
"the %<#[target_feature]%> attribute can only be applied "
908+
"to %<unsafe%> functions");
909+
}
910+
}
901911
else if (result.name == Attrs::LINK_NAME)
902912
{
903913
if (!attribute.has_attr_input ())
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// { dg-options "-w" }
2+
#[target_feature(enable = "sse2")] // { dg-error "attribute can only be applied to .unsafe. functions" }
3+
fn foo() {}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#[target_feature(sse)]
2-
fn foo() {
2+
unsafe fn foo() {
33
let a: usize = 0;
44
}
55

66
fn main() {
7-
foo() // { dg-error "requires unsafe function or block" }
7+
foo(); // { dg-error "requires unsafe function or block" }
8+
unsafe { foo(); }
89
}

0 commit comments

Comments
 (0)