blob: 3b6937aaaf9c9a4e591b5146802458cc6da691b2 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnayeebe9b72020-04-14 16:32:18 -07002use crate::syntax::{error, ident, Api, ExternFn, Lang, Ref, Slice, 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 Tolnayeebe9b72020-04-14 16:32:18 -070032 Type::Slice(ty) => check_type_slice(cx, ty),
David Tolnay7db73692019-10-20 14:51:12 -040033 _ => {}
34 }
35 }
36
David Tolnay26a2a1d2020-03-25 17:26:43 -070037 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040038 match api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070039 Api::Struct(strct) => check_api_struct(cx, strct),
40 Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn),
David Tolnay7db73692019-10-20 14:51:12 -040041 _ => {}
42 }
43 }
44
David Tolnay26a2a1d2020-03-25 17:26:43 -070045 for api in cx.apis {
David Tolnay7db73692019-10-20 14:51:12 -040046 if let Api::CxxFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070047 check_mut_return_restriction(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040048 }
49 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
David Tolnay26a2a1d2020-03-25 17:26:43 -070050 check_multiple_arg_lifetimes(cx, efn);
David Tolnay7db73692019-10-20 14:51:12 -040051 }
52 }
53
David Tolnay26a2a1d2020-03-25 17:26:43 -070054 ident::check_all(cx.apis, cx.errors);
David Tolnay7db73692019-10-20 14:51:12 -040055}
56
David Tolnaya420f012020-03-25 17:55:56 -070057impl Check<'_> {
58 fn error(&mut self, sp: impl ToTokens, msg: impl Display) {
59 self.errors.push(Error::new_spanned(sp, msg));
60 }
61}
62
David Tolnay26a2a1d2020-03-25 17:26:43 -070063fn check_type_ident(cx: &mut Check, ident: &Ident) {
David Tolnayd4e68302020-03-25 12:04:17 -070064 if Atom::from(ident).is_none()
David Tolnay26a2a1d2020-03-25 17:26:43 -070065 && !cx.types.structs.contains_key(ident)
66 && !cx.types.cxx.contains(ident)
67 && !cx.types.rust.contains(ident)
David Tolnayd4e68302020-03-25 12:04:17 -070068 {
David Tolnaya420f012020-03-25 17:55:56 -070069 cx.error(ident, "unsupported type");
David Tolnayd4e68302020-03-25 12:04:17 -070070 }
71}
72
David Tolnay26a2a1d2020-03-25 17:26:43 -070073fn check_type_box(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070074 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070075 if cx.types.cxx.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070076 cx.error(ptr, error::BOX_CXX_TYPE.msg);
David Tolnayd4e68302020-03-25 12:04:17 -070077 }
David Tolnaya420f012020-03-25 17:55:56 -070078
David Tolnayd4e68302020-03-25 12:04:17 -070079 if Atom::from(ident).is_none() {
80 return;
81 }
82 }
David Tolnaya420f012020-03-25 17:55:56 -070083
84 cx.error(ptr, "unsupported target type of Box");
David Tolnayd4e68302020-03-25 12:04:17 -070085}
86
David Tolnay26a2a1d2020-03-25 17:26:43 -070087fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
David Tolnayd4e68302020-03-25 12:04:17 -070088 if let Type::Ident(ident) = &ptr.inner {
David Tolnay26a2a1d2020-03-25 17:26:43 -070089 if cx.types.rust.contains(ident) {
David Tolnaya420f012020-03-25 17:55:56 -070090 cx.error(ptr, "unique_ptr of a Rust type is not supported yet");
David Tolnayd4e68302020-03-25 12:04:17 -070091 }
David Tolnaya420f012020-03-25 17:55:56 -070092
David Tolnayd4e68302020-03-25 12:04:17 -070093 match Atom::from(ident) {
94 None | Some(CxxString) => return,
95 _ => {}
96 }
97 }
David Tolnaya420f012020-03-25 17:55:56 -070098
99 cx.error(ptr, "unsupported unique_ptr target type");
David Tolnayd4e68302020-03-25 12:04:17 -0700100}
101
David Tolnay26a2a1d2020-03-25 17:26:43 -0700102fn check_type_ref(cx: &mut Check, ty: &Ref) {
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700103 match ty.inner {
104 Type::Fn(_) | Type::Void(_) => {}
105 _ => return,
David Tolnayd4e68302020-03-25 12:04:17 -0700106 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700107
108 cx.error(ty, "unsupported reference type");
David Tolnayd4e68302020-03-25 12:04:17 -0700109}
110
David Tolnayeebe9b72020-04-14 16:32:18 -0700111fn check_type_slice(cx: &mut Check, ty: &Slice) {
112 cx.error(ty, "only &[u8] is supported so far, not other slice types");
113}
114
David Tolnay26a2a1d2020-03-25 17:26:43 -0700115fn check_api_struct(cx: &mut Check, strct: &Struct) {
David Tolnayd4e68302020-03-25 12:04:17 -0700116 if strct.fields.is_empty() {
David Tolnaya420f012020-03-25 17:55:56 -0700117 let span = span_for_struct_error(strct);
118 cx.error(span, "structs without any fields are not supported");
David Tolnayd4e68302020-03-25 12:04:17 -0700119 }
David Tolnaya420f012020-03-25 17:55:56 -0700120
David Tolnayd4e68302020-03-25 12:04:17 -0700121 for field in &strct.fields {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700122 if is_unsized(cx, &field.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700123 let desc = describe(cx, &field.ty);
124 let msg = format!("using {} by value is not supported", desc);
125 cx.error(field, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700126 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700127 if let Type::Fn(_) = field.ty {
128 cx.error(
129 field,
130 "function pointers in a struct field are not implemented yet",
131 );
132 }
David Tolnayd4e68302020-03-25 12:04:17 -0700133 }
134}
135
David Tolnay26a2a1d2020-03-25 17:26:43 -0700136fn check_api_fn(cx: &mut Check, efn: &ExternFn) {
David Tolnayd4e68302020-03-25 12:04:17 -0700137 for arg in &efn.args {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700138 if is_unsized(cx, &arg.ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700139 let desc = describe(cx, &arg.ty);
140 let msg = format!("passing {} by value is not supported", desc);
141 cx.error(arg, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700142 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700143 if let Type::Fn(_) = arg.ty {
David Tolnay75dca2e2020-03-25 20:17:52 -0700144 if efn.lang == Lang::Rust {
145 cx.error(
146 arg,
147 "passing a function pointer from C++ to Rust is not implemented yet",
148 );
149 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700150 }
David Tolnayd4e68302020-03-25 12:04:17 -0700151 }
David Tolnaya420f012020-03-25 17:55:56 -0700152
David Tolnayd4e68302020-03-25 12:04:17 -0700153 if let Some(ty) = &efn.ret {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700154 if is_unsized(cx, ty) {
David Tolnaya420f012020-03-25 17:55:56 -0700155 let desc = describe(cx, ty);
156 let msg = format!("returning {} by value is not supported", desc);
157 cx.error(ty, msg);
David Tolnayd4e68302020-03-25 12:04:17 -0700158 }
David Tolnayd7e1f1e2020-03-18 21:06:37 -0700159 if let Type::Fn(_) = ty {
160 cx.error(ty, "returning a function pointer is not implemented yet");
161 }
David Tolnayd4e68302020-03-25 12:04:17 -0700162 }
David Tolnay7db73692019-10-20 14:51:12 -0400163}
164
David Tolnay26a2a1d2020-03-25 17:26:43 -0700165fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400166 match &efn.ret {
167 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700168 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400169 }
170
171 for arg in &efn.args {
172 if let Type::Ref(ty) = &arg.ty {
173 if ty.mutability.is_some() {
David Tolnay26a2a1d2020-03-25 17:26:43 -0700174 return;
David Tolnay7db73692019-10-20 14:51:12 -0400175 }
176 }
177 }
178
David Tolnaya420f012020-03-25 17:55:56 -0700179 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400180 efn,
181 "&mut return type is not allowed unless there is a &mut argument",
David Tolnaya420f012020-03-25 17:55:56 -0700182 );
David Tolnay7db73692019-10-20 14:51:12 -0400183}
184
David Tolnay26a2a1d2020-03-25 17:26:43 -0700185fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) {
David Tolnay7db73692019-10-20 14:51:12 -0400186 match &efn.ret {
187 Some(Type::Ref(_)) => {}
David Tolnay26a2a1d2020-03-25 17:26:43 -0700188 _ => return,
David Tolnay7db73692019-10-20 14:51:12 -0400189 }
190
191 let mut reference_args = 0;
192 for arg in &efn.args {
193 if let Type::Ref(_) = &arg.ty {
194 reference_args += 1;
195 }
196 }
197
Joel Galenson3d4f6122020-04-07 15:54:05 -0700198 if efn.receiver.is_some() {
199 reference_args += 1;
200 }
201
David Tolnay26a2a1d2020-03-25 17:26:43 -0700202 if reference_args != 1 {
David Tolnaya420f012020-03-25 17:55:56 -0700203 cx.error(
David Tolnay7db73692019-10-20 14:51:12 -0400204 efn,
205 "functions that return a reference must take exactly one input reference",
David Tolnaya420f012020-03-25 17:55:56 -0700206 );
David Tolnay7db73692019-10-20 14:51:12 -0400207 }
208}
209
David Tolnay26a2a1d2020-03-25 17:26:43 -0700210fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
David Tolnayd4e68302020-03-25 12:04:17 -0700211 let ident = match ty {
212 Type::Ident(ident) => ident,
David Tolnayeebe9b72020-04-14 16:32:18 -0700213 Type::Slice(_) | Type::Void(_) => return true,
David Tolnayd4e68302020-03-25 12:04:17 -0700214 _ => return false,
215 };
David Tolnay26a2a1d2020-03-25 17:26:43 -0700216 ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
217}
218
David Tolnaya420f012020-03-25 17:55:56 -0700219fn span_for_struct_error(strct: &Struct) -> TokenStream {
220 let struct_token = strct.struct_token;
221 let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new());
222 brace_token.set_span(strct.brace_token.span);
223 quote!(#struct_token #brace_token)
224}
225
David Tolnay26a2a1d2020-03-25 17:26:43 -0700226fn combine_errors(errors: Vec<Error>) -> Result<()> {
227 let mut iter = errors.into_iter();
228 let mut all_errors = match iter.next() {
229 Some(err) => err,
230 None => return Ok(()),
231 };
232 for err in iter {
233 all_errors.combine(err);
234 }
235 Err(all_errors)
David Tolnayd4e68302020-03-25 12:04:17 -0700236}
237
David Tolnaya420f012020-03-25 17:55:56 -0700238fn describe(cx: &mut Check, ty: &Type) -> String {
David Tolnay7db73692019-10-20 14:51:12 -0400239 match ty {
240 Type::Ident(ident) => {
David Tolnaya420f012020-03-25 17:55:56 -0700241 if cx.types.structs.contains_key(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400242 "struct".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700243 } else if cx.types.cxx.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400244 "C++ type".to_owned()
David Tolnaya420f012020-03-25 17:55:56 -0700245 } else if cx.types.rust.contains(ident) {
David Tolnay7db73692019-10-20 14:51:12 -0400246 "opaque Rust type".to_owned()
247 } else if Atom::from(ident) == Some(CxxString) {
248 "C++ string".to_owned()
249 } else {
250 ident.to_string()
251 }
252 }
253 Type::RustBox(_) => "Box".to_owned(),
254 Type::UniquePtr(_) => "unique_ptr".to_owned(),
255 Type::Ref(_) => "reference".to_owned(),
256 Type::Str(_) => "&str".to_owned(),
Adrian Taylorf5dd5522020-04-13 16:50:14 -0700257 Type::Slice(_) => "slice".to_owned(),
258 Type::SliceRefU8(_) => "&[u8]".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700259 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700260 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400261 }
262}