Skip to content

Commit 853d70d

Browse files
committed
lint ImproperCTypes: add considerations for which values can be sourced from non-rust code
1 parent 80c37b7 commit 853d70d

17 files changed

+771
-488
lines changed

compiler/rustc_lint/messages.ftl

+6-2
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,13 @@ lint_improper_ctypes_only_phantomdata = composed only of `PhantomData`
400400
401401
lint_improper_ctypes_opaque = opaque types have no C equivalent
402402
403-
lint_improper_ctypes_pat_help = consider using the base type instead
403+
lint_improper_ctypes_pat_intrange_help = consider using the base type instead
404+
lint_improper_ctypes_pat_intrange_reason = integers constrained to a given range cannot have their value be provided by non-rust code
404405
405-
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
406+
lint_improper_ctypes_ptr_validity_help = consider using a raw pointer, or wrapping `{$ty}` in an `Option<_>`
407+
lint_improper_ctypes_ptr_validity_reason =
408+
boxes and references are assumed to be valid (non-null, non-dangling, aligned) pointers,
409+
which cannot be garanteed if their values are produced by non-rust code
406410
407411
lint_improper_ctypes_sized_ptr_to_unsafe_type =
408412
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

+33-1
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, 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, source_map, sym};
1010
use tracing::debug;
@@ -981,6 +981,38 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
981981
None
982982
}
983983

984+
/// determines wether or not `outer_ty` is an option-like enum, with the same size as its contained type, `ty`.
985+
/// this ASSUMES that `ty` is a type that is already 'inside' of `outer_ty`.
986+
fn is_outer_optionlike_around_ty<'tcx>(
987+
cx: &LateContext<'tcx>,
988+
outer_ty: Ty<'tcx>,
989+
ty: Ty<'tcx>,
990+
) -> bool {
991+
// three things to check to be sure outer_ty is option-like (since we know we reached the current ty from there)
992+
// That outer_ty is an enum, that this enum doesn't have a defined discriminant representation,
993+
// and the the outer_ty's size is that of ty.
994+
if let ty::Adt(def, _) = outer_ty.kind() {
995+
if !matches!(def.adt_kind(), AdtKind::Enum)
996+
|| def.repr().c()
997+
|| def.repr().transparent()
998+
|| def.repr().int.is_none()
999+
{
1000+
false
1001+
} else {
1002+
let (tcx, typing_env) = (cx.tcx, cx.typing_env());
1003+
1004+
// see the insides of super::repr_nullable_ptr()
1005+
let compute_size_skeleton = |t| SizeSkeleton::compute(t, tcx, typing_env).ok();
1006+
match (compute_size_skeleton(ty), compute_size_skeleton(outer_ty)) {
1007+
(Some(sk1), Some(sk2)) => sk1.same_size(sk2),
1008+
_ => false,
1009+
}
1010+
}
1011+
} else {
1012+
false
1013+
}
1014+
}
1015+
9841016
declare_lint_pass!(VariantSizeDifferences => [VARIANT_SIZE_DIFFERENCES]);
9851017

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

0 commit comments

Comments
 (0)