blob: c71295e85293e0d88773d50275ce88b5b5e88630 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnay75dca2e2020-03-25 20:17:52 -07002use crate::syntax::{error, ident, Api, ExternFn, Lang, Ref, Struct, Ty1, Type, Types};
David Tolnay09462ac2020-03-20 14:58:41 -07003use proc_macro2::{Delimiter, Group, Ident, TokenStream};
David Tolnaya420f012020-03-25 17:55:56 -07004use quote::{quote, ToTokens};
5use std::fmt::Display;
David Tolnay7db73692019-10-20 14:51:12 -04006use syn::{Error, Result};
7
David Tolnay26a2a1d2020-03-25 17:26:43 -07008struct Check<'a> {
9 apis: &'a [Api],
10 types: &'a Types<'a>,
11 errors: &'a mut Vec<Error>,
12}
13
David Tolnay7db73692019-10-20 14:51:12 -040014pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
David Tolnay26a2a1d2020-03-25 17:26:43 -070015 let mut errors = Vec::new();
16 let mut cx = Check {
17 apis,
18 types,
19 errors: &mut errors,
20 };
21 do_typecheck(&mut cx);
22 combine_errors(errors)
23}
David Tolnay7db73692019-10-20 14:51:12 -040024
David Tolnay26a2a1d2020-03-25 17:26:43 -070025fn do_typecheck(cx: &mut Check) {
26 for ty in cx.types {
David Tolnay7db73692019-10-20 14:51:12 -040027 match ty {
David Tolnay26a2a1d2020-03-25 17:26:43 -070028 Type::Ident(ident) => check_type_ident(cx, ident),
29 Type::RustBox(ptr) => check_type_box(cx, ptr),
30 Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
31 Type::Ref(ty) => check_type_ref(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040032 _ => {}
33 }
34 }
35
David Tolnay26a2a1d2020-03-25 17:26:43 -070036 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040037 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070038 Api::Struct(strct) => check_api_struct(cx, strct),
39 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040040 _ => {}
41 }
42 }
43
David Tolnay26a2a1d2020-03-25 17:26:43 -070044 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040045 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070046 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040047 }
48 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070049 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040050 }
51 }
52
David Tolnay26a2a1d2020-03-25 17:26:43 -070053 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040054}
55
David Tolnaya420f012020-03-25 17:55:56 -070056impl Check<'_> {
57 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
58 self.errors.push(Error::new_spanned(sp, msg));
59 }
60}
61
David Tolnay26a2a1d2020-03-25 17:26:43 -070062fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070063 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070064 && !cx.types.structs.contains_key(ident)
65 && !cx.types.cxx.contains(ident)
66 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070067 {
David Tolnaya420f012020-03-25 17:55:56 -070068 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070069 }
70}
71
David Tolnay26a2a1d2020-03-25 17:26:43 -070072fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070073 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070074 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070075 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070076 }
David Tolnaya420f012020-03-25 17:55:56 -070077
David Tolnayd4e68302020-03-25 12:04:17 -070078 if Atom::from(ident).is_none() {
79 return;
80 }
81 }
David Tolnaya420f012020-03-25 17:55:56 -070082
83 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070084}
85
David Tolnay26a2a1d2020-03-25 17:26:43 -070086fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070087 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070088 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070089 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -070090 }
David Tolnaya420f012020-03-25 17:55:56 -070091
David Tolnayd4e68302020-03-25 12:04:17 -070092 match Atom::from(ident) {
93 None | Some(CxxString) => return,
94 _ => {}
95 }
96 }
David Tolnaya420f012020-03-25 17:55:56 -070097
98 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -070099}
100
David Tolnay26a2a1d2020-03-25 17:26:43 -0700101fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700102 match ty.inner {
103 Type::Fn(_) | Type::Void(_) => {}
104 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700105 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700106
107 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700108}
109
David Tolnay26a2a1d2020-03-25 17:26:43 -0700110fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnayd4e68302020-03-25 12:04:17 -0700111 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700112 let span = span_for_struct_error(strct);
113 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700114 }
David Tolnaya420f012020-03-25 17:55:56 -0700115
David Tolnayd4e68302020-03-25 12:04:17 -0700116 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700117 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700118 let desc = describe(cx, &field.ty);
119 let msg = format!("using {} by value is not supported", desc);
120 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700121 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700122 if let Type::Fn(_) = field.ty {
123 cx.error(
124 field,
125 "function pointers in a struct field are not implemented yet",
126 );
127 }
David Tolnayd4e68302020-03-25 12:04:17 -0700128 }
129}
130
David Tolnay26a2a1d2020-03-25 17:26:43 -0700131fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd4e68302020-03-25 12:04:17 -0700132 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700133 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700134 let desc = describe(cx, &arg.ty);
135 let msg = format!("passing {} by value is not supported", desc);
136 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700137 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700138 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700139 if efn.lang == Lang::Rust {
140 cx.error(
141 arg,
142 "passing a function pointer from C++ to Rust is not implemented yet",
143 );
144 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700145 }
David Tolnayd4e68302020-03-25 12:04:17 -0700146 }
David Tolnaya420f012020-03-25 17:55:56 -0700147
David Tolnayd4e68302020-03-25 12:04:17 -0700148 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700149 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700150 let desc = describe(cx, ty);
151 let msg = format!("returning {} by value is not supported", desc);
152 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700153 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700154 if let Type::Fn(_) = ty {
155 cx.error(ty, "returning a function pointer is not implemented yet");
156 }
David Tolnayd4e68302020-03-25 12:04:17 -0700157 }
David Tolnay7db73692019-10-20 14:51:12 -0400158}
159
David Tolnay26a2a1d2020-03-25 17:26:43 -0700160fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400161 match &efn.ret {
162 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700163 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400164 }
165
166 for arg in &efn.args {
167 if let Type::Ref(ty) = &arg.ty {
168 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700169 return;
David Tolnay7db73692019-10-20 14:51:12 -0400170 }
171 }
172 }
173
David Tolnaya420f012020-03-25 17:55:56 -0700174 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400175 efn,
176 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700177 );
David Tolnay7db73692019-10-20 14:51:12 -0400178}
179
David Tolnay26a2a1d2020-03-25 17:26:43 -0700180fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400181 match &efn.ret {
182 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700183 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400184 }
185
186 let mut reference_args = 0;
187 for arg in &efn.args {
188 if let Type::Ref(_) = &arg.ty {
189 reference_args += 1;
190 }
191 }
192
David Tolnay26a2a1d2020-03-25 17:26:43 -0700193 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700194 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400195 efn,
196 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700197 );
David Tolnay7db73692019-10-20 14:51:12 -0400198 }
199}
200
David Tolnay26a2a1d2020-03-25 17:26:43 -0700201fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700202 let ident = match ty {
203 Type::Ident(ident) => ident,
204 Type::Void(_) => return true,
205 _ => return false,
206 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700207 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
208}
209
David Tolnaya420f012020-03-25 17:55:56 -0700210fn span_for_struct_error(strct: &Struct) -> TokenStream {
211 let struct_token = strct.struct_token;
212 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
213 brace_token.set_span(strct.brace_token.span);
214 quote!(#struct_token #brace_token)
215}
216
David Tolnay26a2a1d2020-03-25 17:26:43 -0700217fn combine_errors(errors: Vec<Error>) -> Result<()> {
218 let mut iter = errors.into_iter();
219 let mut all_errors = match iter.next() {
220 Some(err) => err,
221 None => return Ok(()),
222 };
223 for err in iter {
224 all_errors.combine(err);
225 }
226 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700227}
228
David Tolnaya420f012020-03-25 17:55:56 -0700229fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400230 match ty {
231 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700232 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400233 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700234 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400235 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700236 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400237 "opaque Rust type".to_owned()
238 } else if Atom::from(ident) == Some(CxxString) {
239 "C++ string".to_owned()
240 } else {
241 ident.to_string()
242 }
243 }
244 Type::RustBox(_) => "Box".to_owned(),
245 Type::UniquePtr(_) => "unique_ptr".to_owned(),
246 Type::Ref(_) => "reference".to_owned(),
247 Type::Str(_) => "&str".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700248 Type::Slice(_) => "slice".to_owned(),
249 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700250 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700251 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400252 }
253}