blob: b9f6660ecfc4f2125e1b8c99356755e30a73e66f [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 Tolnayd763f4c2020-04-22 16:09:19 -07003use crate::syntax::{
David Tolnay77a5e752021-01-01 14:15:18 -08004 error, ident, trivial, Api, Array, Enum, ExternFn, ExternType, Impl, Lang, NamedType, Receiver,
5 Ref, Signature, SliceRef, Struct, Trait, Ty1, Type, TypeAlias, 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};
9use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -040010
David Tolnaya83301c2020-04-30 20:32:37 -070011pub(crate) struct Check<'a> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070012 apis: &'a [Api],
13 types: &'a Types<'a>,
David Tolnaydf344a82020-05-03 23:23:18 -070014 errors: &'a mut Errors,
David Tolnay26a2a1d2020-03-25 17:26:43 -070015}
16
Adrian Taylorc8713432020-10-21 18:20:55 -070017pub(crate) fn typecheck(cx: &mut Errors, apis: &[Api], types: &Types) {
David Tolnay0dd85ff2020-05-03 23:43:33 -070018 do_typecheck(&mut Check {
David Tolnay26a2a1d2020-03-25 17:26:43 -070019 apis,
20 types,
David Tolnay0dd85ff2020-05-03 23:43:33 -070021 errors: cx,
22 });
David Tolnay26a2a1d2020-03-25 17:26:43 -070023}
David Tolnay7db73692019-10-20 14:51:12 -040024
David Tolnay26a2a1d2020-03-25 17:26:43 -070025fn do_typecheck(cx: &mut Check) {
Adrian Taylorc8713432020-10-21 18:20:55 -070026 ident::check_all(cx, cx.apis);
David Tolnay9dcb8332020-04-30 20:37:33 -070027
David Tolnay26a2a1d2020-03-25 17:26:43 -070028 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040029 match ty {
David Tolnay679b15d2020-12-30 20:39:06 -080030 Type::Ident(ident) => check_type_ident(cx, ident),
David Tolnay26a2a1d2020-03-25 17:26:43 -070031 Type::RustBox(ptr) => check_type_box(cx, ptr),
David Tolnayc6d891e2020-04-25 12:06:34 -070032 Type::RustVec(ty) => check_type_rust_vec(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070033 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
David Tolnayb3b24a12020-12-01 15:27:43 -080034 Type::SharedPtr(ptr) => check_type_shared_ptr(cx, ptr),
David Tolnay215e77f2020-12-28 17:09:48 -080035 Type::WeakPtr(ptr) => check_type_weak_ptr(cx, ptr),
David Tolnayfff4c8a2020-04-25 11:45:20 -070036 Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr),
David Tolnay26a2a1d2020-03-25 17:26:43 -070037 Type::Ref(ty) => check_type_ref(cx, ty),
Xiangpeng Hao78762352020-11-12 10:24:18 +080038 Type::Array(array) => check_type_array(cx, array),
David Tolnay1db70c42020-11-14 23:28:58 -080039 Type::Fn(ty) => check_type_fn(cx, ty),
David Tolnay69db8142020-11-25 17:49:25 -080040 Type::SliceRef(ty) => check_type_slice_ref(cx, ty),
41 Type::Str(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -040042 }
43 }
44
David Tolnay26a2a1d2020-03-25 17:26:43 -070045 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040046 match api {
David Tolnayb7eb65e2020-11-27 20:00:53 -080047 Api::Include(_) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -070048 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070049 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay4cefa722020-10-03 21:24:07 -070050 Api::CxxType(ety) | Api::RustType(ety) => check_api_type(cx, ety),
David Tolnay26a2a1d2020-03-25 17:26:43 -070051 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnayb7eb65e2020-11-27 20:00:53 -080052 Api::TypeAlias(alias) => check_api_type_alias(cx, alias),
David Tolnay7e69f892020-10-03 22:20:22 -070053 Api::Impl(imp) => check_api_impl(cx, imp),
David Tolnay7db73692019-10-20 14:51:12 -040054 }
55 }
David Tolnay7db73692019-10-20 14:51:12 -040056}
57
David Tolnaya420f012020-03-25 17:55:56 -070058impl Check<'_> {
David Tolnaya83301c2020-04-30 20:32:37 -070059 pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
David Tolnaydf344a82020-05-03 23:23:18 -070060 self.errors.error(sp, msg);
David Tolnaya420f012020-03-25 17:55:56 -070061 }
62}
63
David Tolnay77a5e752021-01-01 14:15:18 -080064fn check_type_ident(cx: &mut Check, name: &NamedType) {
David Tolnay679b15d2020-12-30 20:39:06 -080065 let ident = &name.rust;
David Tolnayd4e68302020-03-25 12:04:17 -070066 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070067 && !cx.types.structs.contains_key(ident)
Joel Galensonc03402a2020-04-23 17:31:09 -070068 && !cx.types.enums.contains_key(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -070069 && !cx.types.cxx.contains(ident)
70 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070071 {
David Tolnaydbc53772020-10-28 17:28:51 -070072 let msg = format!("unsupported type: {}", ident);
73 cx.error(ident, &msg);
David Tolnayd4e68302020-03-25 12:04:17 -070074 }
75}
76
David Tolnay26a2a1d2020-03-25 17:26:43 -070077fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070078 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -070079 if cx.types.cxx.contains(&ident.rust)
David Tolnay25ed7832020-12-04 18:20:45 -080080 && !cx.types.aliases.contains_key(&ident.rust)
Adrian Taylorc8713432020-10-21 18:20:55 -070081 && !cx.types.structs.contains_key(&ident.rust)
82 && !cx.types.enums.contains_key(&ident.rust)
David Tolnayc880ae22020-08-29 16:46:00 -070083 {
David Tolnaya420f012020-03-25 17:55:56 -070084 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070085 }
David Tolnaya420f012020-03-25 17:55:56 -070086
Adrian Taylorc8713432020-10-21 18:20:55 -070087 if Atom::from(&ident.rust).is_none() {
David Tolnayd4e68302020-03-25 12:04:17 -070088 return;
89 }
90 }
David Tolnaya420f012020-03-25 17:55:56 -070091
92 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070093}
94
David Tolnayc6d891e2020-04-25 12:06:34 -070095fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
96 if let Type::Ident(ident) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -070097 if cx.types.cxx.contains(&ident.rust)
David Tolnayfccf4e52020-12-04 17:04:20 -080098 && !cx.types.aliases.contains_key(&ident.rust)
Adrian Taylorc8713432020-10-21 18:20:55 -070099 && !cx.types.structs.contains_key(&ident.rust)
100 && !cx.types.enums.contains_key(&ident.rust)
David Tolnayc880ae22020-08-29 16:46:00 -0700101 {
David Tolnayc6d891e2020-04-25 12:06:34 -0700102 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +0700103 return;
David Tolnayc6d891e2020-04-25 12:06:34 -0700104 }
105
Adrian Taylorc8713432020-10-21 18:20:55 -0700106 match Atom::from(&ident.rust) {
David Tolnay93e71d02020-11-25 20:16:52 -0800107 None | Some(Char) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
108 | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
109 | Some(F64) | Some(RustString) => return,
110 Some(Bool) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -0700111 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700112 }
113 }
114
David Tolnayc6d891e2020-04-25 12:06:34 -0700115 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700116}
117
David Tolnay26a2a1d2020-03-25 17:26:43 -0700118fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700119 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700120 if cx.types.rust.contains(&ident.rust) {
David Tolnaya420f012020-03-25 17:55:56 -0700121 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayb3b24a12020-12-01 15:27:43 -0800122 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700123 }
David Tolnaya420f012020-03-25 17:55:56 -0700124
Adrian Taylorc8713432020-10-21 18:20:55 -0700125 match Atom::from(&ident.rust) {
David Tolnayd4e68302020-03-25 12:04:17 -0700126 None | Some(CxxString) => return,
127 _ => {}
128 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700129 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700130 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700131 }
David Tolnaya420f012020-03-25 17:55:56 -0700132
133 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700134}
135
David Tolnayb3b24a12020-12-01 15:27:43 -0800136fn check_type_shared_ptr(cx: &mut Check, ptr: &Ty1) {
137 if let Type::Ident(ident) = &ptr.inner {
138 if cx.types.rust.contains(&ident.rust) {
139 cx.error(ptr, "shared_ptr of a Rust type is not supported yet");
140 return;
141 }
142
143 match Atom::from(&ident.rust) {
David Tolnaycd1430c2020-12-28 17:17:14 -0800144 None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
145 | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
146 | Some(F64) | Some(CxxString) => return,
147 Some(Char) | Some(RustString) => {}
David Tolnayb3b24a12020-12-01 15:27:43 -0800148 }
149 } else if let Type::CxxVector(_) = &ptr.inner {
150 cx.error(ptr, "std::shared_ptr<std::vector> is not supported yet");
151 return;
152 }
153
154 cx.error(ptr, "unsupported shared_ptr target type");
155}
156
David Tolnay215e77f2020-12-28 17:09:48 -0800157fn check_type_weak_ptr(cx: &mut Check, ptr: &Ty1) {
158 if let Type::Ident(ident) = &ptr.inner {
159 if cx.types.rust.contains(&ident.rust) {
160 cx.error(ptr, "weak_ptr of a Rust type is not supported yet");
161 return;
162 }
163
164 match Atom::from(&ident.rust) {
165 None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
166 | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
167 | Some(F64) | Some(CxxString) => return,
168 Some(Char) | Some(RustString) => {}
169 }
170 } else if let Type::CxxVector(_) = &ptr.inner {
171 cx.error(ptr, "std::weak_ptr<std::vector> is not supported yet");
172 return;
173 }
174
175 cx.error(ptr, "unsupported weak_ptr target type");
176}
177
David Tolnayfff4c8a2020-04-25 11:45:20 -0700178fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700179 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700180 if cx.types.rust.contains(&ident.rust) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700181 cx.error(
182 ptr,
183 "C++ vector containing a Rust type is not supported yet",
184 );
David Tolnayb3b24a12020-12-01 15:27:43 -0800185 return;
Myron Ahneba35cf2020-02-05 19:41:51 +0700186 }
187
Adrian Taylorc8713432020-10-21 18:20:55 -0700188 match Atom::from(&ident.rust) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700189 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay47e239d2020-08-28 00:32:04 -0700190 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
191 | Some(CxxString) => return,
David Tolnayb3873dc2020-11-25 19:47:49 -0800192 Some(Char) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -0700193 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700194 }
195 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700196
Myron Ahneba35cf2020-02-05 19:41:51 +0700197 cx.error(ptr, "unsupported vector target type");
198}
199
David Tolnay26a2a1d2020-03-25 17:26:43 -0700200fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800201 if ty.mutable && !ty.pinned {
David Tolnaydb42cfd2020-11-15 15:31:40 -0800202 if let Some(requires_pin) = match &ty.inner {
203 Type::Ident(ident) if ident.rust == CxxString || is_opaque_cxx(cx, &ident.rust) => {
204 Some(ident.rust.to_string())
205 }
206 Type::CxxVector(_) => Some("CxxVector<...>".to_owned()),
207 _ => None,
208 } {
209 cx.error(
210 ty,
211 format!(
David Tolnayee0ccbe2020-12-21 13:04:35 -0800212 "mutable reference to C++ type requires a pin -- use Pin<&mut {}>",
David Tolnaydb42cfd2020-11-15 15:31:40 -0800213 requires_pin,
214 ),
215 );
216 }
217 }
218
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700219 match ty.inner {
220 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700221 Type::Ref(_) => {
222 cx.error(ty, "C++ does not allow references to references");
223 return;
224 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700225 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700226 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700227
228 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700229}
230
David Tolnay69db8142020-11-25 17:49:25 -0800231fn check_type_slice_ref(cx: &mut Check, ty: &SliceRef) {
David Tolnaybf9d6e82021-01-02 23:08:22 -0800232 let supported = !is_unsized(cx, &ty.inner)
233 || match &ty.inner {
234 Type::Ident(ident) => cx.types.rust.contains(&ident.rust),
235 _ => false,
236 };
David Tolnay5515a9e2020-11-25 19:07:54 -0800237
238 if !supported {
239 let mutable = if ty.mutable { "mut " } else { "" };
240 let mut msg = format!("unsupported &{}[T] element type", mutable);
241 if let Type::Ident(ident) = &ty.inner {
David Tolnay3adf3c82020-12-27 02:21:45 -0800242 if is_opaque_cxx(cx, &ident.rust) {
David Tolnay5515a9e2020-11-25 19:07:54 -0800243 msg += ": opaque C++ type is not supported yet";
244 }
David Tolnay85ef67a2020-11-25 17:51:27 -0800245 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800246 cx.error(ty, msg);
David Tolnay69db8142020-11-25 17:49:25 -0800247 }
248}
249
Xiangpeng Hao78762352020-11-12 10:24:18 +0800250fn check_type_array(cx: &mut Check, ty: &Array) {
David Tolnaybf9d6e82021-01-02 23:08:22 -0800251 let supported = !is_unsized(cx, &ty.inner);
David Tolnayda2558e2020-11-25 17:15:23 -0800252
253 if !supported {
David Tolnayc4658292020-11-24 21:24:30 -0800254 cx.error(ty, "unsupported array element type");
David Tolnaye8b1bb42020-11-24 20:37:37 -0800255 }
Xiangpeng Hao78762352020-11-12 10:24:18 +0800256}
257
David Tolnay1db70c42020-11-14 23:28:58 -0800258fn check_type_fn(cx: &mut Check, ty: &Signature) {
259 if ty.throws {
260 cx.error(ty, "function pointer returning Result is not supported yet");
261 }
262}
263
David Tolnay26a2a1d2020-03-25 17:26:43 -0700264fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay17a934c2020-11-02 00:40:04 -0800265 let name = &strct.name;
266 check_reserved_name(cx, &name.rust);
David Tolnay0b368ae2020-04-22 17:55:02 -0700267
David Tolnayd4e68302020-03-25 12:04:17 -0700268 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700269 let span = span_for_struct_error(strct);
270 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700271 }
David Tolnaya420f012020-03-25 17:55:56 -0700272
David Tolnay17a934c2020-11-02 00:40:04 -0800273 if cx.types.cxx.contains(&name.rust) {
274 if let Some(ety) = cx.types.untrusted.get(&name.rust) {
David Tolnayc8b50352020-08-29 19:27:01 -0700275 let msg = "extern shared struct must be declared in an `unsafe extern` block";
276 cx.error(ety, msg);
David Tolnayc8b50352020-08-29 19:27:01 -0700277 }
David Tolnayc880ae22020-08-29 16:46:00 -0700278 }
279
David Tolnay16e26202020-11-27 19:28:37 -0800280 for derive in &strct.derives {
281 if derive.what == Trait::ExternType {
282 let msg = format!("derive({}) on shared struct is not supported", derive);
283 cx.error(derive, msg);
284 }
285 }
286
David Tolnayd4e68302020-03-25 12:04:17 -0700287 for field in &strct.fields {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700288 if let Type::Fn(_) = field.ty {
289 cx.error(
290 field,
291 "function pointers in a struct field are not implemented yet",
292 );
David Tolnay817f09e2020-11-25 16:58:35 -0800293 } else if is_unsized(cx, &field.ty) {
294 let desc = describe(cx, &field.ty);
295 let msg = format!("using {} by value is not supported", desc);
296 cx.error(field, msg);
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700297 }
David Tolnayd4e68302020-03-25 12:04:17 -0700298 }
299}
300
Joel Galensonc03402a2020-04-23 17:31:09 -0700301fn check_api_enum(cx: &mut Check, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800302 check_reserved_name(cx, &enm.name.rust);
Joel Galensonc03402a2020-04-23 17:31:09 -0700303
David Tolnay58eee392020-11-20 20:37:58 -0800304 if enm.variants.is_empty() && !enm.explicit_repr {
Joel Galensonc03402a2020-04-23 17:31:09 -0700305 let span = span_for_enum_error(enm);
David Tolnayb4f4fac2020-11-22 00:26:02 -0800306 cx.error(
307 span,
308 "explicit #[repr(...)] is required for enum without any variants",
309 );
Joel Galensonc03402a2020-04-23 17:31:09 -0700310 }
David Tolnaya6a9e942020-11-27 17:22:35 -0800311
312 for derive in &enm.derives {
David Tolnay16e26202020-11-27 19:28:37 -0800313 if derive.what == Trait::Default || derive.what == Trait::ExternType {
314 let msg = format!("derive({}) on shared enum is not supported", derive);
315 cx.error(derive, msg);
David Tolnaya6a9e942020-11-27 17:22:35 -0800316 }
317 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700318}
319
David Tolnay4cefa722020-10-03 21:24:07 -0700320fn check_api_type(cx: &mut Check, ety: &ExternType) {
David Tolnay17a934c2020-11-02 00:40:04 -0800321 check_reserved_name(cx, &ety.name.rust);
David Tolnay3208fd72020-10-03 20:19:40 -0700322
David Tolnay16e26202020-11-27 19:28:37 -0800323 for derive in &ety.derives {
324 if derive.what == Trait::ExternType && ety.lang == Lang::Rust {
325 continue;
326 }
327 let lang = match ety.lang {
328 Lang::Rust => "Rust",
329 Lang::Cxx => "C++",
330 };
331 let msg = format!(
332 "derive({}) on opaque {} type is not supported yet",
333 derive, lang,
334 );
335 cx.error(derive, msg);
336 }
337
David Tolnayb7eb65e2020-11-27 20:00:53 -0800338 if !ety.bounds.is_empty() {
339 let bounds = &ety.bounds;
340 let span = quote!(#(#bounds)*);
341 cx.error(span, "extern type bounds are not implemented yet");
342 }
343
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800344 if let Some(reasons) = cx.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800345 let msg = format!(
346 "needs a cxx::ExternType impl in order to be used as {}",
347 trivial::as_what(&ety.name, reasons),
348 );
349 cx.error(ety, msg);
David Tolnay3208fd72020-10-03 20:19:40 -0700350 }
David Tolnay0b368ae2020-04-22 17:55:02 -0700351}
352
David Tolnay26a2a1d2020-03-25 17:26:43 -0700353fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnay9938b642020-11-15 18:11:40 -0800354 match efn.lang {
355 Lang::Cxx => {
356 if !efn.generics.params.is_empty() && !efn.trusted {
357 let ref span = span_for_generics_error(efn);
358 cx.error(span, "extern C++ function with lifetimes must be declared in `unsafe extern \"C++\"` block");
359 }
360 }
361 Lang::Rust => {
362 if !efn.generics.params.is_empty() && efn.unsafety.is_none() {
363 let ref span = span_for_generics_error(efn);
364 let message = format!(
365 "must be `unsafe fn {}` in order to expose explicit lifetimes to C++",
366 efn.name.rust,
367 );
368 cx.error(span, message);
369 }
370 }
371 }
372
David Tolnayd763f4c2020-04-22 16:09:19 -0700373 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700374 let ref span = span_for_receiver_error(receiver);
375
David Tolnay2d99b002020-12-21 14:34:35 -0800376 if receiver.ty.rust == "Self" {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800377 let mutability = match receiver.mutable {
378 true => "mut ",
379 false => "",
David Tolnaya1f29c42020-04-22 18:01:38 -0700380 };
381 let msg = format!(
382 "unnamed receiver type is only allowed if the surrounding \
383 extern block contains exactly one extern type; \
384 use `self: &{mutability}TheType`",
385 mutability = mutability,
386 );
387 cx.error(span, msg);
Adrian Taylorc8713432020-10-21 18:20:55 -0700388 } else if !cx.types.structs.contains_key(&receiver.ty.rust)
389 && !cx.types.cxx.contains(&receiver.ty.rust)
390 && !cx.types.rust.contains(&receiver.ty.rust)
David Tolnay8b60bf12020-04-22 16:52:19 -0700391 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700392 cx.error(span, "unrecognized receiver type");
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800393 } else if receiver.mutable && !receiver.pinned && is_opaque_cxx(cx, &receiver.ty.rust) {
David Tolnay75aea9a2020-11-15 16:07:25 -0800394 cx.error(
395 span,
396 format!(
David Tolnayee0ccbe2020-12-21 13:04:35 -0800397 "mutable reference to opaque C++ type requires a pin -- use `self: Pin<&mut {}>`",
David Tolnay75aea9a2020-11-15 16:07:25 -0800398 receiver.ty.rust,
399 ),
400 );
David Tolnay8b60bf12020-04-22 16:52:19 -0700401 }
David Tolnayd763f4c2020-04-22 16:09:19 -0700402 }
403
David Tolnayd4e68302020-03-25 12:04:17 -0700404 for arg in &efn.args {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700405 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700406 if efn.lang == Lang::Rust {
407 cx.error(
408 arg,
409 "passing a function pointer from C++ to Rust is not implemented yet",
410 );
411 }
David Tolnay817f09e2020-11-25 16:58:35 -0800412 } else if is_unsized(cx, &arg.ty) {
413 let desc = describe(cx, &arg.ty);
414 let msg = format!("passing {} by value is not supported", desc);
415 cx.error(arg, msg);
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700416 }
David Tolnayd4e68302020-03-25 12:04:17 -0700417 }
David Tolnaya420f012020-03-25 17:55:56 -0700418
David Tolnayd4e68302020-03-25 12:04:17 -0700419 if let Some(ty) = &efn.ret {
David Tolnay817f09e2020-11-25 16:58:35 -0800420 if let Type::Fn(_) = ty {
421 cx.error(ty, "returning a function pointer is not implemented yet");
422 } else if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700423 let desc = describe(cx, ty);
424 let msg = format!("returning {} by value is not supported", desc);
425 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700426 }
427 }
David Tolnay96a826b2020-05-04 00:17:12 -0700428
429 if efn.lang == Lang::Cxx {
430 check_mut_return_restriction(cx, efn);
431 }
432
433 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400434}
435
David Tolnayb7eb65e2020-11-27 20:00:53 -0800436fn check_api_type_alias(cx: &mut Check, alias: &TypeAlias) {
437 for derive in &alias.derives {
438 let msg = format!("derive({}) on extern type alias is not supported", derive);
439 cx.error(derive, msg);
440 }
441}
442
David Tolnay7e69f892020-10-03 22:20:22 -0700443fn check_api_impl(cx: &mut Check, imp: &Impl) {
David Tolnay028d3d22020-11-29 18:09:52 -0800444 let ty = &imp.ty;
445
446 if let Some(negative) = imp.negative_token {
447 let span = quote!(#negative #ty);
448 cx.error(span, "negative impl is not supported yet");
449 return;
450 }
451
David Tolnayf33bc242020-12-04 18:27:02 -0800452 match ty {
453 Type::RustBox(ty)
454 | Type::RustVec(ty)
455 | Type::UniquePtr(ty)
456 | Type::SharedPtr(ty)
David Tolnay215e77f2020-12-28 17:09:48 -0800457 | Type::WeakPtr(ty)
David Tolnayf33bc242020-12-04 18:27:02 -0800458 | Type::CxxVector(ty) => {
459 if let Type::Ident(inner) = &ty.inner {
460 if Atom::from(&inner.rust).is_none() {
461 return;
462 }
David Tolnay7e69f892020-10-03 22:20:22 -0700463 }
464 }
David Tolnayf33bc242020-12-04 18:27:02 -0800465 _ => {}
David Tolnay7e69f892020-10-03 22:20:22 -0700466 }
467
468 cx.error(imp, "unsupported Self type of explicit impl");
469}
470
David Tolnay26a2a1d2020-03-25 17:26:43 -0700471fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400472 match &efn.ret {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800473 Some(Type::Ref(ty)) if ty.mutable => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700474 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400475 }
476
David Tolnaya020d102020-12-16 15:08:20 -0800477 if let Some(r) = &efn.receiver {
478 if r.mutable {
479 return;
480 }
481 }
482
David Tolnay7db73692019-10-20 14:51:12 -0400483 for arg in &efn.args {
484 if let Type::Ref(ty) = &arg.ty {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800485 if ty.mutable {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700486 return;
David Tolnay7db73692019-10-20 14:51:12 -0400487 }
488 }
489 }
490
David Tolnaya420f012020-03-25 17:55:56 -0700491 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400492 efn,
493 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700494 );
David Tolnay7db73692019-10-20 14:51:12 -0400495}
496
David Tolnay26a2a1d2020-03-25 17:26:43 -0700497fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay556738d2020-11-15 13:58:44 -0800498 if efn.lang == Lang::Cxx && efn.trusted {
499 return;
500 }
501
David Tolnay7db73692019-10-20 14:51:12 -0400502 match &efn.ret {
503 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700504 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400505 }
506
507 let mut reference_args = 0;
508 for arg in &efn.args {
509 if let Type::Ref(_) = &arg.ty {
510 reference_args += 1;
511 }
512 }
513
Joel Galenson3d4f6122020-04-07 15:54:05 -0700514 if efn.receiver.is_some() {
515 reference_args += 1;
516 }
517
David Tolnay26a2a1d2020-03-25 17:26:43 -0700518 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700519 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400520 efn,
521 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700522 );
David Tolnay7db73692019-10-20 14:51:12 -0400523 }
524}
525
David Tolnay0b368ae2020-04-22 17:55:02 -0700526fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700527 if ident == "Box"
528 || ident == "UniquePtr"
David Tolnayb3b24a12020-12-01 15:27:43 -0800529 || ident == "SharedPtr"
David Tolnay215e77f2020-12-28 17:09:48 -0800530 || ident == "WeakPtr"
David Tolnay99c93d82020-04-25 14:07:44 -0700531 || ident == "Vec"
532 || ident == "CxxVector"
David Tolnay4de34812020-11-19 14:48:55 -0800533 || ident == "str"
David Tolnay99c93d82020-04-25 14:07:44 -0700534 || Atom::from(ident).is_some()
535 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700536 cx.error(ident, "reserved name");
537 }
538}
539
David Tolnay26a2a1d2020-03-25 17:26:43 -0700540fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnay70e5bfb2020-11-19 14:45:33 -0800541 match ty {
542 Type::Ident(ident) => {
543 let ident = &ident.rust;
544 ident == CxxString || is_opaque_cxx(cx, ident) || cx.types.rust.contains(ident)
545 }
David Tolnay8f8d78d2020-11-24 20:29:49 -0800546 Type::Array(array) => is_unsized(cx, &array.inner),
David Tolnaye0dca7b2020-11-25 17:18:57 -0800547 Type::CxxVector(_) | Type::Fn(_) | Type::Void(_) => true,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800548 Type::RustBox(_)
549 | Type::RustVec(_)
550 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -0800551 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -0800552 | Type::WeakPtr(_)
David Tolnay70e5bfb2020-11-19 14:45:33 -0800553 | Type::Ref(_)
554 | Type::Str(_)
David Tolnay73b72642020-11-25 17:44:05 -0800555 | Type::SliceRef(_) => false,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800556 }
David Tolnaydb42cfd2020-11-15 15:31:40 -0800557}
558
559fn is_opaque_cxx(cx: &mut Check, ty: &Ident) -> bool {
560 cx.types.cxx.contains(ty)
561 && !cx.types.structs.contains_key(ty)
562 && !cx.types.enums.contains_key(ty)
563 && !(cx.types.aliases.contains_key(ty) && cx.types.required_trivial.contains_key(ty))
David Tolnay26a2a1d2020-03-25 17:26:43 -0700564}
565
David Tolnaya420f012020-03-25 17:55:56 -0700566fn span_for_struct_error(strct: &Struct) -> TokenStream {
567 let struct_token = strct.struct_token;
568 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
569 brace_token.set_span(strct.brace_token.span);
570 quote!(#struct_token #brace_token)
571}
572
Joel Galensonc03402a2020-04-23 17:31:09 -0700573fn span_for_enum_error(enm: &Enum) -> TokenStream {
574 let enum_token = enm.enum_token;
575 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
576 brace_token.set_span(enm.brace_token.span);
577 quote!(#enum_token #brace_token)
578}
579
David Tolnayd763f4c2020-04-22 16:09:19 -0700580fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
581 let ampersand = receiver.ampersand;
582 let lifetime = &receiver.lifetime;
583 let mutability = receiver.mutability;
584 if receiver.shorthand {
585 let var = receiver.var;
586 quote!(#ampersand #lifetime #mutability #var)
587 } else {
588 let ty = &receiver.ty;
589 quote!(#ampersand #lifetime #mutability #ty)
590 }
591}
592
David Tolnay9938b642020-11-15 18:11:40 -0800593fn span_for_generics_error(efn: &ExternFn) -> TokenStream {
594 let unsafety = efn.unsafety;
595 let fn_token = efn.fn_token;
596 let generics = &efn.generics;
597 quote!(#unsafety #fn_token #generics)
598}
599
David Tolnaya420f012020-03-25 17:55:56 -0700600fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400601 match ty {
602 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700603 if cx.types.structs.contains_key(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400604 "struct".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700605 } else if cx.types.enums.contains_key(&ident.rust) {
David Tolnay55483722020-08-29 18:27:37 -0700606 "enum".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700607 } else if cx.types.aliases.contains_key(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700608 "C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700609 } else if cx.types.cxx.contains(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700610 "opaque C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700611 } else if cx.types.rust.contains(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400612 "opaque Rust type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700613 } else if Atom::from(&ident.rust) == Some(CxxString) {
David Tolnay7db73692019-10-20 14:51:12 -0400614 "C++ string".to_owned()
David Tolnayb3873dc2020-11-25 19:47:49 -0800615 } else if Atom::from(&ident.rust) == Some(Char) {
616 "C char".to_owned()
David Tolnay7db73692019-10-20 14:51:12 -0400617 } else {
Adrian Taylorc8713432020-10-21 18:20:55 -0700618 ident.rust.to_string()
David Tolnay7db73692019-10-20 14:51:12 -0400619 }
620 }
621 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700622 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400623 Type::UniquePtr(_) => "unique_ptr".to_owned(),
David Tolnayb3b24a12020-12-01 15:27:43 -0800624 Type::SharedPtr(_) => "shared_ptr".to_owned(),
David Tolnay215e77f2020-12-28 17:09:48 -0800625 Type::WeakPtr(_) => "weak_ptr".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400626 Type::Ref(_) => "reference".to_owned(),
627 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700628 Type::CxxVector(_) => "C++ vector".to_owned(),
David Tolnay5515a9e2020-11-25 19:07:54 -0800629 Type::SliceRef(_) => "slice".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700630 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700631 Type::Void(_) => "()".to_owned(),
Xiangpeng Hao78762352020-11-12 10:24:18 +0800632 Type::Array(_) => "array".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400633 }
634}