about summary refs log tree commit diff
path: root/src/test/ui/asm
diff options
context:
space:
mode:
authorTomasz Miąsko <tomasz.miasko@gmail.com>2020-11-24 00:00:00 +0000
committerTomasz Miąsko <tomasz.miasko@gmail.com>2020-11-24 00:00:00 +0000
commit75e00e8cf40faafbe8a19d2f35df66c0ca681bcb (patch)
tree298e54204ee336bb9cdb9da75ba37548053696c4 /src/test/ui/asm
parentbdc1d9774b7f963e87c3ca1946e00a44d5bdbfe5 (diff)
downloadrust-75e00e8cf40faafbe8a19d2f35df66c0ca681bcb.tar.gz
rust-75e00e8cf40faafbe8a19d2f35df66c0ca681bcb.zip
Validate that `#[naked]` is applied to a function definition
Diffstat (limited to 'src/test/ui/asm')
-rw-r--r--src/test/ui/asm/naked-invalid-attr.rs51
-rw-r--r--src/test/ui/asm/naked-invalid-attr.stderr42
2 files changed, 93 insertions, 0 deletions
diff --git a/src/test/ui/asm/naked-invalid-attr.rs b/src/test/ui/asm/naked-invalid-attr.rs
new file mode 100644
index 00000000000..cdb6c17454b
--- /dev/null
+++ b/src/test/ui/asm/naked-invalid-attr.rs
@@ -0,0 +1,51 @@
+// Checks that #[naked] attribute can be placed on function definitions only.
+//
+// ignore-wasm32 asm unsupported
+#![feature(asm)]
+#![feature(naked_functions)]
+#![naked] //~ ERROR should be applied to a function definition
+
+extern "C" {
+    #[naked] //~ ERROR should be applied to a function definition
+    fn f();
+}
+
+#[naked] //~ ERROR should be applied to a function definition
+#[repr(C)]
+struct S {
+    a: u32,
+    b: u32,
+}
+
+trait Invoke {
+    #[naked] //~ ERROR should be applied to a function definition
+    extern "C" fn invoke(&self);
+}
+
+impl Invoke for S {
+    #[naked]
+    extern "C" fn invoke(&self) {
+        unsafe { asm!("", options(noreturn)) }
+    }
+}
+
+#[naked]
+extern "C" fn ok() {
+    unsafe { asm!("", options(noreturn)) }
+}
+
+impl S {
+    #[naked]
+    extern "C" fn g() {
+        unsafe { asm!("", options(noreturn)) }
+    }
+
+    #[naked]
+    extern "C" fn h(&self) {
+        unsafe { asm!("", options(noreturn)) }
+    }
+}
+
+fn main() {
+    #[naked] || {}; //~ ERROR should be applied to a function definition
+}
diff --git a/src/test/ui/asm/naked-invalid-attr.stderr b/src/test/ui/asm/naked-invalid-attr.stderr
new file mode 100644
index 00000000000..beaa34140c9
--- /dev/null
+++ b/src/test/ui/asm/naked-invalid-attr.stderr
@@ -0,0 +1,42 @@
+error: attribute should be applied to a function definition
+  --> $DIR/naked-invalid-attr.rs:9:5
+   |
+LL |     #[naked]
+   |     ^^^^^^^^
+LL |     fn f();
+   |     ------- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/naked-invalid-attr.rs:13:1
+   |
+LL |   #[naked]
+   |   ^^^^^^^^
+LL |   #[repr(C)]
+LL | / struct S {
+LL | |     a: u32,
+LL | |     b: u32,
+LL | | }
+   | |_- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/naked-invalid-attr.rs:50:5
+   |
+LL |     #[naked] || {};
+   |     ^^^^^^^^ ----- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/naked-invalid-attr.rs:21:5
+   |
+LL |     #[naked]
+   |     ^^^^^^^^
+LL |     extern "C" fn invoke(&self);
+   |     ---------------------------- not a function definition
+
+error: attribute should be applied to a function definition
+  --> $DIR/naked-invalid-attr.rs:6:1
+   |
+LL | #![naked]
+   | ^^^^^^^^^
+
+error: aborting due to 5 previous errors
+