blob: 5938387a05c21a14ad50836d5e6ef2cb6a6301f1 [file] [log] [blame]
Lukasz Anforowicz13cf7492021-12-22 15:29:52 +00001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#[macro_use]
6extern crate static_assertions;
7
8#[cfg(test)]
9mod tests {
10 use constructors::*;
Lukasz Anforowicz40c2eb82022-01-11 18:22:31 +000011 use elided_lifetimes::*;
Lukasz Anforowicz13cf7492021-12-22 15:29:52 +000012
13 #[test]
14 fn test_user_provided_constructors() {
Lukasz Anforowicz73326af2022-01-05 01:13:10 +000015 assert_impl_all!(StructWithUserProvidedConstructors: Default);
Lukasz Anforowicz73326af2022-01-05 01:13:10 +000016 let s: StructWithUserProvidedConstructors = Default::default();
Lukasz Anforowicz13cf7492021-12-22 15:29:52 +000017 assert_eq!(42, s.int_field);
Lukasz Anforowicz73326af2022-01-05 01:13:10 +000018
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000019 // TODO(lukasza): Implement and test a user-defined copy constructor / impl
20 // Clone.
21
22 // Trivial-ABI structs implement the Copy trait, even if they have user-defined
23 // constructors.
24 assert_impl_all!(StructWithUserProvidedConstructors: Copy);
25
26 assert_impl_all!(StructWithUserProvidedConstructors: From<i32>);
Lukasz Anforowicz73326af2022-01-05 01:13:10 +000027 let i: StructWithUserProvidedConstructors = 123.into();
28 assert_eq!(123, i.int_field);
Lukasz Anforowicz13cf7492021-12-22 15:29:52 +000029 }
Lukasz Anforowicz74704712021-12-22 15:30:31 +000030
31 #[test]
Lukasz Anforowicz7b0042d2022-01-06 23:00:19 +000032 fn test_inline_constructors() {
33 assert_impl_all!(StructWithInlineConstructors: Default);
34 let s: StructWithInlineConstructors = Default::default();
35 assert_eq!(123, s.int_field);
36
37 // TODO(lukasza): Implement and test a user-defined copy constructor / impl
38 // Clone.
39
40 // Trivial-ABI structs implement the Copy trait, even if they have user-defined
41 // constructors.
42 assert_impl_all!(StructWithUserProvidedConstructors: Copy);
Lukasz Anforowicz40c2eb82022-01-11 18:22:31 +000043 let s_copy = s;
44 assert_eq!(123, s_copy.int_field);
Lukasz Anforowicz7b0042d2022-01-06 23:00:19 +000045
46 assert_impl_all!(StructWithInlineConstructors: From<i32>);
47 let i: StructWithInlineConstructors = 456.into();
48 assert_eq!(456, i.int_field);
49 }
50
51 #[test]
Lukasz Anforowicz0a1b4802021-12-22 15:30:56 +000052 fn test_deleted_constructors() {
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000053 assert_not_impl_all!(StructWithDeletedConstructors: Clone);
54 assert_not_impl_all!(StructWithDeletedConstructors: Copy);
55 assert_not_impl_all!(StructWithDeletedConstructors: Default);
56 assert_not_impl_all!(StructWithDeletedConstructors: From<i32>);
Lukasz Anforowicz0a1b4802021-12-22 15:30:56 +000057 }
58
59 #[test]
Lukasz Anforowicz74704712021-12-22 15:30:31 +000060 fn test_private_constructors() {
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000061 assert_not_impl_all!(StructWithPrivateConstructors: Clone);
62 assert_not_impl_all!(StructWithPrivateConstructors: Copy);
63 assert_not_impl_all!(StructWithPrivateConstructors: Default);
64 assert_not_impl_all!(StructWithPrivateConstructors: From<i32>);
Lukasz Anforowicz74704712021-12-22 15:30:31 +000065 }
Lukasz Anforowicze643ec92021-12-22 15:45:15 +000066
67 #[test]
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000068 #[allow(clippy::clone_on_copy)]
69 fn test_explicitly_defaulted_constructors() {
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000070 assert_impl_all!(StructWithExplicitlyDefaultedConstructors: Default);
71 let s: StructWithExplicitlyDefaultedConstructors = Default::default();
Lukasz Anforowiczbedbdee2022-01-05 01:14:52 +000072 assert_eq!(0, s.field_with_no_initializer); // Using `MaybeUninit<T>::zeroed()`.
Lukasz Anforowicze643ec92021-12-22 15:45:15 +000073 assert_eq!(123, s.field_with_explicit_initializer);
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000074
75 // In some scenarios the bindings generator may be able to ask Rust to
76 // `#[derive(Clone)]` (e.g. when the C++ constructor has been
77 // implicitly or explicitly `=default`-ed + when Rust can mimic how C++
78 // would copy/clone all the fields). Therefore, the test assertions
79 // below may mostly be testing/exercising how Rust derives Clone. This
80 // should be okay.
81 assert_impl_all!(StructWithExplicitlyDefaultedConstructors: Clone);
Lukasz Anforowicz40c2eb82022-01-11 18:22:31 +000082 let s_clone = s.clone();
83 assert_eq!(0, s_clone.field_with_no_initializer);
84 assert_eq!(123, s_clone.field_with_explicit_initializer);
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000085
86 assert_impl_all!(StructWithExplicitlyDefaultedConstructors: Copy);
Lukasz Anforowicze643ec92021-12-22 15:45:15 +000087 }
Lukasz Anforowicz9bab8352021-12-22 17:35:31 +000088
89 #[test]
90 fn test_nontrivial_struct() {
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000091 // Non-trivial types cannot be copied.
92 assert_not_impl_all!(NonTrivialStructWithConstructors: Copy);
93
94 // Non-trivial types cannot be constructed by-value, despite having default
95 // constructor, copy constructor, and constructor taking an int.
96 assert_not_impl_all!(NonTrivialStructWithConstructors: Clone);
Lukasz Anforowicz9bab8352021-12-22 17:35:31 +000097 assert_not_impl_all!(NonTrivialStructWithConstructors: Default);
Lukasz Anforowiczcd32f062022-01-05 01:14:07 +000098 assert_not_impl_all!(NonTrivialStructWithConstructors: From<i32>);
99
100 // TODO(b/200067242): Support constructing non-trivially-relocatable
101 // types. See also <internal link>.
Lukasz Anforowicz9bab8352021-12-22 17:35:31 +0000102 }
Lukasz Anforowicz40c2eb82022-01-11 18:22:31 +0000103
104 #[test]
105 fn test_elided_lifetimes() {
106 assert_impl_all!(ElidedLifetimes: Default);
107 let s: ElidedLifetimes = Default::default();
108 assert_eq!(456, s.int_field);
109
110 // TODO(lukasza): Implement and test a user-defined copy constructor / impl
111 // Clone.
112
113 // Trivial-ABI structs implement the Copy trait, even if they have user-defined
114 // constructors.
115 assert_impl_all!(ElidedLifetimes: Copy);
116 let s_copy = s;
117 assert_eq!(456, s_copy.int_field);
118
119 assert_impl_all!(ElidedLifetimes: From<i32>);
120 let i: ElidedLifetimes = 123.into();
121 assert_eq!(123, i.int_field);
122 }
Lukasz Anforowicz13cf7492021-12-22 15:29:52 +0000123}