blob: f6e46fd19808ea46c34d4e83d8f85c585345a914 [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 {
David Tolnay8b60bf12020-04-22 16:52:19 -0700144 if !cx.types.structs.contains_key(&receiver.ty)
145 && !cx.types.cxx.contains(&receiver.ty)
146 && !cx.types.rust.contains(&receiver.ty)
147 {
148 let span = span_for_receiver_error(receiver);
149 cx.error(span, "unrecognized receiver type");
150 }
151
David Tolnayd763f4c2020-04-22 16:09:19 -0700152 if receiver.lifetime.is_some() {
153 let span = span_for_receiver_error(receiver);
154 cx.error(span, "references with explicit lifetimes are not supported");
155 }
156 }
157
David Tolnayd4e68302020-03-25 12:04:17 -0700158 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700159 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700160 let desc = describe(cx, &arg.ty);
161 let msg = format!("passing {} by value is not supported", desc);
162 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700163 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700164 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700165 if efn.lang == Lang::Rust {
166 cx.error(
167 arg,
168 "passing a function pointer from C++ to Rust is not implemented yet",
169 );
170 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700171 }
David Tolnayd4e68302020-03-25 12:04:17 -0700172 }
David Tolnaya420f012020-03-25 17:55:56 -0700173
David Tolnayd4e68302020-03-25 12:04:17 -0700174 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700175 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700176 let desc = describe(cx, ty);
177 let msg = format!("returning {} by value is not supported", desc);
178 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700179 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700180 if let Type::Fn(_) = ty {
181 cx.error(ty, "returning a function pointer is not implemented yet");
182 }
David Tolnayd4e68302020-03-25 12:04:17 -0700183 }
David Tolnay7db73692019-10-20 14:51:12 -0400184}
185
David Tolnay26a2a1d2020-03-25 17:26:43 -0700186fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400187 match &efn.ret {
188 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700189 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400190 }
191
192 for arg in &efn.args {
193 if let Type::Ref(ty) = &arg.ty {
194 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700195 return;
David Tolnay7db73692019-10-20 14:51:12 -0400196 }
197 }
198 }
199
David Tolnaya420f012020-03-25 17:55:56 -0700200 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400201 efn,
202 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700203 );
David Tolnay7db73692019-10-20 14:51:12 -0400204}
205
David Tolnay26a2a1d2020-03-25 17:26:43 -0700206fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400207 match &efn.ret {
208 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700209 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400210 }
211
212 let mut reference_args = 0;
213 for arg in &efn.args {
214 if let Type::Ref(_) = &arg.ty {
215 reference_args += 1;
216 }
217 }
218
Joel Galenson3d4f6122020-04-07 15:54:05 -0700219 if efn.receiver.is_some() {
220 reference_args += 1;
221 }
222
David Tolnay26a2a1d2020-03-25 17:26:43 -0700223 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700224 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400225 efn,
226 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700227 );
David Tolnay7db73692019-10-20 14:51:12 -0400228 }
229}
230
David Tolnay26a2a1d2020-03-25 17:26:43 -0700231fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700232 let ident = match ty {
233 Type::Ident(ident) => ident,
David Tolnayeebe9b72020-04-14 16:32:18 -0700234 Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700235 _ => return false,
236 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700237 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
238}
239
David Tolnaya420f012020-03-25 17:55:56 -0700240fn span_for_struct_error(strct: &Struct) -> TokenStream {
241 let struct_token = strct.struct_token;
242 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
243 brace_token.set_span(strct.brace_token.span);
244 quote!(#struct_token #brace_token)
245}
246
David Tolnayd763f4c2020-04-22 16:09:19 -0700247fn span_for_receiver_error(receiver: &Receiver) -> TokenStream {
248 let ampersand = receiver.ampersand;
249 let lifetime = &receiver.lifetime;
250 let mutability = receiver.mutability;
251 if receiver.shorthand {
252 let var = receiver.var;
253 quote!(#ampersand #lifetime #mutability #var)
254 } else {
255 let ty = &receiver.ty;
256 quote!(#ampersand #lifetime #mutability #ty)
257 }
258}
259
David Tolnay26a2a1d2020-03-25 17:26:43 -0700260fn combine_errors(errors: Vec<Error>) -> Result<()> {
261 let mut iter = errors.into_iter();
262 let mut all_errors = match iter.next() {
263 Some(err) => err,
264 None => return Ok(()),
265 };
266 for err in iter {
267 all_errors.combine(err);
268 }
269 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700270}
271
David Tolnaya420f012020-03-25 17:55:56 -0700272fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400273 match ty {
274 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700275 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400276 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700277 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400278 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700279 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400280 "opaque Rust type".to_owned()
281 } else if Atom::from(ident) == Some(CxxString) {
282 "C++ string".to_owned()
283 } else {
284 ident.to_string()
285 }
286 }
287 Type::RustBox(_) => "Box".to_owned(),
288 Type::UniquePtr(_) => "unique_ptr".to_owned(),
289 Type::Ref(_) => "reference".to_owned(),
290 Type::Str(_) => "&str".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700291 Type::Slice(_) => "slice".to_owned(),
292 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700293 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700294 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400295 }
296}