Skip to content

Commit a5137ff

Browse files
committed
Use trait, not impl, definition for X_as_Trait functions
When we map a trait impl block, we historically didn't know how to resolve the types in the trait context, so we just used the impl context definitions and assumed the types were the same. Sadly, if we have an associated type that is bounded by a trait, we need to use the trait bound to figure out what to write for the X_as_Trait method declarations. Since we now have the ability to generate full trait-context type resolvers, we do so here.
1 parent f180d14 commit a5137ff

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

c-bindings-gen/src/main.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
947947
// mappings from a trai defined in a different file, we may mis-resolve or
948948
// fail to resolve the mapped types. Thus, we have to construct a new
949949
// resolver for the module that the trait was defined in here first.
950-
let trait_resolver = get_module_type_resolver!(full_trait_path, types.crate_libs, types.crate_types);
950+
let mut trait_resolver = get_module_type_resolver!(full_trait_path, types.crate_libs, types.crate_types);
951951
gen_types.learn_associated_types(trait_obj, &trait_resolver);
952952
let mut impl_associated_types = HashMap::new();
953953
for item in i.items.iter() {
@@ -1107,17 +1107,12 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
11071107
_ => {}
11081108
}
11091109
}
1110-
if uncallable_function {
1111-
let mut trait_resolver = get_module_type_resolver!(full_trait_path, $types.crate_libs, $types.crate_types);
1112-
write_method_params(w, &$trait_meth.sig, "c_void", &mut trait_resolver, Some(&meth_gen_types), true, true);
1113-
} else {
1114-
write_method_params(w, &$m.sig, "c_void", $types, Some(&meth_gen_types), true, true);
1115-
}
1110+
write_method_params(w, &$trait_meth.sig, "c_void", &mut trait_resolver, Some(&meth_gen_types), true, true);
11161111
write!(w, " {{\n\t").unwrap();
11171112
if uncallable_function {
11181113
write!(w, "unreachable!();").unwrap();
11191114
} else {
1120-
write_method_var_decl_body(w, &$m.sig, "", $types, Some(&meth_gen_types), false);
1115+
write_method_var_decl_body(w, &$trait_meth.sig, "", &mut trait_resolver, Some(&meth_gen_types), false);
11211116
let mut takes_self = false;
11221117
for inp in $m.sig.inputs.iter() {
11231118
if let syn::FnArg::Receiver(_) = inp {
@@ -1147,7 +1142,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
11471142
},
11481143
_ => {},
11491144
}
1150-
write_method_call_params(w, &$m.sig, "", $types, Some(&meth_gen_types), &real_type, false);
1145+
write_method_call_params(w, &$trait_meth.sig, "", &mut trait_resolver, Some(&meth_gen_types), &real_type, false);
11511146
}
11521147
write!(w, "\n}}\n").unwrap();
11531148
if let syn::ReturnType::Type(_, rtype) = &$m.sig.output {
@@ -1185,7 +1180,6 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
11851180
assert!(meth.default.is_some());
11861181
let old_gen_types = gen_types;
11871182
gen_types = GenericTypes::new(Some(resolved_path.clone()));
1188-
let mut trait_resolver = get_module_type_resolver!(full_trait_path, types.crate_libs, types.crate_types);
11891183
impl_meth!(meth, meth, full_trait_path, trait_obj, "", &mut trait_resolver);
11901184
gen_types = old_gen_types;
11911185
},

0 commit comments

Comments
 (0)