Skip to content

Rollup of 6 pull requests #111093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
5dd54fd
lint/ctypes: ext. abi fn-ptr in internal abi fn
davidtwco Mar 1, 2023
c1dcf26
abi: avoid ice for non-ffi-safe fn ptrs
davidtwco Mar 1, 2023
92ae35f
lint/ctypes: multiple external fn-ptrs in ty
davidtwco Mar 2, 2023
3e7ca3e
lint/ctypes: check other types for ext. fn-ptr ty
davidtwco Mar 2, 2023
1a7132d
rustdoc-search: fix incorrect doc comment
notriddle Apr 14, 2023
4c11822
rustdoc: restructure type search engine to pick-and-use IDs
notriddle Apr 15, 2023
1ece1ea
Stablize raw-dylib, link_ordinal and -Cdlltool
dpaoliello Mar 27, 2023
b6f81e0
rustdoc-search: give longer notification for type corrections
notriddle Apr 19, 2023
e0a7462
rustdoc-search: clean up `checkIfInGenerics` call at end of `checkType`
notriddle Apr 20, 2023
7529d87
rustdoc-search: make type name correction choice deterministic
notriddle Apr 20, 2023
395840c
rustdoc-search: use more descriptive "x not found; y instead" message
notriddle Apr 20, 2023
d5e7ac5
avoid duplicating TLS state between test std and realstd
RalfJung Apr 28, 2023
ed468ee
Encode def span for foreign RPITITs
compiler-errors Apr 30, 2023
bc68de9
remove pointless `FIXME` in `bootstrap::download`
onur-ozkan May 1, 2023
b3bc5f8
Rollup merge of #108611 - davidtwco:issue-94223-external-abi-fn-ptr-i…
Dylan-DPC May 2, 2023
108a26c
Rollup merge of #109677 - dpaoliello:rawdylib, r=michaelwoerister,wes…
Dylan-DPC May 2, 2023
c9f03bf
Rollup merge of #110371 - notriddle:notriddle/search-corrections, r=G…
Dylan-DPC May 2, 2023
6b3cfaa
Rollup merge of #110946 - RalfJung:tls-realstd, r=m-ou-se
Dylan-DPC May 2, 2023
c6ba892
Rollup merge of #111039 - compiler-errors:foreign-span-rpitit, r=tmiasko
Dylan-DPC May 2, 2023
5c7cd00
Rollup merge of #111069 - ozkanonur:remove-pointless-fixme, r=albertl…
Dylan-DPC May 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
lint/ctypes: check other types for ext. fn-ptr ty
Extend previous checks for external ABI fn-ptrs to use in internal
statics, constants, type aliases and algebraic data types.

Signed-off-by: David Wood <[email protected]>
  • Loading branch information
davidtwco committed Mar 6, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 3e7ca3e8518c9a2c8837a07a505cd85e82c01c81
64 changes: 63 additions & 1 deletion compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
@@ -1324,7 +1324,11 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
}

let mut visitor = FnPtrFinder { visitor: &*self, spans: Vec::new(), tys: Vec::new() };
self.cx.tcx.normalize_erasing_regions(self.cx.param_env, ty).visit_with(&mut visitor);
self.cx
.tcx
.try_normalize_erasing_regions(self.cx.param_env, ty)
.unwrap_or(ty)
.visit_with(&mut visitor);
hir::intravisit::Visitor::visit_ty(&mut visitor, hir_ty);

iter::zip(visitor.tys.drain(..), visitor.spans.drain(..)).collect()
@@ -1348,7 +1352,65 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
}
}

impl ImproperCTypesDefinitions {
fn check_ty_maybe_containing_foreign_fnptr<'tcx>(
&mut self,
cx: &LateContext<'tcx>,
hir_ty: &'tcx hir::Ty<'_>,
ty: Ty<'tcx>,
) {
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition };
for (fn_ptr_ty, span) in vis.find_fn_ptr_ty_with_external_abi(hir_ty, ty) {
vis.check_type_for_ffi_and_report_errors(span, fn_ptr_ty, true, false);
}
}
}

