Do not make `Deref<T>` generic params always a `&T` for traits.
authorMatt Corallo <git@bluematt.me>
Fri, 24 Jun 2022 01:20:33 +0000 (01:20 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 28 Jun 2022 02:26:57 +0000 (02:26 +0000)
We never expect traits to be passed by reference, so when using
the new-style generic resolution, we shouldn't force all `Deref`s
to `&T`, at least if the `T` is a trait.

c-bindings-gen/src/types.rs

index 2ea9221a63f6a6a22a76c01b93e9a0d29ae92db3..598552102f3691ba9f50839ad1e6cda673700e54 100644 (file)
@@ -283,14 +283,16 @@ impl<'a, 'p: 'a> GenericTypes<'a, 'p> {
                                                                        non_lifetimes_processed = true;
                                                                        assert_simple_bound(&trait_bound);
                                                                        let resolved = types.resolve_path(&trait_bound.path, None);
-                                                                       let ref_ty = syn::Type::Reference(syn::TypeReference {
-                                                                               and_token: syn::Token![&](Span::call_site()),
-                                                                               lifetime: None, mutability: None,
-                                                                               elem: Box::new(syn::Type::Path(syn::TypePath {
-                                                                                       qself: None, path: string_path_to_syn_path(&resolved)
-                                                                               })),
+                                                                       let ty = syn::Type::Path(syn::TypePath {
+                                                                               qself: None, path: string_path_to_syn_path(&resolved)
                                                                        });
-                                                                       self.default_generics.insert(p_ident, (ref_ty.clone(), ref_ty));
+                                                                       let ref_ty = parse_quote!(&#ty);
+                                                                       if types.crate_types.traits.get(&resolved).is_some() {
+                                                                               self.default_generics.insert(p_ident, (ty, ref_ty));
+                                                                       } else {
+                                                                               self.default_generics.insert(p_ident, (ref_ty.clone(), ref_ty));
+                                                                       }
+
                                                                        *gen = Some(resolved);
                                                                }
                                                        }