Skip to content

Commit 116bc6d

Browse files
committed
Check for inline assembly in THIR unsafeck
1 parent 36a4d14 commit 116bc6d

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ impl<'thir, 'tcx> Visitor<'thir, 'tcx> for UnsafetyVisitor<'tcx> {
153153
self.requires_unsafe(expr.span, CallToUnsafeFunction);
154154
}
155155
}
156+
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
157+
self.requires_unsafe(expr.span, UseOfInlineAssembly);
158+
}
156159
_ => {}
157160
}
158161

@@ -194,7 +197,6 @@ impl BodyUnsafety {
194197
#[derive(Clone, Copy, PartialEq)]
195198
enum UnsafeOpKind {
196199
CallToUnsafeFunction,
197-
#[allow(dead_code)] // FIXME
198200
UseOfInlineAssembly,
199201
#[allow(dead_code)] // FIXME
200202
InitializingTypeWith,
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0133]: use of inline assembly is unsafe and requires unsafe function or block
2+
--> $DIR/inline_asm.rs:8:5
3+
|
4+
LL | asm!("nop");
5+
| ^^^^^^^^^^^^ use of inline assembly
6+
|
7+
= note: inline assembly is entirely unchecked and can cause undefined behavior
8+
9+
error[E0133]: use of inline assembly is unsafe and requires unsafe function or block
10+
--> $DIR/inline_asm.rs:9:5
11+
|
12+
LL | llvm_asm!("nop");
13+
| ^^^^^^^^^^^^^^^^^ use of inline assembly
14+
|
15+
= note: inline assembly is entirely unchecked and can cause undefined behavior
16+
= note: this error originates in the macro `llvm_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/unsafe/inline_asm.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
4+
#![feature(llvm_asm)]
5+
#![feature(asm)]
6+
7+
fn main() {
8+
asm!("nop"); //~ ERROR use of inline assembly is unsafe and requires unsafe function or block
9+
llvm_asm!("nop"); //~ ERROR use of inline assembly is unsafe and requires unsafe function or block
10+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0133]: use of inline assembly is unsafe and requires unsafe function or block
2+
--> $DIR/inline_asm.rs:8:5
3+
|
4+
LL | asm!("nop");
5+
| ^^^^^^^^^^^^ use of inline assembly
6+
|
7+
= note: inline assembly is entirely unchecked and can cause undefined behavior
8+
9+
error[E0133]: use of inline assembly is unsafe and requires unsafe function or block
10+
--> $DIR/inline_asm.rs:9:5
11+
|
12+
LL | llvm_asm!("nop");
13+
| ^^^^^^^^^^^^^^^^^ use of inline assembly
14+
|
15+
= note: inline assembly is entirely unchecked and can cause undefined behavior
16+
= note: this error originates in the macro `llvm_asm` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
18+
error: aborting due to 2 previous errors
19+
20+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)