@@ -2410,7 +2410,11 @@ impl CodeGenerator for CompInfo {
2410
2410
let mut needs_debug_impl = false ;
2411
2411
let mut needs_partialeq_impl = false ;
2412
2412
let needs_flexarray_impl = flex_array_generic. is_some ( ) ;
2413
- if let Some ( comment) = item. comment ( ctx) {
2413
+ let type_id = item. id ( ) . expect_type_id ( ctx) ;
2414
+
2415
+ if let Some ( comment) = item. comment ( ctx)
2416
+ . or_else ( || Self :: get_typedef_fallback_comment ( ctx, & type_id) )
2417
+ {
2414
2418
attributes. push ( attributes:: doc ( & comment) ) ;
2415
2419
}
2416
2420
@@ -2987,6 +2991,48 @@ impl CompInfo {
2987
2991
}
2988
2992
}
2989
2993
}
2994
+
2995
+ /// Use a fallback comment from a type alias to this type if necessary
2996
+ ///
2997
+ /// The documentation for a type could get lost in the following circumstances:
2998
+ ///
2999
+ /// - We have a type and a type alias with the same canonical path
3000
+ /// - The Documentation is only associated with the type alias
3001
+ ///
3002
+ /// In this case bindgen will not generate the type alias and the documentation would be lost.
3003
+ /// To avoid this, we check here if there is any type alias to this type, which has
3004
+ /// the same canonical path and return the comment as a fallback, if our type does
3005
+ /// not have documentation.
3006
+ fn get_typedef_fallback_comment (
3007
+ ctx : & BindgenContext ,
3008
+ type_id : & crate :: ir:: context:: TypeId ,
3009
+ ) -> Option < String > {
3010
+ if !ctx. options ( ) . generate_comments {
3011
+ return None ;
3012
+ }
3013
+ let type_alias_comment = ctx
3014
+ . items ( )
3015
+ . filter ( |( _id, alias) | {
3016
+ let Some ( this_ty) = alias. as_type ( ) else {
3017
+ return false ;
3018
+ } ;
3019
+ let TypeKind :: Alias ( alias_to) = this_ty. kind ( ) else {
3020
+ return false ;
3021
+ } ;
3022
+ //
3023
+ match ctx. resolve_type ( * alias_to) . kind ( ) {
3024
+ TypeKind :: ResolvedTypeRef ( resolved_typeid) => {
3025
+ resolved_typeid == type_id &&
3026
+ alias. canonical_path ( ctx) ==
3027
+ type_id. canonical_path ( ctx)
3028
+ }
3029
+ _ => false ,
3030
+ }
3031
+ } )
3032
+ . filter_map ( |( _id, item) | item. comment ( ctx) ) ;
3033
+ let alias_comment: Vec < String > = type_alias_comment. collect ( ) ;
3034
+ alias_comment. get ( 0 ) . cloned ( )
3035
+ }
2990
3036
}
2991
3037
2992
3038
impl Method {
0 commit comments