blob: 2e94f4db2a3a9015273d76d1d6fcbb801c78e223 [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 Tolnay9c4ac2e2020-11-15 21:14:03 -0800150 if ty.mutable && !ty.pinned {
David Tolnaydb42cfd2020-11-15 15:31:40 -0800151 if let Some(requires_pin) = match &ty.inner {
152 Type::Ident(ident) if ident.rust == CxxString || is_opaque_cxx(cx, &ident.rust) => {
153 Some(ident.rust.to_string())
154 }
155 Type::CxxVector(_) => Some("CxxVector<...>".to_owned()),
156 _ => None,
157 } {
158 cx.error(
159 ty,
160 format!(
161 "mutable reference to C++ type requires a pin -- use Pin<&mut {}>",
162 requires_pin,
163 ),
164 );
165 }
166 }
167
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700168 match ty.inner {
169 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700170 Type::Ref(_) => {
171 cx.error(ty, "C++ does not allow references to references");
172 return;
173 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700174 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700175 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700176
177 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700178}
179
David Tolnayeebe9b72020-04-14 16:32:18 -0700180fn check_type_slice(cx: &mut Check, ty: &Slice) {
181 cx.error(ty, "only &[u8] is supported so far, not other slice types");
182}
183
David Tolnay1db70c42020-11-14 23:28:58 -0800184fn check_type_fn(cx: &mut Check, ty: &Signature) {
185 if ty.throws {
186 cx.error(ty, "function pointer returning Result is not supported yet");
187 }
188}
189
David Tolnay26a2a1d2020-03-25 17:26:43 -0700190fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay17a934c2020-11-02 00:40:04 -0800191 let name = &strct.name;
192 check_reserved_name(cx, &name.rust);
David Tolnay0b368ae2020-04-22 17:55:02 -0700193
David Tolnayd4e68302020-03-25 12:04:17 -0700194 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700195 let span = span_for_struct_error(strct);
196 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700197 }
David Tolnaya420f012020-03-25 17:55:56 -0700198
David Tolnay17a934c2020-11-02 00:40:04 -0800199 if cx.types.cxx.contains(&name.rust) {
200 if let Some(ety) = cx.types.untrusted.get(&name.rust) {
David Tolnayc8b50352020-08-29 19:27:01 -0700201 let msg = "extern shared struct must be declared in an `unsafe extern` block";
202 cx.error(ety, msg);
David Tolnayc8b50352020-08-29 19:27:01 -0700203 }
David Tolnayc880ae22020-08-29 16:46:00 -0700204 }
205
David Tolnayd4e68302020-03-25 12:04:17 -0700206 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700207 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700208 let desc = describe(cx, &field.ty);
209 let msg = format!("using {} by value is not supported", desc);
210 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700211 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700212 if let Type::Fn(_) = field.ty {
213 cx.error(
214 field,
215 "function pointers in a struct field are not implemented yet",
216 );
217 }
David Tolnayd4e68302020-03-25 12:04:17 -0700218 }
219}
220
Joel Galensonc03402a2020-04-23 17:31:09 -0700221fn check_api_enum(cx: &mut Check, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800222 check_reserved_name(cx, &enm.name.rust);
Joel Galensonc03402a2020-04-23 17:31:09 -0700223
David Tolnay58eee392020-11-20 20:37:58 -0800224 if enm.variants.is_empty() && !enm.explicit_repr {
Joel Galensonc03402a2020-04-23 17:31:09 -0700225 let span = span_for_enum_error(enm);
David Tolnayb4f4fac2020-11-22 00:26:02 -0800226 cx.error(
227 span,
228 "explicit #[repr(...)] is required for enum without any variants",
229 );
Joel Galensonc03402a2020-04-23 17:31:09 -0700230 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700231}
232
David Tolnay4cefa722020-10-03 21:24:07 -0700233fn check_api_type(cx: &mut Check, ety: &ExternType) {
David Tolnay17a934c2020-11-02 00:40:04 -0800234 check_reserved_name(cx, &ety.name.rust);
David Tolnay3208fd72020-10-03 20:19:40 -0700235
David Tolnay17a934c2020-11-02 00:40:04 -0800236 if let Some(reason) = cx.types.required_trivial.get(&ety.name.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700237 let what = match reason {
David Tolnay17a934c2020-11-02 00:40:04 -0800238 TrivialReason::StructField(strct) => format!("a field of `{}`", strct.name.rust),
239 TrivialReason::FunctionArgument(efn) => format!("an argument of `{}`", efn.name.rust),
240 TrivialReason::FunctionReturn(efn) => format!("a return value of `{}`", efn.name.rust),
David Tolnay3208fd72020-10-03 20:19:40 -0700241 };
242 let msg = format!(
243 "needs a cxx::ExternType impl in order to be used as {}",
244 what,
245 );
David Tolnay4cefa722020-10-03 21:24:07 -0700246 cx.error(ety, msg);
David Tolnay3208fd72020-10-03 20:19:40 -0700247 }
David Tolnay0b368ae2020-04-22 17:55:02 -0700248}
249
David Tolnay26a2a1d2020-03-25 17:26:43 -0700250fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnay9938b642020-11-15 18:11:40 -0800251 match efn.lang {
252 Lang::Cxx => {
253 if !efn.generics.params.is_empty() && !efn.trusted {
254 let ref span = span_for_generics_error(efn);
255 cx.error(span, "extern C++ function with lifetimes must be declared in `unsafe extern \"C++\"` block");
256 }
257 }
258 Lang::Rust => {
259 if !efn.generics.params.is_empty() && efn.unsafety.is_none() {
260 let ref span = span_for_generics_error(efn);
261 let message = format!(
262 "must be `unsafe fn {}` in order to expose explicit lifetimes to C++",
263 efn.name.rust,
264 );
265 cx.error(span, message);
266 }
267 }
268 }
269
David Tolnayd763f4c2020-04-22 16:09:19 -0700270 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700271 let ref span = span_for_receiver_error(receiver);
272
Adrian Taylorc8713432020-10-21 18:20:55 -0700273 if receiver.ty.is_self() {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800274 let mutability = match receiver.mutable {
275 true => "mut ",
276 false => "",
David Tolnaya1f29c42020-04-22 18:01:38 -0700277 };
278 let msg = format!(
279 "unnamed receiver type is only allowed if the surrounding \
280 extern block contains exactly one extern type; \
281 use `self: &{mutability}TheType`",
282 mutability = mutability,
283 );
284 cx.error(span, msg);
Adrian Taylorc8713432020-10-21 18:20:55 -0700285 } else if !cx.types.structs.contains_key(&receiver.ty.rust)
286 && !cx.types.cxx.contains(&receiver.ty.rust)
287 && !cx.types.rust.contains(&receiver.ty.rust)
David Tolnay8b60bf12020-04-22 16:52:19 -0700288 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700289 cx.error(span, "unrecognized receiver type");
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800290 } else if receiver.mutable && !receiver.pinned && is_opaque_cxx(cx, &receiver.ty.rust) {
David Tolnay75aea9a2020-11-15 16:07:25 -0800291 cx.error(
292 span,
293 format!(
294 "mutable reference to C++ type requires a pin -- use `self: Pin<&mut {}>`",
295 receiver.ty.rust,
296 ),
297 );
David Tolnay8b60bf12020-04-22 16:52:19 -0700298 }
David Tolnayd763f4c2020-04-22 16:09:19 -0700299 }
300
David Tolnayd4e68302020-03-25 12:04:17 -0700301 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700302 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700303 let desc = describe(cx, &arg.ty);
304 let msg = format!("passing {} by value is not supported", desc);
305 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700306 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700307 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700308 if efn.lang == Lang::Rust {
309 cx.error(
310 arg,
311 "passing a function pointer from C++ to Rust is not implemented yet",
312 );
313 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700314 }
David Tolnayd4e68302020-03-25 12:04:17 -0700315 }
David Tolnaya420f012020-03-25 17:55:56 -0700316
David Tolnayd4e68302020-03-25 12:04:17 -0700317 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700318 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700319 let desc = describe(cx, ty);
320 let msg = format!("returning {} by value is not supported", desc);
321 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700322 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700323 if let Type::Fn(_) = ty {
324 cx.error(ty, "returning a function pointer is not implemented yet");
325 }
David Tolnayd4e68302020-03-25 12:04:17 -0700326 }
David Tolnay96a826b2020-05-04 00:17:12 -0700327
328 if efn.lang == Lang::Cxx {
329 check_mut_return_restriction(cx, efn);
330 }
331
332 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400333}
334
David Tolnay7e69f892020-10-03 22:20:22 -0700335fn check_api_impl(cx: &mut Check, imp: &Impl) {
336 if let Type::UniquePtr(ty) | Type::CxxVector(ty) = &imp.ty {
337 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700338 if Atom::from(&inner.rust).is_none() {
David Tolnay7e69f892020-10-03 22:20:22 -0700339 return;
340 }
341 }
342 }
343
344 cx.error(imp, "unsupported Self type of explicit impl");
345}
346
David Tolnay26a2a1d2020-03-25 17:26:43 -0700347fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400348 match &efn.ret {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800349 Some(Type::Ref(ty)) if ty.mutable => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700350 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400351 }
352
353 for arg in &efn.args {
354 if let Type::Ref(ty) = &arg.ty {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800355 if ty.mutable {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700356 return;
David Tolnay7db73692019-10-20 14:51:12 -0400357 }
358 }
359 }
360
David Tolnaya420f012020-03-25 17:55:56 -0700361 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400362 efn,
363 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700364 );
David Tolnay7db73692019-10-20 14:51:12 -0400365}
366
David Tolnay26a2a1d2020-03-25 17:26:43 -0700367fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay556738d2020-11-15 13:58:44 -0800368 if efn.lang == Lang::Cxx && efn.trusted {
369 return;
370 }
371
David Tolnay7db73692019-10-20 14:51:12 -0400372 match &efn.ret {
373 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700374 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400375 }
376
377 let mut reference_args = 0;
378 for arg in &efn.args {
379 if let Type::Ref(_) = &arg.ty {
380 reference_args += 1;
381 }
382 }
383
Joel Galenson3d4f6122020-04-07 15:54:05 -0700384 if efn.receiver.is_some() {
385 reference_args += 1;
386 }
387
David Tolnay26a2a1d2020-03-25 17:26:43 -0700388 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700389 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400390 efn,
391 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700392 );
David Tolnay7db73692019-10-20 14:51:12 -0400393 }
394}
395
David Tolnay0b368ae2020-04-22 17:55:02 -0700396fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700397 if ident == "Box"
398 || ident == "UniquePtr"
399 || ident == "Vec"
400 || ident == "CxxVector"
David Tolnay4de34812020-11-19 14:48:55 -0800401 || ident == "str"
David Tolnay99c93d82020-04-25 14:07:44 -0700402 || Atom::from(ident).is_some()
403 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700404 cx.error(ident, "reserved name");
405 }
406}
407
David Tolnay26a2a1d2020-03-25 17:26:43 -0700408fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnay70e5bfb2020-11-19 14:45:33 -0800409 match ty {
410 Type::Ident(ident) => {
411 let ident = &ident.rust;
412 ident == CxxString || is_opaque_cxx(cx, ident) || cx.types.rust.contains(ident)
413 }
David Tolnayf35a22d2020-11-19 14:56:50 -0800414 Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => true,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800415 Type::RustBox(_)
416 | Type::RustVec(_)
417 | Type::UniquePtr(_)
418 | Type::Ref(_)
419 | Type::Str(_)
420 | Type::Fn(_)
David Tolnayf35a22d2020-11-19 14:56:50 -0800421 | Type::SliceRefU8(_) => false,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800422 }
David Tolnaydb42cfd2020-11-15 15:31:40 -0800423}
424
425fn is_opaque_cxx(cx: &mut Check, ty: &Ident) -> bool {
426 cx.types.cxx.contains(ty)
427 && !cx.types.structs.contains_key(ty)
428 && !cx.types.enums.contains_key(ty)
429 && !(cx.types.aliases.contains_key(ty) && cx.types.required_trivial.contains_key(ty))
David Tolnay26a2a1d2020-03-25 17:26:43 -0700430}
431
David Tolnaya420f012020-03-25 17:55:56 -0700432fn span_for_struct_error(strct: &Struct) -> TokenStream {
433 let struct_token = strct.struct_token;
434 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
435 brace_token.set_span(strct.brace_token.span);
436 quote!(#struct_token #brace_token)
437}
438
Joel Galensonc03402a2020-04-23 17:31:09 -0700439fn span_for_enum_error(enm: &Enum) -> TokenStream {
440 let enum_token = enm.enum_token;
441 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
442 brace_token.set_span(enm.brace_token.span);
443 quote!(#enum_token #brace_token)
444}
445
David Tolnayd763f4c2020-04-22 16:09:19 -0700446fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
447 let ampersand = receiver.ampersand;
448 let lifetime = &receiver.lifetime;
449 let mutability = receiver.mutability;
450 if receiver.shorthand {
451 let var = receiver.var;
452 quote!(#ampersand #lifetime #mutability #var)
453 } else {
454 let ty = &receiver.ty;
455 quote!(#ampersand #lifetime #mutability #ty)
456 }
457}
458
David Tolnay9938b642020-11-15 18:11:40 -0800459fn span_for_generics_error(efn: &ExternFn) -> TokenStream {
460 let unsafety = efn.unsafety;
461 let fn_token = efn.fn_token;
462 let generics = &efn.generics;
463 quote!(#unsafety #fn_token #generics)
464}
465
David Tolnaya420f012020-03-25 17:55:56 -0700466fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400467 match ty {
468 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700469 if cx.types.structs.contains_key(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400470 "struct".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700471 } else if cx.types.enums.contains_key(&ident.rust) {
David Tolnay55483722020-08-29 18:27:37 -0700472 "enum".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700473 } else if cx.types.aliases.contains_key(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700474 "C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700475 } else if cx.types.cxx.contains(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700476 "opaque C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700477 } else if cx.types.rust.contains(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400478 "opaque Rust type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700479 } else if Atom::from(&ident.rust) == Some(CxxString) {
David Tolnay7db73692019-10-20 14:51:12 -0400480 "C++ string".to_owned()
481 } else {
Adrian Taylorc8713432020-10-21 18:20:55 -0700482 ident.rust.to_string()
David Tolnay7db73692019-10-20 14:51:12 -0400483 }
484 }
485 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700486 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400487 Type::UniquePtr(_) => "unique_ptr".to_owned(),
488 Type::Ref(_) => "reference".to_owned(),
489 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700490 Type::CxxVector(_) => "C++ vector".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700491 Type::Slice(_) => "slice".to_owned(),
492 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700493 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700494 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400495 }
496}