Skip to content

Commit 46603d5

Browse files
committed
Fix init and new method default nullability
1 parent 9130a2d commit 46603d5

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

crates/header-translator/src/method.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ impl<'tu> PartialMethod<'tu> {
421421
}
422422

423423
let result_type = entity.get_result_type().expect("method return type");
424-
let mut result_type = Ty::parse_method_return(result_type, context);
424+
let default_nonnull = (selector == "init" && !is_class) || (selector == "new" && is_class);
425+
let mut result_type = Ty::parse_method_return(result_type, default_nonnull, context);
425426

426427
let memory_management = MemoryManagement::new(is_class, &selector, &result_type, modifiers);
427428

crates/header-translator/src/rust_type.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,22 @@ impl Ty {
12111211
}
12121212
}
12131213

1214-
pub fn parse_method_return(ty: Type<'_>, context: &Context<'_>) -> Self {
1215-
let ty = Inner::parse(ty, Lifetime::Unspecified, context);
1214+
pub fn parse_method_return(ty: Type<'_>, default_nonnull: bool, context: &Context<'_>) -> Self {
1215+
let mut ty = Inner::parse(ty, Lifetime::Unspecified, context);
1216+
1217+
// As in `parse_property_return`, the nullability is not guaranteed by
1218+
// the method, and can also fail in OOM situations, but that is
1219+
// handled by `#[method_id(...)]`
1220+
if default_nonnull {
1221+
match &mut ty {
1222+
Inner::Id { nullability, .. } => {
1223+
if *nullability == Nullability::Unspecified {
1224+
*nullability = Nullability::NonNull;
1225+
}
1226+
}
1227+
_ => warn!(?ty, "`default_nonnull` which is not an object"),
1228+
}
1229+
}
12161230

12171231
ty.visit_lifetime(|lifetime| {
12181232
if lifetime != Lifetime::Unspecified {
@@ -1233,7 +1247,7 @@ impl Ty {
12331247
}
12341248

12351249
pub fn parse_function_return(ty: Type<'_>, context: &Context<'_>) -> Self {
1236-
let mut this = Self::parse_method_return(ty, context);
1250+
let mut this = Self::parse_method_return(ty, false, context);
12371251
this.kind = TyKind::FnReturn;
12381252
this
12391253
}

crates/icrate/src/generated

0 commit comments

Comments
 (0)