blob: 2aefbdfe3e712f6c1d2e36a0a2e2066f9924e19f [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) {
David Tolnay2d1fcc22021-01-03 22:43:19 -080096 match &ty.inner {
97 Type::Ident(ident) => {
98 if cx.types.cxx.contains(&ident.rust)
99 && !cx.types.aliases.contains_key(&ident.rust)
100 && !cx.types.structs.contains_key(&ident.rust)
101 && !cx.types.enums.contains_key(&ident.rust)
102 {
103 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
104 return;
105 }
David Tolnayc6d891e2020-04-25 12:06:34 -0700106
David Tolnay2d1fcc22021-01-03 22:43:19 -0800107 match Atom::from(&ident.rust) {
108 None | Some(Char) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
109 | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
110 | Some(F64) | Some(RustString) => return,
111 Some(Bool) => { /* todo */ }
112 Some(CxxString) => {}
113 }
Myron Ahneba35cf2020-02-05 19:41:51 +0700114 }
David Tolnay2d1fcc22021-01-03 22:43:19 -0800115 Type::Str(_) => return,
116 _ => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700117 }
118
David Tolnayc6d891e2020-04-25 12:06:34 -0700119 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700120}
121
David Tolnay26a2a1d2020-03-25 17:26:43 -0700122fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700123 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700124 if cx.types.rust.contains(&ident.rust) {
David Tolnaya420f012020-03-25 17:55:56 -0700125 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayb3b24a12020-12-01 15:27:43 -0800126 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700127 }
David Tolnaya420f012020-03-25 17:55:56 -0700128
Adrian Taylorc8713432020-10-21 18:20:55 -0700129 match Atom::from(&ident.rust) {
David Tolnayd4e68302020-03-25 12:04:17 -0700130 None | Some(CxxString) => return,
131 _ => {}
132 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700133 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700134 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700135 }
David Tolnaya420f012020-03-25 17:55:56 -0700136
137 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700138}
139
David Tolnayb3b24a12020-12-01 15:27:43 -0800140fn check_type_shared_ptr(cx: &mut Check, ptr: &Ty1) {
141 if let Type::Ident(ident) = &ptr.inner {
142 if cx.types.rust.contains(&ident.rust) {
143 cx.error(ptr, "shared_ptr of a Rust type is not supported yet");
144 return;
145 }
146
147 match Atom::from(&ident.rust) {
David Tolnaycd1430c2020-12-28 17:17:14 -0800148 None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
149 | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
150 | Some(F64) | Some(CxxString) => return,
151 Some(Char) | Some(RustString) => {}
David Tolnayb3b24a12020-12-01 15:27:43 -0800152 }
153 } else if let Type::CxxVector(_) = &ptr.inner {
154 cx.error(ptr, "std::shared_ptr<std::vector> is not supported yet");
155 return;
156 }
157
158 cx.error(ptr, "unsupported shared_ptr target type");
159}
160
David Tolnay215e77f2020-12-28 17:09:48 -0800161fn check_type_weak_ptr(cx: &mut Check, ptr: &Ty1) {
162 if let Type::Ident(ident) = &ptr.inner {
163 if cx.types.rust.contains(&ident.rust) {
164 cx.error(ptr, "weak_ptr of a Rust type is not supported yet");
165 return;
166 }
167
168 match Atom::from(&ident.rust) {
169 None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
170 | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
171 | Some(F64) | Some(CxxString) => return,
172 Some(Char) | Some(RustString) => {}
173 }
174 } else if let Type::CxxVector(_) = &ptr.inner {
175 cx.error(ptr, "std::weak_ptr<std::vector> is not supported yet");
176 return;
177 }
178
179 cx.error(ptr, "unsupported weak_ptr target type");
180}
181
David Tolnayfff4c8a2020-04-25 11:45:20 -0700182fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700183 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700184 if cx.types.rust.contains(&ident.rust) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700185 cx.error(
186 ptr,
187 "C++ vector containing a Rust type is not supported yet",
188 );
David Tolnayb3b24a12020-12-01 15:27:43 -0800189 return;
Myron Ahneba35cf2020-02-05 19:41:51 +0700190 }
191
Adrian Taylorc8713432020-10-21 18:20:55 -0700192 match Atom::from(&ident.rust) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700193 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay47e239d2020-08-28 00:32:04 -0700194 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
195 | Some(CxxString) => return,
David Tolnayb3873dc2020-11-25 19:47:49 -0800196 Some(Char) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -0700197 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700198 }
199 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700200
Myron Ahneba35cf2020-02-05 19:41:51 +0700201 cx.error(ptr, "unsupported vector target type");
202}
203
David Tolnay26a2a1d2020-03-25 17:26:43 -0700204fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800205 if ty.mutable && !ty.pinned {
David Tolnaydb42cfd2020-11-15 15:31:40 -0800206 if let Some(requires_pin) = match &ty.inner {
207 Type::Ident(ident) if ident.rust == CxxString || is_opaque_cxx(cx, &ident.rust) => {
208 Some(ident.rust.to_string())
209 }
210 Type::CxxVector(_) => Some("CxxVector<...>".to_owned()),
211 _ => None,
212 } {
213 cx.error(
214 ty,
215 format!(
David Tolnayee0ccbe2020-12-21 13:04:35 -0800216 "mutable reference to C++ type requires a pin -- use Pin<&mut {}>",
David Tolnaydb42cfd2020-11-15 15:31:40 -0800217 requires_pin,
218 ),
219 );
220 }
221 }
222
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700223 match ty.inner {
224 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700225 Type::Ref(_) => {
226 cx.error(ty, "C++ does not allow references to references");
227 return;
228 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700229 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700230 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700231
232 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700233}
234
David Tolnay69db8142020-11-25 17:49:25 -0800235fn check_type_slice_ref(cx: &mut Check, ty: &SliceRef) {
David Tolnaybf9d6e82021-01-02 23:08:22 -0800236 let supported = !is_unsized(cx, &ty.inner)
237 || match &ty.inner {
238 Type::Ident(ident) => cx.types.rust.contains(&ident.rust),
239 _ => false,
240 };
David Tolnay5515a9e2020-11-25 19:07:54 -0800241
242 if !supported {
243 let mutable = if ty.mutable { "mut " } else { "" };
244 let mut msg = format!("unsupported &{}[T] element type", mutable);
245 if let Type::Ident(ident) = &ty.inner {
David Tolnay3adf3c82020-12-27 02:21:45 -0800246 if is_opaque_cxx(cx, &ident.rust) {
David Tolnay5515a9e2020-11-25 19:07:54 -0800247 msg += ": opaque C++ type is not supported yet";
248 }
David Tolnay85ef67a2020-11-25 17:51:27 -0800249 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800250 cx.error(ty, msg);
David Tolnay69db8142020-11-25 17:49:25 -0800251 }
252}
253
Xiangpeng Hao78762352020-11-12 10:24:18 +0800254fn check_type_array(cx: &mut Check, ty: &Array) {
David Tolnaybf9d6e82021-01-02 23:08:22 -0800255 let supported = !is_unsized(cx, &ty.inner);
David Tolnayda2558e2020-11-25 17:15:23 -0800256
257 if !supported {
David Tolnayc4658292020-11-24 21:24:30 -0800258 cx.error(ty, "unsupported array element type");
David Tolnaye8b1bb42020-11-24 20:37:37 -0800259 }
Xiangpeng Hao78762352020-11-12 10:24:18 +0800260}
261
David Tolnay1db70c42020-11-14 23:28:58 -0800262fn check_type_fn(cx: &mut Check, ty: &Signature) {
263 if ty.throws {
264 cx.error(ty, "function pointer returning Result is not supported yet");
265 }
266}
267
David Tolnay26a2a1d2020-03-25 17:26:43 -0700268fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay17a934c2020-11-02 00:40:04 -0800269 let name = &strct.name;
270 check_reserved_name(cx, &name.rust);
David Tolnay0b368ae2020-04-22 17:55:02 -0700271
David Tolnayd4e68302020-03-25 12:04:17 -0700272 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700273 let span = span_for_struct_error(strct);
274 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700275 }
David Tolnaya420f012020-03-25 17:55:56 -0700276
David Tolnay17a934c2020-11-02 00:40:04 -0800277 if cx.types.cxx.contains(&name.rust) {
278 if let Some(ety) = cx.types.untrusted.get(&name.rust) {
David Tolnayc8b50352020-08-29 19:27:01 -0700279 let msg = "extern shared struct must be declared in an `unsafe extern` block";
280 cx.error(ety, msg);
David Tolnayc8b50352020-08-29 19:27:01 -0700281 }
David Tolnayc880ae22020-08-29 16:46:00 -0700282 }
283
David Tolnay16e26202020-11-27 19:28:37 -0800284 for derive in &strct.derives {
285 if derive.what == Trait::ExternType {
286 let msg = format!("derive({}) on shared struct is not supported", derive);
287 cx.error(derive, msg);
288 }
289 }
290
David Tolnayd4e68302020-03-25 12:04:17 -0700291 for field in &strct.fields {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700292 if let Type::Fn(_) = field.ty {
293 cx.error(
294 field,
295 "function pointers in a struct field are not implemented yet",
296 );
David Tolnay817f09e2020-11-25 16:58:35 -0800297 } else if is_unsized(cx, &field.ty) {
298 let desc = describe(cx, &field.ty);
299 let msg = format!("using {} by value is not supported", desc);
300 cx.error(field, msg);
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700301 }
David Tolnayd4e68302020-03-25 12:04:17 -0700302 }
303}
304
Joel Galensonc03402a2020-04-23 17:31:09 -0700305fn check_api_enum(cx: &mut Check, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800306 check_reserved_name(cx, &enm.name.rust);
Joel Galensonc03402a2020-04-23 17:31:09 -0700307
David Tolnay58eee392020-11-20 20:37:58 -0800308 if enm.variants.is_empty() && !enm.explicit_repr {
Joel Galensonc03402a2020-04-23 17:31:09 -0700309 let span = span_for_enum_error(enm);
David Tolnayb4f4fac2020-11-22 00:26:02 -0800310 cx.error(
311 span,
312 "explicit #[repr(...)] is required for enum without any variants",
313 );
Joel Galensonc03402a2020-04-23 17:31:09 -0700314 }
David Tolnaya6a9e942020-11-27 17:22:35 -0800315
316 for derive in &enm.derives {
David Tolnay16e26202020-11-27 19:28:37 -0800317 if derive.what == Trait::Default || derive.what == Trait::ExternType {
318 let msg = format!("derive({}) on shared enum is not supported", derive);
319 cx.error(derive, msg);
David Tolnaya6a9e942020-11-27 17:22:35 -0800320 }
321 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700322}
323
David Tolnay4cefa722020-10-03 21:24:07 -0700324fn check_api_type(cx: &mut Check, ety: &ExternType) {
David Tolnay17a934c2020-11-02 00:40:04 -0800325 check_reserved_name(cx, &ety.name.rust);
David Tolnay3208fd72020-10-03 20:19:40 -0700326
David Tolnay16e26202020-11-27 19:28:37 -0800327 for derive in &ety.derives {
328 if derive.what == Trait::ExternType && ety.lang == Lang::Rust {
329 continue;
330 }
331 let lang = match ety.lang {
332 Lang::Rust => "Rust",
333 Lang::Cxx => "C++",
334 };
335 let msg = format!(
336 "derive({}) on opaque {} type is not supported yet",
337 derive, lang,
338 );
339 cx.error(derive, msg);
340 }
341
David Tolnayb7eb65e2020-11-27 20:00:53 -0800342 if !ety.bounds.is_empty() {
343 let bounds = &ety.bounds;
344 let span = quote!(#(#bounds)*);
345 cx.error(span, "extern type bounds are not implemented yet");
346 }
347
Adrian Taylorf831a5a2020-12-07 16:49:19 -0800348 if let Some(reasons) = cx.types.required_trivial.get(&ety.name.rust) {
David Tolnay5b1d8632020-12-20 22:05:11 -0800349 let msg = format!(
350 "needs a cxx::ExternType impl in order to be used as {}",
351 trivial::as_what(&ety.name, reasons),
352 );
353 cx.error(ety, msg);
David Tolnay3208fd72020-10-03 20:19:40 -0700354 }
David Tolnay0b368ae2020-04-22 17:55:02 -0700355}
356
David Tolnay26a2a1d2020-03-25 17:26:43 -0700357fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnay9938b642020-11-15 18:11:40 -0800358 match efn.lang {
359 Lang::Cxx => {
360 if !efn.generics.params.is_empty() && !efn.trusted {
361 let ref span = span_for_generics_error(efn);
362 cx.error(span, "extern C++ function with lifetimes must be declared in `unsafe extern \"C++\"` block");
363 }
364 }
365 Lang::Rust => {
366 if !efn.generics.params.is_empty() && efn.unsafety.is_none() {
367 let ref span = span_for_generics_error(efn);
368 let message = format!(
369 "must be `unsafe fn {}` in order to expose explicit lifetimes to C++",
370 efn.name.rust,
371 );
372 cx.error(span, message);
373 }
374 }
375 }
376
David Tolnayd763f4c2020-04-22 16:09:19 -0700377 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700378 let ref span = span_for_receiver_error(receiver);
379
David Tolnay2d99b002020-12-21 14:34:35 -0800380 if receiver.ty.rust == "Self" {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800381 let mutability = match receiver.mutable {
382 true => "mut ",
383 false => "",
David Tolnaya1f29c42020-04-22 18:01:38 -0700384 };
385 let msg = format!(
David Tolnay35f07db2021-02-07 13:38:00 -0800386 "unnamed receiver type is only allowed if the surrounding extern block contains exactly one extern type; use `self: &{mutability}TheType`",
David Tolnaya1f29c42020-04-22 18:01:38 -0700387 mutability = mutability,
388 );
389 cx.error(span, msg);
Fletcher Woodruff0ba10792021-01-20 14:09:04 -0700390 } else if cx.types.enums.contains_key(&receiver.ty.rust) {
David Tolnayd1560842021-02-07 13:36:26 -0800391 cx.error(
392 span,
393 "unsupported receiver type; C++ does not allow member functions on enums",
394 );
Adrian Taylorc8713432020-10-21 18:20:55 -0700395 } else if !cx.types.structs.contains_key(&receiver.ty.rust)
396 && !cx.types.cxx.contains(&receiver.ty.rust)
397 && !cx.types.rust.contains(&receiver.ty.rust)
David Tolnay8b60bf12020-04-22 16:52:19 -0700398 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700399 cx.error(span, "unrecognized receiver type");
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800400 } else if receiver.mutable && !receiver.pinned && is_opaque_cxx(cx, &receiver.ty.rust) {
David Tolnay75aea9a2020-11-15 16:07:25 -0800401 cx.error(
402 span,
403 format!(
David Tolnayee0ccbe2020-12-21 13:04:35 -0800404 "mutable reference to opaque C++ type requires a pin -- use `self: Pin<&mut {}>`",
David Tolnay75aea9a2020-11-15 16:07:25 -0800405 receiver.ty.rust,
406 ),
407 );
David Tolnay8b60bf12020-04-22 16:52:19 -0700408 }
David Tolnayd763f4c2020-04-22 16:09:19 -0700409 }
410
David Tolnayd4e68302020-03-25 12:04:17 -0700411 for arg in &efn.args {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700412 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700413 if efn.lang == Lang::Rust {
414 cx.error(
415 arg,
416 "passing a function pointer from C++ to Rust is not implemented yet",
417 );
418 }
David Tolnay817f09e2020-11-25 16:58:35 -0800419 } else if is_unsized(cx, &arg.ty) {
420 let desc = describe(cx, &arg.ty);
421 let msg = format!("passing {} by value is not supported", desc);
422 cx.error(arg, msg);
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700423 }
David Tolnayd4e68302020-03-25 12:04:17 -0700424 }
David Tolnaya420f012020-03-25 17:55:56 -0700425
David Tolnayd4e68302020-03-25 12:04:17 -0700426 if let Some(ty) = &efn.ret {
David Tolnay817f09e2020-11-25 16:58:35 -0800427 if let Type::Fn(_) = ty {
428 cx.error(ty, "returning a function pointer is not implemented yet");
429 } else if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700430 let desc = describe(cx, ty);
431 let msg = format!("returning {} by value is not supported", desc);
432 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700433 }
434 }
David Tolnay96a826b2020-05-04 00:17:12 -0700435
436 if efn.lang == Lang::Cxx {
437 check_mut_return_restriction(cx, efn);
438 }
439
440 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400441}
442
David Tolnayb7eb65e2020-11-27 20:00:53 -0800443fn check_api_type_alias(cx: &mut Check, alias: &TypeAlias) {
444 for derive in &alias.derives {
445 let msg = format!("derive({}) on extern type alias is not supported", derive);
446 cx.error(derive, msg);
447 }
448}
449
David Tolnay7e69f892020-10-03 22:20:22 -0700450fn check_api_impl(cx: &mut Check, imp: &Impl) {
David Tolnay028d3d22020-11-29 18:09:52 -0800451 let ty = &imp.ty;
452
453 if let Some(negative) = imp.negative_token {
454 let span = quote!(#negative #ty);
455 cx.error(span, "negative impl is not supported yet");
456 return;
457 }
458
David Tolnayf33bc242020-12-04 18:27:02 -0800459 match ty {
460 Type::RustBox(ty)
461 | Type::RustVec(ty)
462 | Type::UniquePtr(ty)
463 | Type::SharedPtr(ty)
David Tolnay215e77f2020-12-28 17:09:48 -0800464 | Type::WeakPtr(ty)
David Tolnayf33bc242020-12-04 18:27:02 -0800465 | Type::CxxVector(ty) => {
466 if let Type::Ident(inner) = &ty.inner {
467 if Atom::from(&inner.rust).is_none() {
468 return;
469 }
David Tolnay7e69f892020-10-03 22:20:22 -0700470 }
471 }
David Tolnayf33bc242020-12-04 18:27:02 -0800472 _ => {}
David Tolnay7e69f892020-10-03 22:20:22 -0700473 }
474
475 cx.error(imp, "unsupported Self type of explicit impl");
476}
477
David Tolnay26a2a1d2020-03-25 17:26:43 -0700478fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400479 match &efn.ret {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800480 Some(Type::Ref(ty)) if ty.mutable => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700481 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400482 }
483
David Tolnaya020d102020-12-16 15:08:20 -0800484 if let Some(r) = &efn.receiver {
485 if r.mutable {
486 return;
487 }
488 }
489
David Tolnay7db73692019-10-20 14:51:12 -0400490 for arg in &efn.args {
491 if let Type::Ref(ty) = &arg.ty {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800492 if ty.mutable {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700493 return;
David Tolnay7db73692019-10-20 14:51:12 -0400494 }
495 }
496 }
497
David Tolnaya420f012020-03-25 17:55:56 -0700498 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400499 efn,
500 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700501 );
David Tolnay7db73692019-10-20 14:51:12 -0400502}
503
David Tolnay26a2a1d2020-03-25 17:26:43 -0700504fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay556738d2020-11-15 13:58:44 -0800505 if efn.lang == Lang::Cxx && efn.trusted {
506 return;
507 }
508
David Tolnay7db73692019-10-20 14:51:12 -0400509 match &efn.ret {
510 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700511 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400512 }
513
514 let mut reference_args = 0;
515 for arg in &efn.args {
516 if let Type::Ref(_) = &arg.ty {
517 reference_args += 1;
518 }
519 }
520
Joel Galenson3d4f6122020-04-07 15:54:05 -0700521 if efn.receiver.is_some() {
522 reference_args += 1;
523 }
524
David Tolnay26a2a1d2020-03-25 17:26:43 -0700525 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700526 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400527 efn,
528 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700529 );
David Tolnay7db73692019-10-20 14:51:12 -0400530 }
531}
532
David Tolnay0b368ae2020-04-22 17:55:02 -0700533fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700534 if ident == "Box"
535 || ident == "UniquePtr"
David Tolnayb3b24a12020-12-01 15:27:43 -0800536 || ident == "SharedPtr"
David Tolnay215e77f2020-12-28 17:09:48 -0800537 || ident == "WeakPtr"
David Tolnay99c93d82020-04-25 14:07:44 -0700538 || ident == "Vec"
539 || ident == "CxxVector"
David Tolnay4de34812020-11-19 14:48:55 -0800540 || ident == "str"
David Tolnay99c93d82020-04-25 14:07:44 -0700541 || Atom::from(ident).is_some()
542 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700543 cx.error(ident, "reserved name");
544 }
545}
546
David Tolnay26a2a1d2020-03-25 17:26:43 -0700547fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnay70e5bfb2020-11-19 14:45:33 -0800548 match ty {
549 Type::Ident(ident) => {
550 let ident = &ident.rust;
551 ident == CxxString || is_opaque_cxx(cx, ident) || cx.types.rust.contains(ident)
552 }
David Tolnay8f8d78d2020-11-24 20:29:49 -0800553 Type::Array(array) => is_unsized(cx, &array.inner),
David Tolnaye0dca7b2020-11-25 17:18:57 -0800554 Type::CxxVector(_) | Type::Fn(_) | Type::Void(_) => true,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800555 Type::RustBox(_)
556 | Type::RustVec(_)
557 | Type::UniquePtr(_)
David Tolnayb3b24a12020-12-01 15:27:43 -0800558 | Type::SharedPtr(_)
David Tolnay215e77f2020-12-28 17:09:48 -0800559 | Type::WeakPtr(_)
David Tolnay70e5bfb2020-11-19 14:45:33 -0800560 | Type::Ref(_)
561 | Type::Str(_)
David Tolnay73b72642020-11-25 17:44:05 -0800562 | Type::SliceRef(_) => false,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800563 }
David Tolnaydb42cfd2020-11-15 15:31:40 -0800564}
565
566fn is_opaque_cxx(cx: &mut Check, ty: &Ident) -> bool {
567 cx.types.cxx.contains(ty)
568 && !cx.types.structs.contains_key(ty)
569 && !cx.types.enums.contains_key(ty)
570 && !(cx.types.aliases.contains_key(ty) && cx.types.required_trivial.contains_key(ty))
David Tolnay26a2a1d2020-03-25 17:26:43 -0700571}
572
David Tolnaya420f012020-03-25 17:55:56 -0700573fn span_for_struct_error(strct: &Struct) -> TokenStream {
574 let struct_token = strct.struct_token;
575 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
576 brace_token.set_span(strct.brace_token.span);
577 quote!(#struct_token #brace_token)
578}
579
Joel Galensonc03402a2020-04-23 17:31:09 -0700580fn span_for_enum_error(enm: &Enum) -> TokenStream {
581 let enum_token = enm.enum_token;
582 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
583 brace_token.set_span(enm.brace_token.span);
584 quote!(#enum_token #brace_token)
585}
586
David Tolnayd763f4c2020-04-22 16:09:19 -0700587fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
588 let ampersand = receiver.ampersand;
589 let lifetime = &receiver.lifetime;
590 let mutability = receiver.mutability;
591 if receiver.shorthand {
592 let var = receiver.var;
593 quote!(#ampersand #lifetime #mutability #var)
594 } else {
595 let ty = &receiver.ty;
596 quote!(#ampersand #lifetime #mutability #ty)
597 }
598}
599
David Tolnay9938b642020-11-15 18:11:40 -0800600fn span_for_generics_error(efn: &ExternFn) -> TokenStream {
601 let unsafety = efn.unsafety;
602 let fn_token = efn.fn_token;
603 let generics = &efn.generics;
604 quote!(#unsafety #fn_token #generics)
605}
606
David Tolnaya420f012020-03-25 17:55:56 -0700607fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400608 match ty {
609 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700610 if cx.types.structs.contains_key(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400611 "struct".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700612 } else if cx.types.enums.contains_key(&ident.rust) {
David Tolnay55483722020-08-29 18:27:37 -0700613 "enum".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700614 } else if cx.types.aliases.contains_key(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700615 "C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700616 } else if cx.types.cxx.contains(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700617 "opaque C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700618 } else if cx.types.rust.contains(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400619 "opaque Rust type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700620 } else if Atom::from(&ident.rust) == Some(CxxString) {
David Tolnay7db73692019-10-20 14:51:12 -0400621 "C++ string".to_owned()
David Tolnayb3873dc2020-11-25 19:47:49 -0800622 } else if Atom::from(&ident.rust) == Some(Char) {
623 "C char".to_owned()
David Tolnay7db73692019-10-20 14:51:12 -0400624 } else {
Adrian Taylorc8713432020-10-21 18:20:55 -0700625 ident.rust.to_string()
David Tolnay7db73692019-10-20 14:51:12 -0400626 }
627 }
628 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700629 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400630 Type::UniquePtr(_) => "unique_ptr".to_owned(),
David Tolnayb3b24a12020-12-01 15:27:43 -0800631 Type::SharedPtr(_) => "shared_ptr".to_owned(),
David Tolnay215e77f2020-12-28 17:09:48 -0800632 Type::WeakPtr(_) => "weak_ptr".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400633 Type::Ref(_) => "reference".to_owned(),
634 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700635 Type::CxxVector(_) => "C++ vector".to_owned(),
David Tolnay5515a9e2020-11-25 19:07:54 -0800636 Type::SliceRef(_) => "slice".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700637 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700638 Type::Void(_) => "()".to_owned(),
Xiangpeng Hao78762352020-11-12 10:24:18 +0800639 Type::Array(_) => "array".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400640 }
641}