Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/architecture/winrt-interface-implementations.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ Python declarations, annotations, imports, and conversion helpers share a
module-local symbol mapping keyed by canonical `TypeIdentity` and symbol role.
Self-class and self-interface references use the actual local declaration;
foreign class `Like` and identity markers import the defining module's symbol
and alias it to the consuming module's reference name. Handler, delegate, array,
and alias it to the consuming module's reference name. Owning class, `Like`,
and identity declarations keep their public names; conflicting foreign imports
or inline required-interface views receive identity-qualified aliases. Each
role is allocated independently, so aliasing an imported class does not rename
its noncolliding `Like` or identity import. Handler, delegate, array,
and result-dictionary conversions use the same mapping as ordinary members.
Self-import filtering distinguishes namespaces, kinds, and named versus closed
generic types, so `IBox<String>` cannot hide a distinct named `IBox_String`.
Expand All @@ -130,8 +134,11 @@ Struct modules retain their public `pack_*`, `unpack_*`, and `*_TYPE` names.
When different struct names normalize to the same helper name (for example,
`URLValue` and `UrlValue`), consumers import those helpers under distinct
identity-qualified aliases. Standalone interface and runtime-class modules keep
embedded struct class names and disambiguate only colliding helper symbols;
standalone struct modules retain aliases for imported fields. The existing
noncolliding embedded struct class names. When an embedded type conflicts with
an owning declaration or another visible type role, its declaration, annotations,
nested defaults, and forward/reverse conversions use the same allocated name;
native struct descriptors retain the original metadata identity.
Standalone struct modules retain aliases for imported fields. The existing
fail-closed guard for identical raw struct names in one closure remains.
The allocator also compares helpers with actual local and imported type/marker
symbols, not unrelated types elsewhere in the package. If a foreign type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub(super) fn generate_struct_helpers(context: &PythonProjectionContext, s: &Typ
return generate_foundation_struct_helpers(context, s, kind);
}

