Skip to content

Exhaustively match on {Statement,Terminator}Kind during const checking #71336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/librustc_mir/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ impl NonConstOp for IfOrMatch {
}
}

#[derive(Debug)]
pub struct InlineAsm;
impl NonConstOp for InlineAsm {}

#[derive(Debug)]
pub struct LiveDrop;
impl NonConstOp for LiveDrop {
Expand Down
32 changes: 27 additions & 5 deletions src/librustc_mir/transform/check_consts/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,14 +478,24 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
StatementKind::Assign(..) | StatementKind::SetDiscriminant { .. } => {
self.super_statement(statement, location);
}
StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _) => {

StatementKind::FakeRead(
FakeReadCause::ForMatchedPlace
| FakeReadCause::ForMatchGuard
| FakeReadCause::ForGuardBinding,
_,
) => {
self.super_statement(statement, location);
self.check_op(ops::IfOrMatch);
}
// FIXME(eddyb) should these really do nothing?
StatementKind::FakeRead(..)
StatementKind::LlvmInlineAsm { .. } => {
self.super_statement(statement, location);
self.check_op(ops::InlineAsm);
}

StatementKind::FakeRead(FakeReadCause::ForLet | FakeReadCause::ForIndex, _)
| StatementKind::StorageLive(_)
| StatementKind::StorageDead(_)
| StatementKind::LlvmInlineAsm { .. }
| StatementKind::Retag { .. }
| StatementKind::AscribeUserType(..)
| StatementKind::Nop => {}
Expand Down Expand Up @@ -572,7 +582,19 @@ impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
}
}

_ => {}
// FIXME: Some of these are only caught by `min_const_fn`, but should error here
// instead.
TerminatorKind::Abort
| TerminatorKind::Assert { .. }
| TerminatorKind::FalseEdges { .. }
| TerminatorKind::FalseUnwind { .. }
| TerminatorKind::GeneratorDrop
| TerminatorKind::Goto { .. }
| TerminatorKind::Resume
| TerminatorKind::Return
| TerminatorKind::SwitchInt { .. }
| TerminatorKind::Unreachable
| TerminatorKind::Yield { .. } => {}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/consts/inline_asm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(llvm_asm)]

const _: () = unsafe { llvm_asm!("nop") };
//~^ ERROR contains unimplemented expression type

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/consts/inline_asm.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0019]: constant contains unimplemented expression type
--> $DIR/inline_asm.rs:3:24
|
LL | const _: () = unsafe { llvm_asm!("nop") };
| ^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0019`.
2 changes: 2 additions & 0 deletions src/test/ui/consts/miri_unleashed/inline_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ static TEST_BAD: () = {
//~^ ERROR could not evaluate static initializer
//~| NOTE in this expansion of llvm_asm!
//~| NOTE inline assembly is not supported
//~| WARN skipping const checks
//~| NOTE in this expansion of llvm_asm!
};
10 changes: 9 additions & 1 deletion src/test/ui/consts/miri_unleashed/inline_asm.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
warning: skipping const checks
--> $DIR/inline_asm.rs:10:14
|
LL | unsafe { llvm_asm!("xor %eax, %eax" ::: "eax"); }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: could not evaluate static initializer
--> $DIR/inline_asm.rs:10:14
|
Expand All @@ -6,6 +14,6 @@ LL | unsafe { llvm_asm!("xor %eax, %eax" ::: "eax"); }
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error
error: aborting due to previous error; 1 warning emitted

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