Skip to content

Commit 099e873

Browse files
committed
Safety doc for FFI tables load constructor
1 parent e1fb507 commit 099e873

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

godot-codegen/src/generator/extension_interface.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use proc_macro2::{Ident, Literal, TokenStream};
1212
use quote::quote;
1313
use regex::Regex;
1414

15-
use crate::util::ident;
15+
use crate::util::{ident, make_load_safety_doc};
1616
use crate::SubmitFn;
1717

1818
pub fn generate_sys_interface_file(
@@ -77,15 +77,14 @@ fn generate_proc_address_funcs(h_path: &Path) -> TokenStream {
7777
}
7878

7979
// Do not derive Copy -- even though the struct is bitwise-copyable, this is rarely needed and may point to an error.
80+
let safety_doc = make_load_safety_doc();
8081
let code = quote! {
8182
pub struct GDExtensionInterface {
8283
#( #fptr_decls )*
8384
}
8485

8586
impl GDExtensionInterface {
86-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
87-
// can cause issues with people assuming they are sufficient.
88-
#[allow(clippy::missing_safety_doc)]
87+
#safety_doc
8988
pub(crate) unsafe fn load(
9089
get_proc_address: crate::GDExtensionInterfaceGetProcAddress,
9190
) -> Self {

godot-codegen/src/generator/method_tables.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::models::domain::{
1616
BuiltinClass, BuiltinMethod, BuiltinVariant, Class, ClassCodegenLevel, ClassLike, ClassMethod,
1717
ExtensionApi, FnDirection, Function, TyName,
1818
};
19-
use crate::util::ident;
19+
use crate::util::{ident, make_load_safety_doc};
2020
use crate::{conv, generator, special_cases, util};
2121

2222
pub fn make_builtin_lifecycle_table(api: &ExtensionApi) -> TokenStream {
@@ -276,6 +276,7 @@ fn make_named_method_table(info: NamedMethodTable) -> TokenStream {
276276

277277
// Assumes that both decls and inits already have a trailing comma.
278278
// This is necessary because some generators emit multiple lines (statements) per element.
279+
let safety_doc = make_load_safety_doc();
279280
quote! {
280281
#imports
281282

@@ -288,9 +289,7 @@ fn make_named_method_table(info: NamedMethodTable) -> TokenStream {
288289
pub const CLASS_COUNT: usize = #class_count;
289290
pub const METHOD_COUNT: usize = #method_count;
290291

291-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
292-
// can cause issues with people assuming they are sufficient.
293-
#[allow(clippy::missing_safety_doc)]
292+
#safety_doc
294293
pub unsafe fn load(
295294
#ctor_parameters
296295
) -> Self {
@@ -374,6 +373,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
374373

375374
// Assumes that inits already have a trailing comma.
376375
// This is necessary because some generators emit multiple lines (statements) per element.
376+
let safety_doc = make_load_safety_doc();
377377
quote! {
378378
#imports
379379

@@ -387,9 +387,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
387387
pub const CLASS_COUNT: usize = #class_count;
388388
pub const METHOD_COUNT: usize = #method_count;
389389

390-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
391-
// can cause issues with people assuming they are sufficient.
392-
#[allow(clippy::missing_safety_doc)]
390+
#safety_doc
393391
#unused_attr
394392
pub unsafe fn load(
395393
#ctor_parameters
@@ -440,6 +438,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
440438

441439
// Assumes that inits already have a trailing comma.
442440
// This is necessary because some generators emit multiple lines (statements) per element.
441+
let safety_doc = make_load_safety_doc();
443442
quote! {
444443
#imports
445444
use crate::StringCache;
@@ -462,9 +461,7 @@ fn make_method_table(info: IndexedMethodTable) -> TokenStream {
462461
pub const CLASS_COUNT: usize = #class_count;
463462
pub const METHOD_COUNT: usize = #method_count;
464463

465-
// TODO: Figure out the right safety preconditions. This currently does not have any because incomplete safety docs
466-
// can cause issues with people assuming they are sufficient.
467-
#[allow(clippy::missing_safety_doc)]
464+
#safety_doc
468465
#unused_attr
469466
pub unsafe fn load() -> Self {
470467
// SAFETY: interface and lifecycle tables are initialized at this point, so we can get 'static references to them.

godot-codegen/src/util.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ pub fn lifetime(s: &str) -> TokenStream {
8585
TokenStream::from_iter([tk_apostrophe, tk_lifetime])
8686
}
8787

88+
pub fn make_load_safety_doc() -> TokenStream {
89+
quote! {
90+
/// # Safety
91+
/// - Must be called exactly once during library initialization.
92+
/// - All parameters (dependencies) must have been initialized and valid.
93+
}
94+
}
95+
8896
// This function is duplicated in godot-macros\src\util\mod.rs
8997
#[rustfmt::skip]
9098
pub fn safe_ident(s: &str) -> Ident {

0 commit comments

Comments
 (0)