| 1 |
// Copyright 2019 The Fuchsia Authors |
| 2 |
// |
| 3 |
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 4 |
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 5 |
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 6 |
// This file may not be copied, modified, or distributed except according to |
| 7 |
// those terms. |
| 8 |
|
| 9 |
#[macro_use] |
| 10 |
extern crate zerocopy_renamed; |
| 11 |
|
| 12 |
#[path = "../include.rs"] |
| 13 |
mod util; |
| 14 |
|
| 15 |
use zerocopy_renamed::{IntoBytes, KnownLayout}; |
| 16 |
|
| 17 |
use self::util::util::AU16; |
| 18 |
|
| 19 |
fn main() {} |
| 20 |
|
| 21 |
// |
| 22 |
// KnownLayout errors |
| 23 |
// |
| 24 |
|
| 25 |
struct NotKnownLayout; |
| 26 |
|
| 27 |
struct NotKnownLayoutDst([u8]); |
| 28 |
|
| 29 |
// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name | |
| 30 |
// -| N | N | N | N | KL00 | |
| 31 |
#[derive(KnownLayout)] |
| 32 |
//~[msrv, stable, nightly]^ ERROR: the size for values of type `[u8]` cannot be known at compilation time |
| 33 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 34 |
struct KL00(u8, NotKnownLayoutDst); |
| 35 |
|
| 36 |
// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name | |
| 37 |
// -| N | N | Y | N | KL02 | |
| 38 |
#[derive(KnownLayout)] |
| 39 |
//~[msrv, stable, nightly]^ ERROR: the size for values of type `[u8]` cannot be known at compilation time |
| 40 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 41 |
struct KL02(u8, [u8]); |
| 42 |
|
| 43 |
// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name | |
| 44 |
// -| Y | N | N | N | KL08 | |
| 45 |
#[derive(KnownLayout)] |
| 46 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotKnownLayoutDst: zerocopy_renamed::KnownLayout` is not satisfied |
| 47 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 48 |
#[repr(C)] |
| 49 |
struct KL08(u8, NotKnownLayoutDst); |
| 50 |
|
| 51 |
// -| `repr(C)`? | generic? | `KnownLayout`? | `Sized`? | Type Name | |
| 52 |
// -| Y | N | N | Y | KL09 | |
| 53 |
#[derive(KnownLayout)] |
| 54 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `NotKnownLayout: zerocopy_renamed::KnownLayout` is not satisfied |
| 55 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 56 |
#[repr(C)] |
| 57 |
struct KL09(NotKnownLayout, NotKnownLayout); |
| 58 |
|
| 59 |
// |
| 60 |
// Immutable errors |
| 61 |
// |
| 62 |
|
| 63 |
#[derive(Immutable)] |
| 64 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `UnsafeCell<()>: zerocopy_renamed::Immutable` is not satisfied |
| 65 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 66 |
struct Immutable1 { |
| 67 |
a: core::cell::UnsafeCell<()>, |
| 68 |
} |
| 69 |
|
| 70 |
#[derive(Immutable)] |
| 71 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `UnsafeCell<u8>: zerocopy_renamed::Immutable` is not satisfied |
| 72 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 73 |
struct Immutable2 { |
| 74 |
a: [core::cell::UnsafeCell<u8>; 0], |
| 75 |
} |
| 76 |
|
| 77 |
// |
| 78 |
// TryFromBytes errors |
| 79 |
// |
| 80 |
|
| 81 |
#[derive(TryFromBytes)] |
| 82 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 83 |
#[repr(packed)] |
| 84 |
struct TryFromBytesPacked { |
| 85 |
//~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type |
| 86 |
foo: AU16, |
| 87 |
} |
| 88 |
|
| 89 |
#[derive(TryFromBytes)] |
| 90 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 91 |
#[repr(packed(1))] |
| 92 |
struct TryFromBytesPackedN { |
| 93 |
//~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type |
| 94 |
foo: AU16, |
| 95 |
} |
| 96 |
|
| 97 |
#[derive(TryFromBytes)] |
| 98 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 99 |
#[repr(C, packed)] |
| 100 |
struct TryFromBytesCPacked { |
| 101 |
//~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type |
| 102 |
foo: AU16, |
| 103 |
} |
| 104 |
|
| 105 |
#[derive(TryFromBytes)] |
| 106 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 107 |
#[repr(C, packed(1))] |
| 108 |
struct TryFromBytesCPackedN { |
| 109 |
//~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type |
| 110 |
foo: AU16, |
| 111 |
} |
| 112 |
|
| 113 |
// |
| 114 |
// IntoBytes errors |
| 115 |
// |
| 116 |
|
| 117 |
// Since `IntoBytes1` has at least one generic parameter, an `IntoBytes` impl is |
| 118 |
// emitted in which each field type is given an `Unaligned` bound. Since `foo`'s |
| 119 |
// type doesn't implement `Unaligned`, this should fail. |
| 120 |
#[derive(IntoBytes)] |
| 121 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `AU16: zerocopy_renamed::Unaligned` is not satisfied |
| 122 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 123 |
#[repr(C)] |
| 124 |
struct IntoBytes1<T> { |
| 125 |
foo: AU16, |
| 126 |
bar: T, |
| 127 |
} |
| 128 |
|
| 129 |
#[derive(IntoBytes)] |
| 130 |
//~[stable, nightly]^ ERROR: `IntoBytes2` has 1 total byte(s) of padding |
| 131 |
//~[msrv]^^ ERROR: the trait bound `(): PaddingFree<IntoBytes2, 1_usize>` is not satisfied |
| 132 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 133 |
#[repr(C)] |
| 134 |
struct IntoBytes2 { |
| 135 |
foo: u8, |
| 136 |
bar: AU16, |
| 137 |
} |
| 138 |
|
| 139 |
#[derive(IntoBytes)] |
| 140 |
//~[stable, nightly]^ ERROR: `IntoBytes3` has 1 total byte(s) of padding |
| 141 |
//~[msrv]^^ ERROR: the trait bound `(): PaddingFree<IntoBytes3, 1_usize>` is not satisfied |
| 142 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 143 |
#[repr(C, packed(2))] |
| 144 |
struct IntoBytes3 { |
| 145 |
foo: u8, |
| 146 |
// We'd prefer to use AU64 here, but you can't use aligned types in |
| 147 |
// packed structs. |
| 148 |
bar: u64, |
| 149 |
} |
| 150 |
|
| 151 |
type SliceU8 = [u8]; |
| 152 |
|
| 153 |
// Padding between `u8` and `SliceU8`. `SliceU8` doesn't syntactically look like |
| 154 |
// a slice, so this case is handled by our `Sized` support. |
| 155 |
// |
| 156 |
// NOTE(#1708): This exists to ensure that our error messages are good when a |
| 157 |
// field is unsized. |
| 158 |
#[derive(IntoBytes)] |
| 159 |
//~[stable, nightly]^ ERROR: the size for values of type `[u8]` cannot be known at compilation time |
| 160 |
//~[msrv]^^ ERROR: the size for values of type `[u8]` cannot be known at compilation time |
| 161 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 162 |
#[repr(C)] |
| 163 |
struct IntoBytes4 { |
| 164 |
a: u8, |
| 165 |
b: SliceU8, |
| 166 |
//~[stable, nightly]^ ERROR: `[u8]` is unsized |
| 167 |
//~[msrv]^^ ERROR: the size for values of type `[u8]` cannot be known at compilation time |
| 168 |
} |
| 169 |
|
| 170 |
// Padding between `u8` and `[u16]`. `[u16]` is syntactically identifiable as a |
| 171 |
// slice, so this case is handled by our `repr(C)` slice DST support. |
| 172 |
#[derive(IntoBytes)] |
| 173 |
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes5, true>` is not satisfied |
| 174 |
//~[stable, nightly]^^ ERROR: `IntoBytes5` has one or more padding bytes |
| 175 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 176 |
#[repr(C)] |
| 177 |
struct IntoBytes5 { |
| 178 |
a: u8, |
| 179 |
b: [u16], |
| 180 |
} |
| 181 |
|
| 182 |
// Trailing padding after `[u8]`. `[u8]` is syntactically identifiable as a |
| 183 |
// slice, so this case is handled by our `repr(C)` slice DST support. |
| 184 |
#[derive(IntoBytes)] |
| 185 |
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes6, true>` is not satisfied |
| 186 |
//~[stable, nightly]^^ ERROR: `IntoBytes6` has one or more padding bytes |
| 187 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 188 |
#[repr(C)] |
| 189 |
struct IntoBytes6 { |
| 190 |
a: u16, |
| 191 |
b: [u8], |
| 192 |
} |
| 193 |
|
| 194 |
// Padding between `u8` and `u16` and also trailing padding after `[u8]`. `[u8]` |
| 195 |
// is syntactically identifiable as a slice, so this case is handled by our |
| 196 |
// `repr(C)` slice DST support. |
| 197 |
#[derive(IntoBytes)] |
| 198 |
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes7, true>` is not satisfied |
| 199 |
//~[stable, nightly]^^ ERROR: `IntoBytes7` has one or more padding bytes |
| 200 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 201 |
#[repr(C)] |
| 202 |
struct IntoBytes7 { |
| 203 |
a: u8, |
| 204 |
b: u16, |
| 205 |
c: [u8], |
| 206 |
} |
| 207 |
|
| 208 |
#[derive(IntoBytes)] |
| 209 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 210 |
#[repr(C, C)] // zerocopy-derive conservatively treats these as conflicting reprs |
| 211 |
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint |
| 212 |
struct IntoBytes8 { |
| 213 |
a: u8, |
| 214 |
} |
| 215 |
|
| 216 |
#[derive(IntoBytes)] |
| 217 |
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout |
| 218 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 219 |
struct IntoBytes9<T> { |
| 220 |
t: T, |
| 221 |
} |
| 222 |
|
| 223 |
#[derive(IntoBytes)] |
| 224 |
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout |
| 225 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 226 |
#[repr(packed(2))] |
| 227 |
struct IntoBytes10<T> { |
| 228 |
t: T, |
| 229 |
} |
| 230 |
|
| 231 |
// `repr(C, packed(2))` can still have padding between heterogeneous fields. |
| 232 |
#[derive(IntoBytes)] |
| 233 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 234 |
#[repr(C, packed(2))] |
| 235 |
struct IntoBytes11<T> { |
| 236 |
byte: u8, |
| 237 |
// For `T = AU16`, this field remains 2-aligned and leaves a padding byte |
| 238 |
// after `byte`. |
| 239 |
t: T, |
| 240 |
} |
| 241 |
|
| 242 |
fn is_into_bytes_11<T: IntoBytes>() { |
| 243 |
if false { |
| 244 |
is_into_bytes_11::<IntoBytes11<AU16>>(); |
| 245 |
//~[stable, nightly]^ ERROR: the trait bound `AU16: zerocopy_renamed::Unaligned` is not satisfied |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// `repr(C, align(2))` is not sufficient to guarantee the layout of this type. |
| 250 |
#[derive(IntoBytes)] |
| 251 |
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout |
| 252 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 253 |
#[repr(C, align(2))] |
| 254 |
struct IntoBytes12<T> { |
| 255 |
t: T, |
| 256 |
} |
| 257 |
|
| 258 |
// NOTE(#3063): This exists to ensure that our analysis for structs with |
| 259 |
// syntactic DSTs accounts for `align(N)`. |
| 260 |
#[derive(IntoBytes)] |
| 261 |
//~[msrv]^ ERROR: the trait bound `(): DynamicPaddingFree<IntoBytes13, true>` is not satisfied |
| 262 |
//~[stable, nightly]^^ ERROR: `IntoBytes13` has one or more padding bytes |
| 263 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 264 |
#[repr(C, align(2))] |
| 265 |
struct IntoBytes13([u8]); |
| 266 |
|
| 267 |
// A homogeneous multi-field optimization must still account for an explicit |
| 268 |
// outer alignment. For `T = u8` and `N = 0`, odd-length tails have trailing |
| 269 |
// padding. |
| 270 |
#[derive(IntoBytes)] |
| 271 |
//~[msrv, stable, nightly]^ ERROR: must have a non-align #[repr(...)] attribute in order to guarantee this type's memory layout |
| 272 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 273 |
#[repr(C, align(2))] |
| 274 |
struct IntoBytes14<T, const N: usize>([T; N], [T]); |
| 275 |
|
| 276 |
// A type captured at a macro invocation site can have the same spelling as a |
| 277 |
// type parameter introduced by the macro while resolving to a different type. |
| 278 |
// The homogeneous `repr(C)` optimization must not treat them as the same type. |
| 279 |
#[cfg(nightly)] |
| 280 |
mod into_bytes_15 { |
| 281 |
use super::IntoBytes; |
| 282 |
|
| 283 |
type T = u8; |
| 284 |
|
| 285 |
zerocopy_derive::__test_hygienically_mixed_into_bytes!(); |
| 286 |
|
| 287 |
fn assert_into_bytes<T: IntoBytes>() { |
| 288 |
if false { |
| 289 |
assert_into_bytes::<IntoBytes15<u8>>(); |
| 290 |
assert_into_bytes::<IntoBytes15<u16>>(); |
| 291 |
//~[nightly]^ ERROR: type mismatch resolving `<u8 as Identity>::Type == u16` |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
// |
| 297 |
// Unaligned errors |
| 298 |
// |
| 299 |
|
| 300 |
#[derive(Unaligned)] |
| 301 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 302 |
#[repr(C, align(2))] |
| 303 |
//~[msrv, stable, nightly]^ ERROR: cannot derive `Unaligned` on type with alignment greater than 1 |
| 304 |
struct Unaligned1; |
| 305 |
|
| 306 |
#[derive(Unaligned)] |
| 307 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 308 |
#[repr(transparent, align(2))] |
| 309 |
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint |
| 310 |
//~[msrv, stable, nightly]^^ ERROR: transparent struct cannot have other repr hints |
| 311 |
struct Unaligned2 { |
| 312 |
foo: u8, |
| 313 |
} |
| 314 |
|
| 315 |
#[derive(Unaligned)] |
| 316 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 317 |
#[repr(packed, align(2))] |
| 318 |
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint |
| 319 |
struct Unaligned3; |
| 320 |
//~[stable, nightly]^ ERROR: type has conflicting packed and align representation hints |
| 321 |
|
| 322 |
#[derive(Unaligned)] |
| 323 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 324 |
#[repr(align(1), align(2))] |
| 325 |
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint |
| 326 |
struct Unaligned4; |
| 327 |
|
| 328 |
#[derive(Unaligned)] |
| 329 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 330 |
#[repr(align(2), align(4))] |
| 331 |
//~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint |
| 332 |
struct Unaligned5; |
| 333 |
|
| 334 |
#[derive(Unaligned)] |
| 335 |
//~[msrv, stable, nightly]^ ERROR: must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment |
| 336 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 337 |
struct Unaligned6; |
| 338 |
|
| 339 |
#[derive(Unaligned)] |
| 340 |
//~[msrv, stable, nightly]^ ERROR: must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment |
| 341 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 342 |
#[repr(packed(2))] |
| 343 |
struct Unaligned7; |
| 344 |
|
| 345 |
// Test the error message emitted when conflicting reprs appear on different |
| 346 |
// lines. On the nightly compiler, this emits a "joint span" that spans both |
| 347 |
// problematic repr token trees and everything in between. |
| 348 |
#[derive(Copy, Clone)] |
| 349 |
#[repr(packed(2), C)] |
| 350 |
//~[nightly]^ ERROR: this conflicts with another representation hint |
| 351 |
#[derive(Unaligned)] |
| 352 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 353 |
#[repr(C, packed(2))] |
| 354 |
//~[msrv, stable]^ ERROR: this conflicts with another representation hint |
| 355 |
struct WeirdReprSpan; |
| 356 |
|
| 357 |
#[derive(SplitAt)] |
| 358 |
//~[msrv, stable, nightly]^ ERROR: the trait bound `SplitAtNotKnownLayout: zerocopy_renamed::KnownLayout` is not satisfied |
| 359 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 360 |
#[repr(C)] |
| 361 |
struct SplitAtNotKnownLayout([u8]); |
| 362 |
|
| 363 |
#[derive(SplitAt, KnownLayout)] |
| 364 |
//~[msrv]^ ERROR: type mismatch resolving `<u8 as zerocopy_renamed::KnownLayout>::PointerMetadata == usize` |
| 365 |
//~[msrv, stable, nightly]^^ ERROR: the trait bound `u8: SplitAt` is not satisfied |
| 366 |
#[zerocopy(crate = "zerocopy_renamed")] |
| 367 |
#[repr(C)] |
| 368 |
struct SplitAtSized(u8); |
| 369 |
|
| 370 |
#[derive(KnownLayout)] |
| 371 |
#[zerocopy(crate = "zerocopy_renamed"typo)] |
| 372 |
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a path as the value |
| 373 |
#[repr(C)] |
| 374 |
struct InvalidCrateSuffix(u8); |
| 375 |
|
| 376 |
#[derive(KnownLayout)] |
| 377 |
#[zerocopy(crate = "Self::foo")] |
| 378 |
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path |
| 379 |
#[repr(C)] |
| 380 |
struct InvalidCrateSelf(u8); |
| 381 |
|
| 382 |
#[derive(KnownLayout)] |
| 383 |
#[zerocopy(crate = "::crate::foo")] |
| 384 |
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path |
| 385 |
#[repr(C)] |
| 386 |
struct InvalidCrateLeadingColonRelative(u8); |
| 387 |
|
| 388 |
#[derive(KnownLayout)] |
| 389 |
#[zerocopy(crate = "foo::crate::bar")] |
| 390 |
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path |
| 391 |
#[repr(C)] |
| 392 |
struct InvalidCrateNonLeadingCrate(u8); |
| 393 |
|
| 394 |
#[derive(KnownLayout)] |
| 395 |
#[zerocopy(crate = "foo::super::bar")] |
| 396 |
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path |
| 397 |
#[repr(C)] |
| 398 |
struct InvalidCrateNonLeadingSuper(u8); |
| 399 |
|
| 400 |
#[derive(KnownLayout)] |
| 401 |
#[zerocopy(crate = "foo::Self::bar")] |
| 402 |
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path |
| 403 |
#[repr(C)] |
| 404 |
struct InvalidCrateMiddleSelf(u8); |
| 405 |
|
| 406 |
#[derive(KnownLayout)] |
| 407 |
#[zerocopy(crate = "foo::self")] |
| 408 |
//~[msrv, stable, nightly]^ ERROR: `crate` attribute requires a valid module path |
| 409 |
#[repr(C)] |
| 410 |
struct InvalidCrateTrailingSelf(u8); |