Skip to content

Commit 4906ee4

Browse files
committed
lint ImproperCTypes: rm improper_ctype_definitions [...]
for now, let's fully remove this lint. it might be reintroduced later as a way to make the lints ignore the inside of some ADT definitions
1 parent bb98a7a commit 4906ee4

File tree

63 files changed

+178
-717
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+178
-717
lines changed

compiler/rustc_lint/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ fn register_builtins(store: &mut LintStore) {
341341
IMPROPER_C_CALLBACKS,
342342
IMPROPER_C_FN_DEFINITIONS,
343343
IMPROPER_C_VAR_DEFINITIONS,
344-
IMPROPER_CTYPE_DEFINITIONS,
345344
IMPROPER_CTYPES
346345
);
347346

compiler/rustc_lint/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use {rustc_ast as ast, rustc_hir as hir};
1212

1313
mod improper_ctypes; // these filed do the implementation for ImproperCTypesDefinitions,ImproperCTypesDeclarations
1414
pub(crate) use improper_ctypes::{
15-
IMPROPER_C_CALLBACKS, IMPROPER_C_FN_DEFINITIONS, IMPROPER_C_VAR_DEFINITIONS,
16-
IMPROPER_CTYPE_DEFINITIONS, IMPROPER_CTYPES, ImproperCTypesLint,
15+
IMPROPER_C_CALLBACKS, IMPROPER_C_FN_DEFINITIONS, IMPROPER_C_VAR_DEFINITIONS, IMPROPER_CTYPES,
16+
ImproperCTypesLint,
1717
};
1818

