summaryrefslogtreecommitdiff
path: root/yjit/src/utils.rs
diff options
context:
space:
mode:
authorAlan Wu <XrXr@users.noreply.github.com>2022-04-29 18:20:23 -0400
committerGitHub <noreply@github.com>2022-04-29 18:20:23 -0400
commit5c843a1a6e24aeabb3497065a362caf7b3e2d3b1 (patch)
tree3933db10caabd1ac0f527f8a2e6e7c0d84f63611 /yjit/src/utils.rs
parent7c039e423cb59c9e5d76df9f1dc1bf8b1a1b9a6b (diff)
YJIT: Enable default rustc lints (warnings) (#5864)
`rustc` performs in depth dead code analysis and issues warning even for things like unused struct fields and unconstructed enum variants. This was annoying for us during the port but hopefully they are less of an issue now. This patch enables all the unused warnings we disabled and address all the warnings we previously ignored. Generally, the approach I've taken is to use `cfg!` instead of using the `cfg` attribute and to delete code where it makes sense. I've put `#[allow(unused)]` on things we intentionally keep around for printf style debugging and on items that are too annoying to keep warning-free in all build configs.
Notes
Notes: Merged-By: maximecb <maximecb@ruby-lang.org>
Diffstat (limited to 'yjit/src/utils.rs')
-rw-r--r--yjit/src/utils.rs35
1 files changed, 34 insertions, 1 deletions
diff --git a/yjit/src/utils.rs b/yjit/src/utils.rs
index 227e3e5f32..02fbce47d8 100644
--- a/yjit/src/utils.rs
+++ b/yjit/src/utils.rs
@@ -1,3 +1,5 @@
+#![allow(dead_code)] // Some functions for print debugging in here
+
use crate::asm::x86_64::*;
use crate::asm::*;
use crate::cruby::*;
@@ -50,6 +52,25 @@ impl IntoUsize for u8 {
}
}
+/// Compute an offset in bytes of a given struct field
+#[allow(unused)]
+macro_rules! offset_of {
+ ($struct_type:ty, $field_name:tt) => {{
+ // This is basically the exact example for
+ // "creating a pointer to uninitialized data" from `std::ptr::addr_of_mut`.
+ // We make a dummy local that hopefully is optimized away because we never
+ // read or write its contents. Doing this dance to avoid UB.
+ let mut instance = std::mem::MaybeUninit::<$struct_type>::uninit();
+
+ let base_ptr = instance.as_mut_ptr();
+ let field_ptr = unsafe { std::ptr::addr_of_mut!((*base_ptr).$field_name) };
+
+ (field_ptr as usize) - (base_ptr as usize)
+ }};
+}
+#[allow(unused)]
+pub(crate) use offset_of;
+
#[cfg(test)]
mod tests {
#[test]
@@ -66,6 +87,18 @@ mod tests {
let max: usize = u32::MAX.as_usize();
assert_eq!(max, u32::MAX.try_into().unwrap());
}
+
+ #[test]
+ fn test_offset_of() {
+ #[repr(C)]
+ struct Foo {
+ a: u8,
+ b: u64,
+ }
+
+ assert_eq!(0, offset_of!(Foo, a), "C99 6.7.2.1p13 says no padding at the front");
+ assert_eq!(8, offset_of!(Foo, b), "ABI dependent, but should hold");
+ }
}
// TODO: we may want to move this function into yjit.c, maybe add a convenient Rust-side wrapper
@@ -170,7 +203,7 @@ pub fn print_value(cb: &mut CodeBlock, opnd: X86Opnd) {
pop_regs(cb);
}
-// Generate code to print constant string to stdout
+/// Generate code to print constant string to stdout
pub fn print_str(cb: &mut CodeBlock, str: &str) {
extern "sysv64" fn print_str_cfun(ptr: *const u8, num_bytes: usize) {
unsafe {