Skip to content

Commit e316c66

Browse files
debuginfo: Add debuginfo::with_source_location_override() function...
... and use it to fix a debug-location issue with constants.
1 parent fed1249 commit e316c66

File tree

4 files changed

+113
-3
lines changed

4 files changed

+113
-3
lines changed

src/librustc_trans/trans/consts.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ struct FunctionDebugContextData {
695695
fn_metadata: DISubprogram,
696696
argument_counter: Cell<uint>,
697697
source_locations_enabled: Cell<bool>,
698+
source_location_override: Cell<bool>,
698699
}
699700

700701
enum VariableAccess<'a> {
@@ -1174,6 +1175,12 @@ pub fn set_source_location(fcx: &FunctionContext,
11741175
return;
11751176
}
11761177
FunctionDebugContext::RegularContext(box ref function_debug_context) => {
1178+
if function_debug_context.source_location_override.get() {
1179+
// Just ignore any attempts to set a new debug location while
1180+
// the override is active.
1181+
return;
1182+
}
1183+
11771184
let cx = fcx.ccx;
11781185

11791186
debug!("set_source_location: {}", cx.sess().codemap().span_to_string(span));
@@ -1192,6 +1199,35 @@ pub fn set_source_location(fcx: &FunctionContext,
11921199
}
11931200
}
11941201

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

14171454

src/librustc_trans/trans/expr.rs

Lines changed: 8 additions & 2 deletions
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());
Lines changed: 67 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)