blob: 15e19b8e05d0e81d6a3af41837459472d70ed660 [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 Tolnaye8b1bb42020-11-24 20:37:37 -08005 error, ident, Api, Array, Enum, ExternFn, ExternType, Impl, Lang, Receiver, Ref, Signature,
David Tolnayb7eb65e2020-11-27 20:00:53 -08006 SliceRef, Struct, Trait, Ty1, Type, TypeAlias, 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),
Xiangpeng Hao78762352020-11-12 10:24:18 +080037 Type::Array(array) => check_type_array(cx, array),
David Tolnay1db70c42020-11-14 23:28:58 -080038 Type::Fn(ty) => check_type_fn(cx, ty),
David Tolnay69db8142020-11-25 17:49:25 -080039 Type::SliceRef(ty) => check_type_slice_ref(cx, ty),
40 Type::Str(_) | Type::Void(_) => {}
David Tolnay7db73692019-10-20 14:51:12 -040041 }
42 }
43
David Tolnay26a2a1d2020-03-25 17:26:43 -070044 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040045 match api {
David Tolnayb7eb65e2020-11-27 20:00:53 -080046 Api::Include(_) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -070047 Api::Struct(strct) => check_api_struct(cx, strct),
Joel Galensonc03402a2020-04-23 17:31:09 -070048 Api::Enum(enm) => check_api_enum(cx, enm),
David Tolnay4cefa722020-10-03 21:24:07 -070049 Api::CxxType(ety) | Api::RustType(ety) => check_api_type(cx, ety),
David Tolnay26a2a1d2020-03-25 17:26:43 -070050 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnayb7eb65e2020-11-27 20:00:53 -080051 Api::TypeAlias(alias) => check_api_type_alias(cx, alias),
David Tolnay7e69f892020-10-03 22:20:22 -070052 Api::Impl(imp) => check_api_impl(cx, imp),
David Tolnay7db73692019-10-20 14:51:12 -040053 }
54 }
David Tolnay7db73692019-10-20 14:51:12 -040055}
56
David Tolnaya420f012020-03-25 17:55:56 -070057impl Check<'_> {
David Tolnaya83301c2020-04-30 20:32:37 -070058 pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
David Tolnaydf344a82020-05-03 23:23:18 -070059 self.errors.error(sp, msg);
David Tolnaya420f012020-03-25 17:55:56 -070060 }
61}
62
David Tolnay26a2a1d2020-03-25 17:26:43 -070063fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070064 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070065 && !cx.types.structs.contains_key(ident)
Joel Galensonc03402a2020-04-23 17:31:09 -070066 && !cx.types.enums.contains_key(ident)
David Tolnay26a2a1d2020-03-25 17:26:43 -070067 && !cx.types.cxx.contains(ident)
68 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070069 {
David Tolnaydbc53772020-10-28 17:28:51 -070070 let msg = format!("unsupported type: {}", ident);
71 cx.error(ident, &msg);
David Tolnayd4e68302020-03-25 12:04:17 -070072 }
73}
74
David Tolnay26a2a1d2020-03-25 17:26:43 -070075fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070076 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -070077 if cx.types.cxx.contains(&ident.rust)
78 && !cx.types.structs.contains_key(&ident.rust)
79 && !cx.types.enums.contains_key(&ident.rust)
David Tolnayc880ae22020-08-29 16:46:00 -070080 {
David Tolnaya420f012020-03-25 17:55:56 -070081 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070082 }
David Tolnaya420f012020-03-25 17:55:56 -070083
Adrian Taylorc8713432020-10-21 18:20:55 -070084 if Atom::from(&ident.rust).is_none() {
David Tolnayd4e68302020-03-25 12:04:17 -070085 return;
86 }
87 }
David Tolnaya420f012020-03-25 17:55:56 -070088
89 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070090}
91
David Tolnayc6d891e2020-04-25 12:06:34 -070092fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
93 if let Type::Ident(ident) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -070094 if cx.types.cxx.contains(&ident.rust)
95 && !cx.types.structs.contains_key(&ident.rust)
96 && !cx.types.enums.contains_key(&ident.rust)
David Tolnayc880ae22020-08-29 16:46:00 -070097 {
David Tolnayc6d891e2020-04-25 12:06:34 -070098 cx.error(ty, "Rust Vec containing C++ type is not supported yet");
Myron Ahneba35cf2020-02-05 19:41:51 +070099 return;
David Tolnayc6d891e2020-04-25 12:06:34 -0700100 }
101
Adrian Taylorc8713432020-10-21 18:20:55 -0700102 match Atom::from(&ident.rust) {
David Tolnay93e71d02020-11-25 20:16:52 -0800103 None | Some(Char) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
104 | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
105 | Some(F64) | Some(RustString) => return,
106 Some(Bool) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -0700107 Some(CxxString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700108 }
109 }
110
David Tolnayc6d891e2020-04-25 12:06:34 -0700111 cx.error(ty, "unsupported element type of Vec");
Myron Ahneba35cf2020-02-05 19:41:51 +0700112}
113
David Tolnay26a2a1d2020-03-25 17:26:43 -0700114fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -0700115 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700116 if cx.types.rust.contains(&ident.rust) {
David Tolnaya420f012020-03-25 17:55:56 -0700117 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -0700118 }
David Tolnaya420f012020-03-25 17:55:56 -0700119
Adrian Taylorc8713432020-10-21 18:20:55 -0700120 match Atom::from(&ident.rust) {
David Tolnayd4e68302020-03-25 12:04:17 -0700121 None | Some(CxxString) => return,
122 _ => {}
123 }
David Tolnay4377a9e2020-04-24 15:20:26 -0700124 } else if let Type::CxxVector(_) = &ptr.inner {
Myron Ahneba35cf2020-02-05 19:41:51 +0700125 return;
David Tolnayd4e68302020-03-25 12:04:17 -0700126 }
David Tolnaya420f012020-03-25 17:55:56 -0700127
128 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700129}
130
David Tolnayfff4c8a2020-04-25 11:45:20 -0700131fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
Myron Ahneba35cf2020-02-05 19:41:51 +0700132 if let Type::Ident(ident) = &ptr.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700133 if cx.types.rust.contains(&ident.rust) {
David Tolnayc0faaf62020-04-25 12:18:38 -0700134 cx.error(
135 ptr,
136 "C++ vector containing a Rust type is not supported yet",
137 );
Myron Ahneba35cf2020-02-05 19:41:51 +0700138 }
139
Adrian Taylorc8713432020-10-21 18:20:55 -0700140 match Atom::from(&ident.rust) {
David Tolnay6bd63de2020-04-25 12:20:09 -0700141 None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
David Tolnay47e239d2020-08-28 00:32:04 -0700142 | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
143 | Some(CxxString) => return,
David Tolnayb3873dc2020-11-25 19:47:49 -0800144 Some(Char) => { /* todo */ }
David Tolnay6bd63de2020-04-25 12:20:09 -0700145 Some(Bool) | Some(RustString) => {}
Myron Ahneba35cf2020-02-05 19:41:51 +0700146 }
147 }
David Tolnayc0faaf62020-04-25 12:18:38 -0700148
Myron Ahneba35cf2020-02-05 19:41:51 +0700149 cx.error(ptr, "unsupported vector target type");
150}
151
David Tolnay26a2a1d2020-03-25 17:26:43 -0700152fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800153 if ty.mutable && !ty.pinned {
David Tolnaydb42cfd2020-11-15 15:31:40 -0800154 if let Some(requires_pin) = match &ty.inner {
155 Type::Ident(ident) if ident.rust == CxxString || is_opaque_cxx(cx, &ident.rust) => {
156 Some(ident.rust.to_string())
157 }
158 Type::CxxVector(_) => Some("CxxVector<...>".to_owned()),
159 _ => None,
160 } {
161 cx.error(
162 ty,
163 format!(
164 "mutable reference to C++ type requires a pin -- use Pin<&mut {}>",
165 requires_pin,
166 ),
167 );
168 }
169 }
170
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700171 match ty.inner {
172 Type::Fn(_) | Type::Void(_) => {}
David Tolnay776fd892020-04-28 13:38:21 -0700173 Type::Ref(_) => {
174 cx.error(ty, "C++ does not allow references to references");
175 return;
176 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700177 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700178 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700179
180 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700181}
182
David Tolnay69db8142020-11-25 17:49:25 -0800183fn check_type_slice_ref(cx: &mut Check, ty: &SliceRef) {
David Tolnay5515a9e2020-11-25 19:07:54 -0800184 let supported = match &ty.inner {
185 Type::Str(_) | Type::SliceRef(_) => false,
186 element => !is_unsized(cx, element),
187 };
188
189 if !supported {
190 let mutable = if ty.mutable { "mut " } else { "" };
191 let mut msg = format!("unsupported &{}[T] element type", mutable);
192 if let Type::Ident(ident) = &ty.inner {
193 if cx.types.rust.contains(&ident.rust) {
194 msg += ": opaque Rust type is not supported yet";
195 } else if is_opaque_cxx(cx, &ident.rust) {
196 msg += ": opaque C++ type is not supported yet";
197 }
David Tolnay85ef67a2020-11-25 17:51:27 -0800198 }
David Tolnay5515a9e2020-11-25 19:07:54 -0800199 cx.error(ty, msg);
David Tolnay69db8142020-11-25 17:49:25 -0800200 }
201}
202
Xiangpeng Hao78762352020-11-12 10:24:18 +0800203fn check_type_array(cx: &mut Check, ty: &Array) {
David Tolnayda2558e2020-11-25 17:15:23 -0800204 let supported = match &ty.inner {
David Tolnay73b72642020-11-25 17:44:05 -0800205 Type::Str(_) | Type::SliceRef(_) => false,
David Tolnayda2558e2020-11-25 17:15:23 -0800206 element => !is_unsized(cx, element),
207 };
208
209 if !supported {
David Tolnayc4658292020-11-24 21:24:30 -0800210 cx.error(ty, "unsupported array element type");
David Tolnaye8b1bb42020-11-24 20:37:37 -0800211 }
Xiangpeng Hao78762352020-11-12 10:24:18 +0800212}
213
David Tolnay1db70c42020-11-14 23:28:58 -0800214fn check_type_fn(cx: &mut Check, ty: &Signature) {
215 if ty.throws {
216 cx.error(ty, "function pointer returning Result is not supported yet");
217 }
218}
219
David Tolnay26a2a1d2020-03-25 17:26:43 -0700220fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay17a934c2020-11-02 00:40:04 -0800221 let name = &strct.name;
222 check_reserved_name(cx, &name.rust);
David Tolnay0b368ae2020-04-22 17:55:02 -0700223
David Tolnayd4e68302020-03-25 12:04:17 -0700224 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700225 let span = span_for_struct_error(strct);
226 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700227 }
David Tolnaya420f012020-03-25 17:55:56 -0700228
David Tolnay17a934c2020-11-02 00:40:04 -0800229 if cx.types.cxx.contains(&name.rust) {
230 if let Some(ety) = cx.types.untrusted.get(&name.rust) {
David Tolnayc8b50352020-08-29 19:27:01 -0700231 let msg = "extern shared struct must be declared in an `unsafe extern` block";
232 cx.error(ety, msg);
David Tolnayc8b50352020-08-29 19:27:01 -0700233 }
David Tolnayc880ae22020-08-29 16:46:00 -0700234 }
235
David Tolnay16e26202020-11-27 19:28:37 -0800236 for derive in &strct.derives {
237 if derive.what == Trait::ExternType {
238 let msg = format!("derive({}) on shared struct is not supported", derive);
239 cx.error(derive, msg);
240 }
241 }
242
David Tolnayd4e68302020-03-25 12:04:17 -0700243 for field in &strct.fields {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700244 if let Type::Fn(_) = field.ty {
245 cx.error(
246 field,
247 "function pointers in a struct field are not implemented yet",
248 );
David Tolnay817f09e2020-11-25 16:58:35 -0800249 } else if is_unsized(cx, &field.ty) {
250 let desc = describe(cx, &field.ty);
251 let msg = format!("using {} by value is not supported", desc);
252 cx.error(field, msg);
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700253 }
David Tolnayd4e68302020-03-25 12:04:17 -0700254 }
255}
256
Joel Galensonc03402a2020-04-23 17:31:09 -0700257fn check_api_enum(cx: &mut Check, enm: &Enum) {
David Tolnay17a934c2020-11-02 00:40:04 -0800258 check_reserved_name(cx, &enm.name.rust);
Joel Galensonc03402a2020-04-23 17:31:09 -0700259
David Tolnay58eee392020-11-20 20:37:58 -0800260 if enm.variants.is_empty() && !enm.explicit_repr {
Joel Galensonc03402a2020-04-23 17:31:09 -0700261 let span = span_for_enum_error(enm);
David Tolnayb4f4fac2020-11-22 00:26:02 -0800262 cx.error(
263 span,
264 "explicit #[repr(...)] is required for enum without any variants",
265 );
Joel Galensonc03402a2020-04-23 17:31:09 -0700266 }
David Tolnaya6a9e942020-11-27 17:22:35 -0800267
268 for derive in &enm.derives {
David Tolnay16e26202020-11-27 19:28:37 -0800269 if derive.what == Trait::Default || derive.what == Trait::ExternType {
270 let msg = format!("derive({}) on shared enum is not supported", derive);
271 cx.error(derive, msg);
David Tolnaya6a9e942020-11-27 17:22:35 -0800272 }
273 }
Joel Galensonc03402a2020-04-23 17:31:09 -0700274}
275
David Tolnay4cefa722020-10-03 21:24:07 -0700276fn check_api_type(cx: &mut Check, ety: &ExternType) {
David Tolnay17a934c2020-11-02 00:40:04 -0800277 check_reserved_name(cx, &ety.name.rust);
David Tolnay3208fd72020-10-03 20:19:40 -0700278
David Tolnay16e26202020-11-27 19:28:37 -0800279 for derive in &ety.derives {
280 if derive.what == Trait::ExternType && ety.lang == Lang::Rust {
281 continue;
282 }
283 let lang = match ety.lang {
284 Lang::Rust => "Rust",
285 Lang::Cxx => "C++",
286 };
287 let msg = format!(
288 "derive({}) on opaque {} type is not supported yet",
289 derive, lang,
290 );
291 cx.error(derive, msg);
292 }
293
David Tolnayb7eb65e2020-11-27 20:00:53 -0800294 if !ety.bounds.is_empty() {
295 let bounds = &ety.bounds;
296 let span = quote!(#(#bounds)*);
297 cx.error(span, "extern type bounds are not implemented yet");
298 }
299
David Tolnay17a934c2020-11-02 00:40:04 -0800300 if let Some(reason) = cx.types.required_trivial.get(&ety.name.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700301 let what = match reason {
David Tolnay17a934c2020-11-02 00:40:04 -0800302 TrivialReason::StructField(strct) => format!("a field of `{}`", strct.name.rust),
303 TrivialReason::FunctionArgument(efn) => format!("an argument of `{}`", efn.name.rust),
304 TrivialReason::FunctionReturn(efn) => format!("a return value of `{}`", efn.name.rust),
David Tolnay3208fd72020-10-03 20:19:40 -0700305 };
306 let msg = format!(
307 "needs a cxx::ExternType impl in order to be used as {}",
308 what,
309 );
David Tolnay4cefa722020-10-03 21:24:07 -0700310 cx.error(ety, msg);
David Tolnay3208fd72020-10-03 20:19:40 -0700311 }
David Tolnay0b368ae2020-04-22 17:55:02 -0700312}
313
David Tolnay26a2a1d2020-03-25 17:26:43 -0700314fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnay9938b642020-11-15 18:11:40 -0800315 match efn.lang {
316 Lang::Cxx => {
317 if !efn.generics.params.is_empty() && !efn.trusted {
318 let ref span = span_for_generics_error(efn);
319 cx.error(span, "extern C++ function with lifetimes must be declared in `unsafe extern \"C++\"` block");
320 }
321 }
322 Lang::Rust => {
323 if !efn.generics.params.is_empty() && efn.unsafety.is_none() {
324 let ref span = span_for_generics_error(efn);
325 let message = format!(
326 "must be `unsafe fn {}` in order to expose explicit lifetimes to C++",
327 efn.name.rust,
328 );
329 cx.error(span, message);
330 }
331 }
332 }
333
David Tolnayd763f4c2020-04-22 16:09:19 -0700334 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700335 let ref span = span_for_receiver_error(receiver);
336
Adrian Taylorc8713432020-10-21 18:20:55 -0700337 if receiver.ty.is_self() {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800338 let mutability = match receiver.mutable {
339 true => "mut ",
340 false => "",
David Tolnaya1f29c42020-04-22 18:01:38 -0700341 };
342 let msg = format!(
343 "unnamed receiver type is only allowed if the surrounding \
344 extern block contains exactly one extern type; \
345 use `self: &{mutability}TheType`",
346 mutability = mutability,
347 );
348 cx.error(span, msg);
Adrian Taylorc8713432020-10-21 18:20:55 -0700349 } else if !cx.types.structs.contains_key(&receiver.ty.rust)
350 && !cx.types.cxx.contains(&receiver.ty.rust)
351 && !cx.types.rust.contains(&receiver.ty.rust)
David Tolnay8b60bf12020-04-22 16:52:19 -0700352 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700353 cx.error(span, "unrecognized receiver type");
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800354 } else if receiver.mutable && !receiver.pinned && is_opaque_cxx(cx, &receiver.ty.rust) {
David Tolnay75aea9a2020-11-15 16:07:25 -0800355 cx.error(
356 span,
357 format!(
358 "mutable reference to C++ type requires a pin -- use `self: Pin<&mut {}>`",
359 receiver.ty.rust,
360 ),
361 );
David Tolnay8b60bf12020-04-22 16:52:19 -0700362 }
David Tolnayd763f4c2020-04-22 16:09:19 -0700363 }
364
David Tolnayd4e68302020-03-25 12:04:17 -0700365 for arg in &efn.args {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700366 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700367 if efn.lang == Lang::Rust {
368 cx.error(
369 arg,
370 "passing a function pointer from C++ to Rust is not implemented yet",
371 );
372 }
David Tolnay817f09e2020-11-25 16:58:35 -0800373 } else if is_unsized(cx, &arg.ty) {
374 let desc = describe(cx, &arg.ty);
375 let msg = format!("passing {} by value is not supported", desc);
376 cx.error(arg, msg);
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700377 }
David Tolnayd4e68302020-03-25 12:04:17 -0700378 }
David Tolnaya420f012020-03-25 17:55:56 -0700379
David Tolnayd4e68302020-03-25 12:04:17 -0700380 if let Some(ty) = &efn.ret {
David Tolnay817f09e2020-11-25 16:58:35 -0800381 if let Type::Fn(_) = ty {
382 cx.error(ty, "returning a function pointer is not implemented yet");
383 } else if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700384 let desc = describe(cx, ty);
385 let msg = format!("returning {} by value is not supported", desc);
386 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700387 }
388 }
David Tolnay96a826b2020-05-04 00:17:12 -0700389
390 if efn.lang == Lang::Cxx {
391 check_mut_return_restriction(cx, efn);
392 }
393
394 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -0400395}
396
David Tolnayb7eb65e2020-11-27 20:00:53 -0800397fn check_api_type_alias(cx: &mut Check, alias: &TypeAlias) {
398 for derive in &alias.derives {
399 let msg = format!("derive({}) on extern type alias is not supported", derive);
400 cx.error(derive, msg);
401 }
402}
403
David Tolnay7e69f892020-10-03 22:20:22 -0700404fn check_api_impl(cx: &mut Check, imp: &Impl) {
405 if let Type::UniquePtr(ty) | Type::CxxVector(ty) = &imp.ty {
406 if let Type::Ident(inner) = &ty.inner {
Adrian Taylorc8713432020-10-21 18:20:55 -0700407 if Atom::from(&inner.rust).is_none() {
David Tolnay7e69f892020-10-03 22:20:22 -0700408 return;
409 }
410 }
411 }
412
413 cx.error(imp, "unsupported Self type of explicit impl");
414}
415
David Tolnay26a2a1d2020-03-25 17:26:43 -0700416fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400417 match &efn.ret {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800418 Some(Type::Ref(ty)) if ty.mutable => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700419 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400420 }
421
422 for arg in &efn.args {
423 if let Type::Ref(ty) = &arg.ty {
David Tolnay9c4ac2e2020-11-15 21:14:03 -0800424 if ty.mutable {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700425 return;
David Tolnay7db73692019-10-20 14:51:12 -0400426 }
427 }
428 }
429
David Tolnaya420f012020-03-25 17:55:56 -0700430 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400431 efn,
432 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700433 );
David Tolnay7db73692019-10-20 14:51:12 -0400434}
435
David Tolnay26a2a1d2020-03-25 17:26:43 -0700436fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay556738d2020-11-15 13:58:44 -0800437 if efn.lang == Lang::Cxx && efn.trusted {
438 return;
439 }
440
David Tolnay7db73692019-10-20 14:51:12 -0400441 match &efn.ret {
442 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700443 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400444 }
445
446 let mut reference_args = 0;
447 for arg in &efn.args {
448 if let Type::Ref(_) = &arg.ty {
449 reference_args += 1;
450 }
451 }
452
Joel Galenson3d4f6122020-04-07 15:54:05 -0700453 if efn.receiver.is_some() {
454 reference_args += 1;
455 }
456
David Tolnay26a2a1d2020-03-25 17:26:43 -0700457 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700458 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400459 efn,
460 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700461 );
David Tolnay7db73692019-10-20 14:51:12 -0400462 }
463}
464
David Tolnay0b368ae2020-04-22 17:55:02 -0700465fn check_reserved_name(cx: &mut Check, ident: &Ident) {
David Tolnay99c93d82020-04-25 14:07:44 -0700466 if ident == "Box"
467 || ident == "UniquePtr"
468 || ident == "Vec"
469 || ident == "CxxVector"
David Tolnay4de34812020-11-19 14:48:55 -0800470 || ident == "str"
David Tolnay99c93d82020-04-25 14:07:44 -0700471 || Atom::from(ident).is_some()
472 {
David Tolnay0b368ae2020-04-22 17:55:02 -0700473 cx.error(ident, "reserved name");
474 }
475}
476
David Tolnay26a2a1d2020-03-25 17:26:43 -0700477fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnay70e5bfb2020-11-19 14:45:33 -0800478 match ty {
479 Type::Ident(ident) => {
480 let ident = &ident.rust;
481 ident == CxxString || is_opaque_cxx(cx, ident) || cx.types.rust.contains(ident)
482 }
David Tolnay8f8d78d2020-11-24 20:29:49 -0800483 Type::Array(array) => is_unsized(cx, &array.inner),
David Tolnaye0dca7b2020-11-25 17:18:57 -0800484 Type::CxxVector(_) | Type::Fn(_) | Type::Void(_) => true,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800485 Type::RustBox(_)
486 | Type::RustVec(_)
487 | Type::UniquePtr(_)
488 | Type::Ref(_)
489 | Type::Str(_)
David Tolnay73b72642020-11-25 17:44:05 -0800490 | Type::SliceRef(_) => false,
David Tolnay70e5bfb2020-11-19 14:45:33 -0800491 }
David Tolnaydb42cfd2020-11-15 15:31:40 -0800492}
493
494fn is_opaque_cxx(cx: &mut Check, ty: &Ident) -> bool {
495 cx.types.cxx.contains(ty)
496 && !cx.types.structs.contains_key(ty)
497 && !cx.types.enums.contains_key(ty)
498 && !(cx.types.aliases.contains_key(ty) && cx.types.required_trivial.contains_key(ty))
David Tolnay26a2a1d2020-03-25 17:26:43 -0700499}
500
David Tolnaya420f012020-03-25 17:55:56 -0700501fn span_for_struct_error(strct: &Struct) -> TokenStream {
502 let struct_token = strct.struct_token;
503 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
504 brace_token.set_span(strct.brace_token.span);
505 quote!(#struct_token #brace_token)
506}
507
Joel Galensonc03402a2020-04-23 17:31:09 -0700508fn span_for_enum_error(enm: &Enum) -> TokenStream {
509 let enum_token = enm.enum_token;
510 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
511 brace_token.set_span(enm.brace_token.span);
512 quote!(#enum_token #brace_token)
513}
514
David Tolnayd763f4c2020-04-22 16:09:19 -0700515fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
516 let ampersand = receiver.ampersand;
517 let lifetime = &receiver.lifetime;
518 let mutability = receiver.mutability;
519 if receiver.shorthand {
520 let var = receiver.var;
521 quote!(#ampersand #lifetime #mutability #var)
522 } else {
523 let ty = &receiver.ty;
524 quote!(#ampersand #lifetime #mutability #ty)
525 }
526}
527
David Tolnay9938b642020-11-15 18:11:40 -0800528fn span_for_generics_error(efn: &ExternFn) -> TokenStream {
529 let unsafety = efn.unsafety;
530 let fn_token = efn.fn_token;
531 let generics = &efn.generics;
532 quote!(#unsafety #fn_token #generics)
533}
534
David Tolnaya420f012020-03-25 17:55:56 -0700535fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400536 match ty {
537 Type::Ident(ident) => {
Adrian Taylorc8713432020-10-21 18:20:55 -0700538 if cx.types.structs.contains_key(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400539 "struct".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700540 } else if cx.types.enums.contains_key(&ident.rust) {
David Tolnay55483722020-08-29 18:27:37 -0700541 "enum".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700542 } else if cx.types.aliases.contains_key(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700543 "C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700544 } else if cx.types.cxx.contains(&ident.rust) {
David Tolnay3208fd72020-10-03 20:19:40 -0700545 "opaque C++ type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700546 } else if cx.types.rust.contains(&ident.rust) {
David Tolnay7db73692019-10-20 14:51:12 -0400547 "opaque Rust type".to_owned()
Adrian Taylorc8713432020-10-21 18:20:55 -0700548 } else if Atom::from(&ident.rust) == Some(CxxString) {
David Tolnay7db73692019-10-20 14:51:12 -0400549 "C++ string".to_owned()
David Tolnayb3873dc2020-11-25 19:47:49 -0800550 } else if Atom::from(&ident.rust) == Some(Char) {
551 "C char".to_owned()
David Tolnay7db73692019-10-20 14:51:12 -0400552 } else {
Adrian Taylorc8713432020-10-21 18:20:55 -0700553 ident.rust.to_string()
David Tolnay7db73692019-10-20 14:51:12 -0400554 }
555 }
556 Type::RustBox(_) => "Box".to_owned(),
Myron Ahneba35cf2020-02-05 19:41:51 +0700557 Type::RustVec(_) => "Vec".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400558 Type::UniquePtr(_) => "unique_ptr".to_owned(),
559 Type::Ref(_) => "reference".to_owned(),
560 Type::Str(_) => "&str".to_owned(),
David Tolnayfa85fce2020-04-25 15:03:25 -0700561 Type::CxxVector(_) => "C++ vector".to_owned(),
David Tolnay5515a9e2020-11-25 19:07:54 -0800562 Type::SliceRef(_) => "slice".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700563 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700564 Type::Void(_) => "()".to_owned(),
Xiangpeng Hao78762352020-11-12 10:24:18 +0800565 Type::Array(_) => "array".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400566 }
567}