blob: fa51ed379ac97b028607f50be3ecbd8c0c666c21 [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::{
3 error, ident, Api, ExternFn, Lang, Receiver, Ref, Slice, Struct, Ty1, Type, Types,
4};
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),
42 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040043 _ => {}
44 }
45 }
46
David Tolnay26a2a1d2020-03-25 17:26:43 -070047 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040048 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070049 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040050 }
51 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070052 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040053 }
54 }
55
David Tolnay26a2a1d2020-03-25 17:26:43 -070056 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040057}
58
David Tolnaya420f012020-03-25 17:55:56 -070059impl Check<'_> {
60 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
61 self.errors.push(Error::new_spanned(sp, msg));
62 }
63}
64
David Tolnay26a2a1d2020-03-25 17:26:43 -070065fn check_type_ident(cx: &mut Check, ident: &Ident) {
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)
68 && !cx.types.cxx.contains(ident)
69 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070070 {
David Tolnaya420f012020-03-25 17:55:56 -070071 cx.error(ident, "unsupported type");
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 {
David Tolnay26a2a1d2020-03-25 17:26:43 -070077 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070078 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070079 }
David Tolnaya420f012020-03-25 17:55:56 -070080
David Tolnayd4e68302020-03-25 12:04:17 -070081 if Atom::from(ident).is_none() {
82 return;
83 }
84 }
David Tolnaya420f012020-03-25 17:55:56 -070085
86 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070087}
88
David Tolnay26a2a1d2020-03-25 17:26:43 -070089fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070090 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070091 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070092 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -070093 }
David Tolnaya420f012020-03-25 17:55:56 -070094
David Tolnayd4e68302020-03-25 12:04:17 -070095 match Atom::from(ident) {
96 None | Some(CxxString) => return,
97 _ => {}
98 }
99 }
David Tolnaya420f012020-03-25 17:55:56 -0700100
101 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700102}
103
David Tolnay26a2a1d2020-03-25 17:26:43 -0700104fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700105 if ty.lifetime.is_some() {
106 cx.error(ty, "references with explicit lifetimes are not supported");
107 }
108
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700109 match ty.inner {
110 Type::Fn(_) | Type::Void(_) => {}
111 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700112 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700113
114 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700115}
116
David Tolnayeebe9b72020-04-14 16:32:18 -0700117fn check_type_slice(cx: &mut Check, ty: &Slice) {
118 cx.error(ty, "only &[u8] is supported so far, not other slice types");
119}
120
David Tolnay26a2a1d2020-03-25 17:26:43 -0700121fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnayd4e68302020-03-25 12:04:17 -0700122 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700123 let span = span_for_struct_error(strct);
124 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700125 }
David Tolnaya420f012020-03-25 17:55:56 -0700126
David Tolnayd4e68302020-03-25 12:04:17 -0700127 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700128 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700129 let desc = describe(cx, &field.ty);
130 let msg = format!("using {} by value is not supported", desc);
131 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700132 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700133 if let Type::Fn(_) = field.ty {
134 cx.error(
135 field,
136 "function pointers in a struct field are not implemented yet",
137 );
138 }
David Tolnayd4e68302020-03-25 12:04:17 -0700139 }
140}
141
David Tolnay26a2a1d2020-03-25 17:26:43 -0700142fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd763f4c2020-04-22 16:09:19 -0700143 if let Some(receiver) = &efn.receiver {
144 if receiver.lifetime.is_some() {
145 let span = span_for_receiver_error(receiver);
146 cx.error(span, "references with explicit lifetimes are not supported");
147 }
148 }
149
David Tolnayd4e68302020-03-25 12:04:17 -0700150 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700151 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700152 let desc = describe(cx, &arg.ty);
153 let msg = format!("passing {} by value is not supported", desc);
154 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700155 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700156 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700157 if efn.lang == Lang::Rust {
158 cx.error(
159 arg,
160 "passing a function pointer from C++ to Rust is not implemented yet",
161 );
162 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700163 }
David Tolnayd4e68302020-03-25 12:04:17 -0700164 }
David Tolnaya420f012020-03-25 17:55:56 -0700165
David Tolnayd4e68302020-03-25 12:04:17 -0700166 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700167 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700168 let desc = describe(cx, ty);
169 let msg = format!("returning {} by value is not supported", desc);
170 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700171 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700172 if let Type::Fn(_) = ty {
173 cx.error(ty, "returning a function pointer is not implemented yet");
174 }
David Tolnayd4e68302020-03-25 12:04:17 -0700175 }
David Tolnay7db73692019-10-20 14:51:12 -0400176}
177
David Tolnay26a2a1d2020-03-25 17:26:43 -0700178fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400179 match &efn.ret {
180 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700181 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400182 }
183
184 for arg in &efn.args {
185 if let Type::Ref(ty) = &arg.ty {
186 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700187 return;
David Tolnay7db73692019-10-20 14:51:12 -0400188 }
189 }
190 }
191
David Tolnaya420f012020-03-25 17:55:56 -0700192 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400193 efn,
194 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700195 );
David Tolnay7db73692019-10-20 14:51:12 -0400196}
197
David Tolnay26a2a1d2020-03-25 17:26:43 -0700198fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400199 match &efn.ret {
200 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700201 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400202 }
203
204 let mut reference_args = 0;
205 for arg in &efn.args {
206 if let Type::Ref(_) = &arg.ty {
207 reference_args += 1;
208 }
209 }
210
Joel Galenson3d4f6122020-04-07 15:54:05 -0700211 if efn.receiver.is_some() {
212 reference_args += 1;
213 }
214
David Tolnay26a2a1d2020-03-25 17:26:43 -0700215 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700216 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400217 efn,
218 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700219 );
David Tolnay7db73692019-10-20 14:51:12 -0400220 }
221}
222
David Tolnay26a2a1d2020-03-25 17:26:43 -0700223fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700224 let ident = match ty {
225 Type::Ident(ident) => ident,
David Tolnayeebe9b72020-04-14 16:32:18 -0700226 Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700227 _ => return false,
228 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700229 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
230}
231
David Tolnaya420f012020-03-25 17:55:56 -0700232fn span_for_struct_error(strct: &Struct) -> TokenStream {
233 let struct_token = strct.struct_token;
234 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
235 brace_token.set_span(strct.brace_token.span);
236 quote!(#struct_token #brace_token)
237}
238
David Tolnayd763f4c2020-04-22 16:09:19 -0700239fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
240 let ampersand = receiver.ampersand;
241 let lifetime = &receiver.lifetime;
242 let mutability = receiver.mutability;
243 if receiver.shorthand {
244 let var = receiver.var;
245 quote!(#ampersand #lifetime #mutability #var)
246 } else {
247 let ty = &receiver.ty;
248 quote!(#ampersand #lifetime #mutability #ty)
249 }
250}
251
David Tolnay26a2a1d2020-03-25 17:26:43 -0700252fn combine_errors(errors: Vec<Error>) -> Result<()> {
253 let mut iter = errors.into_iter();
254 let mut all_errors = match iter.next() {
255 Some(err) => err,
256 None => return Ok(()),
257 };
258 for err in iter {
259 all_errors.combine(err);
260 }
261 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700262}
263
David Tolnaya420f012020-03-25 17:55:56 -0700264fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400265 match ty {
266 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700267 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400268 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700269 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400270 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700271 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400272 "opaque Rust type".to_owned()
273 } else if Atom::from(ident) == Some(CxxString) {
274 "C++ string".to_owned()
275 } else {
276 ident.to_string()
277 }
278 }
279 Type::RustBox(_) => "Box".to_owned(),
280 Type::UniquePtr(_) => "unique_ptr".to_owned(),
281 Type::Ref(_) => "reference".to_owned(),
282 Type::Str(_) => "&str".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700283 Type::Slice(_) => "slice".to_owned(),
284 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700285 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700286 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400287 }
288}