blob: 0d25e774adfed71a909dcc5b599e3ec15d79d9ef [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 Tolnay8b60bf12020-04-22 16:52:19 -0700151 if !cx.types.structs.contains_key(&receiver.ty)
152 && !cx.types.cxx.contains(&receiver.ty)
153 && !cx.types.rust.contains(&receiver.ty)
154 {
155 let span = span_for_receiver_error(receiver);
156 cx.error(span, "unrecognized receiver type");
157 }
158
David Tolnayd763f4c2020-04-22 16:09:19 -0700159 if receiver.lifetime.is_some() {
160 let span = span_for_receiver_error(receiver);
161 cx.error(span, "references with explicit lifetimes are not supported");
162 }
163 }
164
David Tolnayd4e68302020-03-25 12:04:17 -0700165 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700166 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700167 let desc = describe(cx, &arg.ty);
168 let msg = format!("passing {} by value is not supported", desc);
169 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700170 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700171 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700172 if efn.lang == Lang::Rust {
173 cx.error(
174 arg,
175 "passing a function pointer from C++ to Rust is not implemented yet",
176 );
177 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700178 }
David Tolnayd4e68302020-03-25 12:04:17 -0700179 }
David Tolnaya420f012020-03-25 17:55:56 -0700180
David Tolnayd4e68302020-03-25 12:04:17 -0700181 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700182 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700183 let desc = describe(cx, ty);
184 let msg = format!("returning {} by value is not supported", desc);
185 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700186 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700187 if let Type::Fn(_) = ty {
188 cx.error(ty, "returning a function pointer is not implemented yet");
189 }
David Tolnayd4e68302020-03-25 12:04:17 -0700190 }
David Tolnay7db73692019-10-20 14:51:12 -0400191}
192
David Tolnay26a2a1d2020-03-25 17:26:43 -0700193fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400194 match &efn.ret {
195 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700196 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400197 }
198
199 for arg in &efn.args {
200 if let Type::Ref(ty) = &arg.ty {
201 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700202 return;
David Tolnay7db73692019-10-20 14:51:12 -0400203 }
204 }
205 }
206
David Tolnaya420f012020-03-25 17:55:56 -0700207 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400208 efn,
209 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700210 );
David Tolnay7db73692019-10-20 14:51:12 -0400211}
212
David Tolnay26a2a1d2020-03-25 17:26:43 -0700213fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400214 match &efn.ret {
215 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700216 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400217 }
218
219 let mut reference_args = 0;
220 for arg in &efn.args {
221 if let Type::Ref(_) = &arg.ty {
222 reference_args += 1;
223 }
224 }
225
Joel Galenson3d4f6122020-04-07 15:54:05 -0700226 if efn.receiver.is_some() {
227 reference_args += 1;
228 }
229
David Tolnay26a2a1d2020-03-25 17:26:43 -0700230 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700231 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400232 efn,
233 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700234 );
David Tolnay7db73692019-10-20 14:51:12 -0400235 }
236}
237
David Tolnay0b368ae2020-04-22 17:55:02 -0700238fn check_reserved_name(cx: &mut Check, ident: &Ident) {
239 if ident == "Box" || ident == "UniquePtr" || Atom::from(ident).is_some() {
240 cx.error(ident, "reserved name");
241 }
242}
243
David Tolnay26a2a1d2020-03-25 17:26:43 -0700244fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700245 let ident = match ty {
246 Type::Ident(ident) => ident,
David Tolnayeebe9b72020-04-14 16:32:18 -0700247 Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700248 _ => return false,
249 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700250 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
251}
252
David Tolnaya420f012020-03-25 17:55:56 -0700253fn span_for_struct_error(strct: &Struct) -> TokenStream {
254 let struct_token = strct.struct_token;
255 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
256 brace_token.set_span(strct.brace_token.span);
257 quote!(#struct_token #brace_token)
258}
259
David Tolnayd763f4c2020-04-22 16:09:19 -0700260fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
261 let ampersand = receiver.ampersand;
262 let lifetime = &receiver.lifetime;
263 let mutability = receiver.mutability;
264 if receiver.shorthand {
265 let var = receiver.var;
266 quote!(#ampersand #lifetime #mutability #var)
267 } else {
268 let ty = &receiver.ty;
269 quote!(#ampersand #lifetime #mutability #ty)
270 }
271}
272
David Tolnay26a2a1d2020-03-25 17:26:43 -0700273fn combine_errors(errors: Vec<Error>) -> Result<()> {
274 let mut iter = errors.into_iter();
275 let mut all_errors = match iter.next() {
276 Some(err) => err,
277 None => return Ok(()),
278 };
279 for err in iter {
280 all_errors.combine(err);
281 }
282 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700283}
284
David Tolnaya420f012020-03-25 17:55:56 -0700285fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400286 match ty {
287 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700288 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400289 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700290 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400291 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700292 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400293 "opaque Rust type".to_owned()
294 } else if Atom::from(ident) == Some(CxxString) {
295 "C++ string".to_owned()
296 } else {
297 ident.to_string()
298 }
299 }
300 Type::RustBox(_) => "Box".to_owned(),
301 Type::UniquePtr(_) => "unique_ptr".to_owned(),
302 Type::Ref(_) => "reference".to_owned(),
303 Type::Str(_) => "&str".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700304 Type::Slice(_) => "slice".to_owned(),
305 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700306 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700307 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400308 }
309}