Skip to content

Commit 4d60acd

Browse files
Pasta-coderP-E-P
authored andcommitted
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): 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 <0001jayant@gmail.com>
1 parent 9288848 commit 4d60acd

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

gcc/rust/util/rust-attributes.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,13 @@ AttributeChecker::visit (AST::Function &fun)
885885
"must be of the form: %<#[target_feature(enable = "
886886
"\"name\")]%>");
887887
}
888+
else if (!fun.get_qualifiers ().is_unsafe ())
889+
{
890+
rust_error_at (
891+
attribute.get_locus (),
892+
"the %<#[target_feature]%> attribute can only be applied "
893+
"to %<unsafe%> functions");
894+
}
888895
}
889896
else if (result.name == "no_mangle")
890897
{
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(sse)] // { dg-error "attribute can only be applied" }
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)