From de66639bbcbff44eda26ef8ba7a79a4f82dc8a9a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 23 Sep 2024 12:43:44 -0400 Subject: [PATCH 1/2] Revert "Add recursion limit to FFI safety lint" This reverts commit 716044751b7a36c634be709e37923e3dbdf53888. --- compiler/rustc_lint/messages.ftl | 2 -- compiler/rustc_lint/src/types.rs | 20 +++---------------- tests/crashes/130310.rs | 15 ++++++++++++++ .../improper-types-stack-overflow-130310.rs | 20 ------------------- ...mproper-types-stack-overflow-130310.stderr | 11 ---------- 5 files changed, 18 insertions(+), 50 deletions(-) create mode 100644 tests/crashes/130310.rs delete mode 100644 tests/ui/lint/improper-types-stack-overflow-130310.rs delete mode 100644 tests/ui/lint/improper-types-stack-overflow-130310.stderr diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index e71c5676ce4f5..0e3d34355a1f2 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -395,8 +395,6 @@ lint_improper_ctypes_opaque = opaque types have no C equivalent lint_improper_ctypes_pat_help = consider using the base type instead lint_improper_ctypes_pat_reason = pattern types have no C equivalent - -lint_improper_ctypes_recursion_limit_reached = type is infinitely recursive lint_improper_ctypes_slice_help = consider using a raw pointer instead lint_improper_ctypes_slice_reason = slices have no C equivalent diff --git a/compiler/rustc_lint/src/types.rs b/compiler/rustc_lint/src/types.rs index f5a24f9808d2a..15fe18adbfb10 100644 --- a/compiler/rustc_lint/src/types.rs +++ b/compiler/rustc_lint/src/types.rs @@ -592,8 +592,6 @@ struct CTypesVisitorState<'tcx> { /// The original type being checked, before we recursed /// to any other types it contains. base_ty: Ty<'tcx>, - /// Number of times we recursed while checking the type - recursion_depth: usize, } enum FfiResult<'tcx> { @@ -899,23 +897,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { // Protect against infinite recursion, for example // `struct S(*mut S);`. + // FIXME: A recursion limit is necessary as well, for irregular + // recursive types. if !acc.cache.insert(ty) { return FfiSafe; } - // Additional recursion check for more complex types like - // `struct A { v: *const A>, ... }` for which the - // cache check above won't be enough (fixes #130310) - if !tcx.recursion_limit().value_within_limit(acc.recursion_depth) { - return FfiUnsafe { - ty: acc.base_ty, - reason: fluent::lint_improper_ctypes_recursion_limit_reached, - help: None, - }; - } - - acc.recursion_depth += 1; - match *ty.kind() { ty::Adt(def, args) => { if let Some(boxed) = ty.boxed_ty() @@ -1261,8 +1248,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { return; } - let mut acc = - CTypesVisitorState { cache: FxHashSet::default(), base_ty: ty, recursion_depth: 0 }; + let mut acc = CTypesVisitorState { cache: FxHashSet::default(), base_ty: ty }; match self.check_type_for_ffi(&mut acc, ty) { FfiResult::FfiSafe => {} FfiResult::FfiPhantom(ty) => { diff --git a/tests/crashes/130310.rs b/tests/crashes/130310.rs new file mode 100644 index 0000000000000..d59dd39983c78 --- /dev/null +++ b/tests/crashes/130310.rs @@ -0,0 +1,15 @@ +//@ known-bug: rust-lang/rust#130310 + +use std::marker::PhantomData; + +#[repr(C)] +struct A { + a: *const A>, + p: PhantomData, +} + +extern "C" { + fn f(a: *const A<()>); +} + +fn main() {} diff --git a/tests/ui/lint/improper-types-stack-overflow-130310.rs b/tests/ui/lint/improper-types-stack-overflow-130310.rs deleted file mode 100644 index 60eb873981756..0000000000000 --- a/tests/ui/lint/improper-types-stack-overflow-130310.rs +++ /dev/null @@ -1,20 +0,0 @@ -// Regression test for #130310 -// Tests that we do not fall into infinite -// recursion while checking FFI safety of -// recursive types like `A` below - -//@ build-pass -use std::marker::PhantomData; - -#[repr(C)] -struct A { - a: *const A>, // Recursive because of this field - p: PhantomData, -} - -extern "C" { - fn f(a: *const A<()>); - //~^ WARN `extern` block uses type `*const A<()>`, which is not FFI-safe -} - -fn main() {} diff --git a/tests/ui/lint/improper-types-stack-overflow-130310.stderr b/tests/ui/lint/improper-types-stack-overflow-130310.stderr deleted file mode 100644 index 6981bb2575542..0000000000000 --- a/tests/ui/lint/improper-types-stack-overflow-130310.stderr +++ /dev/null @@ -1,11 +0,0 @@ -warning: `extern` block uses type `*const A<()>`, which is not FFI-safe - --> $DIR/improper-types-stack-overflow-130310.rs:16:13 - | -LL | fn f(a: *const A<()>); - | ^^^^^^^^^^^^ not FFI-safe - | - = note: type is infinitely recursive - = note: `#[warn(improper_ctypes)]` on by default - -warning: 1 warning emitted - From 9050b3374588fdb1b15a862a495b042dd64edd5a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 23 Sep 2024 12:55:56 -0400 Subject: [PATCH 2/2] Add a test --- .../lint/lint-ctypes-non-recursion-limit.rs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/ui/lint/lint-ctypes-non-recursion-limit.rs diff --git a/tests/ui/lint/lint-ctypes-non-recursion-limit.rs b/tests/ui/lint/lint-ctypes-non-recursion-limit.rs new file mode 100644 index 0000000000000..61e95dc5a464c --- /dev/null +++ b/tests/ui/lint/lint-ctypes-non-recursion-limit.rs @@ -0,0 +1,32 @@ +//@ check-pass + +#![recursion_limit = "5"] +#![allow(unused)] +#![deny(improper_ctypes)] + +#[repr(C)] +struct F1(*const ()); +#[repr(C)] +struct F2(*const ()); +#[repr(C)] +struct F3(*const ()); +#[repr(C)] +struct F4(*const ()); +#[repr(C)] +struct F5(*const ()); +#[repr(C)] +struct F6(*const ()); + +#[repr(C)] +struct B { + f1: F1, + f2: F2, + f3: F3, + f4: F4, + f5: F5, + f6: F6, +} + +extern "C" fn foo(_: B) {} + +fn main() {}