Skip to content

Commit 9a747eb

Browse files
committed
ImproperCTypes: also check in traits
Add new areas that are checked by ImproperCTypes lints: Function declarations(*) and definitions in traits and impls *) from the perspective of an FFI boundary, those are actually definitions
1 parent f74e4b5 commit 9a747eb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

compiler/rustc_lint/src/types/improper_ctypes.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,4 +1866,54 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesLint {
18661866
self.check_foreign_fn(cx, CItemKind::ExportedFunction, mir_sig, decl, mod_id, 0);
18671867
}
18681868
}
1869+
1870+
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, tr_it: &hir::TraitItem<'tcx>) {
1871+
match tr_it.kind {
1872+
hir::TraitItemKind::Const(hir_ty, _) => {
1873+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1874+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1875+
}
1876+
hir::TraitItemKind::Fn(sig, trait_fn) => {
1877+
match trait_fn {
1878+
// if the method is defined here,
1879+
// there is a matching ``LateLintPass::check_fn`` call,
1880+
// let's not redo that work
1881+
hir::TraitFn::Provided(_) => return,
1882+
hir::TraitFn::Required(_) => (),
1883+
}
1884+
let local_id = tr_it.owner_id.def_id;
1885+
1886+
self.check_fn_for_external_abi_fnptr(cx, local_id, sig.decl);
1887+
if !sig.header.abi.is_rustic_abi() {
1888+
let mir_sig = cx.tcx.fn_sig(local_id).instantiate_identity();
1889+
let mod_id = cx.tcx.parent_module_from_def_id(local_id).to_def_id();
1890+
self.check_foreign_fn(cx, CItemKind::ExportedFunction, mir_sig, sig.decl, mod_id, 0);
1891+
}
1892+
}
1893+
hir::TraitItemKind::Type(_, ty_maybe) => {
1894+
if let Some(hir_ty) = ty_maybe {
1895+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1896+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1897+
}
1898+
}
1899+
}
1900+
}
1901+
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, im_it: &hir::ImplItem<'tcx>) {
1902+
// note: we do not skip these checks eventhough they might generate dupe warnings because:
1903+
// - the corresponding trait might be in another crate
1904+
// - the corresponding trait might have some templating involved, so only the impl has the full type information
1905+
match im_it.kind {
1906+
hir::ImplItemKind::Type(hir_ty) => {
1907+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1908+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1909+
}
1910+
hir::ImplItemKind::Fn(_sig, _) => {
1911+
// see ``LateLintPass::check_fn``
1912+
}
1913+
hir::ImplItemKind::Const(hir_ty, _) => {
1914+
let ty = cx.tcx.type_of(hir_ty.hir_id.owner.def_id).instantiate_identity();
1915+
self.check_type_for_external_abi_fnptr(cx, hir_ty, ty);
1916+
}
1917+
}
1918+
}
18691919
}

0 commit comments

Comments
 (0)