Skip to content

Commit d77fc9f

Browse files
committed
Rollup merge of rust-lang#23074 - michaelwoerister:constants-debug-locs, r=alexcrichton
With this PR in-place constants are handled correctly with respect to debug location assignment. The PR also adds an (unrelated) test case for debug locations in `extern \"C\"` functions. Fixes rust-lang#22432
2 parents c39833e + 215d287 commit d77fc9f

File tree

5 files changed

+181
-3
lines changed

5 files changed

+181
-3
lines changed

src/librustc_trans/trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub fn get_const_expr<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
173173
&**expr
174174
} else {
175175
ccx.sess().span_bug(ref_expr.span,
176-
&format!("get_const_val given non-constant item {}",
176+
&format!("get_const_expr given non-constant item {}",
177177
item.repr(ccx.tcx())));
178178
}
179179
}

src/librustc_trans/trans/debuginfo.rs

+37
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,7 @@ struct FunctionDebugContextData {
697697
fn_metadata: DISubprogram,
698698
argument_counter: Cell<uint>,
699699
source_locations_enabled: Cell<bool>,
700+
source_location_override: Cell<bool>,
700701
}
701702

702703
enum VariableAccess<'a> {
@@ -1176,6 +1177,12 @@ pub fn set_source_location(fcx: &FunctionContext,
11761177
return;
11771178
}
11781179
FunctionDebugContext::RegularContext(box ref function_debug_context) => {
1180+
if function_debug_context.source_location_override.get() {
1181+
// Just ignore any attempts to set a new debug location while
1182+
// the override is active.
1183+
return;
1184+
}
1185+
11791186
let cx = fcx.ccx;
11801187

11811188
debug!("set_source_location: {}", cx.sess().codemap().span_to_string(span));
@@ -1194,6 +1201,35 @@ pub fn set_source_location(fcx: &FunctionContext,
11941201
}
11951202
}
11961203

1204+
/// This function makes sure that all debug locations emitted while executing
1205+
/// `wrapped_function` are set to the given `debug_loc`.
1206+
pub fn with_source_location_override<F, R>(fcx: &FunctionContext,
1207+
debug_loc: DebugLoc,
1208+
wrapped_function: F) -> R
1209+
where F: FnOnce() -> R
1210+
{
1211+
match fcx.debug_context {
1212+
FunctionDebugContext::DebugInfoDisabled => {
1213+
wrapped_function()
1214+
}
1215+
FunctionDebugContext::FunctionWithoutDebugInfo => {
1216+
set_debug_location(fcx.ccx, UnknownLocation);
1217+
wrapped_function()
1218+
}
1219+
FunctionDebugContext::RegularContext(box ref function_debug_context) => {
1220+
if function_debug_context.source_location_override.get() {
1221+
wrapped_function()
1222+
} else {
1223+
debug_loc.apply(fcx);
1224+
function_debug_context.source_location_override.set(true);
1225+
let result = wrapped_function();
1226+
function_debug_context.source_location_override.set(false);
1227+
result
1228+
}
1229+
}
1230+
}
1231+
}
1232+
11971233
/// Clears the current debug location.
11981234
///
11991235
/// Instructions generated hereafter won't be assigned a source location.
@@ -1414,6 +1450,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
14141450
fn_metadata: fn_metadata,
14151451
argument_counter: Cell::new(1),
14161452
source_locations_enabled: Cell::new(false),
1453+
source_location_override: Cell::new(false),
14171454
};
14181455

14191456