let (namespace, name, fields) = match s {
let (namespace, metadata_name, fields) = match s {
TypeMeta::Struct {
namespace,
name,
Expand All @@ -110,6 +110,7 @@ pub(super) fn generate_struct_helpers(context: &PythonProjectionContext, s: &Typ
_ => return String::new(),
};
let mut out = String::new();
let name = context.struct_symbol(s, PythonSymbol::Type);
let pack = context.struct_symbol(s, PythonSymbol::Pack);
let unpack = context.struct_symbol(s, PythonSymbol::Unpack);
let private_pack = context.struct_symbol(s, PythonSymbol::PrivatePack);
Expand Down Expand Up @@ -218,7 +219,7 @@ pub(super) fn generate_struct_helpers(context: &PythonProjectionContext, s: &Typ
out.push_str(&format!("{private_unpack} = {unpack}\n"));

// Type constant
let full_name = format!("{}.{}", namespace, name);
let full_name = format!("{}.{}", namespace, metadata_name);
let field_types: Vec<String> = fields.iter().map(|f| py_dynwinrt_type(&f.typ)).collect();
out.push_str(&format!(
"{type_constant} = DynWinRTType.struct_type('{}', [{}])\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl Projector<'_> {
if self.context.is_packaged() {
py_runtime_type_symbol(self.context, typ, name)
} else {
name.clone()
self.context.reference_name_for_type(typ)
}
}

Expand Down
127 changes: 122 additions & 5 deletions tools/dynwinrt-codegen/src/codegen/winrt/python/naming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,8 @@ impl PythonProjectionContext {
let mut roles = vec![PythonSymbol::Type];
if identity.kind() == Some(TypeIdentityKind::Class) {
roles.extend([PythonSymbol::Like, PythonSymbol::Identity]);
} else if identity.kind() == Some(TypeIdentityKind::Interface) {
roles.push(PythonSymbol::Identity);
}
for role in roles {
symbols.insert(
Expand Down Expand Up @@ -862,16 +864,18 @@ impl PythonProjectionContext {
.iter()
.filter(|(identity, role)| !can_alias_helper(identity, *role))
.count();
let owns_struct_symbol = group.iter().any(|(identity, _)| {
owner.as_ref() == Some(identity)
&& identity.kind() == Some(TypeIdentityKind::Struct)
});
let fixed_type_count = group
.iter()
.filter(|(identity, role)| {
!can_alias_helper(identity, *role) && !role.is_registration()
})
.count();
for (identity, role) in group {
let rename = if role.is_registration() {
fixed_symbol_count > 1
} else {
owner.as_ref() != Some(identity)
&& (can_alias_helper(identity, *role) || owns_struct_symbol)
&& (can_alias_helper(identity, *role) || fixed_type_count > 1)
};
if !rename {
continue;
Expand Down Expand Up @@ -1385,6 +1389,119 @@ pub fn to_snake_case_filename(name: &str) -> String {
mod tests {
use super::*;

#[test]
fn companion_aliases_freeze_roles_and_use_only_visible_symbols() {
let owner = TypeIdentity::named(TypeIdentityKind::Class, "Audit", "Widget");
let peer = TypeIdentity::named(TypeIdentityKind::Class, "Audit", "WidgetLike");
let occupied =
TypeIdentity::named(TypeIdentityKind::Enum, "Other", "Audit_WidgetLike_class");
let identities = [owner.clone(), peer.clone(), occupied.clone()];
let imports = [
(peer.clone(), PythonSymbol::Type),
(peer.clone(), PythonSymbol::Like),
(occupied.clone(), PythonSymbol::Type),
];
for packaged in [false, true] {
let context = PythonProjectionContext::new(identities.clone(), packaged).unwrap();
let module = context.with_local_types(Some(owner.clone()), &[], imports.clone(), []);
for role in [
PythonSymbol::Type,
PythonSymbol::Like,
PythonSymbol::Identity,
] {
assert_eq!(
module.symbol_reference(&owner, role),
context.symbol_declaration(&owner, role)
);
}
let alias = module.reference_name(&peer);
assert!(alias.starts_with("Audit_WidgetLike_class_"), "{alias}");
assert_eq!(
module.symbol_reference(&peer, PythonSymbol::Like),
"WidgetLikeLike"
);
assert_eq!(module.reference_name(&occupied), "Audit_WidgetLike_class");
let reversed =
PythonProjectionContext::new(identities.clone().into_iter().rev(), packaged)
.unwrap();
assert_eq!(
module.module_symbols,
reversed
.with_local_types(
Some(owner.clone()),
&[],
imports.clone().into_iter().rev(),
[]
)
.module_symbols
);
let unused = context.with_local_types(Some(owner.clone()), &[], [], []);
assert!(unused.module_symbols.is_empty());
let no_fallback = context.with_local_types(
Some(owner.clone()),
&[],
imports[..2].iter().cloned(),
[],
);
assert_eq!(no_fallback.reference_name(&peer), "Audit_WidgetLike_class");

let imported_like = context.with_local_types(
Some(peer.clone()),
&[],
[
(owner.clone(), PythonSymbol::Type),
(owner.clone(), PythonSymbol::Like),
],
[],
);
assert_eq!(imported_like.reference_name(&owner), "Widget");
assert_eq!(
imported_like.symbol_reference(&owner, PythonSymbol::Like),
"Audit_Widget_classLike"
);
}
}

#[test]
fn interface_identity_declarations_and_foreign_identity_imports_are_reserved() {
let owner = TypeIdentity::named(TypeIdentityKind::Interface, "Audit", "IUse");
let peer = TypeIdentity::named(TypeIdentityKind::Class, "Audit", "_IUseIdentity");
let base = TypeIdentity::named(TypeIdentityKind::Class, "Audit", "Base");
let foreign = TypeIdentity::named(TypeIdentityKind::Enum, "Audit", "_BaseIdentity");
let context = PythonProjectionContext::packaged([
owner.clone(),
peer.clone(),
base.clone(),
foreign.clone(),
])
.unwrap();
let module = context.with_local_types(
Some(owner.clone()),
&[],
[
(peer.clone(), PythonSymbol::Type),
(peer.clone(), PythonSymbol::Like),
(base.clone(), PythonSymbol::Identity),
(foreign.clone(), PythonSymbol::Type),
],
[],
);
assert_eq!(
module.symbol_reference(&owner, PythonSymbol::Identity),
"_IUseIdentity"
);
assert_ne!(module.reference_name(&peer), "_IUseIdentity");
assert_eq!(
module.symbol_reference(&peer, PythonSymbol::Like),
"_IUseIdentityLike"
);
assert_eq!(
module.symbol_import(&base, PythonSymbol::Identity),
"_BaseIdentity as _Audit_Base_classIdentity"
);
assert_ne!(module.reference_name(&foreign), "_BaseIdentity");
}

#[test]
fn module_helper_aliases_preserve_isolated_exports_and_input_order() {
let alpha = TypeMeta::Struct {
Expand Down
10 changes: 3 additions & 7 deletions tools/dynwinrt-codegen/src/codegen/winrt/python/stub_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,10 @@ pub(super) fn emit_struct_stub(context: &PythonProjectionContext, s: &TypeMeta)
);
}

let (_namespace, name, fields) = match s {
TypeMeta::Struct {
namespace,
name,
fields,
} => (namespace, name, fields),
_ => return String::new(),
let TypeMeta::Struct { fields, .. } = s else {
return String::new();
};
let name = context.struct_symbol(s, PythonSymbol::Type);
let mut out = String::new();
let slot_names = fields.iter().map(py_struct_slot_name).collect::<Vec<_>>();

Expand Down
Loading
Loading