From d75c93dad24c17ce384e55d7824c9f4d0b2f158a Mon Sep 17 00:00:00 2001 From: niacdoial Date: Sat, 24 Jan 2026 13:37:53 +0100 Subject: [PATCH] ImproperCTypes: reorder simple type checks Another interal change that shouldn't impact rustc users. the list of simpler type-based decisions made by `visit_type` are reordered and are given better documentation. --- .../rustc_lint/src/types/improper_ctypes.rs | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_lint/src/types/improper_ctypes.rs b/compiler/rustc_lint/src/types/improper_ctypes.rs index 5be1ed1b000e..54dd54451d84 100644 --- a/compiler/rustc_lint/src/types/improper_ctypes.rs +++ b/compiler/rustc_lint/src/types/improper_ctypes.rs @@ -600,19 +600,23 @@ fn visit_type(&mut self, state: VisitorState, ty: Ty<'tcx>) -> FfiResult<'tcx> { } } - ty::Char => FfiUnsafe { + // Pattern types are just extra invariants on the type that you need to uphold, + // but only the base type is relevant for being representable in FFI. + // (note: this lint was written when pattern types could only be integers constrained to ranges) + ty::Pat(pat_ty, _) => self.visit_type(state, pat_ty), + + // types which likely have a stable representation, if the target architecture defines those + // note: before rust 1.77, 128-bit ints were not FFI-safe on x86_64 + ty::Int(..) | ty::Uint(..) | ty::Float(..) => FfiResult::FfiSafe, + + ty::Bool => FfiResult::FfiSafe, + + ty::Char => FfiResult::FfiUnsafe { ty, reason: msg!("the `char` type has no C equivalent"), help: Some(msg!("consider using `u32` or `libc::wchar_t` instead")), }, - // It's just extra invariants on the type that you need to uphold, - // but only the base type is relevant for being representable in FFI. - ty::Pat(base, ..) => self.visit_type(state, base), - - // Primitive types with a stable representation. - ty::Bool | ty::Int(..) | ty::Uint(..) | ty::Float(..) | ty::Never => FfiSafe, - ty::Slice(_) => FfiUnsafe { ty, reason: msg!("slices have no C equivalent"), @@ -687,6 +691,8 @@ fn visit_type(&mut self, state: VisitorState, ty: Ty<'tcx>) -> FfiResult<'tcx> { ty::Foreign(..) => FfiSafe, + ty::Never => FfiSafe, + // While opaque types are checked for earlier, if a projection in a struct field // normalizes to an opaque type, then it will reach this branch. ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }) => {