rust: add support for b_ndebug

Rust has a `debug_assert!()` macro, which is designed to be toggled on
the command line. It is on by default in debug builds, and off by
default in release builds, in cargo. This matches what meson's b_ndebug
option does in `if-release` mode.
pull/11716/head
Dylan Baker 2 years ago committed by Nirbheek Chauhan
parent f80f40fa4f
commit c62989ce80
  1. 6
      docs/markdown/snippets/rustc-ndebug.md
  2. 6
      mesonbuild/compilers/rust.py
  3. 13
      test cases/rust/1 basic/meson.build
  4. 1
      test cases/rust/1 basic/prog.rs

@ -0,0 +1,6 @@
## Rust now supports the b_ndebug option
Which controls the `debug_assertions` cfg, which in turn controls
`debug_assert!()` macro. This macro is roughly equivalent to C's `assert()`, as
it can be toggled with command line options, unlike Rust's `assert!()`, which
cannot be turned off, and is not designed to be.

@ -63,7 +63,7 @@ class RustCompiler(Compiler):
is_cross=is_cross, full_version=full_version,
linker=linker)
self.exe_wrapper = exe_wrapper
self.base_options.add(OptionKey('b_colorout'))
self.base_options.update({OptionKey(o) for o in ['b_colorout', 'b_ndebug']})
if 'link' in self.linker.id:
self.base_options.add(OptionKey('b_vscrt'))
@ -204,6 +204,10 @@ class RustCompiler(Compiler):
# pic is on by rustc
return []
def get_assert_args(self, disable: bool) -> T.List[str]:
action = "no" if disable else "yes"
return ['-C', f'debug-assertions={action}']
class ClippyRustCompiler(RustCompiler):

@ -1,4 +1,4 @@
project('rustprog', 'rust')
project('rustprog', 'rust', default_options : ['b_ndebug=true'])
e = executable('rust-program', 'prog.rs',
rust_args : ['-C', 'lto'], # Just a test
@ -7,3 +7,14 @@ e = executable('rust-program', 'prog.rs',
test('rusttest', e)
subdir('subdir')
# this should fail due to debug_assert
test(
'debug_assert_on',
executable(
'rust-program2',
'prog.rs',
override_options : ['b_ndebug=false'],
),
should_fail : true,
)

@ -1,4 +1,5 @@
fn main() {
let foo = "rust compiler is working";
debug_assert!(false, "debug_asserts on!");
println!("{}", foo);
}

Loading…
Cancel
Save