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