Skip to content

Commit 210d61f

Browse files
committed
Auto merge of #52571 - oli-obk:promotion_abort, r=nagisa
Abort if a promoted fails to be const evaluable and its runtime checks didn't trigger r? @eddyb cc @RalfJung @nagisa cc #49760
2 parents 02b0479 + 233a6e1 commit 210d61f

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
@@ -18,7 +18,7 @@ use rustc_data_structures::indexed_vec::Idx;
1818
use rustc_data_structures::sync::Lrc;
1919

2020
use base;
21-
use common::{CodegenCx, C_null, C_undef, C_usize};
21+
use common::{CodegenCx, C_undef, C_usize};
2222
use builder::{Builder, MemFlags};
2323
use value::Value;
2424
use type_of::LayoutLlvmExt;
@@ -411,7 +411,10 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
411411
.unwrap_or_else(|err| {
412412
match constant.literal {
413413
mir::Literal::Promoted { .. } => {
414-
// FIXME: generate a panic here
414+
// this is unreachable as long as runtime
415+
// and compile-time agree on values
416+
// With floats that won't always be true
417+
// so we generate an abort below
415418
},
416419
mir::Literal::Value { .. } => {
417420
err.report_as_error(
@@ -420,10 +423,12 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
420423
);
421424
},
422425
}
426+
let fnname = bx.cx.get_intrinsic(&("llvm.trap"));
427+
bx.call(fnname, &[], None);
423428
// We've errored, so we don't have to produce working code.
424429
let layout = bx.cx.layout_of(ty);
425430
PlaceRef::new_sized(
426-
C_null(layout.llvm_type(bx.cx).ptr_to()),
431+
C_undef(layout.llvm_type(bx.cx).ptr_to()),
427432
layout,
428433
layout.align,
429434
).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)