1919
use crate::lints::{

compiler/rustc_lint/src/types/improper_ctypes.rs

Lines changed: 11 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ enum CItemKind {
104104
ExportedStatic,
105105
/// `extern "C"` function pointers -> IMPROPER_C_CALLBACKS,
106106
Callback,
107-
/// `repr(C)` structs/enums/unions -> IMPROPER_CTYPE_DEFINITIONS
108-
#[allow(unused)]
109-
AdtDef,
110107
}
111108

112109
#[derive(Clone, Debug)]
@@ -446,8 +443,6 @@ enum CTypesVisitorState {
446443
// uses bitflags from CTypesVisitorStateFlags
447444
StaticTy = CTypesVisitorStateFlags::STATIC,
448445
ExportedStaticTy = CTypesVisitorStateFlags::STATIC | CTypesVisitorStateFlags::FN_DEFINED,
449-
#[allow(unused)]
450-
AdtDef = CTypesVisitorStateFlags::THEORETICAL,
451446
ArgumentTyInDefinition = CTypesVisitorStateFlags::FUNC | CTypesVisitorStateFlags::FN_DEFINED,
452447
ReturnTyInDefinition = CTypesVisitorStateFlags::FUNC
453448
| CTypesVisitorStateFlags::FN_RETURN
@@ -507,11 +502,6 @@ impl CTypesVisitorState {
507502
((self as u8) & THEORETICAL) != 0 && self.is_in_function()
508503
}
509504

510-
/// whether the type is currently being defined
511-
fn is_being_defined(self) -> bool {
512-
self == Self::AdtDef
513-
}
514-
515505
/// whether we can expect type parameters and co in a given type
516506
fn can_expect_ty_params(self) -> bool {
517507
use CTypesVisitorStateFlags::*;
@@ -526,11 +516,7 @@ impl CTypesVisitorState {
526516
/// whether the value for that type might come from the non-rust side of a FFI boundary
527517
/// this is particularly useful for non-raw pointers, since rust assume they are non-null
528518
fn value_may_be_unchecked(self) -> bool {
529-
if self == Self::AdtDef {
530-
// some ADTs are only used to go through the FFI boundary in one direction,
531-
// so let's not make hasty judgement
532-
false
533-
} else if self.is_in_static() {
519+
if self.is_in_static() {
534520
// FIXME: this is evidently untrue for non-mut static variables
535521
// (assuming the cross-FFI code respects this)
536522
true
@@ -614,9 +600,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
614600
outer_ty: Option<Ty<'tcx>>,
615601
ty: Ty<'tcx>,
616602
) -> FfiResult<'tcx> {
617-
if state.is_being_defined()
618-
|| (state.is_in_function_return()
619-
&& matches!(outer_ty.map(|ty| ty.kind()), None | Some(ty::FnPtr(..)),))
603+
if state.is_in_function_return()
604+
&& matches!(outer_ty.map(|ty| ty.kind()), None | Some(ty::FnPtr(..)))
620605
{
621606
FfiResult::FfiSafe
622607
} else {
@@ -814,7 +799,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
814799
&mut self,
815800
state: CTypesVisitorState,
816801
ty: Ty<'tcx>,
817-
def: ty::AdtDef<'tcx>,
802+
def: AdtDef<'tcx>,
818803
variant: &ty::VariantDef,
819804
args: GenericArgsRef<'tcx>,
820805
) -> FfiResult<'tcx> {
@@ -968,9 +953,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
968953
fn visit_struct_or_union(
969954
&mut self,
970955
state: CTypesVisitorState,
971-
outer_ty: Option<Ty<'tcx>>,
972956
ty: Ty<'tcx>,
973-
def: ty::AdtDef<'tcx>,
957+
def: AdtDef<'tcx>,
974958
args: GenericArgsRef<'tcx>,
975959
) -> FfiResult<'tcx> {
976960
debug_assert!(matches!(def.adt_kind(), AdtKind::Struct | AdtKind::Union));
@@ -1031,20 +1015,17 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
10311015
// which is partly why we keep the details as to why that struct is FFI-unsafe)
10321016
// - if the struct is from another crate, then there's not much that can be done anyways
10331017
//
1034-
// if outer_ty.is_some() || !state.is_being_defined() then this enum is visited in the middle of another lint,
1018+
// this enum is visited in the middle of another lint,
10351019
// so we override the "cause type" of the lint
1036-
let override_cause_ty =
1037-
if state.is_being_defined() { outer_ty.and(Some(ty)) } else { Some(ty) };
1038-
1039-
ffires.with_overrides(override_cause_ty)
1020+
ffires.with_overrides(Some(ty))
10401021
}
10411022

10421023
fn visit_enum(
10431024
&mut self,
10441025
state: CTypesVisitorState,
10451026
outer_ty: Option<Ty<'tcx>>,
10461027
ty: Ty<'tcx>,
1047-
def: ty::AdtDef<'tcx>,
1028+
def: AdtDef<'tcx>,
10481029
args: GenericArgsRef<'tcx>,
10491030
) -> FfiResult<'tcx> {
10501031
debug_assert!(matches!(def.adt_kind(), AdtKind::Enum));
@@ -1131,12 +1112,10 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11311112
ffires += variants_uninhabited_ffires.into_iter().reduce(|r1, r2| r1 + r2).unwrap();
11321113
}
11331114

1134-
// if outer_ty.is_some() || !state.is_being_defined() then this enum is visited in the middle of another lint,
1115+
// this enum is visited in the middle of another lint,
11351116
// so we override the "cause type" of the lint
11361117
// (for more detail, see comment in ``visit_struct_union`` before its call to ``ffires.with_overrides``)
1137-
let override_cause_ty =
1138-
if state.is_being_defined() { outer_ty.and(Some(ty)) } else { Some(ty) };
1139-
ffires.with_overrides(override_cause_ty)
1118+
ffires.with_overrides(Some(ty))
11401119
}
11411120
}
11421121

@@ -1185,7 +1164,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
11851164
{
11861165
return self.visit_cstr(outer_ty, ty);
11871166
}
1188-
self.visit_struct_or_union(state, outer_ty, ty, def, args)
1167+
self.visit_struct_or_union(state, ty, def, args)
11891168
}
11901169
AdtKind::Enum => self.visit_enum(state, outer_ty, ty, def, args),
11911170
}
@@ -1475,71 +1454,6 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
14751454
}
14761455
}
14771456

