@@ -94,19 +94,42 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
9494 let api_level = class. api_level ;
9595 let init_level = api_level. to_init_level ( ) ;
9696
97+ // These attributes are for our nightly docs pipeline, which enables "only available in ..." labels in the HTML output. The website CI sets
98+ // RUSTFLAGS="--cfg published_docs" during the `cargo +nightly doc` invocation. They are applied to classes, interface traits, sidecar modules,
99+ // the notification enum, other enums and default-parameter extender structs.
100+ //
101+ // In other parts of the codebase, #[cfg] statements are replaced with #[doc(cfg)] using sed/sd. However, that doesn't work here, because
102+ // generated files are output in ./target/build/debug. Upon doing sed/sd replacements on these files, cargo doc will either treat them as
103+ // unchanged (doing nothing), or rebuild the generated files into a _different_ folder. Therefore, the generator itself must already provide
104+ // the correct attributes from the start.
105+ let ( cfg_attributes, cfg_inner_attributes) ;
106+ if class. is_experimental {
107+ cfg_attributes = quote ! {
108+ // #[cfg(feature = "experimental-godot-api")]
109+ #[ cfg_attr( published_docs, doc( cfg( feature = "experimental-godot-api" ) ) ) ]
110+ } ;
111+ cfg_inner_attributes = quote ! {
112+ // #![cfg(feature = "experimental-godot-api")]
113+ #![ cfg_attr( published_docs, doc( cfg( feature = "experimental-godot-api" ) ) ) ]
114+ } ;
115+ } else {
116+ cfg_attributes = TokenStream :: new ( ) ;
117+ cfg_inner_attributes = TokenStream :: new ( ) ;
118+ } ;
119+
97120 let FnDefinitions {
98121 functions : methods,
99122 builders,
100- } = make_class_methods ( class, & class. methods , ctx) ;
123+ } = make_class_methods ( class, & class. methods , & cfg_attributes , ctx) ;
101124
102- let enums = enums:: make_enums ( & class. enums ) ;
125+ let enums = enums:: make_enums ( & class. enums , & cfg_attributes ) ;
103126 let constants = constants:: make_constants ( & class. constants ) ;
104127 let inherits_macro = format_ident ! ( "unsafe_inherits_transitive_{}" , class_name. rust_ty) ;
105128 let deref_impl = make_deref_impl ( class_name, & base_ty) ;
106129
107130 let all_bases = ctx. inheritance_tree ( ) . collect_all_bases ( class_name) ;
108131 let ( notification_enum, notification_enum_name) =
109- notifications:: make_notification_enum ( class_name, & all_bases, ctx) ;
132+ notifications:: make_notification_enum ( class_name, & all_bases, & cfg_attributes , ctx) ;
110133
111134 // Associated "sidecar" module is made public if there are other symbols related to the class, which are not
112135 // in top-level godot::engine module (notification enums are not in the sidecar, but in godot::engine::notify).
@@ -120,11 +143,13 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
120143 has_sidecar_module,
121144 ) ;
122145 let module_doc = docs:: make_module_doc ( class_name) ;
146+
123147 let virtual_trait = virtual_traits:: make_virtual_methods_trait (
124148 class,
125149 & all_bases,
126150 & virtual_trait_str,
127151 & notification_enum_name,
152+ & cfg_attributes,
128153 view,
129154 ) ;
130155
@@ -156,6 +181,7 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
156181 let imports = util:: make_imports ( ) ;
157182 let tokens = quote ! {
158183 #![ doc = #module_doc]
184+ #cfg_inner_attributes
159185
160186 #imports
161187 use crate :: engine:: notify:: * ;
@@ -166,6 +192,7 @@ fn make_class(class: &Class, ctx: &mut Context, view: &ApiView) -> GeneratedClas
166192
167193 #[ doc = #class_doc]
168194 #[ doc = #construct_doc]
195+ #cfg_attributes
169196 #[ derive( Debug ) ]
170197 #[ repr( C ) ]
171198 pub struct #class_name {
@@ -406,12 +433,17 @@ fn make_bounds(class: &Class) -> (Ident, Ident) {
406433 ( assoc_memory, assoc_dyn_memory)
407434}
408435
409- fn make_class_methods ( class : & Class , methods : & [ ClassMethod ] , ctx : & mut Context ) -> FnDefinitions {
436+ fn make_class_methods (
437+ class : & Class ,
438+ methods : & [ ClassMethod ] ,
439+ cfg_attributes : & TokenStream ,
440+ ctx : & mut Context ,
441+ ) -> FnDefinitions {
410442 let get_method_table = class. api_level . table_global_getter ( ) ;
411443
412- let definitions = methods
413- . iter ( )
414- . map ( |method| make_class_method_definition ( class , method , & get_method_table , ctx ) ) ;
444+ let definitions = methods. iter ( ) . map ( |method| {
445+ make_class_method_definition ( class , method , & get_method_table , cfg_attributes , ctx )
446+ } ) ;
415447
416448 FnDefinitions :: expand ( definitions)
417449}
@@ -420,6 +452,7 @@ fn make_class_method_definition(
420452 class : & Class ,
421453 method : & ClassMethod ,
422454 get_method_table : & Ident ,
455+ cfg_attributes : & TokenStream ,
423456 ctx : & mut Context ,
424457) -> FnDefinition {
425458 let FnDirection :: Outbound { hash } = method. direction ( ) else {
@@ -489,5 +522,6 @@ fn make_class_method_definition(
489522 ptrcall_invocation,
490523 } ,
491524 None ,
525+ cfg_attributes,
492526 )
493527}
0 commit comments