diff options
| author | Dylan Baker <dylan@pnwbakers.com> | 2023-02-22 13:30:58 -0800 |
|---|---|---|
| committer | Nirbheek Chauhan <nirbheek.chauhan@gmail.com> | 2023-04-21 15:18:56 +0530 |
| commit | c62989ce80c562c655795ffc6fc799b9a048dc59 (patch) | |
| tree | 657bc94a2d4c2061c35a16e506c0c59309807692 | |
| parent | f80f40fa4feb0fa3f365f3debcbe2a43d7d125af (diff) | |
| download | meson-c62989ce80c562c655795ffc6fc799b9a048dc59.tar.gz | |
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.
| -rw-r--r-- | docs/markdown/snippets/rustc-ndebug.md | 6 | ||||
| -rw-r--r-- | mesonbuild/compilers/rust.py | 6 | ||||
| -rw-r--r-- | test cases/rust/1 basic/meson.build | 13 | ||||
| -rw-r--r-- | test cases/rust/1 basic/prog.rs | 1 |
4 files changed, 24 insertions, 2 deletions
diff --git a/docs/markdown/snippets/rustc-ndebug.md b/docs/markdown/snippets/rustc-ndebug.md new file mode 100644 index 000000000..e35386591 --- /dev/null +++ b/docs/markdown/snippets/rustc-ndebug.md @@ -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. diff --git a/mesonbuild/compilers/rust.py b/mesonbuild/compilers/rust.py index 1c43f7a36..3f353e24e 100644 --- a/mesonbuild/compilers/rust.py +++ b/mesonbuild/compilers/rust.py @@ -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): diff --git a/test cases/rust/1 basic/meson.build b/test cases/rust/1 basic/meson.build index 63ad375de..3ba987710 100644 --- a/test cases/rust/1 basic/meson.build +++ b/test cases/rust/1 basic/meson.build @@ -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, +) diff --git a/test cases/rust/1 basic/prog.rs b/test cases/rust/1 basic/prog.rs index f1b3d303d..69d29359c 100644 --- a/test cases/rust/1 basic/prog.rs +++ b/test cases/rust/1 basic/prog.rs @@ -1,4 +1,5 @@ fn main() { let foo = "rust compiler is working"; + debug_assert!(false, "debug_asserts on!"); println!("{}", foo); } |
