about summary refs log tree commit diff
path: root/library/core
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2023-05-20 12:21:00 +0530
committerGitHub <noreply@github.com>2023-05-20 12:21:00 +0530
commite892e32df48f9fc180e085fe0d8a4969525c0130 (patch)
tree2558d635a673efac0474da4b7cc05e9b4cfdc0f1 /library/core
parent13f3585dc6ad4332a171b3ff9a6a3c933241dbb0 (diff)
parent30c0e4e72b85c7d125dd05a071027965e2236d64 (diff)
downloadrust-e892e32df48f9fc180e085fe0d8a4969525c0130.tar.gz
rust-e892e32df48f9fc180e085fe0d8a4969525c0130.zip
Rollup merge of #111665 - est31:offset_of_tests, r=WaffleLapkin
Add more tests for the offset_of macro

Implements what I [suggested in the tracking issue](https://github.com/rust-lang/rust/issues/106655#issuecomment-1535007205), plus some further improvements:

* ensuring that offset_of!(Self, ...) works iff inside an impl block
* ensuring that the output type is usize and doesn't coerce. this can be changed in the future, but if it is done, it should be a conscious decision
* improving the privacy checking test
* ensuring that generics don't let you escape the unsized check

r? `````@WaffleLapkin`````
Diffstat (limited to 'library/core')
-rw-r--r--library/core/tests/mem.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs
index 7f033816901..0a9850146e5 100644
--- a/library/core/tests/mem.rs
+++ b/library/core/tests/mem.rs
@@ -386,6 +386,21 @@ fn offset_of() {
     // Layout of tuples is unstable
     assert!(offset_of!((u8, u16), 0) <= size_of::<(u8, u16)>() - 1);
     assert!(offset_of!((u8, u16), 1) <= size_of::<(u8, u16)>() - 2);
+
+    #[repr(C)]
+    struct Generic<T> {
+        x: u8,
+        y: u32,
+        z: T
+    }
+
+    // Ensure that this type of generics works
+    fn offs_of_z<T>() -> usize {
+        offset_of!(Generic<T>, z)
+    }
+
+    assert_eq!(offset_of!(Generic<u8>, z), 8);
+    assert_eq!(offs_of_z::<u8>(), 8);
 }
 
 #[test]