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 % 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(); } }