-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with value_of / align_of traits (#1089)
We were trying to figure out the values of those properties during runtime, which is not possible. Fix the code to look for the information in the vtable instead.
- Loading branch information
Showing
5 changed files
with
73 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
//! This test case checks the behavior of align_of_val for traits. | ||
#![allow(dead_code)] | ||
|
||
use std::mem::align_of_val; | ||
|
||
trait T {} | ||
|
||
struct A { | ||
id: u128, | ||
} | ||
|
||
impl T for A {} | ||
|
||
#[cfg_attr(kani, kani::proof)] | ||
fn check_align_simple() { | ||
let a = A { id: 0 }; | ||
let t: &dyn T = &a; | ||
assert_eq!(align_of_val(t), 8); | ||
assert_eq!(align_of_val(&t), 8); | ||
} | ||
|
||
trait Wrapper<T: ?Sized> { | ||
fn inner(&self) -> &T; | ||
} | ||
|
||
struct Concrete<T: ?Sized> { | ||
id: i16, | ||
inner: T, | ||
} | ||
|
||
impl<T: ?Sized> Wrapper<T> for Concrete<T> { | ||
fn inner(&self) -> &T { | ||
&self.inner | ||
} | ||
} | ||
|
||
#[cfg_attr(kani, kani::proof)] | ||
fn check_align_inner() { | ||
let val = 10u8; | ||
let conc_wrapper: Concrete<u8> = Concrete { id: 0, inner: val }; | ||
let trait_wrapper = &conc_wrapper as &dyn Wrapper<u8>; | ||
|
||
assert_eq!(align_of_val(conc_wrapper.inner()), 1); // This is the alignment of val. | ||
assert_eq!(align_of_val(&conc_wrapper), 2); // This is the alignment of Concrete. | ||
assert_eq!(align_of_val(trait_wrapper), 2); // This is also the alignment of Concrete. | ||
assert_eq!(align_of_val(&trait_wrapper), 8); // This is the alignment of the fat pointer. | ||
} | ||
|
||
// This can be run with rustc for comparison. | ||
fn main() { | ||
check_align_simple(); | ||
check_align_inner(); | ||
} |
11 changes: 7 additions & 4 deletions
11
...insics/SizeOfVal/fixme_size_of_fat_ptr.rs → ...i/Intrinsics/SizeOfVal/size_of_fat_ptr.rs
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
File renamed without changes.
File renamed without changes.