blob: c2ee1d1b6cee9c45d360201dc26dcfa972eb17ce [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnay9dcb8332020-04-30 20:37:33 -07002use crate::syntax::namespace::Namespace;
David Tolnayd763f4c2020-04-22 16:09:19 -07003use crate::syntax::{
Joel Galensonc03402a2020-04-23 17:31:09 -07004 error, ident, Api, Enum, ExternFn, ExternType, Lang, Receiver, Ref, Slice, Struct, Ty1, Type,
5 Types,
David Tolnayd763f4c2020-04-22 16:09:19 -07006};
David Tolnay09462ac2020-03-20 14:58:41 -07007use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07008use quote::{quote, ToTokens};
Joel Galensonc03402a2020-04-23 17:31:09 -07009use std::collections::HashSet;
David Tolnaya420f012020-03-25 17:55:56 -070010use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -040011use syn::{Error, Result};
12
David Tolnay26a2a1d2020-03-25 17:26:43 -070013struct Check<'a> {
David Tolnay9dcb8332020-04-30 20:37:33 -070014 namespace: &'a Namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070015 apis: &'a [Api],
16 types: &'a Types<'a>,
17 errors: &'a mut Vec<Error>,
18}
19
David Tolnay9dcb8332020-04-30 20:37:33 -070020pub(crate) fn typecheck(namespace: &Namespace, apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070021 let mut errors = Vec::new();
22 let mut cx = Check {
David Tolnay9dcb8332020-04-30 20:37:33 -070023 namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070024 apis,
25 types,
26 errors: &mut errors,
27 };
28 do_typecheck(&mut cx);
29 combine_errors(errors)
30}
David Tolnay7db73692019-10-20 14:51:12 -040031
David Tolnay26a2a1d2020-03-25 17:26:43 -070032fn do_typecheck(cx: &mut Check) {
David Tolnay9dcb8332020-04-30 20:37:33 -070033 for segment in cx.namespace {
34 cx.errors.extend(ident::check(segment).err());
35 }
36
David Tolnay26a2a1d2020-03-25 17:26:43 -070037 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040038 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070039 Type::Ident(ident) => check_type_ident(cx, ident),
40 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070041 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070042 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070043 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070044 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070045 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040046 _ => {}
47 }
48 }
49
David Tolnay26a2a1d2020-03-25 17:26:43 -070050 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040051 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070052 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070053 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay0b368ae2020-04-22 17:55:02 -070054 Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070055 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040056 _ => {}
57 }
58 }
59
David Tolnay26a2a1d2020-03-25 17:26:43 -070060 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040061 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070062 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040063 }
64 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070065 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040066 }
67 }
68
David Tolnay26a2a1d2020-03-25 17:26:43 -070069 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040070}
71
David Tolnaya420f012020-03-25 17:55:56 -070072impl Check<'_> {
73 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
74 self.errors.push(Error::new_spanned(sp, msg));
75 }
76}
77
David Tolnay26a2a1d2020-03-25 17:26:43 -070078fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070079 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070080 && !cx.types.structs.contains_key(ident)
Joel Galensonc03402a2020-04-23 17:31:09 -070081 && !cx.types.enums.contains_key(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -070082 && !cx.types.cxx.contains(ident)
83 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070084 {
David Tolnaya420f012020-03-25 17:55:56 -070085 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070086 }
87}
88
David Tolnay26a2a1d2020-03-25 17:26:43 -070089fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070090 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070091 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070092 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070093 }
David Tolnaya420f012020-03-25 17:55:56 -070094
David Tolnayd4e68302020-03-25 12:04:17 -070095 if Atom::from(ident).is_none() {
96 return;
97 }
98 }
David Tolnaya420f012020-03-25 17:55:56 -070099
100 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -0700101}
102
David Tolnayc6d891e2020-04-25 12:06:34 -0700103fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
104 if let Type::Ident(ident) = &ty.inner {
105 if cx.types.cxx.contains(ident) {
106 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +0700107 return;
David Tolnayc6d891e2020-04-25 12:06:34 -0700108 }
109
110 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700111 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
112 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
113 Some(Bool) | Some(RustString) => { /* todo */ }
114 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700115 }
116 }
117
David Tolnayc6d891e2020-04-25 12:06:34 -0700118 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700119}
120
David Tolnay26a2a1d2020-03-25 17:26:43 -0700121fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700122 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700123 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700124 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700125 }
David Tolnaya420f012020-03-25 17:55:56 -0700126
David Tolnayd4e68302020-03-25 12:04:17 -0700127 match Atom::from(ident) {
128 None | Some(CxxString) => return,
129 _ => {}
130 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700131 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700132 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700133 }
David Tolnaya420f012020-03-25 17:55:56 -0700134
135 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700136}
137
David Tolnayfff4c8a2020-04-25 11:45:20 -0700138fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700139 if let Type::Ident(ident) = &ptr.inner {
140 if cx.types.rust.contains(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700141 cx.error(
142 ptr,
143 "C++ vector containing a Rust type is not supported yet",
144 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700145 }
146
147 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700148 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
149 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return,
150 Some(CxxString) => { /* todo */ }
151 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700152 }
153 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700154
Myron Ahneba35cf2020-02-05 19:41:51 +0700155 cx.error(ptr, "unsupported vector target type");
156}
157
David Tolnay26a2a1d2020-03-25 17:26:43 -0700158fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700159 if ty.lifetime.is_some() {
160 cx.error(ty, "references with explicit lifetimes are not supported");
161 }
162
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700163 match ty.inner {
164 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700165 Type::Ref(_) => {
166 cx.error(ty, "C++ does not allow references to references");
167 return;
168 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700169 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700170 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700171
172 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700173}
174
David Tolnayeebe9b72020-04-14 16:32:18 -0700175fn check_type_slice(cx: &mut Check, ty: &Slice) {
176 cx.error(ty, "only &[u8] is supported so far, not other slice types");
177}
178
David Tolnay26a2a1d2020-03-25 17:26:43 -0700179fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay0b368ae2020-04-22 17:55:02 -0700180 check_reserved_name(cx, &strct.ident);
181
David Tolnayd4e68302020-03-25 12:04:17 -0700182 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700183 let span = span_for_struct_error(strct);
184 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700185 }
David Tolnaya420f012020-03-25 17:55:56 -0700186
David Tolnayd4e68302020-03-25 12:04:17 -0700187 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700188 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700189 let desc = describe(cx, &field.ty);
190 let msg = format!("using {} by value is not supported", desc);
191 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700192 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700193 if let Type::Fn(_) = field.ty {
194 cx.error(
195 field,
196 "function pointers in a struct field are not implemented yet",
197 );
198 }
David Tolnayd4e68302020-03-25 12:04:17 -0700199 }
200}
201
Joel Galensonc03402a2020-04-23 17:31:09 -0700202fn check_api_enum(cx: &mut Check, enm: &Enum) {
203 check_reserved_name(cx, &enm.ident);
204
205 if enm.variants.is_empty() {
206 let span = span_for_enum_error(enm);
207 cx.error(span, "enums without any variants are not supported");
208 }
209
210 let mut discriminants = HashSet::new();
211 enm.variants.iter().fold(0, |next_discriminant, variant| {
David Tolnayd7984c22020-04-30 20:09:31 -0700212 let discriminant = variant.discriminant.unwrap_or(next_discriminant);
Joel Galensonc03402a2020-04-23 17:31:09 -0700213 if !discriminants.insert(discriminant) {
214 let msg = format!("discriminant value `{}` already exists", discriminant);
215 cx.error(span_for_enum_error(enm), msg);
216 }
217 discriminant + 1
218 });
219}
220
David Tolnay0b368ae2020-04-22 17:55:02 -0700221fn check_api_type(cx: &mut Check, ty: &ExternType) {
222 check_reserved_name(cx, &ty.ident);
223}
224
David Tolnay26a2a1d2020-03-25 17:26:43 -0700225fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700226 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700227 let ref span = span_for_receiver_error(receiver);
228
229 if receiver.ty == "Self" {
230 let mutability = match receiver.mutability {
231 Some(_) => "mut ",
232 None => "",
233 };
234 let msg = format!(
235 "unnamed receiver type is only allowed if the surrounding \
236 extern block contains exactly one extern type; \
237 use `self: &{mutability}TheType`",
238 mutability = mutability,
239 );
240 cx.error(span, msg);
241 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700242 && !cx.types.cxx.contains(&receiver.ty)
243 && !cx.types.rust.contains(&receiver.ty)
244 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700245 cx.error(span, "unrecognized receiver type");
246 }
247
David Tolnayd763f4c2020-04-22 16:09:19 -0700248 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700249 cx.error(span, "references with explicit lifetimes are not supported");
250 }
251 }
252
David Tolnayd4e68302020-03-25 12:04:17 -0700253 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700254 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700255 let desc = describe(cx, &arg.ty);
256 let msg = format!("passing {} by value is not supported", desc);
257 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700258 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700259 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700260 if efn.lang == Lang::Rust {
261 cx.error(
262 arg,
263 "passing a function pointer from C++ to Rust is not implemented yet",
264 );
265 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700266 }
David Tolnayd4e68302020-03-25 12:04:17 -0700267 }
David Tolnaya420f012020-03-25 17:55:56 -0700268
David Tolnayd4e68302020-03-25 12:04:17 -0700269 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700270 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700271 let desc = describe(cx, ty);
272 let msg = format!("returning {} by value is not supported", desc);
273 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700274 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700275 if let Type::Fn(_) = ty {
276 cx.error(ty, "returning a function pointer is not implemented yet");
277 }
David Tolnayd4e68302020-03-25 12:04:17 -0700278 }
David Tolnay7db73692019-10-20 14:51:12 -0400279}
280
David Tolnay26a2a1d2020-03-25 17:26:43 -0700281fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400282 match &efn.ret {
283 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700284 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400285 }
286
287 for arg in &efn.args {
288 if let Type::Ref(ty) = &arg.ty {
289 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700290 return;
David Tolnay7db73692019-10-20 14:51:12 -0400291 }
292 }
293 }
294
David Tolnaya420f012020-03-25 17:55:56 -0700295 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400296 efn,
297 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700298 );
David Tolnay7db73692019-10-20 14:51:12 -0400299}
300
David Tolnay26a2a1d2020-03-25 17:26:43 -0700301fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400302 match &efn.ret {
303 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700304 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400305 }
306
307 let mut reference_args = 0;
308 for arg in &efn.args {
309 if let Type::Ref(_) = &arg.ty {
310 reference_args += 1;
311 }
312 }
313
Joel Galenson3d4f6122020-04-07 15:54:05 -0700314 if efn.receiver.is_some() {
315 reference_args += 1;
316 }
317
David Tolnay26a2a1d2020-03-25 17:26:43 -0700318 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700319 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400320 efn,
321 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700322 );
David Tolnay7db73692019-10-20 14:51:12 -0400323 }
324}
325
David Tolnay0b368ae2020-04-22 17:55:02 -0700326fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700327 if ident == "Box"
328 || ident == "UniquePtr"
329 || ident == "Vec"
330 || ident == "CxxVector"
331 || Atom::from(ident).is_some()
332 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700333 cx.error(ident, "reserved name");
334 }
335}
336
David Tolnay26a2a1d2020-03-25 17:26:43 -0700337fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700338 let ident = match ty {
339 Type::Ident(ident) => ident,
David Tolnaye70303c2020-04-25 15:02:37 -0700340 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700341 _ => return false,
342 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700343 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
344}
345
David Tolnaya420f012020-03-25 17:55:56 -0700346fn span_for_struct_error(strct: &Struct) -> TokenStream {
347 let struct_token = strct.struct_token;
348 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
349 brace_token.set_span(strct.brace_token.span);
350 quote!(#struct_token #brace_token)
351}
352
Joel Galensonc03402a2020-04-23 17:31:09 -0700353fn span_for_enum_error(enm: &Enum) -> TokenStream {
354 let enum_token = enm.enum_token;
355 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
356 brace_token.set_span(enm.brace_token.span);
357 quote!(#enum_token #brace_token)
358}
359
David Tolnayd763f4c2020-04-22 16:09:19 -0700360fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
361 let ampersand = receiver.ampersand;
362 let lifetime = &receiver.lifetime;
363 let mutability = receiver.mutability;
364 if receiver.shorthand {
365 let var = receiver.var;
366 quote!(#ampersand #lifetime #mutability #var)
367 } else {
368 let ty = &receiver.ty;
369 quote!(#ampersand #lifetime #mutability #ty)
370 }
371}
372
David Tolnay26a2a1d2020-03-25 17:26:43 -0700373fn combine_errors(errors: Vec<Error>) -> Result<()> {
374 let mut iter = errors.into_iter();
375 let mut all_errors = match iter.next() {
376 Some(err) => err,
377 None => return Ok(()),
378 };
379 for err in iter {
380 all_errors.combine(err);
381 }
382 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700383}
384
David Tolnaya420f012020-03-25 17:55:56 -0700385fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400386 match ty {
387 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700388 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400389 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700390 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400391 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700392 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400393 "opaque Rust type".to_owned()
394 } else if Atom::from(ident) == Some(CxxString) {
395 "C++ string".to_owned()
396 } else {
397 ident.to_string()
398 }
399 }
400 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700401 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400402 Type::UniquePtr(_) => "unique_ptr".to_owned(),
403 Type::Ref(_) => "reference".to_owned(),
404 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700405 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700406 Type::Slice(_) => "slice".to_owned(),
407 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700408 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700409 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400410 }
411}