Skip to content

Commit 221f677

Browse files
authored
Rollup merge of #71336 - ecstatic-morse:check-consts-asm, r=oli-obk
Exhaustively match on `{Statement,Terminator}Kind` during const checking This adds a pre-monomorphization error for inline assembly in a const context as well. r? @oli-obk
2 parents e69141a + 152c065 commit 221f677

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

src/librustc_mir/transform/check_consts/ops.rs

+4
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ impl NonConstOp for IfOrMatch {
147147
}
148148
}
149149

150+
#[derive(Debug)]
151+
pub struct InlineAsm;
152+
impl NonConstOp for InlineAsm {}
153+
150154
#[derive(Debug)]
151155
pub struct LiveDrop;
152156
impl NonConstOp for LiveDrop {

src/librustc_mir/transform/check_consts/validation.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,24 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
478478
StatementKind::Assign(..) | StatementKind::SetDiscriminant { .. } => {
479479
self.super_statement(statement, location);
480480
}
481-
StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _) => {
481+
482+
StatementKind::FakeRead(
483+
FakeReadCause::ForMatchedPlace
484+
| FakeReadCause::ForMatchGuard
485+
| FakeReadCause::ForGuardBinding,
486+
_,
487+
) => {
488+
self.super_statement(statement, location);
482489
self.check_op(ops::IfOrMatch);
483490
}
484-
// FIXME(eddyb) should these really do nothing?
485-
StatementKind::FakeRead(..)
491+
StatementKind::LlvmInlineAsm { .. } => {
492+
self.super_statement(statement, location);
493+
self.check_op(ops::InlineAsm);
494+
}
495+
496+
StatementKind::FakeRead(FakeReadCause::ForLet | FakeReadCause::ForIndex, _)
486497
| StatementKind::StorageLive(_)
487498
| StatementKind::StorageDead(_)
488-
| StatementKind::LlvmInlineAsm { .. }
489499
| StatementKind::Retag { .. }
490500
| StatementKind::AscribeUserType(..)
491501
| StatementKind::Nop => {}
@@ -572,7 +582,19 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
572582
}
573583
}
574584

575-
_ => {}
585+
// FIXME: Some of these are only caught by `min_const_fn`, but should error here
586+
// instead.
587+
TerminatorKind::Abort
588+
| TerminatorKind::Assert { .. }
589+
| TerminatorKind::FalseEdges { .. }
590+
| TerminatorKind::FalseUnwind { .. }
591+
| TerminatorKind::GeneratorDrop
592+
| TerminatorKind::Goto { .. }
593+
| TerminatorKind::Resume
594+
| TerminatorKind::Return
595+
| TerminatorKind::SwitchInt { .. }
596+
| TerminatorKind::Unreachable
597+
| TerminatorKind::Yield { .. } => {}
576598
}
577599
}
578600
}

src/test/ui/consts/inline_asm.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(llvm_asm)]
2+
3+
const _: () = unsafe { llvm_asm!("nop") };
4+
//~^ ERROR contains unimplemented expression type
5+
6+
fn main() {}

src/test/ui/consts/inline_asm.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0019]: constant contains unimplemented expression type
2+
--> $DIR/inline_asm.rs:3:24
3+
|
4+
LL | const _: () = unsafe { llvm_asm!("nop") };
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0019`.

src/test/ui/consts/miri_unleashed/inline_asm.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ static TEST_BAD: () = {
1111
//~^ ERROR could not evaluate static initializer
1212
//~| NOTE in this expansion of llvm_asm!
1313
//~| NOTE inline assembly is not supported
14+
//~| WARN skipping const checks
15+
//~| NOTE in this expansion of llvm_asm!
1416
};
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
warning: skipping const checks
2+
--> $DIR/inline_asm.rs:10:14
3+
|
4+
LL | unsafe { llvm_asm!("xor %eax, %eax" ::: "eax"); }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
8+
19
error[E0080]: could not evaluate static initializer
210
--> $DIR/inline_asm.rs:10:14
311
|
@@ -6,6 +14,6 @@ LL | unsafe { llvm_asm!("xor %eax, %eax" ::: "eax"); }
614
|
715
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
816

9-
error: aborting due to previous error
17+
error: aborting due to previous error; 1 warning emitted
1018

1119
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)