Skip to content

Commit

Permalink
Merge pull request #461 from dtolnay/sizetest
Browse files Browse the repository at this point in the history
Add additional size tests
  • Loading branch information
dtolnay authored May 19, 2024
2 parents abc7dff + 6f894e7 commit 464c2eb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
with:
components: rust-src
- name: Enable type layout randomization
run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout >> $GITHUB_ENV
run: echo RUSTFLAGS=${RUSTFLAGS}\ -Zrandomize-layout\ --cfg=randomize_layout >> $GITHUB_ENV
- run: cargo check
env:
RUSTFLAGS: --cfg procmacro2_nightly_testing ${{env.RUSTFLAGS}}
Expand All @@ -78,6 +78,22 @@ jobs:
env:
RUSTFLAGS: -Z allow-features= --cfg procmacro2_backtrace ${{env.RUSTFLAGS}}

layout:
name: Layout
needs: pre_ci
if: needs.pre_ci.outputs.continue
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
with:
components: rust-src
- run: cargo test --test test_size
- run: cargo test --test test_size --features span-locations
- run: cargo test --test test_size --no-default-features
- run: cargo test --test test_size --no-default-features --features span-locations

msrv:
name: Rust 1.56.0
needs: pre_ci
Expand Down
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn main() {
println!("cargo:rustc-check-cfg=cfg(procmacro2_backtrace)");
println!("cargo:rustc-check-cfg=cfg(procmacro2_nightly_testing)");
println!("cargo:rustc-check-cfg=cfg(procmacro2_semver_exempt)");
println!("cargo:rustc-check-cfg=cfg(randomize_layout)");
println!("cargo:rustc-check-cfg=cfg(span_locations)");
println!("cargo:rustc-check-cfg=cfg(super_unstable)");
println!("cargo:rustc-check-cfg=cfg(wrap_proc_macro)");
Expand Down
49 changes: 40 additions & 9 deletions tests/test_size.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,73 @@
#![cfg(not(randomize_layout))]

extern crate proc_macro;

use std::mem;

#[rustversion::attr(before(1.32), ignore)]
#[rustversion::attr(before(1.64), ignore)]
#[test]
fn test_proc_macro_span_size() {
fn test_proc_macro_size() {
assert_eq!(mem::size_of::<proc_macro::Span>(), 4);
assert_eq!(mem::size_of::<Option<proc_macro::Span>>(), 4);
assert_eq!(mem::size_of::<proc_macro::Group>(), 20);
assert_eq!(mem::size_of::<proc_macro::Ident>(), 12);
assert_eq!(mem::size_of::<proc_macro::Punct>(), 8);
assert_eq!(mem::size_of::<proc_macro::Literal>(), 16);
assert_eq!(mem::size_of::<proc_macro::TokenStream>(), 4);
}

#[cfg_attr(not(all(not(wrap_proc_macro), not(span_locations))), ignore)]
#[test]
fn test_proc_macro2_fallback_span_size_without_locations() {
fn test_proc_macro2_fallback_size_without_locations() {
assert_eq!(mem::size_of::<proc_macro2::Span>(), 0);
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 1);
assert_eq!(mem::size_of::<proc_macro2::Group>(), 16);
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 32);
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 8);
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 24);
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 8);
}

#[cfg_attr(not(all(not(wrap_proc_macro), span_locations)), ignore)]
#[test]
fn test_proc_macro2_fallback_span_size_with_locations() {
fn test_proc_macro2_fallback_size_with_locations() {
assert_eq!(mem::size_of::<proc_macro2::Span>(), 8);
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 12);
assert_eq!(mem::size_of::<proc_macro2::Group>(), 24);
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 40);
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 16);
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 32);
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 8);
}

#[rustversion::attr(before(1.32), ignore)]
#[rustversion::attr(before(1.71), ignore)]
#[rustversion::attr(
since(1.32),
since(1.71),
cfg_attr(not(all(wrap_proc_macro, not(span_locations))), ignore)
)]
#[test]
fn test_proc_macro2_wrapper_span_size_without_locations() {
fn test_proc_macro2_wrapper_size_without_locations() {
assert_eq!(mem::size_of::<proc_macro2::Span>(), 4);
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 8);
assert_eq!(mem::size_of::<proc_macro2::Group>(), 24);
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 32);
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 12);
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 24);
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 32);
}

#[cfg_attr(not(all(wrap_proc_macro, span_locations)), ignore)]
#[rustversion::attr(before(1.65), ignore)]
#[rustversion::attr(
since(1.65),
cfg_attr(not(all(wrap_proc_macro, span_locations)), ignore)
)]
#[test]
fn test_proc_macro2_wrapper_span_size_with_locations() {
fn test_proc_macro2_wrapper_size_with_locations() {
assert_eq!(mem::size_of::<proc_macro2::Span>(), 12);
assert_eq!(mem::size_of::<Option<proc_macro2::Span>>(), 12);
assert_eq!(mem::size_of::<proc_macro2::Group>(), 32);
assert_eq!(mem::size_of::<proc_macro2::Ident>(), 40);
assert_eq!(mem::size_of::<proc_macro2::Punct>(), 20);
assert_eq!(mem::size_of::<proc_macro2::Literal>(), 32);
assert_eq!(mem::size_of::<proc_macro2::TokenStream>(), 32);
}

0 comments on commit 464c2eb

Please sign in to comment.