src/librustc_trans/trans/expr.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,21 @@ pub fn trans_into<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
147147
ast::ExprPath(..) => {
148148
match bcx.def(expr.id) {
149149
def::DefConst(did) => {
150-
let expr = consts::get_const_expr(bcx.ccx(), did, expr);
150+
let const_expr = consts::get_const_expr(bcx.ccx(), did, expr);
151151
// Temporarily get cleanup scopes out of the way,
152152
// as they require sub-expressions to be contained
153153
// inside the current AST scope.
154154
// These should record no cleanups anyways, `const`
155155
// can't have destructors.
156156
let scopes = mem::replace(&mut *bcx.fcx.scopes.borrow_mut(),
157157
vec![]);
158-
bcx = trans_into(bcx, expr, dest);
158+
// Lock emitted debug locations to the location of
159+
// the constant reference expression.
160+
debuginfo::with_source_location_override(bcx.fcx,
161+
expr.debug_loc(),
162+
|| {
163+
bcx = trans_into(bcx, const_expr, dest)
164+
});
159165
let scopes = mem::replace(&mut *bcx.fcx.scopes.borrow_mut(),
160166
scopes);
161167
assert!(scopes.is_empty());
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2013-2015 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-android: FIXME(#10381)
12+
// min-lldb-version: 310
13+
14+
// compile-flags:-g
15+
16+
#![allow(unused_variables)]
17+
#![allow(dead_code)]
18+
#![omit_gdb_pretty_printer_section]
19+
20+
// This test makes sure that the compiler doesn't crash when trying to assign
21+
// debug locations to const-expressions.
22+
23+
use std::sync::MUTEX_INIT;
24+
use std::cell::UnsafeCell;
25+
26+
const CONSTANT: u64 = 3 + 4;
27+
28+
struct Struct {
29+
a: isize,
30+
b: usize,
31+
}
32+
const STRUCT: Struct = Struct { a: 1, b: 2 };
33+
34+
struct TupleStruct(u32);
35+
const TUPLE_STRUCT: TupleStruct = TupleStruct(4);
36+
37+
enum Enum {
38+
Variant1(char),
39+
Variant2 { a: u8 },
40+
Variant3
41+
}
42+
43+
const VARIANT1: Enum = Enum::Variant1('v');
44+
const VARIANT2: Enum = Enum::Variant2 { a: 2 };
45+
const VARIANT3: Enum = Enum::Variant3;
46+
47+
const STRING: &'static str = "String";
48+
49+
const VEC: [u32; 8] = [0; 8];
50+
51+
const NESTED: (Struct, TupleStruct) = (STRUCT, TUPLE_STRUCT);
52+
53+
const UNSAFE_CELL: UnsafeCell<bool> = UnsafeCell { value: false };
54+
55+
fn main() {
56+
let mut _constant = CONSTANT;
57+
let mut _struct = STRUCT;
58+
let mut _tuple_struct = TUPLE_STRUCT;
59+
let mut _variant1 = VARIANT1;
60+
let mut _variant2 = VARIANT2;
61+
let mut _variant3 = VARIANT3;
62+
let mut _string = STRING;
63+
let mut _vec = VEC;
64+
let mut _nested = NESTED;
65+
let mut _extern = MUTEX_INIT;
66+
let mut _unsafe_cell = UNSAFE_CELL;
67+
}

src/test/debuginfo/extern-c-fn.rs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2013-2014 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+
// min-lldb-version: 310
12+
13+
// compile-flags:-g
14+
15+
// === GDB TESTS ===================================================================================
16+
// gdb-command:run
17+
18+
// gdb-command:print s
19+
// gdb-check:$1 = [...]"abcd"
20+
// gdb-command:print len
21+
// gdb-check:$2 = 20
22+
// gdb-command:print local0
23+
// gdb-check:$3 = 19
24+
// gdb-command:print local1
25+
// gdb-check:$4 = true
26+
// gdb-command:print local2
27+
// gdb-check:$5 = 20.5
28+
29+
// gdb-command:continue
30+
31+
// === LLDB TESTS ==================================================================================
32+
// lldb-command:run
33+
34+
// lldb-command:print len
35+
// lldb-check:[...]$0 = 20
36+
// lldb-command:print local0
37+
// lldb-check:[...]$1 = 19
38+
// lldb-command:print local1
39+
// lldb-check:[...]$2 = true
40+
// lldb-command:print local2
41+
// lldb-check:[...]$3 = 20.5
42+
43+
// lldb-command:continue
44+
45+
#![allow(unused_variables)]
46+
#![allow(dead_code)]
47+
#![omit_gdb_pretty_printer_section]
48+
49+
50+
#[no_mangle]
51+
pub unsafe extern "C" fn fn_with_c_abi(s: *const u8, len: i32) -> i32 {
52+
let local0 = len - 1;
53+
let local1 = len > 2;
54+
let local2 = (len as f64) + 0.5;
55+
56+
zzz(); // #break
57+
58+
return 0;
59+
}
60+
61+
fn main() {
62+
unsafe {
63+
fn_with_c_abi(b"abcd\0".as_ptr(), 20);
64+
}
65+
}
66+
67+
#[inline(never)]
68+
fn zzz() {()}

0 commit comments

Comments
 (0)