1478-
#[allow(unused)]
1479-
fn check_for_adtdef(&mut self, ty: Ty<'tcx>) -> FfiResult<'tcx> {
1480-
use FfiResult::*;
1481-
let ty = erase_and_maybe_normalize(self.cx, ty);
1482-
1483-
let mut ffires = match *ty.kind() {
1484-
ty::Adt(def, args) => {
1485-
if !def.did().is_local() {
1486-
bug!(
1487-
"check_adtdef expected to visit a locally-defined struct/enum/union not {:?}",
1488-
def
1489-
);
1490-
}
1491-
1492-
// question: how does this behave when running for "special" ADTs in the stdlib?
1493-
// answer: none of CStr, CString, Box, and PhantomData are repr(C)
1494-
let state = CTypesVisitorState::AdtDef;
1495-
match def.adt_kind() {
1496-
AdtKind::Struct | AdtKind::Union => {
1497-
self.visit_struct_or_union(state, None, ty, def, args)
1498-
}
1499-
AdtKind::Enum => self.visit_enum(state, None, ty, def, args),
1500-
}
1501-
}
1502-
r @ _ => {
1503-
bug!("expected to inspect the type of an `extern \"ABI\"` FnPtr, not {:?}", r,)
1504-
}
1505-
};
1506-
1507-
match &mut ffires {
1508-
// due to the way type visits work, any unsafeness that comes from the fields inside an ADT
1509-
// is uselessly "prefixed" with the fact that yes, the error occurs in that ADT
1510-
// we remove the prefixes here.
1511-
FfiUnsafe(explanations) => {
1512-
explanations.iter_mut().for_each(|explanation| {
1513-
if let Some(inner_reason) = explanation.reason.inner.take() {
1514-
debug_assert_eq!(explanation.reason.ty, ty);
1515-
debug_assert_eq!(
1516-
explanation.reason.note,
1517-
fluent::lint_improper_ctypes_struct_dueto
1518-
);
1519-
if let Some(help) = &explanation.reason.help {
1520-
// there is an actual help message in the normally useless prefix
1521-
// make sure it gets through
1522-
debug_assert_eq!(
1523-
help,
1524-
&fluent::lint_improper_ctypes_struct_consider_transparent
1525-
);
1526-
explanation.override_cause_ty = Some(inner_reason.ty);
1527-
explanation.reason.inner = Some(inner_reason);
1528-
} else {
1529-
explanation.reason = inner_reason;
1530-
}
1531-
}
1532-
});
1533-
}
1534-
1535-
// also, turn FfiPhantom into FfiSafe: unlike other places we can check, we don't want
1536-
// FfiPhantom to end up emitting a lint
1537-
ffires @ FfiPhantom(_) => *ffires = FfiSafe,
1538-
FfiSafe => {}
1539-
}
1540-
ffires
1541-
}
1542-
15431457
fn check_arg_for_power_alignment(&self, cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
15441458
let tcx = cx.tcx;
15451459
assert!(tcx.sess.target.os == "aix");
@@ -1822,15 +1736,13 @@ impl ImproperCTypesLint {
18221736
CItemKind::ImportedExtern => IMPROPER_CTYPES,
18231737
CItemKind::ExportedFunction => IMPROPER_C_FN_DEFINITIONS,
18241738
CItemKind::ExportedStatic => IMPROPER_C_VAR_DEFINITIONS,
1825-
CItemKind::AdtDef => IMPROPER_CTYPE_DEFINITIONS,
18261739
CItemKind::Callback => IMPROPER_C_CALLBACKS,
18271740
};
18281741
let desc = match fn_mode {
18291742
CItemKind::ImportedExtern => "`extern` block",
18301743
CItemKind::ExportedFunction => "`extern` fn",
18311744
CItemKind::ExportedStatic => "foreign-code-reachable static",
18321745
CItemKind::Callback => "`extern` callback",
1833-
CItemKind::AdtDef => "`repr(C)` type",
18341746
};
18351747
for reason in reasons.iter_mut() {
18361748
reason.span_note = if let ty::Adt(def, _) = reason.ty.kind()
@@ -1860,9 +1772,6 @@ impl ImproperCTypesLint {
18601772
/// In other words, `extern "<abi>" fn` definitions and trait-method declarations.
18611773
/// This only matters if `<abi>` is external (e.g. `C`).
18621774
///
1863-
/// `IMPROPER_CTYPE_DEFINITIONS` checks structs/enums/unions marked with `repr(C)`,
1864-
/// assuming they are to have a fully C-compatible layout.
1865-
///
18661775
/// and now combinatorics for pointees
18671776
impl<'tcx> LateLintPass<'tcx> for ImproperCTypesLint {
18681777
fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, it: &hir::ForeignItem<'tcx>) {
@@ -2131,33 +2040,6 @@ declare_lint! {
21312040
"proper use of libc types in foreign-code-compatible callbacks"
21322041
}
21332042

2134-
declare_lint! {
2135-
/// The `improper_ctype_definitions` lint detects incorrect use of types in
2136-
/// foreign-compatible structs, enums, and union definitions.
2137-
///
2138-
/// ### Example
2139-
///
2140-
/// ```rust
2141-
/// repr(C) struct StringWrapper{
2142-
/// length: usize,
2143-
/// strung: &str,
2144-
/// }
2145-
/// ```
2146-
///
2147-
/// {{produces}}
2148-
///
2149-
/// ### Explanation
2150-
///
2151-
/// The compiler has several checks to verify that types designed to be
2152-
/// compatible with foreign interfaces follow certain rules to be safe.
2153-
/// This lint is issued when it detects a probable mistake in a definition.
2154-
/// The lint usually should provide a description of the issue,
2155-
/// along with possibly a hint on how to resolve it.
2156-
pub(crate) IMPROPER_CTYPE_DEFINITIONS,
2157-
Warn,
2158-
"proper use of libc types when defining foreign-code-compatible structs"
2159-
}
2160-
21612043
declare_lint! {
21622044
/// The `uses_power_alignment` lint detects specific `repr(C)`
21632045
/// aggregates on AIX.
@@ -2218,6 +2100,5 @@ declare_lint_pass!(ImproperCTypesLint => [
22182100
IMPROPER_C_FN_DEFINITIONS,
22192101
IMPROPER_C_VAR_DEFINITIONS,
22202102
IMPROPER_C_CALLBACKS,
2221-
IMPROPER_CTYPE_DEFINITIONS,
22222103
USES_POWER_ALIGNMENT,
22232104
]);

library/panic_unwind/src/gcc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ static CANARY: u8 = 0;
5252
// The first two field must be `_Unwind_Exception` and `canary`,
5353
// as it may be accessed by a different version of the std with a different compiler.
5454
#[repr(C)]
55-
#[allow(unknown_lints, renamed_and_removed_lints, improper_ctypes_definitions)] // FIXME delete line once improper_c_fn_definitions exists upstream
56-
#[allow(improper_ctype_definitions)] // Boxed dyn is a fat pointer
5755
struct Exception {
5856
_uwe: uw::_Unwind_Exception,
5957
canary: *const u8,

library/proc_macro/src/bridge/client.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,6 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
358358

359359
#[repr(C)]
360360
#[derive(Copy, Clone)]
361-
#[allow(unknown_lints, renamed_and_removed_lints, improper_ctypes_definitions)] // FIXME delete line once improper_c_fn_definitions exists upstream
362-
#[allow(improper_ctype_definitions)] // so many C-incompatible double-width pointers
363361
pub enum ProcMacro {
364362
CustomDerive {
365363
trait_name: &'static str,

src/tools/rust-analyzer/crates/ide-db/src/generated/lints.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,15 @@ pub const DEFAULT_LINTS: &[Lint] = &[
461461
deny_since: None,
462462
},
463463
Lint {
464-
label: "improper_ctypes",
465-
description: r##"proper use of libc types in foreign modules"##,
464+
label: "improper_c_var_definitions",
465+
description: r##"proper use of libc types when making static variables foreign-code-accessible"##,
466466
default_severity: Severity::Warning,
467467
warn_since: None,
468468
deny_since: None,
469469
},
470470
Lint {
471-
label: "improper_ctype_definitions",
472-
description: r##"proper use of libc types when defining foreign-code-compatible structs"##,
471+
label: "improper_ctypes",
472+
description: r##"proper use of libc types in foreign modules"##,
473473
default_severity: Severity::Warning,
474474
warn_since: None,
475475
deny_since: None,

tests/auxiliary/minicore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
asm_experimental_arch,
2828
unboxed_closures
2929
)]
30-
#![allow(unused, improper_ctype_definitions, internal_features)]
30+
#![allow(unused, internal_features)]
3131
#![no_std]
3232
#![no_core]
3333

tests/ui/abi/abi-sysv64-arg-passing.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// the sysv64 ABI on Windows.
3434

3535
#[allow(dead_code)]
36-
#[allow(improper_ctypes, improper_ctype_definitions)]
36+
#[allow(improper_ctypes)]
3737

3838
#[cfg(target_arch = "x86_64")]
3939
mod tests {
@@ -72,7 +72,6 @@ mod tests {
7272
}
7373

7474
#[repr(C)]
75-
#[allow(improper_ctype_definitions)]
7675
pub struct Empty;
7776

7877
#[repr(C)]

tests/ui/abi/arm-unadjusted-intrinsic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ pub struct int8x16_t(pub(crate) [i8; 16]);
2525
impl Copy for int8x16_t {}
2626

2727
#[repr(C)]
28-
#[allow(improper_ctype_definitions)]
2928
pub struct int8x16x4_t(pub int8x16_t, pub int8x16_t, pub int8x16_t, pub int8x16_t);
3029
impl Copy for int8x16x4_t {}
3130

tests/ui/abi/compatibility.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
#![feature(unsized_fn_params, transparent_unions)]
6464
#![no_core]
6565
#![allow(unused, internal_features)]
66-
#![allow(improper_ctype_definitions, improper_c_fn_definitions, improper_c_callbacks)]
66+
#![allow(improper_c_fn_definitions, improper_c_callbacks)]
6767

6868
// FIXME: some targets are broken in various ways.
6969
// Hence there are `cfg` throughout this test to disable parts of it on those targets.

tests/ui/abi/extern/extern-pass-empty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ run-pass
2-
#![allow(improper_ctypes, improper_ctype_definitions)]
2+
#![allow(improper_ctypes)]
33
// FIXME: this test is inherently not FFI-safe.
44

55
// Test a foreign function that accepts empty struct.

tests/ui/backtrace/auxiliary/dylib-dep-helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![crate_type = "cdylib"]
44
#![crate_type = "rlib"]
55

6-
#![allow(improper_ctype_definitions, improper_c_fn_definitions)]
6+
#![allow(improper_c_fn_definitions)]
77

88
type Pos = (&'static str, u32);
99

tests/ui/const-generics/defaults/repr-c-issue-82792.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//@ run-pass
44

55
#[repr(C)]
6-
#[allow(improper_ctype_definitions)]
76
pub struct Loaf<T: Sized, const N: usize = 1> {
87
head: [T; N],
98
slice: [T],

tests/ui/consts/extra-const-ub/detect-extra-ub.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ const PARTIAL_POINTER: () = unsafe {
7474
}
7575
// `Align` ensures that the entire thing has pointer alignment again.
7676
#[repr(C)]
77-
#[allow(improper_ctype_definitions)]
7877
struct Align {
7978
p: Packed,
8079
align: usize,

0 commit comments

Comments
 (0)