]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #22980 - alexcrichton:debug-assertions, r=pnkfelix
authorManish Goregaokar <manishsmail@gmail.com>
Fri, 6 Mar 2015 03:28:30 +0000 (08:58 +0530)
committerManish Goregaokar <manishsmail@gmail.com>
Fri, 6 Mar 2015 03:28:30 +0000 (08:58 +0530)
 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]: https://github.com/rust-lang/rfcs/pull/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 #22492
[breaking-change]

1  2 
src/libcore/macros.rs
src/librustc_trans/trans/base.rs

diff --combined src/libcore/macros.rs
index f56206e8782d2e739075500fbc25e717f62d879e,96c9beece1c021af4a4158c9e596aec1f42e1069..c2860d435114f64329d48c30daae5224a4c7b4cf
@@@ -100,10 -100,12 +100,12 @@@ macro_rules! assert_eq 
  /// This will invoke the `panic!` macro if the provided expression cannot be
  /// evaluated to `true` at runtime.
  ///
- /// Unlike `assert!`, `debug_assert!` statements can be disabled by passing
- /// `--cfg ndebug` to the compiler. This makes `debug_assert!` useful for
- /// checks that are too expensive to be present in a release build but may be
- /// helpful during development.
+ /// Unlike `assert!`, `debug_assert!` statements are only enabled in non
+ /// optimized builds by default. An optimized build will omit all
+ /// `debug_assert!` statements unless `-C debug-assertions` is passed to the
+ /// compiler. This makes `debug_assert!` useful for checks that are too
+ /// expensive to be present in a release build but may be helpful during
+ /// development.
  ///
  /// # Example
  ///
  #[macro_export]
  #[stable(feature = "rust1", since = "1.0.0")]
  macro_rules! debug_assert {
-     ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
+     ($($arg:tt)*) => (if cfg!(debug_assertions) { assert!($($arg)*); })
  }
  
  /// Asserts that two expressions are equal to each other, testing equality in
  ///
  /// On panic, this macro will print the values of the expressions.
  ///
- /// Unlike `assert_eq!`, `debug_assert_eq!` statements can be disabled by
- /// passing `--cfg ndebug` to the compiler. This makes `debug_assert_eq!`
- /// useful for checks that are too expensive to be present in a release build
- /// but may be helpful during development.
+ /// Unlike `assert_eq!`, `debug_assert_eq!` statements are only enabled in non
+ /// optimized builds by default. An optimized build will omit all
+ /// `debug_assert_eq!` statements unless `-C debug-assertions` is passed to the
+ /// compiler. This makes `debug_assert_eq!` useful for checks that are too
+ /// expensive to be present in a release build but may be helpful during
+ /// development.
  ///
  /// # Example
  ///
  /// ```
  #[macro_export]
  macro_rules! debug_assert_eq {
-     ($($arg:tt)*) => (if cfg!(not(ndebug)) { assert_eq!($($arg)*); })
+     ($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
  }
  
  /// Short circuiting evaluation on Err
@@@ -227,7 -231,7 +231,7 @@@ macro_rules! writeln 
  ///
  /// ```rust
  /// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
 -///     for i in std::iter::count(0_u32, 1) {
 +///     for i in std::iter::count(0, 1) {
  ///         if 3*i < i { panic!("u32 overflow"); }
  ///         if x < 3*i { return i-1; }
  ///     }
index 8302d0abe214e7cb5ddfc6a0a02ce7fa49aedbb9,cf4d0e543e3767d99263823be2b667d5012ae795..f49905613d24cbd2a241880971d395022b2dd4a8
@@@ -833,11 -833,11 +833,11 @@@ pub fn fail_if_zero_or_overflows<'blk, 
  
      let (is_zero, is_signed) = match rhs_t.sty {
          ty::ty_int(t) => {
 -            let zero = C_integral(Type::int_from_ty(cx.ccx(), t), 0u64, false);
 +            let zero = C_integral(Type::int_from_ty(cx.ccx(), t), 0, false);
              (ICmp(cx, llvm::IntEQ, rhs, zero, debug_loc), true)
          }
          ty::ty_uint(t) => {
 -            let zero = C_integral(Type::uint_from_ty(cx.ccx(), t), 0u64, false);
 +            let zero = C_integral(Type::uint_from_ty(cx.ccx(), t), 0, false);
              (ICmp(cx, llvm::IntEQ, rhs, zero, debug_loc), false)
          }
          _ => {
@@@ -3089,7 -3089,7 +3089,7 @@@ pub fn trans_crate<'tcx>(analysis: ty::
      let check_overflow = if let Some(v) = tcx.sess.opts.debugging_opts.force_overflow_checks {
          v
      } else {
-         !attr::contains_name(&krate.config, "ndebug")
+         tcx.sess.opts.debug_assertions
      };
  
      // Before we touch LLVM, make sure that multithreading is enabled.