Skip to content

Commit 22a1716

Browse files
author
Robin Kruppe
committed
[improper_ctypes] Suggest repr(transparent) for structs
The suggestion is unconditional, so following it could lead to further errors. This is already the case for the repr(C) suggestion, which makes this acceptable, though not *good*. Checking up-front whether the suggestion can help would be great but applies more broadly (and would require some refactoring to avoid duplicating the checks).
1 parent 9b5f47e commit 22a1716

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/librustc_lint/types.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
425425
return FfiUnsafe {
426426
ty: ty,
427427
reason: "this struct has unspecified layout",
428-
help: Some("consider adding a #[repr(C)] attribute to this struct"),
428+
help: Some("consider adding a #[repr(C)] or #[repr(transparent)] \
429+
attribute to this struct"),
429430
};
430431
}
431432

src/test/ui/lint-ctypes.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ note: lint level defined here
99
|
1010
11 | #![deny(improper_ctypes)]
1111
| ^^^^^^^^^^^^^^^
12-
= help: consider adding a #[repr(C)] attribute to this struct
12+
= help: consider adding a #[repr(C)] or #[repr(transparent)] attribute to this struct
1313

1414
error: `extern` block uses type `Foo` which is not FFI-safe: this struct has unspecified layout
1515
--> $DIR/lint-ctypes.rs:55:28
1616
|
1717
55 | pub fn ptr_type2(size: *const Foo); //~ ERROR: uses type `Foo`
1818
| ^^^^^^^^^^
1919
|
20-
= help: consider adding a #[repr(C)] attribute to this struct
20+
= help: consider adding a #[repr(C)] or #[repr(transparent)] attribute to this struct
2121

2222
error: `extern` block uses type `[u32]` which is not FFI-safe: slices have no C equivalent
2323
--> $DIR/lint-ctypes.rs:56:26
@@ -41,7 +41,7 @@ error: `extern` block uses type `std::boxed::Box<u32>` which is not FFI-safe: th
4141
58 | pub fn box_type(p: Box<u32>); //~ ERROR uses type `std::boxed::Box<u32>`
4242
| ^^^^^^^^
4343
|
44-
= help: consider adding a #[repr(C)] attribute to this struct
44+
= help: consider adding a #[repr(C)] or #[repr(transparent)] attribute to this struct
4545

4646
error: `extern` block uses type `char` which is not FFI-safe: the `char` type has no C equivalent
4747
--> $DIR/lint-ctypes.rs:59:25
@@ -129,7 +129,7 @@ error: `extern` block uses type `std::boxed::Box<u32>` which is not FFI-safe: th
129129
71 | pub fn fn_contained(p: RustBadRet); //~ ERROR: uses type `std::boxed::Box<u32>`
130130
| ^^^^^^^^^^
131131
|
132-
= help: consider adding a #[repr(C)] attribute to this struct
132+
= help: consider adding a #[repr(C)] or #[repr(transparent)] attribute to this struct
133133

134134
error: `extern` block uses type `i128` which is not FFI-safe: 128-bit integers don't currently have a known stable ABI
135135
--> $DIR/lint-ctypes.rs:72:32
@@ -151,7 +151,7 @@ error: `extern` block uses type `std::boxed::Box<u32>` which is not FFI-safe: th
151151
74 | pub fn transparent_fn(p: TransparentBadFn); //~ ERROR: uses type `std::boxed::Box<u32>`
152152
| ^^^^^^^^^^^^^^^^
153153
|
154-
= help: consider adding a #[repr(C)] attribute to this struct
154+
= help: consider adding a #[repr(C)] or #[repr(transparent)] attribute to this struct
155155

156156
error: aborting due to 20 previous errors
157157

0 commit comments

Comments
 (0)