blob: 4c2fff385bb0ab897bd616e4f7732a91aa2d476c [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnaybb16d532020-03-16 12:30:21 -07002use crate::syntax::{error, ident, Api, ExternFn, Lang::*, Ref, Ty1, Type, Types, Var};
David Tolnay7db73692019-10-20 14:51:12 -04003use proc_macro2::Ident;
4use syn::{Error, Result};
5
6pub(crate) fn typecheck(apis: &[Api], types: &Types) -> Result<()> {
7 let mut errors = Vec::new();
8
9 for ty in types {
10 match ty {
11 Type::Ident(ident) => {
12 if Atom::from(ident).is_none()
13 && !types.structs.contains_key(ident)
14 && !types.cxx.contains(ident)
15 && !types.rust.contains(ident)
16 {
17 errors.push(unsupported_type(ident));
18 }
19 }
20 Type::RustBox(ptr) => {
21 if let Type::Ident(ident) = &ptr.inner {
22 if types.cxx.contains(ident) {
23 errors.push(unsupported_cxx_type_in_box(ptr));
24 }
25 if Atom::from(ident).is_none() {
26 continue;
27 }
28 }
29 errors.push(unsupported_box_target(ptr));
30 }
31 Type::UniquePtr(ptr) => {
32 if let Type::Ident(ident) = &ptr.inner {
33 if types.rust.contains(ident) {
34 errors.push(unsupported_rust_type_in_unique_ptr(ptr));
35 }
36 match Atom::from(ident) {
37 None | Some(CxxString) => continue,
38 _ => {}
39 }
40 }
41 errors.push(unsupported_unique_ptr_target(ptr));
42 }
David Tolnay1fa1ae42020-03-15 23:39:58 -070043 Type::Ref(ty) => {
44 if let Type::Void(_) = ty.inner {
45 errors.push(unsupported_reference_type(ty));
46 }
47 }
David Tolnay7db73692019-10-20 14:51:12 -040048 _ => {}
49 }
50 }
51
52 for api in apis {
53 match api {
54 Api::Struct(strct) => {
55 for field in &strct.fields {
56 if is_unsized(&field.ty, types) {
57 errors.push(field_by_value(field, types));
58 }
59 }
60 }
61 Api::CxxFunction(efn) | Api::RustFunction(efn) => {
62 for arg in &efn.args {
63 if is_unsized(&arg.ty, types) {
64 errors.push(argument_by_value(arg, types));
65 }
66 }
67 if let Some(ty) = &efn.ret {
68 if is_unsized(ty, types) {
69 errors.push(return_by_value(ty, types));
70 }
71 }
David Tolnaybb16d532020-03-16 12:30:21 -070072 if efn.throws && efn.lang == Cxx {
David Tolnayc0a166d2020-03-16 00:40:15 -070073 errors.push(Error::new_spanned(
74 efn,
David Tolnaybb16d532020-03-16 12:30:21 -070075 "fallible C++ functions are not implemented yet",
76 ));
77 }
78 if efn.throws && efn.lang == Rust {
79 errors.push(Error::new_spanned(
80 efn,
81 "fallible Rust functions are not implemented yet",
David Tolnayc0a166d2020-03-16 00:40:15 -070082 ));
83 }
David Tolnay7db73692019-10-20 14:51:12 -040084 }
85 _ => {}
86 }
87 }
88
89 for api in apis {
90 if let Api::CxxFunction(efn) = api {
91 errors.extend(check_mut_return_restriction(efn).err());
92 }
93 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
94 errors.extend(check_multiple_arg_lifetimes(efn).err());
95 }
96 }
97
98 ident::check_all(apis, &mut errors);
99
100 let mut iter = errors.into_iter();
101 let mut all_errors = match iter.next() {
102 Some(err) => err,
103 None => return Ok(()),
104 };
105 for err in iter {
106 all_errors.combine(err);
107 }
108 Err(all_errors)
109}
110
111fn is_unsized(ty: &Type, types: &Types) -> bool {
112 let ident = match ty {
113 Type::Ident(ident) => ident,
David Tolnay1fa1ae42020-03-15 23:39:58 -0700114 Type::Void(_) => return true,
David Tolnay7db73692019-10-20 14:51:12 -0400115 _ => return false,
116 };
David Tolnaya52602b2020-03-06 10:24:34 -0800117 ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
David Tolnay7db73692019-10-20 14:51:12 -0400118}
119
120fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
121 match &efn.ret {
122 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
123 _ => return Ok(()),
124 }
125
126 for arg in &efn.args {
127 if let Type::Ref(ty) = &arg.ty {
128 if ty.mutability.is_some() {
129 return Ok(());
130 }
131 }
132 }
133
134 Err(Error::new_spanned(
135 efn,
136 "&mut return type is not allowed unless there is a &mut argument",
137 ))
138}
139
140fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
141 match &efn.ret {
142 Some(Type::Ref(_)) => {}
143 _ => return Ok(()),
144 }
145
146 let mut reference_args = 0;
147 for arg in &efn.args {
148 if let Type::Ref(_) = &arg.ty {
149 reference_args += 1;
150 }
151 }
152
153 if reference_args == 1 {
154 Ok(())
155 } else {
156 Err(Error::new_spanned(
157 efn,
158 "functions that return a reference must take exactly one input reference",
159 ))
160 }
161}
162
163fn describe(ty: &Type, types: &Types) -> String {
164 match ty {
165 Type::Ident(ident) => {
166 if types.structs.contains_key(ident) {
167 "struct".to_owned()
168 } else if types.cxx.contains(ident) {
169 "C++ type".to_owned()
170 } else if types.rust.contains(ident) {
171 "opaque Rust type".to_owned()
172 } else if Atom::from(ident) == Some(CxxString) {
173 "C++ string".to_owned()
174 } else {
175 ident.to_string()
176 }
177 }
178 Type::RustBox(_) => "Box".to_owned(),
179 Type::UniquePtr(_) => "unique_ptr".to_owned(),
180 Type::Ref(_) => "reference".to_owned(),
181 Type::Str(_) => "&str".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700182 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400183 }
184}
185
186fn unsupported_type(ident: &Ident) -> Error {
187 Error::new(ident.span(), "unsupported type")
188}
189
David Tolnay1fa1ae42020-03-15 23:39:58 -0700190fn unsupported_reference_type(ty: &Ref) -> Error {
191 Error::new_spanned(ty, "unsupported reference type")
192}
193
David Tolnay7db73692019-10-20 14:51:12 -0400194fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
195 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
196}
197
198fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
199 Error::new_spanned(unique_ptr, "unsupported target type of Box")
200}
201
202fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
203 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
204}
205
206fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
207 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
208}
209
210fn field_by_value(field: &Var, types: &Types) -> Error {
211 let desc = describe(&field.ty, types);
212 let message = format!("using {} by value is not supported", desc);
213 Error::new_spanned(field, message)
214}
215
216fn argument_by_value(arg: &Var, types: &Types) -> Error {
217 let desc = describe(&arg.ty, types);
218 let message = format!("passing {} by value is not supported", desc);
219 Error::new_spanned(arg, message)
220}
221
222fn return_by_value(ty: &Type, types: &Types) -> Error {
223 let desc = describe(ty, types);
224 let message = format!("returning {} by value is not supported", desc);
225 Error::new_spanned(ty, message)
226}