Debian Codegraph @

debian/rust-vendor/zerocopy/tests/ui/late-compile-pass.rs

raw
1 // Copyright 2026 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 // These tests cause errors which are generated by a later compilation pass than
10 // the other errors we generate, and so if they're compiled in the same file,
11 // the compiler will never get to that pass, and so we won't get the errors.
12
13 include!("../include.rs");
14
15 use util::AU16;
16 use zerocopy::{transmute, transmute_mut, try_transmute};
17
18 // Although this is not a soundness requirement, we currently require that the
19 // size of the destination type is not smaller than the size of the source type.
20 const TRANSMUTE_DECREASE_SIZE: u8 = transmute!(AU16(0));
21 //~[msrv, stable, nightly]^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
22 //~[stable, nightly]^^ ERROR: transmuting from 2-byte type to 1-byte type: `AU16` -> `u8`
23
24 // `transmute!` does not support transmuting from a smaller type to a larger
25 // one.
26 const TRANSMUTE_INCREASE_SIZE: AU16 = transmute!(0u8);
27 //~[msrv, stable, nightly]^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
28 //~[stable, nightly]^^ ERROR: transmuting from 1-byte type to 2-byte type: `u8` -> `AU16`
29
30 // `transmute!` does not support transmuting from a smaller type to a larger
31 // one.
32 const TRANSMUTE_INCREASE_SIZE_ALLOW_SHRINK: AU16 = transmute!(#![allow(shrink)] 0u8);
33 //~[msrv, stable, nightly]^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
34 //~[stable, nightly]^^ ERROR: transmuting from 1-byte type to 2-byte type: `u8` -> `Transmute<u8, AU16>`
35
36 const ARRAY_OF_U8S: [u8; 2] = [0u8; 2];
37
38 // `transmute_mut!` cannot, generally speaking, be used in const contexts.
39 const TRANSMUTE_MUT_CONST_CONTEXT: &mut [u8; 2] = transmute_mut!(&mut ARRAY_OF_U8S);
40 //~[msrv]^ ERROR: mutable references are not allowed in constants
41 //~[msrv]^^ ERROR: calls in constants are limited to constant functions, tuple structs and tuple variants
42 //~[msrv]^^^ ERROR: calls in constants are limited to constant functions, tuple structs and tuple variants
43 //~[msrv]^^^^ ERROR: temporary value dropped while borrowed
44 //~[stable]^^^^^ ERROR: cannot call non-const method `Wrap::<&mut [u8; 2], &mut [u8; 2]>::transmute_mut_inference_helper` in constants
45 //~[stable]^^^^^^ ERROR: cannot call non-const method `Wrap::<&mut [u8; 2], &mut [u8; 2]>::transmute_mut` in constants
46 //~[nightly]^^^^^^^ ERROR: cannot call non-const method `zerocopy::util::macro_util::Wrap::<&mut [u8; 2], &mut [u8; 2]>::transmute_mut_inference_helper` in constants
47 //~[nightly]^^^^^^^^ ERROR: cannot call non-const method `zerocopy::util::macro_util::Wrap::<&mut [u8; 2], &mut [u8; 2]>::transmute_mut` in constants
48
49 // Should fail because the file is 4 bytes long, not 8.
50 const INCLUDE_VALUE_WRONG_SIZE: u64 = zerocopy::include_value!("../../testdata/include_value/data");
51 //~[msrv, stable, nightly]^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
52 //~[stable, nightly]^^ ERROR: transmuting from 4-byte type to 8-byte type: `[u8; 4]` -> `u64`
53
54 fn main() {
55 // Although this is not a soundness requirement, we currently require that
56 // the size of the destination type is not smaller than the size of the
57 // source type.
58 let _decrease_size: Result<u8, _> = try_transmute!(AU16(0));
59 //~[msrv, stable, nightly]^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
60
61 // `try_transmute!` does not support transmuting from a smaller type to a larger
62 // one.
63 let _increase_size: Result<AU16, _> = try_transmute!(0u8);
64 //~[msrv, stable, nightly]^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
65
66 fn _transmute_mut_increase_lifetime() {
67 let mut x = 0u64;
68 // It is illegal to increase the lifetime scope.
69 let _: &'static mut u64 = zerocopy::transmute_mut!(&mut x);
70 //~[msrv, stable, nightly]^ ERROR: `x` does not live long enough
71 }
72
73 fn _transmute_ref_increase_lifetime() {
74 let x = 0u64;
75 // It is illegal to increase the lifetime scope.
76 let _: &'static u64 = zerocopy::transmute_ref!(&x);
77 //~[msrv, stable, nightly]^ ERROR: `x` does not live long enough
78 }
79 }