Skip to content

codegen "unreachable" for invalid SetDiscriminant #67054

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 4 commits into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions src/librustc_codegen_ssa/mir/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
},
}
// Allow RalfJ to sleep soundly knowing that even refactorings that remove
// the above error (or silence it under some conditions) will not cause UB
// the above error (or silence it under some conditions) will not cause UB.
bx.abort();
// We've errored, so we don't have to produce working code.
// We still have to return an operand but it doesn't matter,
// this code is unreachable.
let ty = self.monomorphize(&constant.literal.ty);
let layout = bx.cx().layout_of(ty);
bx.load_operand(PlaceRef::new_sized(
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_codegen_ssa/mir/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
variant_index: VariantIdx
) {
if self.layout.for_variant(bx.cx(), variant_index).abi.is_uninhabited() {
bx.unreachable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to start a new block, or stop the remaining MIR statements/terminator from being codegen'd.

cc @rkruppe @nagisa

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I made this an abort, also matching what we do for uninhabited returns. But it seems odd to me that we'd give those well-defined "trap" semantics as opposed to exploiting their UB.

return;
}
match self.layout.variants {
Expand Down Expand Up @@ -488,10 +489,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
},
Err(_) => {
// This is unreachable as long as runtime
// and compile-time agree on values
// and compile-time agree perfectly.
// With floats that won't always be true,
// so we generate an abort.
// so we generate a (safe) abort.
bx.abort();
// We still have to return a place but it doesn't matter,
// this code is unreachable.
let llval = bx.cx().const_undef(
bx.cx().type_ptr_to(bx.cx().backend_type(layout))
);
Expand Down
43 changes: 43 additions & 0 deletions src/test/codegen/set-discriminant-invalid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// compile-flags: -C opt-level=0
#![crate_type = "lib"]

pub enum ApiError {}
#[allow(dead_code)]
pub struct TokioError {
b: bool,
}
pub enum Error {
Api {
source: ApiError,
},
Ethereum,
Tokio {
source: TokioError,
},
}
struct Api;
impl IntoError<Error> for Api
{
type Source = ApiError;
// CHECK-LABEL: @into_error
// CHECK: unreachable
// Also check the next two instructions to make sure we do not match against `unreachable`
// elsewhere in the code (e.g., in the closure bode).
// CHECK-NEXT: load
// CHECK-NEXT: ret
#[no_mangle]
fn into_error(self, error: Self::Source) -> Error {
Error::Api {
source: (|v| v)(error),
}
}
}

pub trait IntoError<E>
{
/// The underlying error
type Source;

/// Combine the information to produce the error
fn into_error(self, source: Self::Source) -> E;
}