/// `ImproperCTypesDefinitions` checks items outside of foreign items (e.g. stuff that isn't in
/// `extern "C" { }` blocks):
///
/// - `extern "<abi>" fn` definitions are checked in the same way as the
/// `ImproperCtypesDeclarations` visitor checks functions if `<abi>` is external (e.g. "C").
/// - All other items which contain types (e.g. other functions, struct definitions, etc) are
/// checked for extern fn-ptrs with external ABIs.
impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
match item.kind {
hir::ItemKind::Static(ty, ..)
| hir::ItemKind::Const(ty, ..)
| hir::ItemKind::TyAlias(ty, ..) => {
self.check_ty_maybe_containing_foreign_fnptr(
cx,
ty,
cx.tcx.type_of(item.owner_id).subst_identity(),
);
}
// See `check_fn`..
hir::ItemKind::Fn(..) => {}
// See `check_field_def`..
hir::ItemKind::Union(..) | hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) => {}
// Doesn't define something that can contain a external type to be checked.
hir::ItemKind::Impl(..)
| hir::ItemKind::TraitAlias(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::OpaqueTy(..)
| hir::ItemKind::GlobalAsm(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::Mod(..)
| hir::ItemKind::Macro(..)
| hir::ItemKind::Use(..)
| hir::ItemKind::ExternCrate(..) => {}
}
}

fn check_field_def(&mut self, cx: &LateContext<'tcx>, field: &'tcx hir::FieldDef<'tcx>) {
self.check_ty_maybe_containing_foreign_fnptr(
cx,
field.ty,
cx.tcx.type_of(field.def_id).subst_identity(),
);
}

fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
1 change: 1 addition & 0 deletions tests/ui/hashmap/hashmap-memory.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-pass

#![allow(improper_ctypes_definitions)]
#![allow(non_camel_case_types)]
#![allow(dead_code)]
#![allow(unused_mut)]
33 changes: 33 additions & 0 deletions tests/ui/lint/lint-ctypes-94223.rs
Original file line number Diff line number Diff line change
@@ -7,3 +7,36 @@ pub fn bad(f: extern "C" fn([u8])) {}
pub fn bad_twice(f: Result<extern "C" fn([u8]), extern "C" fn([u8])>) {}
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
//~^^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe

struct BadStruct(extern "C" fn([u8]));
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe

enum BadEnum {
A(extern "C" fn([u8])),
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
}

enum BadUnion {
A(extern "C" fn([u8])),
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe
}

type Foo = extern "C" fn([u8]);
//~^ ERROR `extern` fn uses type `[u8]`, which is not FFI-safe

pub struct FfiUnsafe;

#[allow(improper_ctypes_definitions)]
extern "C" fn f(_: FfiUnsafe) {
unimplemented!()
}

pub static BAD: extern "C" fn(FfiUnsafe) = f;
//~^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe

pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
//~^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
//~^^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe

pub const BAD_CONST: extern "C" fn(FfiUnsafe) = f;
//~^ ERROR `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
94 changes: 93 additions & 1 deletion tests/ui/lint/lint-ctypes-94223.stderr
Original file line number Diff line number Diff line change
@@ -30,5 +30,97 @@ LL | pub fn bad_twice(f: Result<extern "C" fn([u8]), extern "C" fn([u8])>) {}
= help: consider using a raw pointer instead
= note: slices have no C equivalent

error: aborting due to 3 previous errors
error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:11:18
|
LL | struct BadStruct(extern "C" fn([u8]));
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent

error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:15:7
|
LL | A(extern "C" fn([u8])),
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent

error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:20:7
|
LL | A(extern "C" fn([u8])),
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent

error: `extern` fn uses type `[u8]`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:24:12
|
LL | type Foo = extern "C" fn([u8]);
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider using a raw pointer instead
= note: slices have no C equivalent

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:34:17
|
LL | pub static BAD: extern "C" fn(FfiUnsafe) = f;
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:37:30
|
LL | pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:37:56
|
LL | pub static BAD_TWICE: Result<extern "C" fn(FfiUnsafe), extern "C" fn(FfiUnsafe)> = Ok(f);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: `extern` fn uses type `FfiUnsafe`, which is not FFI-safe
--> $DIR/lint-ctypes-94223.rs:41:22
|
LL | pub const BAD_CONST: extern "C" fn(FfiUnsafe) = f;
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
note: the type is defined here
--> $DIR/lint-ctypes-94223.rs:27:1
|
LL | pub struct FfiUnsafe;
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 11 previous errors