Skip to content

Commit d7517ec

Browse files
Rollup merge of rust-lang#130758 - compiler-errors:ctype-recursion-limit, r=jieyouxu
Revert "Add recursion limit to FFI safety lint" It's not necessarily clear if warning when we hit the recursion limit is the right thing to do, first of all. **More importantly**, this PR was implemented incorrectly in the first place; it was not decrementing the recursion limit when stepping out of a type, so it would trigger when a ctype has more than RECURSION_LIMIT fields *anywhere* in the type's set of recursively reachable fields. Reverts rust-lang#130598 Reopens rust-lang#130310 Fixes rust-lang#130757
2 parents 54fd38b + 9050b33 commit d7517ec

6 files changed

+50
-50
lines changed

compiler/rustc_lint/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,6 @@ lint_improper_ctypes_opaque = opaque types have no C equivalent
395395
lint_improper_ctypes_pat_help = consider using the base type instead
396396
397397
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
398-
399-
lint_improper_ctypes_recursion_limit_reached = type is infinitely recursive
400398
lint_improper_ctypes_slice_help = consider using a raw pointer instead
401399
402400
lint_improper_ctypes_slice_reason = slices have no C equivalent

compiler/rustc_lint/src/types.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,6 @@ struct CTypesVisitorState<'tcx> {
592592
/// The original type being checked, before we recursed
593593
/// to any other types it contains.
594594
base_ty: Ty<'tcx>,
595-
/// Number of times we recursed while checking the type
596-
recursion_depth: usize,
597595
}
598596

599597
enum FfiResult<'tcx> {
@@ -899,23 +897,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
899897

900898
// Protect against infinite recursion, for example
901899
// `struct S(*mut S);`.
900+
// FIXME: A recursion limit is necessary as well, for irregular
901+
// recursive types.
902902
if !acc.cache.insert(ty) {
903903
return FfiSafe;
904904
}
905905

906-
// Additional recursion check for more complex types like
907-
// `struct A<T> { v: *const A<A<T>>, ... }` for which the
908-
// cache check above won't be enough (fixes #130310)
909-
if !tcx.recursion_limit().value_within_limit(acc.recursion_depth) {
910-
return FfiUnsafe {
911-
ty: acc.base_ty,
912-
reason: fluent::lint_improper_ctypes_recursion_limit_reached,
913-
help: None,
914-
};
915-
}
916-
917-
acc.recursion_depth += 1;
918-
919906
match *ty.kind() {
920907
ty::Adt(def, args) => {
921908
if let Some(boxed) = ty.boxed_ty()
@@ -1261,8 +1248,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12611248
return;
12621249
}
12631250

1264-
let mut acc =
1265-
CTypesVisitorState { cache: FxHashSet::default(), base_ty: ty, recursion_depth: 0 };
1251+
let mut acc = CTypesVisitorState { cache: FxHashSet::default(), base_ty: ty };
12661252
match self.check_type_for_ffi(&mut acc, ty) {
12671253
FfiResult::FfiSafe => {}
12681254
FfiResult::FfiPhantom(ty) => {

tests/crashes/130310.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ known-bug: rust-lang/rust#130310
2+
3+
use std::marker::PhantomData;
4+
5+
#[repr(C)]
6+
struct A<T> {
7+
a: *const A<A<T>>,
8+
p: PhantomData<T>,
9+
}
10+
11+
extern "C" {
12+
fn f(a: *const A<()>);
13+
}
14+
15+
fn main() {}

tests/ui/lint/improper-types-stack-overflow-130310.rs

-20
This file was deleted.

tests/ui/lint/improper-types-stack-overflow-130310.stderr

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@ check-pass
2+
3+
#![recursion_limit = "5"]
4+
#![allow(unused)]
5+
#![deny(improper_ctypes)]
6+
7+
#[repr(C)]
8+
struct F1(*const ());
9+
#[repr(C)]
10+
struct F2(*const ());
11+
#[repr(C)]
12+
struct F3(*const ());
13+
#[repr(C)]
14+
struct F4(*const ());
15+
#[repr(C)]
16+
struct F5(*const ());
17+
#[repr(C)]
18+
struct F6(*const ());
19+
20+
#[repr(C)]
21+
struct B {
22+
f1: F1,
23+
f2: F2,
24+
f3: F3,
25+
f4: F4,
26+
f5: F5,
27+
f6: F6,
28+
}
29+
30+
extern "C" fn foo(_: B) {}
31+
32+
fn main() {}

0 commit comments

Comments
 (0)