-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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](#106655 (comment)), 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`
- Loading branch information
Showing
9 changed files
with
345 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![feature(offset_of)] | ||
|
||
use std::mem::offset_of; | ||
|
||
struct S { | ||
v: u8, | ||
w: u16, | ||
} | ||
|
||
|
||
fn main() { | ||
let _: u8 = offset_of!(S, v); //~ ERROR mismatched types | ||
let _: u16 = offset_of!(S, v); //~ ERROR mismatched types | ||
let _: u32 = offset_of!(S, v); //~ ERROR mismatched types | ||
let _: u64 = offset_of!(S, v); //~ ERROR mismatched types | ||
let _: isize = offset_of!(S, v); //~ ERROR mismatched types | ||
let _: usize = offset_of!(S, v); | ||
|
||
offset_of!(S, v) //~ ERROR mismatched types | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/offset-of-output-type.rs:12:17 | ||
| | ||
LL | let _: u8 = offset_of!(S, v); | ||
| -- ^^^^^^^^^^^^^^^^ expected `u8`, found `usize` | ||
| | | ||
| expected due to this | ||
| | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/offset-of-output-type.rs:13:18 | ||
| | ||
LL | let _: u16 = offset_of!(S, v); | ||
| --- ^^^^^^^^^^^^^^^^ expected `u16`, found `usize` | ||
| | | ||
| expected due to this | ||
| | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/offset-of-output-type.rs:14:18 | ||
| | ||
LL | let _: u32 = offset_of!(S, v); | ||
| --- ^^^^^^^^^^^^^^^^ expected `u32`, found `usize` | ||
| | | ||
| expected due to this | ||
| | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/offset-of-output-type.rs:15:18 | ||
| | ||
LL | let _: u64 = offset_of!(S, v); | ||
| --- ^^^^^^^^^^^^^^^^ expected `u64`, found `usize` | ||
| | | ||
| expected due to this | ||
| | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/offset-of-output-type.rs:16:20 | ||
| | ||
LL | let _: isize = offset_of!(S, v); | ||
| ----- ^^^^^^^^^^^^^^^^ expected `isize`, found `usize` | ||
| | | ||
| expected due to this | ||
| | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/offset-of-output-type.rs:19:5 | ||
| | ||
LL | fn main() { | ||
| - expected `()` because of default return type | ||
... | ||
LL | offset_of!(S, v) | ||
| ^^^^^^^^^^^^^^^^ expected `()`, found `usize` | ||
| | ||
= note: this error originates in the macro `offset_of` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,46 @@ | ||
error[E0603]: struct `Bar` is private | ||
--> $DIR/offset-of-private.rs:25:19 | ||
| | ||
LL | offset_of!(m::Bar, public); | ||
| ^^^ private struct | ||
| | ||
note: the struct `Bar` is defined here | ||
--> $DIR/offset-of-private.rs:14:5 | ||
| | ||
LL | struct Bar { | ||
| ^^^^^^^^^^ | ||
|
||
error[E0603]: struct `Bar` is private | ||
--> $DIR/offset-of-private.rs:26:19 | ||
| | ||
LL | offset_of!(m::Bar, private); | ||
| ^^^ private struct | ||
| | ||
note: the struct `Bar` is defined here | ||
--> $DIR/offset-of-private.rs:14:5 | ||
| | ||
LL | struct Bar { | ||
| ^^^^^^^^^^ | ||
|
||
error[E0616]: field `private` of struct `Foo` is private | ||
--> $DIR/offset-of-private.rs:15:24 | ||
--> $DIR/offset-of-private.rs:22:24 | ||
| | ||
LL | offset_of!(m::Foo, private); | ||
| ^^^^^^^ private field | ||
|
||
error: aborting due to previous error | ||
error[E0616]: field `1` of struct `FooTuple` is private | ||
--> $DIR/offset-of-private.rs:24:29 | ||
| | ||
LL | offset_of!(m::FooTuple, 1); | ||
| ^ private field | ||
|
||
error[E0616]: field `private` of struct `Bar` is private | ||
--> $DIR/offset-of-private.rs:26:24 | ||
| | ||
LL | offset_of!(m::Bar, private); | ||
| ^^^^^^^ private field | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0616`. | ||
Some errors have detailed explanations: E0603, E0616. | ||
For more information about an error, try `rustc --explain E0603`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#![feature(offset_of)] | ||
|
||
use std::mem::offset_of; | ||
|
||
struct C<T> { | ||
v: T, | ||
w: T, | ||
} | ||
|
||
struct S { | ||
v: u8, | ||
w: u16, | ||
} | ||
|
||
impl S { | ||
fn v_offs() -> usize { | ||
offset_of!(Self, v) | ||
} | ||
fn v_offs_wrong_syntax() { | ||
offset_of!(Self, Self::v); //~ ERROR no rules expected the token `::` | ||
offset_of!(S, Self); //~ ERROR expected identifier, found keyword `Self` | ||
//~| no field `Self` on type `S` | ||
} | ||
fn offs_in_c() -> usize { | ||
offset_of!(C<Self>, w) | ||
} | ||
fn offs_in_c_colon() -> usize { | ||
offset_of!(C::<Self>, w) | ||
} | ||
} | ||
|
||
mod m { | ||
use std::mem::offset_of; | ||
fn off() { | ||
offset_of!(self::S, v); //~ ERROR cannot find type `S` in module | ||
offset_of!(super::S, v); | ||
offset_of!(crate::S, v); | ||
} | ||
impl super::n::T { | ||
fn v_offs_self() -> usize { | ||
offset_of!(Self, v) //~ ERROR field `v` of struct `T` is private | ||
} | ||
} | ||
} | ||
|
||
mod n { | ||
pub struct T { v: u8, } | ||
} | ||
|
||
fn main() { | ||
offset_of!(self::S, v); | ||
offset_of!(Self, v); //~ ERROR cannot find type `Self` in this scope | ||
|
||
offset_of!(S, self); //~ ERROR expected identifier, found keyword `self` | ||
//~| no field `self` on type `S` | ||
offset_of!(S, v.self); //~ ERROR expected identifier, found keyword `self` | ||
//~| no field `self` on type `u8` | ||
} |
Oops, something went wrong.