Skip to content

Commit 212d55f

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): Error if target_feature is on safe function. gcc/testsuite/ChangeLog: * rust/compile/target_feature-unsafe.rs: New test. * rust/compile/unsafe11.rs: Mark function as unsafe to satisfy new check. Signed-off-by: Jayant Chauhan <0001jayant@gmail.com>
1 parent c35ffdb commit 212d55f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

gcc/rust/util/rust-attributes.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,28 @@ AttributeChecker::visit (AST::Function &fun)
866866
}
867867
else if (result.name == "no_mangle")
868868
check_no_mangle_function (attribute, fun);
869+
else if (result.name == Attrs::TARGET_FEATURE)
870+
{
871+
if (!fun.get_qualifiers ().is_unsafe ())
872+
{
873+
rust_error_at (
874+
attribute.get_locus (),
875+
"the %<#[target_feature]%> attribute can only be applied "
876+
"to %<unsafe%> functions");
877+
}
878+
}
879+
else if (result.name == Attrs::ALLOW || result.name == "deny"
880+
|| result.name == "warn" || result.name == "forbid")
881+
{
882+
if (!attribute.has_attr_input ())
883+
{
884+
rust_error_at (attribute.get_locus (),
885+
"malformed %qs attribute input", name);
886+
rust_inform (attribute.get_locus (),
887+
"must be of the form: %<#[%s(lint1, lint2, ...)]%>",
888+
name);
889+
}
890+
}
869891
}
870892
if (fun.has_body ())
871893
fun.get_definition ().value ()->accept_vis (*this);
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() {}

gcc/testsuite/rust/compile/unsafe11.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[target_feature(sse)]
2-
fn foo() {
2+
unsafe fn foo() {
33
let a: usize = 0;
44
}
55

0 commit comments

Comments
 (0)