blob: b95123d0c2ad60e35b87c636f7a6ee9bea0c9d79 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnaydf344a82020-05-03 23:23:18 -07002use crate::syntax::report::Errors;
David Tolnay3208fd72020-10-03 20:19:40 -07003use crate::syntax::types::TrivialReason;
David Tolnayd763f4c2020-04-22 16:09:19 -07004use crate::syntax::{
David Tolnay1db70c42020-11-14 23:28:58 -08005 error, ident, Api, Enum, ExternFn, ExternType, Impl, Lang, Receiver, Ref, Signature, Slice,
6 Struct, Ty1, Type, 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 Tolnay26a2a1d2020-03-25 17:26:43 -070013 apis: &'a [Api],
14 types: &'a Types<'a>,
David Tolnaydf344a82020-05-03 23:23:18 -070015 errors: &'a mut Errors,
David Tolnay26a2a1d2020-03-25 17:26:43 -070016}
17
Adrian Taylorc8713432020-10-21 18:20:55 -070018pub(crate) fn typecheck(cx: &mut Errors, apis: &[Api], types: &Types) {
David Tolnay0dd85ff2020-05-03 23:43:33 -070019 do_typecheck(&mut Check {
David Tolnay26a2a1d2020-03-25 17:26:43 -070020 apis,
21 types,
David Tolnay0dd85ff2020-05-03 23:43:33 -070022 errors: cx,
23 });
David Tolnay26a2a1d2020-03-25 17:26:43 -070024}
David Tolnay7db73692019-10-20 14:51:12 -040025
David Tolnay26a2a1d2020-03-25 17:26:43 -070026fn do_typecheck(cx: &mut Check) {
Adrian Taylorc8713432020-10-21 18:20:55 -070027 ident::check_all(cx, cx.apis);
David Tolnay9dcb8332020-04-30 20:37:33 -070028
David Tolnay26a2a1d2020-03-25 17:26:43 -070029 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040030 match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -070031 Type::Ident(ident) => check_type_ident(cx, &ident.rust),
David Tolnay26a2a1d2020-03-25 17:26:43 -070032 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070033 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070034 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070035 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070036 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070037 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay1db70c42020-11-14 23:28:58 -080038 Type::Fn(ty) => check_type_fn(cx, ty),
39 Type::Str(_) | Type::Void(_) | Type::SliceRefU8(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -040040 }
41 }
42
David Tolnay26a2a1d2020-03-25 17:26:43 -070043 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040044 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070045 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070046 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay4cefa722020-10-03 21:24:07 -070047 Api::CxxType(ety) | Api::RustType(ety) => check_api_type(cx, ety),
David Tolnay26a2a1d2020-03-25 17:26:43 -070048 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7e69f892020-10-03 22:20:22 -070049 Api::Impl(imp) => check_api_impl(cx, imp),
David Tolnay5ecaea92020-11-14 23:25:42 -080050 Api::Include(_) | Api::TypeAlias(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -040051 }
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 Tolnaydbc53772020-10-28 17:28:51 -070068 let msg = format!("unsupported type: {}", ident);
69 cx.error(ident, &msg);
David Tolnayd4e68302020-03-25 12:04:17 -070070 }
71}
72
David Tolnay26a2a1d2020-03-25 17:26:43 -070073fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070074 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -070075 if cx.types.cxx.contains(&ident.rust)
76 && !cx.types.structs.contains_key(&ident.rust)
77 && !cx.types.enums.contains_key(&ident.rust)
David Tolnayc880ae22020-08-29 16:46:00 -070078 {
David Tolnaya420f012020-03-25 17:55:56 -070079 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070080 }
David Tolnaya420f012020-03-25 17:55:56 -070081
Adrian Taylorc8713432020-10-21 18:20:55 -070082 if Atom::from(&ident.rust).is_none() {
David Tolnayd4e68302020-03-25 12:04:17 -070083 return;
84 }
85 }
David Tolnaya420f012020-03-25 17:55:56 -070086
87 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070088}
89
David Tolnayc6d891e2020-04-25 12:06:34 -070090fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
91 if let Type::Ident(ident) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -070092 if cx.types.cxx.contains(&ident.rust)
93 && !cx.types.structs.contains_key(&ident.rust)
94 && !cx.types.enums.contains_key(&ident.rust)
David Tolnayc880ae22020-08-29 16:46:00 -070095 {
David Tolnayc6d891e2020-04-25 12:06:34 -070096 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +070097 return;
David Tolnayc6d891e2020-04-25 12:06:34 -070098 }
99
Adrian Taylorc8713432020-10-21 18:20:55 -0700100 match Atom::from(&ident.rust) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700101 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay33f56ad2020-08-27 17:06:35 -0700102 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
103 | Some(RustString) => return,
104 Some(Bool) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -0700105 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700106 }
107 }
108
David Tolnayc6d891e2020-04-25 12:06:34 -0700109 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700110}
111
David Tolnay26a2a1d2020-03-25 17:26:43 -0700112fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700113 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700114 if cx.types.rust.contains(&ident.rust) {
David Tolnaya420f012020-03-25 17:55:56 -0700115 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700116 }
David Tolnaya420f012020-03-25 17:55:56 -0700117
Adrian Taylorc8713432020-10-21 18:20:55 -0700118 match Atom::from(&ident.rust) {
David Tolnayd4e68302020-03-25 12:04:17 -0700119 None | Some(CxxString) => return,
120 _ => {}
121 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700122 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700123 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700124 }
David Tolnaya420f012020-03-25 17:55:56 -0700125
126 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700127}
128
David Tolnayfff4c8a2020-04-25 11:45:20 -0700129fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700130 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700131 if cx.types.rust.contains(&ident.rust) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700132 cx.error(
133 ptr,
134 "C++ vector containing a Rust type is not supported yet",
135 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700136 }
137
Adrian Taylorc8713432020-10-21 18:20:55 -0700138 match Atom::from(&ident.rust) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700139 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay47e239d2020-08-28 00:32:04 -0700140 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
141 | Some(CxxString) => return,
David Tolnay6bd63de2020-04-25 12:20:09 -0700142 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700143 }
144 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700145
Myron Ahneba35cf2020-02-05 19:41:51 +0700146 cx.error(ptr, "unsupported vector target type");
147}
148
David Tolnay26a2a1d2020-03-25 17:26:43 -0700149fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700150 if ty.lifetime.is_some() {
151 cx.error(ty, "references with explicit lifetimes are not supported");
152 }
153
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700154 match ty.inner {
155 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700156 Type::Ref(_) => {
157 cx.error(ty, "C++ does not allow references to references");
158 return;
159 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700160 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700161 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700162
163 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700164}
165
David Tolnayeebe9b72020-04-14 16:32:18 -0700166fn check_type_slice(cx: &mut Check, ty: &Slice) {
167 cx.error(ty, "only &[u8] is supported so far, not other slice types");
168}
169
David Tolnay1db70c42020-11-14 23:28:58 -0800170fn check_type_fn(cx: &mut Check, ty: &Signature) {
171 if ty.throws {
172 cx.error(ty, "function pointer returning Result is not supported yet");
173 }
174}
175
David Tolnay26a2a1d2020-03-25 17:26:43 -0700176fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay17a934c2020-11-02 00:40:04 -0800177 let name = &strct.name;
178 check_reserved_name(cx, &name.rust);
David Tolnay0b368ae2020-04-22 17:55:02 -0700179
David Tolnayd4e68302020-03-25 12:04:17 -0700180 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700181 let span = span_for_struct_error(strct);
182 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700183 }
David Tolnaya420f012020-03-25 17:55:56 -0700184
David Tolnay17a934c2020-11-02 00:40:04 -0800185 if cx.types.cxx.contains(&name.rust) {
186 if let Some(ety) = cx.types.untrusted.get(&name.rust) {
David Tolnayc8b50352020-08-29 19:27:01 -0700187 let msg = "extern shared struct must be declared in an `unsafe extern` block";
188 cx.error(ety, msg);
David Tolnayc8b50352020-08-29 19:27:01 -0700189 }
David Tolnayc880ae22020-08-29 16:46:00 -0700190 }
191
David Tolnayd4e68302020-03-25 12:04:17 -0700192 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700193 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700194 let desc = describe(cx, &field.ty);
195 let msg = format!("using {} by value is not supported", desc);
196 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700197 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700198 if let Type::Fn(_) = field.ty {
199 cx.error(
200 field,
201 "function pointers in a struct field are not implemented yet",
202 );
203 }
David Tolnayd4e68302020-03-25 12:04:17 -0700204 }
205}
206
Joel Galensonc03402a2020-04-23 17:31:09 -0700207fn check_api_enum(cx: &mut Check, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800208 check_reserved_name(cx, &enm.name.rust);
Joel Galensonc03402a2020-04-23 17:31:09 -0700209
210 if enm.variants.is_empty() {
211 let span = span_for_enum_error(enm);
212 cx.error(span, "enums without any variants are not supported");
213 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700214}
215
David Tolnay4cefa722020-10-03 21:24:07 -0700216fn check_api_type(cx: &mut Check, ety: &ExternType) {
David Tolnay17a934c2020-11-02 00:40:04 -0800217 check_reserved_name(cx, &ety.name.rust);
David Tolnay3208fd72020-10-03 20:19:40 -0700218
David Tolnay17a934c2020-11-02 00:40:04 -0800219 if let Some(reason) = cx.types.required_trivial.get(&ety.name.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700220 let what = match reason {
David Tolnay17a934c2020-11-02 00:40:04 -0800221 TrivialReason::StructField(strct) => format!("a field of `{}`", strct.name.rust),
222 TrivialReason::FunctionArgument(efn) => format!("an argument of `{}`", efn.name.rust),
223 TrivialReason::FunctionReturn(efn) => format!("a return value of `{}`", efn.name.rust),
David Tolnay3208fd72020-10-03 20:19:40 -0700224 };
225 let msg = format!(
226 "needs a cxx::ExternType impl in order to be used as {}",
227 what,
228 );
David Tolnay4cefa722020-10-03 21:24:07 -0700229 cx.error(ety, msg);
David Tolnay3208fd72020-10-03 20:19:40 -0700230 }
David Tolnay0b368ae2020-04-22 17:55:02 -0700231}
232
David Tolnay26a2a1d2020-03-25 17:26:43 -0700233fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700234 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700235 let ref span = span_for_receiver_error(receiver);
236
Adrian Taylorc8713432020-10-21 18:20:55 -0700237 if receiver.ty.is_self() {
David Tolnaya1f29c42020-04-22 18:01:38 -0700238 let mutability = match receiver.mutability {
239 Some(_) => "mut ",
240 None => "",
241 };
242 let msg = format!(
243 "unnamed receiver type is only allowed if the surrounding \
244 extern block contains exactly one extern type; \
245 use `self: &{mutability}TheType`",
246 mutability = mutability,
247 );
248 cx.error(span, msg);
Adrian Taylorc8713432020-10-21 18:20:55 -0700249 } else if !cx.types.structs.contains_key(&receiver.ty.rust)
250 && !cx.types.cxx.contains(&receiver.ty.rust)
251 && !cx.types.rust.contains(&receiver.ty.rust)
David Tolnay8b60bf12020-04-22 16:52:19 -0700252 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700253 cx.error(span, "unrecognized receiver type");
254 }
255
David Tolnayd763f4c2020-04-22 16:09:19 -0700256 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700257 cx.error(span, "references with explicit lifetimes are not supported");
258 }
259 }
260
David Tolnayd4e68302020-03-25 12:04:17 -0700261 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700262 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700263 let desc = describe(cx, &arg.ty);
264 let msg = format!("passing {} by value is not supported", desc);
265 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700266 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700267 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700268 if efn.lang == Lang::Rust {
269 cx.error(
270 arg,
271 "passing a function pointer from C++ to Rust is not implemented yet",
272 );
273 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700274 }
David Tolnayd4e68302020-03-25 12:04:17 -0700275 }
David Tolnaya420f012020-03-25 17:55:56 -0700276
David Tolnayd4e68302020-03-25 12:04:17 -0700277 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700278 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700279 let desc = describe(cx, ty);
280 let msg = format!("returning {} by value is not supported", desc);
281 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700282 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700283 if let Type::Fn(_) = ty {
284 cx.error(ty, "returning a function pointer is not implemented yet");
285 }
David Tolnayd4e68302020-03-25 12:04:17 -0700286 }
David Tolnay96a826b2020-05-04 00:17:12 -0700287
288 if efn.lang == Lang::Cxx {
289 check_mut_return_restriction(cx, efn);
290 }
291
292 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400293}
294
David Tolnay7e69f892020-10-03 22:20:22 -0700295fn check_api_impl(cx: &mut Check, imp: &Impl) {
296 if let Type::UniquePtr(ty) | Type::CxxVector(ty) = &imp.ty {
297 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700298 if Atom::from(&inner.rust).is_none() {
David Tolnay7e69f892020-10-03 22:20:22 -0700299 return;
300 }
301 }
302 }
303
304 cx.error(imp, "unsupported Self type of explicit impl");
305}
306
David Tolnay26a2a1d2020-03-25 17:26:43 -0700307fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400308 match &efn.ret {
309 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700310 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400311 }
312
313 for arg in &efn.args {
314 if let Type::Ref(ty) = &arg.ty {
315 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700316 return;
David Tolnay7db73692019-10-20 14:51:12 -0400317 }
318 }
319 }
320
David Tolnaya420f012020-03-25 17:55:56 -0700321 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400322 efn,
323 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700324 );
David Tolnay7db73692019-10-20 14:51:12 -0400325}
326
David Tolnay26a2a1d2020-03-25 17:26:43 -0700327fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400328 match &efn.ret {
329 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700330 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400331 }
332
333 let mut reference_args = 0;
334 for arg in &efn.args {
335 if let Type::Ref(_) = &arg.ty {
336 reference_args += 1;
337 }
338 }
339
Joel Galenson3d4f6122020-04-07 15:54:05 -0700340 if efn.receiver.is_some() {
341 reference_args += 1;
342 }
343
David Tolnay26a2a1d2020-03-25 17:26:43 -0700344 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700345 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400346 efn,
347 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700348 );
David Tolnay7db73692019-10-20 14:51:12 -0400349 }
350}
351
David Tolnay0b368ae2020-04-22 17:55:02 -0700352fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700353 if ident == "Box"
354 || ident == "UniquePtr"
355 || ident == "Vec"
356 || ident == "CxxVector"
357 || Atom::from(ident).is_some()
358 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700359 cx.error(ident, "reserved name");
360 }
361}
362
David Tolnay26a2a1d2020-03-25 17:26:43 -0700363fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700364 let ident = match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700365 Type::Ident(ident) => &ident.rust,
David Tolnaye70303c2020-04-25 15:02:37 -0700366 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700367 _ => return false,
368 };
David Tolnay89849952020-08-29 18:32:29 -0700369 ident == CxxString
David Tolnayc880ae22020-08-29 16:46:00 -0700370 || cx.types.cxx.contains(ident)
371 && !cx.types.structs.contains_key(ident)
372 && !cx.types.enums.contains_key(ident)
David Tolnay3208fd72020-10-03 20:19:40 -0700373 && !(cx.types.aliases.contains_key(ident)
374 && cx.types.required_trivial.contains_key(ident))
David Tolnay89849952020-08-29 18:32:29 -0700375 || cx.types.rust.contains(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -0700376}
377
David Tolnaya420f012020-03-25 17:55:56 -0700378fn span_for_struct_error(strct: &Struct) -> TokenStream {
379 let struct_token = strct.struct_token;
380 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
381 brace_token.set_span(strct.brace_token.span);
382 quote!(#struct_token #brace_token)
383}
384
Joel Galensonc03402a2020-04-23 17:31:09 -0700385fn span_for_enum_error(enm: &Enum) -> TokenStream {
386 let enum_token = enm.enum_token;
387 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
388 brace_token.set_span(enm.brace_token.span);
389 quote!(#enum_token #brace_token)
390}
391
David Tolnayd763f4c2020-04-22 16:09:19 -0700392fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
393 let ampersand = receiver.ampersand;
394 let lifetime = &receiver.lifetime;
395 let mutability = receiver.mutability;
396 if receiver.shorthand {
397 let var = receiver.var;
398 quote!(#ampersand #lifetime #mutability #var)
399 } else {
400 let ty = &receiver.ty;
401 quote!(#ampersand #lifetime #mutability #ty)
402 }
403}
404
David Tolnaya420f012020-03-25 17:55:56 -0700405fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400406 match ty {
407 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700408 if cx.types.structs.contains_key(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400409 "struct".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700410 } else if cx.types.enums.contains_key(&ident.rust) {
David Tolnay55483722020-08-29 18:27:37 -0700411 "enum".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700412 } else if cx.types.aliases.contains_key(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700413 "C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700414 } else if cx.types.cxx.contains(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700415 "opaque C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700416 } else if cx.types.rust.contains(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400417 "opaque Rust type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700418 } else if Atom::from(&ident.rust) == Some(CxxString) {
David Tolnay7db73692019-10-20 14:51:12 -0400419 "C++ string".to_owned()
420 } else {
Adrian Taylorc8713432020-10-21 18:20:55 -0700421 ident.rust.to_string()
David Tolnay7db73692019-10-20 14:51:12 -0400422 }
423 }
424 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700425 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400426 Type::UniquePtr(_) => "unique_ptr".to_owned(),
427 Type::Ref(_) => "reference".to_owned(),
428 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700429 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700430 Type::Slice(_) => "slice".to_owned(),
431 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700432 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700433 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400434 }
435}