blob: 5e8cc096a3c3314ad6f484f881c3f33027a63f02 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnayd763f4c2020-04-22 16:09:19 -07002use crate::syntax::{
David Tolnay0b368ae2020-04-22 17:55:02 -07003 error, ident, Api, ExternFn, ExternType, Lang, Receiver, Ref, Slice, Struct, Ty1, Type, Types,
David Tolnayd763f4c2020-04-22 16:09:19 -07004};
David Tolnay09462ac2020-03-20 14:58:41 -07005use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07006use quote::{quote, ToTokens};
7use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -04008use syn::{Error, Result};
9
David Tolnay26a2a1d2020-03-25 17:26:43 -070010struct Check<'a> {
11 apis: &'a [Api],
12 types: &'a Types<'a>,
13 errors: &'a mut Vec<Error>,
14}
15
David Tolnay7db73692019-10-20 14:51:12 -040016pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070017 let mut errors = Vec::new();
18 let mut cx = Check {
19 apis,
20 types,
21 errors: &mut errors,
22 };
23 do_typecheck(&mut cx);
24 combine_errors(errors)
25}
David Tolnay7db73692019-10-20 14:51:12 -040026
David Tolnay26a2a1d2020-03-25 17:26:43 -070027fn do_typecheck(cx: &mut Check) {
28 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040029 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070030 Type::Ident(ident) => check_type_ident(cx, ident),
31 Type::RustBox(ptr) => check_type_box(cx, ptr),
32 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
33 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnayeebe9b72020-04-14 16:32:18 -070034 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040035 _ => {}
36 }
37 }
38
David Tolnay26a2a1d2020-03-25 17:26:43 -070039 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040040 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070041 Api::Struct(strct) => check_api_struct(cx, strct),
David Tolnay0b368ae2020-04-22 17:55:02 -070042 Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty),
David Tolnay26a2a1d2020-03-25 17:26:43 -070043 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040044 _ => {}
45 }
46 }
47
David Tolnay26a2a1d2020-03-25 17:26:43 -070048 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040049 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070050 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070053 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040054 }
55 }
56
David Tolnay26a2a1d2020-03-25 17:26:43 -070057 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040058}
59
David Tolnaya420f012020-03-25 17:55:56 -070060impl Check<'_> {
61 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
62 self.errors.push(Error::new_spanned(sp, msg));
63 }
64}
65
David Tolnay26a2a1d2020-03-25 17:26:43 -070066fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070067 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070068 && !cx.types.structs.contains_key(ident)
69 && !cx.types.cxx.contains(ident)
70 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070071 {
David Tolnaya420f012020-03-25 17:55:56 -070072 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070073 }
74}
75
David Tolnay26a2a1d2020-03-25 17:26:43 -070076fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070077 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070078 if cx.types.cxx.contains(ident) {
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
David Tolnayd4e68302020-03-25 12:04:17 -070082 if Atom::from(ident).is_none() {
83 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 Tolnay26a2a1d2020-03-25 17:26:43 -070090fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070091 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070092 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070093 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -070094 }
David Tolnaya420f012020-03-25 17:55:56 -070095
David Tolnayd4e68302020-03-25 12:04:17 -070096 match Atom::from(ident) {
97 None | Some(CxxString) => return,
98 _ => {}
99 }
100 }
David Tolnaya420f012020-03-25 17:55:56 -0700101
102 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700103}
104
David Tolnay26a2a1d2020-03-25 17:26:43 -0700105fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700106 if ty.lifetime.is_some() {
107 cx.error(ty, "references with explicit lifetimes are not supported");
108 }
109
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700110 match ty.inner {
111 Type::Fn(_) | Type::Void(_) => {}
112 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700113 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700114
115 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700116}
117
David Tolnayeebe9b72020-04-14 16:32:18 -0700118fn check_type_slice(cx: &mut Check, ty: &Slice) {
119 cx.error(ty, "only &[u8] is supported so far, not other slice types");
120}
121
David Tolnay26a2a1d2020-03-25 17:26:43 -0700122fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnay0b368ae2020-04-22 17:55:02 -0700123 check_reserved_name(cx, &strct.ident);
124
David Tolnayd4e68302020-03-25 12:04:17 -0700125 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700126 let span = span_for_struct_error(strct);
127 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700128 }
David Tolnaya420f012020-03-25 17:55:56 -0700129
David Tolnayd4e68302020-03-25 12:04:17 -0700130 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700131 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700132 let desc = describe(cx, &field.ty);
133 let msg = format!("using {} by value is not supported", desc);
134 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700135 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700136 if let Type::Fn(_) = field.ty {
137 cx.error(
138 field,
139 "function pointers in a struct field are not implemented yet",
140 );
141 }
David Tolnayd4e68302020-03-25 12:04:17 -0700142 }
143}
144
David Tolnay0b368ae2020-04-22 17:55:02 -0700145fn check_api_type(cx: &mut Check, ty: &ExternType) {
146 check_reserved_name(cx, &ty.ident);
147}
148
David Tolnay26a2a1d2020-03-25 17:26:43 -0700149fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700150 if let Some(receiver) = &efn.receiver {
David Tolnaya1f29c42020-04-22 18:01:38 -0700151 let ref span = span_for_receiver_error(receiver);
152
153 if receiver.ty == "Self" {
154 let mutability = match receiver.mutability {
155 Some(_) => "mut ",
156 None => "",
157 };
158 let msg = format!(
159 "unnamed receiver type is only allowed if the surrounding \
160 extern block contains exactly one extern type; \
161 use `self: &{mutability}TheType`",
162 mutability = mutability,
163 );
164 cx.error(span, msg);
165 } else if !cx.types.structs.contains_key(&receiver.ty)
David Tolnay8b60bf12020-04-22 16:52:19 -0700166 && !cx.types.cxx.contains(&receiver.ty)
167 && !cx.types.rust.contains(&receiver.ty)
168 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700169 cx.error(span, "unrecognized receiver type");
170 }
171
David Tolnayd763f4c2020-04-22 16:09:19 -0700172 if receiver.lifetime.is_some() {
David Tolnayd763f4c2020-04-22 16:09:19 -0700173 cx.error(span, "references with explicit lifetimes are not supported");
174 }
175 }
176
David Tolnayd4e68302020-03-25 12:04:17 -0700177 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700178 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700179 let desc = describe(cx, &arg.ty);
180 let msg = format!("passing {} by value is not supported", desc);
181 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700182 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700183 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700184 if efn.lang == Lang::Rust {
185 cx.error(
186 arg,
187 "passing a function pointer from C++ to Rust is not implemented yet",
188 );
189 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700190 }
David Tolnayd4e68302020-03-25 12:04:17 -0700191 }
David Tolnaya420f012020-03-25 17:55:56 -0700192
David Tolnayd4e68302020-03-25 12:04:17 -0700193 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700194 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700195 let desc = describe(cx, ty);
196 let msg = format!("returning {} by value is not supported", desc);
197 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700198 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700199 if let Type::Fn(_) = ty {
200 cx.error(ty, "returning a function pointer is not implemented yet");
201 }
David Tolnayd4e68302020-03-25 12:04:17 -0700202 }
David Tolnay7db73692019-10-20 14:51:12 -0400203}
204
David Tolnay26a2a1d2020-03-25 17:26:43 -0700205fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400206 match &efn.ret {
207 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700208 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400209 }
210
211 for arg in &efn.args {
212 if let Type::Ref(ty) = &arg.ty {
213 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700214 return;
David Tolnay7db73692019-10-20 14:51:12 -0400215 }
216 }
217 }
218
David Tolnaya420f012020-03-25 17:55:56 -0700219 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400220 efn,
221 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700222 );
David Tolnay7db73692019-10-20 14:51:12 -0400223}
224
David Tolnay26a2a1d2020-03-25 17:26:43 -0700225fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400226 match &efn.ret {
227 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700228 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400229 }
230
231 let mut reference_args = 0;
232 for arg in &efn.args {
233 if let Type::Ref(_) = &arg.ty {
234 reference_args += 1;
235 }
236 }
237
Joel Galenson3d4f6122020-04-07 15:54:05 -0700238 if efn.receiver.is_some() {
239 reference_args += 1;
240 }
241
David Tolnay26a2a1d2020-03-25 17:26:43 -0700242 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700243 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400244 efn,
245 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700246 );
David Tolnay7db73692019-10-20 14:51:12 -0400247 }
248}
249
David Tolnay0b368ae2020-04-22 17:55:02 -0700250fn check_reserved_name(cx: &mut Check, ident: &Ident) {
251 if ident == "Box" || ident == "UniquePtr" || Atom::from(ident).is_some() {
252 cx.error(ident, "reserved name");
253 }
254}
255
David Tolnay26a2a1d2020-03-25 17:26:43 -0700256fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700257 let ident = match ty {
258 Type::Ident(ident) => ident,
David Tolnayeebe9b72020-04-14 16:32:18 -0700259 Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700260 _ => return false,
261 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700262 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
263}
264
David Tolnaya420f012020-03-25 17:55:56 -0700265fn span_for_struct_error(strct: &Struct) -> TokenStream {
266 let struct_token = strct.struct_token;
267 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
268 brace_token.set_span(strct.brace_token.span);
269 quote!(#struct_token #brace_token)
270}
271
David Tolnayd763f4c2020-04-22 16:09:19 -0700272fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
273 let ampersand = receiver.ampersand;
274 let lifetime = &receiver.lifetime;
275 let mutability = receiver.mutability;
276 if receiver.shorthand {
277 let var = receiver.var;
278 quote!(#ampersand #lifetime #mutability #var)
279 } else {
280 let ty = &receiver.ty;
281 quote!(#ampersand #lifetime #mutability #ty)
282 }
283}
284
David Tolnay26a2a1d2020-03-25 17:26:43 -0700285fn combine_errors(errors: Vec<Error>) -> Result<()> {
286 let mut iter = errors.into_iter();
287 let mut all_errors = match iter.next() {
288 Some(err) => err,
289 None => return Ok(()),
290 };
291 for err in iter {
292 all_errors.combine(err);
293 }
294 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700295}
296
David Tolnaya420f012020-03-25 17:55:56 -0700297fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400298 match ty {
299 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700300 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400301 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700302 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400303 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700304 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400305 "opaque Rust type".to_owned()
306 } else if Atom::from(ident) == Some(CxxString) {
307 "C++ string".to_owned()
308 } else {
309 ident.to_string()
310 }
311 }
312 Type::RustBox(_) => "Box".to_owned(),
313 Type::UniquePtr(_) => "unique_ptr".to_owned(),
314 Type::Ref(_) => "reference".to_owned(),
315 Type::Str(_) => "&str".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700316 Type::Slice(_) => "slice".to_owned(),
317 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700318 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700319 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400320 }
321}