Skip to content

Commit ebdf2ab

Browse files
committed
Auto merge of #146271 - niacdoial:improperctypes-refactor1, r=tgross35
lint ImproperCTypes: refactor linting architecture (part 1) This is the first PR in an effort to split #134697 into individually-mergeable parts. This one focuses on properly packaging the lint and its tests, as well as properly separate the "linting" and "type-checking" code. There is exactly one user-visible change: the safety of `Option<Box<FFISafePointee>>` is now the same in `extern` blocks and function definitions: it is safe. r? `@tgross35` because you are already looking at the original
2 parents f4b2f68 + 20f050b commit ebdf2ab

Some content is hidden

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

44 files changed

+1143
-1002
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4022,6 +4022,7 @@ dependencies = [
40224022
name = "rustc_lint"
40234023
version = "0.0.0"
40244024
dependencies = [
4025+
"bitflags",
40254026
"rustc_abi",
40264027
"rustc_ast",
40274028
"rustc_ast_pretty",

compiler/rustc_lint/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
bitflags = "2.4.1"
89
rustc_abi = { path = "../rustc_abi" }
910
rustc_ast = { path = "../rustc_ast" }
1011
rustc_ast_pretty = { path = "../rustc_ast_pretty" }

compiler/rustc_lint/src/foreign_modules.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ impl ClashingExternDeclarations {
136136
ty::TypingEnv::non_body_analysis(tcx, this_fi.owner_id),
137137
existing_decl_ty,
138138
this_decl_ty,
139-
types::CItemKind::Declaration,
140139
) {
141140
let orig = name_of_extern_decl(tcx, existing_did);
142141

@@ -214,10 +213,9 @@ fn structurally_same_type<'tcx>(
214213
typing_env: ty::TypingEnv<'tcx>,
215214
a: Ty<'tcx>,
216215
b: Ty<'tcx>,
217-
ckind: types::CItemKind,
218216
) -> bool {
219217
let mut seen_types = UnordSet::default();
220-
let result = structurally_same_type_impl(&mut seen_types, tcx, typing_env, a, b, ckind);
218+
let result = structurally_same_type_impl(&mut seen_types, tcx, typing_env, a, b);
221219
if cfg!(debug_assertions) && result {
222220
// Sanity-check: must have same ABI, size and alignment.
223221
// `extern` blocks cannot be generic, so we'll always get a layout here.
@@ -236,7 +234,6 @@ fn structurally_same_type_impl<'tcx>(
236234
typing_env: ty::TypingEnv<'tcx>,
237235
a: Ty<'tcx>,
238236
b: Ty<'tcx>,
239-
ckind: types::CItemKind,
240237
) -> bool {
241238
debug!("structurally_same_type_impl(tcx, a = {:?}, b = {:?})", a, b);
242239

@@ -307,33 +304,26 @@ fn structurally_same_type_impl<'tcx>(
307304
typing_env,
308305
tcx.type_of(a_did).instantiate(tcx, a_gen_args),
309306
tcx.type_of(b_did).instantiate(tcx, b_gen_args),
310-
ckind,
311307
)
312308
},
313309
)
314310
}
315311
(ty::Array(a_ty, a_len), ty::Array(b_ty, b_len)) => {
316312
// For arrays, we also check the length.
317313
a_len == b_len
318-
&& structurally_same_type_impl(
319-
seen_types, tcx, typing_env, *a_ty, *b_ty, ckind,
320-
)
314+
&& structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
321315
}
322316
(ty::Slice(a_ty), ty::Slice(b_ty)) => {
323-
structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty, ckind)
317+
structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
324318
}
325319
(ty::RawPtr(a_ty, a_mutbl), ty::RawPtr(b_ty, b_mutbl)) => {
326320
a_mutbl == b_mutbl
327-
&& structurally_same_type_impl(
328-
seen_types, tcx, typing_env, *a_ty, *b_ty, ckind,
329-
)
321+
&& structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
330322
}
331323
(ty::Ref(_a_region, a_ty, a_mut), ty::Ref(_b_region, b_ty, b_mut)) => {
332324
// For structural sameness, we don't need the region to be same.
333325
a_mut == b_mut
334-
&& structurally_same_type_impl(
335-
seen_types, tcx, typing_env, *a_ty, *b_ty, ckind,
336-
)
326+
&& structurally_same_type_impl(seen_types, tcx, typing_env, *a_ty, *b_ty)
337327
}
338328
(ty::FnDef(..), ty::FnDef(..)) => {
339329
let a_poly_sig = a.fn_sig(tcx);
@@ -347,15 +337,14 @@ fn structurally_same_type_impl<'tcx>(
347337
(a_sig.abi, a_sig.safety, a_sig.c_variadic)
348338
== (b_sig.abi, b_sig.safety, b_sig.c_variadic)
349339
&& a_sig.inputs().iter().eq_by(b_sig.inputs().iter(), |a, b| {
350-
structurally_same_type_impl(seen_types, tcx, typing_env, *a, *b, ckind)
340+
structurally_same_type_impl(seen_types, tcx, typing_env, *a, *b)
351341
})
352342
&& structurally_same_type_impl(
353343
seen_types,
354344
tcx,
355345
typing_env,
356346
a_sig.output(),
357347
b_sig.output(),
358-
ckind,
359348
)
360349
}
361350
(ty::Tuple(..), ty::Tuple(..)) => {
@@ -383,14 +372,14 @@ fn structurally_same_type_impl<'tcx>(
383372
// An Adt and a primitive or pointer type. This can be FFI-safe if non-null
384373
// enum layout optimisation is being applied.
385374
(ty::Adt(..) | ty::Pat(..), _) if is_primitive_or_pointer(b) => {
386-
if let Some(a_inner) = types::repr_nullable_ptr(tcx, typing_env, a, ckind) {
375+
if let Some(a_inner) = types::repr_nullable_ptr(tcx, typing_env, a) {
387376
a_inner == b
388377
} else {
389378
false
390379
}
391380
}
392381
(_, ty::Adt(..) | ty::Pat(..)) if is_primitive_or_pointer(a) => {
393-
if let Some(b_inner) = types::repr_nullable_ptr(tcx, typing_env, b, ckind) {
382+
if let Some(b_inner) = types::repr_nullable_ptr(tcx, typing_env, b) {
394383
b_inner == a
395384
} else {
396385
false

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ late_lint_methods!(
194194
DefaultCouldBeDerived: DefaultCouldBeDerived::default(),
195195
DerefIntoDynSupertrait: DerefIntoDynSupertrait,
196196
DropForgetUseless: DropForgetUseless,
197-
ImproperCTypesDeclarations: ImproperCTypesDeclarations,
198-
ImproperCTypesDefinitions: ImproperCTypesDefinitions,
197+
ImproperCTypesLint: ImproperCTypesLint,
199198
InvalidFromUtf8: InvalidFromUtf8,
200199
VariantSizeDifferences: VariantSizeDifferences,
201200
PathStatements: PathStatements,

0 commit comments

Comments
 (0)