blob: f8239646387c0a2f244c2bfd97415bab79dc6bba [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 Tolnaydf344a82020-05-03 23:23:18 -07003use crate::syntax::report::Errors;
David Tolnayd763f4c2020-04-22 16:09:19 -07004use crate::syntax::{
Joel Galensonc03402a2020-04-23 17:31:09 -07005 error, ident, Api, Enum, ExternFn, ExternType, Lang, Receiver, Ref, Slice, Struct, Ty1, Type,
6 Types,
David Tolnayd763f4c2020-04-22 16:09:19 -07007};
David Tolnay09462ac2020-03-20 14:58:41 -07008use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07009use quote::{quote, ToTokens};
10use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -040011
David Tolnaya83301c2020-04-30 20:32:37 -070012pub(crate) struct Check<'a> {
David Tolnay9dcb8332020-04-30 20:37:33 -070013 namespace: &'a Namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070014 apis: &'a [Api],
15 types: &'a Types<'a>,
David Tolnaydf344a82020-05-03 23:23:18 -070016 errors: &'a mut Errors,
David Tolnay26a2a1d2020-03-25 17:26:43 -070017}
18
David Tolnay0dd85ff2020-05-03 23:43:33 -070019pub(crate) fn typecheck(cx: &mut Errors, namespace: &Namespace, apis: &[Api], types: &Types) {
20 do_typecheck(&mut Check {
David Tolnay9dcb8332020-04-30 20:37:33 -070021 namespace,
David Tolnay26a2a1d2020-03-25 17:26:43 -070022 apis,
23 types,
David Tolnay0dd85ff2020-05-03 23:43:33 -070024 errors: cx,
25 });
David Tolnay26a2a1d2020-03-25 17:26:43 -070026}
David Tolnay7db73692019-10-20 14:51:12 -040027
David Tolnay26a2a1d2020-03-25 17:26:43 -070028fn do_typecheck(cx: &mut Check) {
David Tolnay6b6423e2020-04-30 20:46:24 -070029 ident::check_all(cx, cx.namespace, cx.apis);
David Tolnay9dcb8332020-04-30 20:37:33 -070030
David Tolnay26a2a1d2020-03-25 17:26:43 -070031 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040032 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070033 Type::Ident(ident) => check_type_ident(cx, ident),
34 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070035 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070036 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070037 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070038 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070039 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040040 _ => {}
41 }
42 }
43
David Tolnay26a2a1d2020-03-25 17:26:43 -070044 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040045 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070046 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070047 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay0b368ae2020-04-22 17:55:02 -070048 Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070049 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040050 _ => {}
51 }
52 }
David Tolnay7db73692019-10-20 14:51:12 -040053}
54
David Tolnaya420f012020-03-25 17:55:56 -070055impl Check<'_> {
David Tolnaya83301c2020-04-30 20:32:37 -070056 pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
David Tolnaydf344a82020-05-03 23:23:18 -070057 self.errors.error(sp, msg);
David Tolnaya420f012020-03-25 17:55:56 -070058 }
59}
60
David Tolnay26a2a1d2020-03-25 17:26:43 -070061fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070062 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070063 && !cx.types.structs.contains_key(ident)
Joel Galensonc03402a2020-04-23 17:31:09 -070064 && !cx.types.enums.contains_key(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -070065 && !cx.types.cxx.contains(ident)
66 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070067 {
David Tolnaya420f012020-03-25 17:55:56 -070068 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070069 }
70}
71
David Tolnay26a2a1d2020-03-25 17:26:43 -070072fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070073 if let Type::Ident(ident) = &ptr.inner {
David Tolnayc880ae22020-08-29 16:46:00 -070074 if cx.types.cxx.contains(ident)
75 && !cx.types.structs.contains_key(ident)
76 && !cx.types.enums.contains_key(ident)
77 {
David Tolnaya420f012020-03-25 17:55:56 -070078 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070079 }
David Tolnaya420f012020-03-25 17:55:56 -070080
David Tolnayd4e68302020-03-25 12:04:17 -070081 if Atom::from(ident).is_none() {
82 return;
83 }
84 }
David Tolnaya420f012020-03-25 17:55:56 -070085
86 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070087}
88
David Tolnayc6d891e2020-04-25 12:06:34 -070089fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
90 if let Type::Ident(ident) = &ty.inner {
David Tolnayc880ae22020-08-29 16:46:00 -070091 if cx.types.cxx.contains(ident)
92 && !cx.types.structs.contains_key(ident)
93 && !cx.types.enums.contains_key(ident)
94 {
David Tolnayc6d891e2020-04-25 12:06:34 -070095 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +070096 return;
David Tolnayc6d891e2020-04-25 12:06:34 -070097 }
98
99 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700100 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay33f56ad2020-08-27 17:06:35 -0700101 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
102 | Some(RustString) => return,
103 Some(Bool) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -0700104 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700105 }
106 }
107
David Tolnayc6d891e2020-04-25 12:06:34 -0700108 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700109}
110
David Tolnay26a2a1d2020-03-25 17:26:43 -0700111fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700112 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700113 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -0700114 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700115 }
David Tolnaya420f012020-03-25 17:55:56 -0700116
David Tolnayd4e68302020-03-25 12:04:17 -0700117 match Atom::from(ident) {
118 None | Some(CxxString) => return,
119 _ => {}
120 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700121 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700122 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700123 }
David Tolnaya420f012020-03-25 17:55:56 -0700124
125 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700126}
127
David Tolnayfff4c8a2020-04-25 11:45:20 -0700128fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700129 if let Type::Ident(ident) = &ptr.inner {
130 if cx.types.rust.contains(ident) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700131 cx.error(
132 ptr,
133 "C++ vector containing a Rust type is not supported yet",
134 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700135 }
136
137 match Atom::from(ident) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700138 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay47e239d2020-08-28 00:32:04 -0700139 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
140 | Some(CxxString) => return,
David Tolnay6bd63de2020-04-25 12:20:09 -0700141 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700142 }
143 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700144
Myron Ahneba35cf2020-02-05 19:41:51 +0700145 cx.error(ptr, "unsupported vector target type");
146}
147
David Tolnay26a2a1d2020-03-25 17:26:43 -0700148fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700149 if ty.lifetime.is_some() {
150 cx.error(ty, "references with explicit lifetimes are not supported");
151 }
152
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700153 match ty.inner {
154 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700155 Type::Ref(_) => {
156 cx.error(ty, "C++ does not allow references to references");
157 return;
158 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700159 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700160 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700161
162 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700163}
164
David Tolnayeebe9b72020-04-14 16:32:18 -0700165fn check_type_slice(cx: &mut Check, ty: &Slice) {
166 cx.error(ty, "only &[u8] is supported so far, not other slice types");
167}
168
David Tolnay26a2a1d2020-03-25 17:26:43 -0700169fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnayc8b50352020-08-29 19:27:01 -0700170 let ident = &strct.ident;
171 check_reserved_name(cx, ident);
David Tolnay0b368ae2020-04-22 17:55:02 -0700172
David Tolnayd4e68302020-03-25 12:04:17 -0700173 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700174 let span = span_for_struct_error(strct);
175 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700176 }
David Tolnaya420f012020-03-25 17:55:56 -0700177
David Tolnayc8b50352020-08-29 19:27:01 -0700178 if cx.types.cxx.contains(ident) {
179 if let Some(ety) = cx.types.untrusted.get(ident) {
180 let msg = "extern shared struct must be declared in an `unsafe extern` block";
181 cx.error(ety, msg);
182 } else {
183 let span = span_for_struct_error(strct);
184 cx.error(span, "extern C++ structs are not implemented yet");
185 }
David Tolnayc880ae22020-08-29 16:46:00 -0700186 }
187
David Tolnayd4e68302020-03-25 12:04:17 -0700188 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700189 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700190 let desc = describe(cx, &field.ty);
191 let msg = format!("using {} by value is not supported", desc);
192 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700193 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700194 if let Type::Fn(_) = field.ty {
195 cx.error(
196 field,
197 "function pointers in a struct field are not implemented yet",
198 );
199 }
David Tolnayd4e68302020-03-25 12:04:17 -0700200 }
201}
202
Joel Galensonc03402a2020-04-23 17:31:09 -0700203fn check_api_enum(cx: &mut Check, enm: &Enum) {
204 check_reserved_name(cx, &enm.ident);
205
206 if enm.variants.is_empty() {
207 let span = span_for_enum_error(enm);
208 cx.error(span, "enums without any variants are not supported");
209 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700210}
211
David Tolnay0b368ae2020-04-22 17:55:02 -0700212fn check_api_type(cx: &mut Check, ty: &ExternType) {
213 check_reserved_name(cx, &ty.ident);
214}
215
David Tolnay26a2a1d2020-03-25 17:26:43 -0700216fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700217 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700218 let ref span = span_for_receiver_error(receiver);
219
220 if receiver.ty == "Self" {
221 let mutability = match receiver.mutability {
222 Some(_) => "mut ",
223 None => "",
224 };
225 let msg = format!(
226 "unnamed receiver type is only allowed if the surrounding \
227 extern block contains exactly one extern type; \
228 use `self: &{mutability}TheType`",
229 mutability = mutability,
230 );
231 cx.error(span, msg);
232 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700233 && !cx.types.cxx.contains(&receiver.ty)
234 && !cx.types.rust.contains(&receiver.ty)
235 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700236 cx.error(span, "unrecognized receiver type");
237 }
238
David Tolnayd763f4c2020-04-22 16:09:19 -0700239 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700240 cx.error(span, "references with explicit lifetimes are not supported");
241 }
242 }
243
David Tolnayd4e68302020-03-25 12:04:17 -0700244 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700245 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700246 let desc = describe(cx, &arg.ty);
247 let msg = format!("passing {} by value is not supported", desc);
248 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700249 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700250 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700251 if efn.lang == Lang::Rust {
252 cx.error(
253 arg,
254 "passing a function pointer from C++ to Rust is not implemented yet",
255 );
256 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700257 }
David Tolnayd4e68302020-03-25 12:04:17 -0700258 }
David Tolnaya420f012020-03-25 17:55:56 -0700259
David Tolnayd4e68302020-03-25 12:04:17 -0700260 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700261 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700262 let desc = describe(cx, ty);
263 let msg = format!("returning {} by value is not supported", desc);
264 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700265 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700266 if let Type::Fn(_) = ty {
267 cx.error(ty, "returning a function pointer is not implemented yet");
268 }
David Tolnayd4e68302020-03-25 12:04:17 -0700269 }
David Tolnay96a826b2020-05-04 00:17:12 -0700270
271 if efn.lang == Lang::Cxx {
272 check_mut_return_restriction(cx, efn);
273 }
274
275 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400276}
277
David Tolnay26a2a1d2020-03-25 17:26:43 -0700278fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400279 match &efn.ret {
280 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700281 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400282 }
283
284 for arg in &efn.args {
285 if let Type::Ref(ty) = &arg.ty {
286 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700287 return;
David Tolnay7db73692019-10-20 14:51:12 -0400288 }
289 }
290 }
291
David Tolnaya420f012020-03-25 17:55:56 -0700292 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400293 efn,
294 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700295 );
David Tolnay7db73692019-10-20 14:51:12 -0400296}
297
David Tolnay26a2a1d2020-03-25 17:26:43 -0700298fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400299 match &efn.ret {
300 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700301 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400302 }
303
304 let mut reference_args = 0;
305 for arg in &efn.args {
306 if let Type::Ref(_) = &arg.ty {
307 reference_args += 1;
308 }
309 }
310
Joel Galenson3d4f6122020-04-07 15:54:05 -0700311 if efn.receiver.is_some() {
312 reference_args += 1;
313 }
314
David Tolnay26a2a1d2020-03-25 17:26:43 -0700315 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700316 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400317 efn,
318 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700319 );
David Tolnay7db73692019-10-20 14:51:12 -0400320 }
321}
322
David Tolnay0b368ae2020-04-22 17:55:02 -0700323fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700324 if ident == "Box"
325 || ident == "UniquePtr"
326 || ident == "Vec"
327 || ident == "CxxVector"
328 || Atom::from(ident).is_some()
329 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700330 cx.error(ident, "reserved name");
331 }
332}
333
David Tolnay26a2a1d2020-03-25 17:26:43 -0700334fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700335 let ident = match ty {
336 Type::Ident(ident) => ident,
David Tolnaye70303c2020-04-25 15:02:37 -0700337 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700338 _ => return false,
339 };
David Tolnay89849952020-08-29 18:32:29 -0700340 ident == CxxString
David Tolnayc880ae22020-08-29 16:46:00 -0700341 || cx.types.cxx.contains(ident)
342 && !cx.types.structs.contains_key(ident)
343 && !cx.types.enums.contains_key(ident)
David Tolnay89849952020-08-29 18:32:29 -0700344 || cx.types.rust.contains(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -0700345}
346
David Tolnaya420f012020-03-25 17:55:56 -0700347fn span_for_struct_error(strct: &Struct) -> TokenStream {
348 let struct_token = strct.struct_token;
349 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
350 brace_token.set_span(strct.brace_token.span);
351 quote!(#struct_token #brace_token)
352}
353
Joel Galensonc03402a2020-04-23 17:31:09 -0700354fn span_for_enum_error(enm: &Enum) -> TokenStream {
355 let enum_token = enm.enum_token;
356 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
357 brace_token.set_span(enm.brace_token.span);
358 quote!(#enum_token #brace_token)
359}
360
David Tolnayd763f4c2020-04-22 16:09:19 -0700361fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
362 let ampersand = receiver.ampersand;
363 let lifetime = &receiver.lifetime;
364 let mutability = receiver.mutability;
365 if receiver.shorthand {
366 let var = receiver.var;
367 quote!(#ampersand #lifetime #mutability #var)
368 } else {
369 let ty = &receiver.ty;
370 quote!(#ampersand #lifetime #mutability #ty)
371 }
372}
373
David Tolnaya420f012020-03-25 17:55:56 -0700374fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400375 match ty {
376 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700377 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400378 "struct".to_owned()
David Tolnay55483722020-08-29 18:27:37 -0700379 } else if cx.types.enums.contains_key(ident) {
380 "enum".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700381 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400382 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700383 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400384 "opaque Rust type".to_owned()
385 } else if Atom::from(ident) == Some(CxxString) {
386 "C++ string".to_owned()
387 } else {
388 ident.to_string()
389 }
390 }
391 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700392 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400393 Type::UniquePtr(_) => "unique_ptr".to_owned(),
394 Type::Ref(_) => "reference".to_owned(),
395 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700396 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700397 Type::Slice(_) => "slice".to_owned(),
398 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700399 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700400 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400401 }
402}