Skip to content

Commit 0aaa819

Browse files
committed
Auto merge of #52624 - oli-obk:promotion_abort_backport, r=nikomatsakis
[beta] Abort instead of UB if promotion fails original PR: #52571 (not beta approved yet) r? @nikomatsakis cc @RalfJung
2 parents 655d22a + 9e621bb commit 0aaa819

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/librustc_codegen_llvm/mir/operand.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::ty::layout::{self, Align, LayoutOf, TyLayout};
1717
use rustc_data_structures::indexed_vec::Idx;
1818

1919
use base;
20-
use common::{self, CodegenCx, C_null, C_undef, C_usize};
20+
use common::{self, CodegenCx, C_undef, C_usize};
2121
use builder::{Builder, MemFlags};
2222
use value::Value;
2323
use type_of::LayoutLlvmExt;
@@ -413,7 +413,10 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
413413
.unwrap_or_else(|err| {
414414
match constant.literal {
415415
mir::Literal::Promoted { .. } => {
416-
// FIXME: generate a panic here
416+
// this is unreachable as long as runtime
417+
// and compile-time agree on values
418+
// With floats that won't always be true
419+
// so we generate an abort below
417420
},
418421
mir::Literal::Value { .. } => {
419422
err.report_as_error(
@@ -422,10 +425,12 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
422425
);
423426
},
424427
}
428+
let fnname = bx.cx.get_intrinsic(&("llvm.trap"));
429+
bx.call(fnname, &[], None);
425430
// We've errored, so we don't have to produce working code.
426431
let layout = bx.cx.layout_of(ty);
427432
PlaceRef::new_sized(
428-
C_null(layout.llvm_type(bx.cx).ptr_to()),
433+
C_undef(layout.llvm_type(bx.cx).ptr_to()),
429434
layout,
430435
layout.align,
431436
).load(bx)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// ignore-wasm32
12+
// ignore-emscripten
13+
14+
#![feature(const_fn)]
15+
#![allow(const_err)]
16+
17+
use std::env;
18+
use std::process::{Command, Stdio};
19+
20+
const fn bar() -> usize { 0 - 1 }
21+
22+
fn foo() {
23+
let _: &'static _ = &bar();
24+
}
25+
26+
fn main() {
27+
let args: Vec<String> = env::args().collect();
28+
if args.len() > 1 && args[1] == "test" {
29+
foo();
30+
return;
31+
}
32+
33+
let mut p = Command::new(&args[0])
34+
.stdout(Stdio::piped())
35+
.stdin(Stdio::piped())
36+
.arg("test").output().unwrap();
37+
assert!(!p.status.success());
38+
}

0 commit comments

Comments
 (0)