Skip to content

Commit fd56162

Browse files
committed
Auto merge of rust-lang#113921 - davidtwco:lint-ctypes-issue-113900, r=petrochenkov
lint/ctypes: only try normalize Fixes rust-lang#113900. Now that this lint runs on any external-ABI fn-ptr, normalization won't always succeed, so use `try_normalize_erasing_regions` instead.
2 parents fc8a3e3 + 09434a2 commit fd56162

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

compiler/rustc_lint/src/types.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -966,12 +966,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
966966
args: GenericArgsRef<'tcx>,
967967
) -> FfiResult<'tcx> {
968968
let field_ty = field.ty(self.cx.tcx, args);
969-
if field_ty.has_opaque_types() {
970-
self.check_type_for_ffi(cache, field_ty)
971-
} else {
972-
let field_ty = self.cx.tcx.normalize_erasing_regions(self.cx.param_env, field_ty);
973-
self.check_type_for_ffi(cache, field_ty)
974-
}
969+
let field_ty = self
970+
.cx
971+
.tcx
972+
.try_normalize_erasing_regions(self.cx.param_env, field_ty)
973+
.unwrap_or(field_ty);
974+
self.check_type_for_ffi(cache, field_ty)
975975
}
976976

977977
/// Checks if the given `VariantDef`'s field types are "ffi-safe".
@@ -1320,7 +1320,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13201320
if let Some(ty) = self
13211321
.cx
13221322
.tcx
1323-
.normalize_erasing_regions(self.cx.param_env, ty)
1323+
.try_normalize_erasing_regions(self.cx.param_env, ty)
1324+
.unwrap_or(ty)
13241325
.visit_with(&mut ProhibitOpaqueTypes)
13251326
.break_value()
13261327
{
@@ -1338,16 +1339,12 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13381339
is_static: bool,
13391340
is_return_type: bool,
13401341
) {
1341-
// We have to check for opaque types before `normalize_erasing_regions`,
1342-
// which will replace opaque types with their underlying concrete type.
13431342
if self.check_for_opaque_ty(sp, ty) {
13441343
// We've already emitted an error due to an opaque type.
13451344
return;
13461345
}
13471346

1348-
// it is only OK to use this function because extern fns cannot have
1349-
// any generic types right now:
1350-
let ty = self.cx.tcx.normalize_erasing_regions(self.cx.param_env, ty);
1347+
let ty = self.cx.tcx.try_normalize_erasing_regions(self.cx.param_env, ty).unwrap_or(ty);
13511348

13521349
// C doesn't really support passing arrays by value - the only way to pass an array by value
13531350
// is through a struct. So, first test that the top level isn't an array, and then

tests/ui/lint/lint-ctypes-113900.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
3+
// Extending `improper_ctypes` to check external-ABI fn-ptrs means that it can encounter
4+
// projections which cannot be normalized - unsurprisingly, this shouldn't crash the compiler.
5+
6+
trait Bar {
7+
type Assoc;
8+
}
9+
10+
type Foo<T> = extern "C" fn() -> <T as Bar>::Assoc;
11+
12+
fn main() {}

0 commit comments

Comments
 (0)