blob: 5876cf71adb6732d91fe7c6d7822409c386637b5 [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 Tolnaydb42cfd2020-11-15 15:31:40 -0800154 if ty.mutability.is_some() && !ty.pinned {
155 if let Some(requires_pin) = match &ty.inner {
156 Type::Ident(ident) if ident.rust == CxxString || is_opaque_cxx(cx, &ident.rust) => {
157 Some(ident.rust.to_string())
158 }
159 Type::CxxVector(_) => Some("CxxVector<...>".to_owned()),
160 _ => None,
161 } {
162 cx.error(
163 ty,
164 format!(
165 "mutable reference to C++ type requires a pin -- use Pin<&mut {}>",
166 requires_pin,
167 ),
168 );
169 }
170 }
171
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700172 match ty.inner {
173 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700174 Type::Ref(_) => {
175 cx.error(ty, "C++ does not allow references to references");
176 return;
177 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700178 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700179 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700180
181 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700182}
183
David Tolnayeebe9b72020-04-14 16:32:18 -0700184fn check_type_slice(cx: &mut Check, ty: &Slice) {
185 cx.error(ty, "only &[u8] is supported so far, not other slice types");
186}
187
David Tolnay1db70c42020-11-14 23:28:58 -0800188fn check_type_fn(cx: &mut Check, ty: &Signature) {
189 if ty.throws {
190 cx.error(ty, "function pointer returning Result is not supported yet");
191 }
192}
193
David Tolnay26a2a1d2020-03-25 17:26:43 -0700194fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay17a934c2020-11-02 00:40:04 -0800195 let name = &strct.name;
196 check_reserved_name(cx, &name.rust);
David Tolnay0b368ae2020-04-22 17:55:02 -0700197
David Tolnayd4e68302020-03-25 12:04:17 -0700198 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700199 let span = span_for_struct_error(strct);
200 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700201 }
David Tolnaya420f012020-03-25 17:55:56 -0700202
David Tolnay17a934c2020-11-02 00:40:04 -0800203 if cx.types.cxx.contains(&name.rust) {
204 if let Some(ety) = cx.types.untrusted.get(&name.rust) {
David Tolnayc8b50352020-08-29 19:27:01 -0700205 let msg = "extern shared struct must be declared in an `unsafe extern` block";
206 cx.error(ety, msg);
David Tolnayc8b50352020-08-29 19:27:01 -0700207 }
David Tolnayc880ae22020-08-29 16:46:00 -0700208 }
209
David Tolnayd4e68302020-03-25 12:04:17 -0700210 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700211 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700212 let desc = describe(cx, &field.ty);
213 let msg = format!("using {} by value is not supported", desc);
214 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700215 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700216 if let Type::Fn(_) = field.ty {
217 cx.error(
218 field,
219 "function pointers in a struct field are not implemented yet",
220 );
221 }
David Tolnayd4e68302020-03-25 12:04:17 -0700222 }
223}
224
Joel Galensonc03402a2020-04-23 17:31:09 -0700225fn check_api_enum(cx: &mut Check, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800226 check_reserved_name(cx, &enm.name.rust);
Joel Galensonc03402a2020-04-23 17:31:09 -0700227
228 if enm.variants.is_empty() {
229 let span = span_for_enum_error(enm);
230 cx.error(span, "enums without any variants are not supported");
231 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700232}
233
David Tolnay4cefa722020-10-03 21:24:07 -0700234fn check_api_type(cx: &mut Check, ety: &ExternType) {
David Tolnay17a934c2020-11-02 00:40:04 -0800235 check_reserved_name(cx, &ety.name.rust);
David Tolnay3208fd72020-10-03 20:19:40 -0700236
David Tolnay17a934c2020-11-02 00:40:04 -0800237 if let Some(reason) = cx.types.required_trivial.get(&ety.name.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700238 let what = match reason {
David Tolnay17a934c2020-11-02 00:40:04 -0800239 TrivialReason::StructField(strct) => format!("a field of `{}`", strct.name.rust),
240 TrivialReason::FunctionArgument(efn) => format!("an argument of `{}`", efn.name.rust),
241 TrivialReason::FunctionReturn(efn) => format!("a return value of `{}`", efn.name.rust),
David Tolnay3208fd72020-10-03 20:19:40 -0700242 };
243 let msg = format!(
244 "needs a cxx::ExternType impl in order to be used as {}",
245 what,
246 );
David Tolnay4cefa722020-10-03 21:24:07 -0700247 cx.error(ety, msg);
David Tolnay3208fd72020-10-03 20:19:40 -0700248 }
David Tolnay0b368ae2020-04-22 17:55:02 -0700249}
250
David Tolnay26a2a1d2020-03-25 17:26:43 -0700251fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700252 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700253 let ref span = span_for_receiver_error(receiver);
254
Adrian Taylorc8713432020-10-21 18:20:55 -0700255 if receiver.ty.is_self() {
David Tolnaya1f29c42020-04-22 18:01:38 -0700256 let mutability = match receiver.mutability {
257 Some(_) => "mut ",
258 None => "",
259 };
260 let msg = format!(
261 "unnamed receiver type is only allowed if the surrounding \
262 extern block contains exactly one extern type; \
263 use `self: &{mutability}TheType`",
264 mutability = mutability,
265 );
266 cx.error(span, msg);
Adrian Taylorc8713432020-10-21 18:20:55 -0700267 } else if !cx.types.structs.contains_key(&receiver.ty.rust)
268 && !cx.types.cxx.contains(&receiver.ty.rust)
269 && !cx.types.rust.contains(&receiver.ty.rust)
David Tolnay8b60bf12020-04-22 16:52:19 -0700270 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700271 cx.error(span, "unrecognized receiver type");
272 }
273
David Tolnayd763f4c2020-04-22 16:09:19 -0700274 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700275 cx.error(span, "references with explicit lifetimes are not supported");
276 }
277 }
278
David Tolnayd4e68302020-03-25 12:04:17 -0700279 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700280 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700281 let desc = describe(cx, &arg.ty);
282 let msg = format!("passing {} by value is not supported", desc);
283 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700284 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700285 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700286 if efn.lang == Lang::Rust {
287 cx.error(
288 arg,
289 "passing a function pointer from C++ to Rust is not implemented yet",
290 );
291 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700292 }
David Tolnayd4e68302020-03-25 12:04:17 -0700293 }
David Tolnaya420f012020-03-25 17:55:56 -0700294
David Tolnayd4e68302020-03-25 12:04:17 -0700295 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700296 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700297 let desc = describe(cx, ty);
298 let msg = format!("returning {} by value is not supported", desc);
299 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700300 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700301 if let Type::Fn(_) = ty {
302 cx.error(ty, "returning a function pointer is not implemented yet");
303 }
David Tolnayd4e68302020-03-25 12:04:17 -0700304 }
David Tolnay96a826b2020-05-04 00:17:12 -0700305
306 if efn.lang == Lang::Cxx {
307 check_mut_return_restriction(cx, efn);
308 }
309
310 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400311}
312
David Tolnay7e69f892020-10-03 22:20:22 -0700313fn check_api_impl(cx: &mut Check, imp: &Impl) {
314 if let Type::UniquePtr(ty) | Type::CxxVector(ty) = &imp.ty {
315 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700316 if Atom::from(&inner.rust).is_none() {
David Tolnay7e69f892020-10-03 22:20:22 -0700317 return;
318 }
319 }
320 }
321
322 cx.error(imp, "unsupported Self type of explicit impl");
323}
324
David Tolnay26a2a1d2020-03-25 17:26:43 -0700325fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400326 match &efn.ret {
327 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700328 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400329 }
330
331 for arg in &efn.args {
332 if let Type::Ref(ty) = &arg.ty {
333 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700334 return;
David Tolnay7db73692019-10-20 14:51:12 -0400335 }
336 }
337 }
338
David Tolnaya420f012020-03-25 17:55:56 -0700339 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400340 efn,
341 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700342 );
David Tolnay7db73692019-10-20 14:51:12 -0400343}
344
David Tolnay26a2a1d2020-03-25 17:26:43 -0700345fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay556738d2020-11-15 13:58:44 -0800346 if efn.lang == Lang::Cxx && efn.trusted {
347 return;
348 }
349
David Tolnay7db73692019-10-20 14:51:12 -0400350 match &efn.ret {
351 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700352 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400353 }
354
355 let mut reference_args = 0;
356 for arg in &efn.args {
357 if let Type::Ref(_) = &arg.ty {
358 reference_args += 1;
359 }
360 }
361
Joel Galenson3d4f6122020-04-07 15:54:05 -0700362 if efn.receiver.is_some() {
363 reference_args += 1;
364 }
365
David Tolnay26a2a1d2020-03-25 17:26:43 -0700366 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700367 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400368 efn,
369 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700370 );
David Tolnay7db73692019-10-20 14:51:12 -0400371 }
372}
373
David Tolnay0b368ae2020-04-22 17:55:02 -0700374fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700375 if ident == "Box"
376 || ident == "UniquePtr"
377 || ident == "Vec"
378 || ident == "CxxVector"
379 || Atom::from(ident).is_some()
380 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700381 cx.error(ident, "reserved name");
382 }
383}
384
David Tolnay26a2a1d2020-03-25 17:26:43 -0700385fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700386 let ident = match ty {
Adrian Taylorc8713432020-10-21 18:20:55 -0700387 Type::Ident(ident) => &ident.rust,
David Tolnaye70303c2020-04-25 15:02:37 -0700388 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700389 _ => return false,
390 };
David Tolnaydb42cfd2020-11-15 15:31:40 -0800391 ident == CxxString || is_opaque_cxx(cx, ident) || cx.types.rust.contains(ident)
392}
393
394fn is_opaque_cxx(cx: &mut Check, ty: &Ident) -> bool {
395 cx.types.cxx.contains(ty)
396 && !cx.types.structs.contains_key(ty)
397 && !cx.types.enums.contains_key(ty)
398 && !(cx.types.aliases.contains_key(ty) && cx.types.required_trivial.contains_key(ty))
David Tolnay26a2a1d2020-03-25 17:26:43 -0700399}
400
David Tolnaya420f012020-03-25 17:55:56 -0700401fn span_for_struct_error(strct: &Struct) -> TokenStream {
402 let struct_token = strct.struct_token;
403 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
404 brace_token.set_span(strct.brace_token.span);
405 quote!(#struct_token #brace_token)
406}
407
Joel Galensonc03402a2020-04-23 17:31:09 -0700408fn span_for_enum_error(enm: &Enum) -> TokenStream {
409 let enum_token = enm.enum_token;
410 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
411 brace_token.set_span(enm.brace_token.span);
412 quote!(#enum_token #brace_token)
413}
414
David Tolnayd763f4c2020-04-22 16:09:19 -0700415fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
416 let ampersand = receiver.ampersand;
417 let lifetime = &receiver.lifetime;
418 let mutability = receiver.mutability;
419 if receiver.shorthand {
420 let var = receiver.var;
421 quote!(#ampersand #lifetime #mutability #var)
422 } else {
423 let ty = &receiver.ty;
424 quote!(#ampersand #lifetime #mutability #ty)
425 }
426}
427
David Tolnaya420f012020-03-25 17:55:56 -0700428fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400429 match ty {
430 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700431 if cx.types.structs.contains_key(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400432 "struct".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700433 } else if cx.types.enums.contains_key(&ident.rust) {
David Tolnay55483722020-08-29 18:27:37 -0700434 "enum".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700435 } else if cx.types.aliases.contains_key(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700436 "C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700437 } else if cx.types.cxx.contains(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700438 "opaque C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700439 } else if cx.types.rust.contains(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400440 "opaque Rust type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700441 } else if Atom::from(&ident.rust) == Some(CxxString) {
David Tolnay7db73692019-10-20 14:51:12 -0400442 "C++ string".to_owned()
443 } else {
Adrian Taylorc8713432020-10-21 18:20:55 -0700444 ident.rust.to_string()
David Tolnay7db73692019-10-20 14:51:12 -0400445 }
446 }
447 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700448 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400449 Type::UniquePtr(_) => "unique_ptr".to_owned(),
450 Type::Ref(_) => "reference".to_owned(),
451 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700452 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700453 Type::Slice(_) => "slice".to_owned(),
454 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700455 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700456 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400457 }
458}