Skip to content

Commit c54fc00

Browse files
committed
Remove ndebug, add config of debug assertions
This commit removes the ndebug support from Cargo and also adds a new configuration option for profiles, `debug-assertions`, which controls whether debug assertions in the compiler are turned on or not. Closes rust-lang#1398
1 parent c6b9324 commit c54fc00

9 files changed

+34
-35
lines changed

src/cargo/core/manifest.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub struct Profile {
117117
pub lto: bool,
118118
pub codegen_units: Option<u32>, // None = use rustc default
119119
pub debuginfo: bool,
120-
pub ndebug: bool,
120+
pub debug_assertions: bool,
121121
pub rpath: bool,
122122
pub test: bool,
123123
pub doc: bool,
@@ -410,6 +410,7 @@ impl Profile {
410410
pub fn default_dev() -> Profile {
411411
Profile {
412412
debuginfo: true,
413+
debug_assertions: true,
413414
..Profile::default()
414415
}
415416
}
@@ -418,7 +419,6 @@ impl Profile {
418419
Profile {
419420
opt_level: 3,
420421
debuginfo: false,
421-
ndebug: true,
422422
..Profile::default()
423423
}
424424
}
@@ -452,7 +452,7 @@ impl Default for Profile {
452452
lto: false,
453453
codegen_units: None,
454454
debuginfo: false,
455-
ndebug: false,
455+
debug_assertions: false,
456456
rpath: false,
457457
test: false,
458458
doc: false,

src/cargo/ops/cargo_rustc/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ fn build_base_args(cx: &Context,
555555
profile: &Profile,
556556
crate_types: &[&str]) {
557557
let Profile {
558-
opt_level, lto, codegen_units, debuginfo, ndebug, rpath, test,
558+
opt_level, lto, codegen_units, debuginfo, debug_assertions, rpath, test,
559559
doc: _doc,
560560
} = *profile;
561561

@@ -599,8 +599,10 @@ fn build_base_args(cx: &Context,
599599
cmd.arg("-g");
600600
}
601601

602-
if ndebug {
603-
cmd.args(&["--cfg", "ndebug"]);
602+
if debug_assertions && opt_level > 0 {
603+
cmd.args(&["-C", "debug-assertions=on"]);
604+
} else if !debug_assertions && opt_level == 0 {
605+
cmd.args(&["-C", "debug-assertions=off"]);
604606
}
605607

606608
if test && target.harness() {

src/cargo/util/toml.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ pub struct TomlProfile {
231231
lto: Option<bool>,
232232
codegen_units: Option<u32>,
233233
debug: Option<bool>,
234+
debug_assertions: Option<bool>,
234235
rpath: Option<bool>,
235236
}
236237

@@ -813,7 +814,7 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
813814

814815
fn merge(profile: Profile, toml: Option<&TomlProfile>) -> Profile {
815816
let &TomlProfile {
816-
opt_level, lto, codegen_units, debug, rpath
817+
opt_level, lto, codegen_units, debug, debug_assertions, rpath
817818
} = match toml {
818819
Some(toml) => toml,
819820
None => return profile,
@@ -823,7 +824,7 @@ fn build_profiles(profiles: &Option<TomlProfiles>) -> Profiles {
823824
lto: lto.unwrap_or(profile.lto),
824825
codegen_units: codegen_units,
825826
debuginfo: debug.unwrap_or(profile.debuginfo),
826-
ndebug: !debug.unwrap_or(!profile.ndebug),
827+
debug_assertions: debug_assertions.unwrap_or(profile.debug_assertions),
827828
rpath: rpath.unwrap_or(profile.rpath),
828829
test: profile.test,
829830
doc: profile.doc,

src/doc/manifest.md

+5
Original file line numberDiff line numberDiff line change
@@ -164,34 +164,39 @@ opt-level = 0 # Controls the --opt-level the compiler builds with
164164
debug = true # Controls whether the compiler passes -g or `--cfg ndebug`
165165
rpath = false # Controls whether the compiler passes `-C rpath`
166166
lto = false # Controls `-C lto` for binaries and staticlibs
167+
debug-assertions = true # Controls whether debug assertions are enabled
167168

168169
# The release profile, used for `cargo build --release`
169170
[profile.release]
170171
opt-level = 3
171172
debug = false
172173
rpath = false
173174
lto = false
175+
debug-assertions = false
174176

175177
# The testing profile, used for `cargo test`
176178
[profile.test]
177179
opt-level = 0
178180
debug = true
179181
rpath = false
180182
lto = false
183+
debug-assertions = true
181184

182185
# The benchmarking profile, used for `cargo bench`
183186
[profile.bench]
184187
opt-level = 3
185188
debug = false
186189
rpath = false
187190
lto = false
191+
debug-assertions = false
188192

189193
# The documentation profile, used for `cargo doc`
190194
[profile.doc]
191195
opt-level = 0
192196
debug = true
193197
rpath = false
194198
lto = false
199+
debug-assertions = true
195200
```
196201

197202
# The `[features]` Section

tests/test_cargo_bench.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -428,20 +428,17 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
428428
compiling = COMPILING, running = RUNNING,
429429
dir = p.url()).as_slice()));
430430

431-
assert_that(p.cargo_process("bench").arg("foo"),
431+
assert_that(p.cargo("bench").arg("foo"),
432432
execs().with_status(0)
433-
.with_stdout(format!("\
434-
{compiling} foo v0.0.1 ({dir})
433+
.with_stdout(&format!("\
435434
{running} target[..]release[..]foo-[..]
436435
437436
running 1 test
438437
test foo ... bench: 0 ns/iter (+/- 0)
439438
440439
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
441440
442-
",
443-
compiling = COMPILING, running = RUNNING,
444-
dir = p.url()).as_slice()));
441+
", running = RUNNING)));
445442
});
446443

447444
// Regression test for running cargo-bench twice with

tests/test_cargo_compile.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ test!(lto_build {
815815
{running} `rustc src[..]main.rs --crate-name test --crate-type bin \
816816
-C opt-level=3 \
817817
-C lto \
818-
--cfg ndebug \
819818
--out-dir {dir}[..]target[..]release \
820819
--emit=dep-info,link \
821820
-L dependency={dir}[..]target[..]release \
@@ -871,7 +870,6 @@ test!(verbose_release_build {
871870
{compiling} test v0.0.0 ({url})
872871
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib \
873872
-C opt-level=3 \
874-
--cfg ndebug \
875873
-C metadata=[..] \
876874
-C extra-filename=-[..] \
877875
--out-dir {dir}[..]target[..]release \
@@ -917,7 +915,6 @@ test!(verbose_release_build_deps {
917915
{running} `rustc foo[..]src[..]lib.rs --crate-name foo \
918916
--crate-type dylib --crate-type rlib -C prefer-dynamic \
919917
-C opt-level=3 \
920-
--cfg ndebug \
921918
-C metadata=[..] \
922919
-C extra-filename=-[..] \
923920
--out-dir {dir}[..]target[..]release[..]deps \
@@ -927,7 +924,6 @@ test!(verbose_release_build_deps {
927924
{compiling} test v0.0.0 ({url})
928925
{running} `rustc src[..]lib.rs --crate-name test --crate-type lib \
929926
-C opt-level=3 \
930-
--cfg ndebug \
931927
-C metadata=[..] \
932928
-C extra-filename=-[..] \
933929
--out-dir {dir}[..]target[..]release \
@@ -1025,10 +1021,10 @@ test!(standard_build_no_ndebug {
10251021
.file("Cargo.toml", &basic_bin_manifest("foo"))
10261022
.file("src/foo.rs", r#"
10271023
fn main() {
1028-
if cfg!(ndebug) {
1029-
println!("fast")
1030-
} else {
1024+
if cfg!(debug_assertions) {
10311025
println!("slow")
1026+
} else {
1027+
println!("fast")
10321028
}
10331029
}
10341030
"#);
@@ -1043,10 +1039,10 @@ test!(release_build_ndebug {
10431039
.file("Cargo.toml", &basic_bin_manifest("foo"))
10441040
.file("src/foo.rs", r#"
10451041
fn main() {
1046-
if cfg!(ndebug) {
1047-
println!("fast")
1048-
} else {
1042+
if cfg!(debug_assertions) {
10491043
println!("slow")
1044+
} else {
1045+
println!("fast")
10501046
}
10511047
}
10521048
"#);

tests/test_cargo_profiles.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ test!(profile_overrides {
2929
{compiling} test v0.0.0 ({url})
3030
{running} `rustc src{sep}lib.rs --crate-name test --crate-type lib \
3131
-C opt-level=1 \
32-
--cfg ndebug \
32+
-C debug-assertions=on \
3333
-C metadata=[..] \
3434
-C extra-filename=-[..] \
3535
-C rpath \

tests/test_cargo_run.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ test!(example_with_release_flag {
241241
extern crate bar;
242242
243243
fn main() {
244-
if cfg!(ndebug) {
245-
println!("fast1")
246-
} else {
244+
if cfg!(debug_assertions) {
247245
println!("slow1")
246+
} else {
247+
println!("fast1")
248248
}
249249
bar::baz();
250250
}
@@ -260,10 +260,10 @@ test!(example_with_release_flag {
260260
"#)
261261
.file("bar/src/bar.rs", r#"
262262
pub fn baz() {
263-
if cfg!(ndebug) {
264-
println!("fast2")
265-
} else {
263+
if cfg!(debug_assertions) {
266264
println!("slow2")
265+
} else {
266+
println!("fast2")
267267
}
268268
}
269269
"#);
@@ -273,7 +273,6 @@ test!(example_with_release_flag {
273273
{compiling} bar v0.0.1 ({url})
274274
{running} `rustc bar{sep}src{sep}bar.rs --crate-name bar --crate-type lib \
275275
-C opt-level=3 \
276-
--cfg ndebug \
277276
-C metadata=[..] \
278277
-C extra-filename=[..] \
279278
--out-dir {dir}{sep}target{sep}release{sep}deps \
@@ -283,7 +282,6 @@ test!(example_with_release_flag {
283282
{compiling} foo v0.0.1 ({url})
284283
{running} `rustc examples{sep}a.rs --crate-name a --crate-type bin \
285284
-C opt-level=3 \
286-
--cfg ndebug \
287285
--out-dir {dir}{sep}target{sep}release{sep}examples \
288286
--emit=dep-info,link \
289287
-L dependency={dir}{sep}target{sep}release \
@@ -369,7 +367,7 @@ test!(release_works {
369367
authors = []
370368
"#)
371369
.file("src/main.rs", r#"
372-
fn main() { if !cfg!(ndebug) { panic!() } }
370+
fn main() { if cfg!(debug_assertions) { panic!() } }
373371
"#);
374372

375373
assert_that(p.cargo_process("run").arg("--release"),

tests/test_cargo_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ test!(example_bin_same_name {
12551255
assert_that(p.process(&p.bin("examples/foo")),
12561256
execs().with_status(0).with_stdout("example\n"));
12571257

1258-
assert_that(p.cargo_process("run"),
1258+
assert_that(p.cargo("run"),
12591259
execs().with_status(0)
12601260
.with_stdout(format!("\
12611261
{compiling} foo v0.0.1 ([..])

0 commit comments

Comments
 (0)