forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rustc: Add a debug_assertions #[cfg] directive
This commit is an implementation of [RFC 563][rfc] which adds a new `cfg(debug_assertions)` directive which is specially recognized and calculated by the compiler. The flag is turned off at any optimization level greater than 1 and may also be explicitly controlled through the `-C debug-assertions` flag. [rfc]: rust-lang/rfcs#563 The `debug_assert!` and `debug_assert_eq!` macros now respect this instead of the `ndebug` variable and `ndebug` no longer holds any meaning to the standard library. Code which was previously relying on `not(ndebug)` to gate expensive code should be updated to rely on `debug_assertions` instead. Closes rust-lang#22492 [breaking-change]
- Loading branch information
1 parent
14f0942
commit eca6dac
Showing
9 changed files
with
96 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
-include ../tools.mk | ||
|
||
all: | ||
$(RUSTC) debug.rs -C debug-assertions=no | ||
$(call RUN,debug) good | ||
$(RUSTC) debug.rs -C opt-level=0 | ||
$(call RUN,debug) bad | ||
$(RUSTC) debug.rs -C opt-level=1 | ||
$(call RUN,debug) good | ||
$(RUSTC) debug.rs -C opt-level=2 | ||
$(call RUN,debug) good | ||
$(RUSTC) debug.rs -C opt-level=3 | ||
$(call RUN,debug) good | ||
$(RUSTC) debug.rs -O | ||
$(call RUN,debug) good | ||
$(RUSTC) debug.rs | ||
$(call RUN,debug) bad | ||
$(RUSTC) debug.rs -C debug-assertions=yes -O | ||
$(call RUN,debug) bad | ||
$(RUSTC) debug.rs -C debug-assertions=yes -C opt-level=1 | ||
$(call RUN,debug) bad |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![deny(warnings)] | ||
|
||
use std::env; | ||
use std::thread; | ||
|
||
fn main() { | ||
let should_fail = env::args().nth(1) == Some("bad".to_string()); | ||
|
||
assert_eq!(thread::spawn(debug_assert_eq).join().is_err(), should_fail); | ||
assert_eq!(thread::spawn(debug_assert).join().is_err(), should_fail); | ||
assert_eq!(thread::spawn(overflow).join().is_err(), should_fail); | ||
} | ||
|
||
fn debug_assert_eq() { | ||
let mut hit1 = false; | ||
let mut hit2 = false; | ||
debug_assert_eq!({ hit1 = true; 1 }, { hit2 = true; 2 }); | ||
assert!(!hit1); | ||
assert!(!hit2); | ||
} | ||
|
||
fn debug_assert() { | ||
let mut hit = false; | ||
debug_assert!({ hit = true; false }); | ||
assert!(!hit); | ||
} | ||
|
||
fn overflow() { | ||
fn add(a: u8, b: u8) -> u8 { a + b } | ||
|
||
add(200u8, 200u8); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters