Skip to content

Commit b27bb61

Browse files
committed
lint ImproperCTypes: add considerations for which values can be sourced from non-rust code
1 parent 366f8f2 commit b27bb61

17 files changed

+905
-625
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,13 @@ lint_improper_ctypes_only_phantomdata = composed only of `PhantomData`
415415
416416
lint_improper_ctypes_opaque = opaque types have no C equivalent
417417
418-
lint_improper_ctypes_pat_help = consider using the base type instead
419-
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
418+
lint_improper_ctypes_pat_intrange_help = consider using the base type instead
419+
lint_improper_ctypes_pat_intrange_reason = integers constrained to a given range cannot have their value be provided by non-rust code
420+
421+
lint_improper_ctypes_ptr_validity_help = consider using a raw pointer, or wrapping `{$ty}` in an `Option<_>`
422+
lint_improper_ctypes_ptr_validity_reason =
423+
boxes and references are assumed to be valid (non-null, non-dangling, aligned) pointers,
424+
which cannot be garanteed if their values are produced by non-rust code
420425
421426
lint_improper_ctypes_sized_ptr_to_unsafe_type =
422427
this reference (`{$ty}`) is ABI-compatible with a C pointer, but `{$inner_ty}` itself does not have a C layout

compiler/rustc_lint/src/types.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_abi::{BackendRepr, TagEncoding, Variants, WrappingRange};
44
use rustc_hir::{Expr, ExprKind, HirId, LangItem};
55
use rustc_middle::bug;
66
use rustc_middle::ty::layout::{LayoutOf, SizeSkeleton};
7-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
7+
use rustc_middle::ty::{self, AdtKind, Ty, TyCtxt, TypeVisitableExt};
88
use rustc_session::{declare_lint, declare_lint_pass, impl_lint_pass};
99
use rustc_span::{Span, Symbol, sym};
1010
use tracing::debug;
@@ -935,6 +935,38 @@ fn get_nullable_type_from_pat<'tcx>(
935935
}
936936
}
937937

938+
/// determines wether or not `outer_ty` is an option-like enum, with the same size as its contained type, `ty`.
939+
/// this ASSUMES that `ty` is a type that is already 'inside' of `outer_ty`.
940+
fn is_outer_optionlike_around_ty<'tcx>(
941+
cx: &LateContext<'tcx>,
942+
outer_ty: Ty<'tcx>,
943+
ty: Ty<'tcx>,
944+
) -> bool {
945+
// three things to check to be sure outer_ty is option-like (since we know we reached the current ty from there)
946+
// That outer_ty is an enum, that this enum doesn't have a defined discriminant representation,
947+
// and the the outer_ty's size is that of ty.
948+
if let ty::Adt(def, _) = outer_ty.kind() {
949+
if !matches!(def.adt_kind(), AdtKind::Enum)
950+
|| def.repr().c()
951+
|| def.repr().transparent()
952+
|| def.repr().int.is_none()
953+
{
954+
false
955+
} else {
956+
let (tcx, typing_env) = (cx.tcx, cx.typing_env());
957+
958+
// see the insides of super::repr_nullable_ptr()
959+
let compute_size_skeleton = |t| SizeSkeleton::compute(t, tcx, typing_env).ok();
960+
match (compute_size_skeleton(ty), compute_size_skeleton(outer_ty)) {
961+
(Some(sk1), Some(sk2)) => sk1.same_size(sk2),
962+
_ => false,
963+
}
964+
}
965+
} else {
966+
false
967+
}
968+
}
969+
938970
declare_lint_pass!(VariantSizeDifferences => [VARIANT_SIZE_DIFFERENCES]);
939971

940972
impl<'tcx> LateLintPass<'tcx> for VariantSizeDifferences {

0 commit comments

Comments
 (0)