blob: b7814459c6215a017db2568c4213ad5e2845c8d4 [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 }
David Tolnay7db73692019-10-20 14:51:12 -040078 }
79 _ => {}
80 }
81 }
82
83 for api in apis {
84 if let Api::CxxFunction(efn) = api {
85 errors.extend(check_mut_return_restriction(efn).err());
86 }
87 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
88 errors.extend(check_multiple_arg_lifetimes(efn).err());
89 }
90 }
91
92 ident::check_all(apis, &mut errors);
93
94 let mut iter = errors.into_iter();
95 let mut all_errors = match iter.next() {
96 Some(err) => err,
97 None => return Ok(()),
98 };
99 for err in iter {
100 all_errors.combine(err);
101 }
102 Err(all_errors)
103}
104
105fn is_unsized(ty: &Type, types: &Types) -> bool {
106 let ident = match ty {
107 Type::Ident(ident) => ident,
David Tolnay1fa1ae42020-03-15 23:39:58 -0700108 Type::Void(_) => return true,
David Tolnay7db73692019-10-20 14:51:12 -0400109 _ => return false,
110 };
David Tolnaya52602b2020-03-06 10:24:34 -0800111 ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
David Tolnay7db73692019-10-20 14:51:12 -0400112}
113
114fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
115 match &efn.ret {
116 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
117 _ => return Ok(()),
118 }
119
120 for arg in &efn.args {
121 if let Type::Ref(ty) = &arg.ty {
122 if ty.mutability.is_some() {
123 return Ok(());
124 }
125 }
126 }
127
128 Err(Error::new_spanned(
129 efn,
130 "&mut return type is not allowed unless there is a &mut argument",
131 ))
132}
133
134fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
135 match &efn.ret {
136 Some(Type::Ref(_)) => {}
137 _ => return Ok(()),
138 }
139
140 let mut reference_args = 0;
141 for arg in &efn.args {
142 if let Type::Ref(_) = &arg.ty {
143 reference_args += 1;
144 }
145 }
146
147 if reference_args == 1 {
148 Ok(())
149 } else {
150 Err(Error::new_spanned(
151 efn,
152 "functions that return a reference must take exactly one input reference",
153 ))
154 }
155}
156
157fn describe(ty: &Type, types: &Types) -> String {
158 match ty {
159 Type::Ident(ident) => {
160 if types.structs.contains_key(ident) {
161 "struct".to_owned()
162 } else if types.cxx.contains(ident) {
163 "C++ type".to_owned()
164 } else if types.rust.contains(ident) {
165 "opaque Rust type".to_owned()
166 } else if Atom::from(ident) == Some(CxxString) {
167 "C++ string".to_owned()
168 } else {
169 ident.to_string()
170 }
171 }
172 Type::RustBox(_) => "Box".to_owned(),
173 Type::UniquePtr(_) => "unique_ptr".to_owned(),
174 Type::Ref(_) => "reference".to_owned(),
175 Type::Str(_) => "&str".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700176 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400177 }
178}
179
180fn unsupported_type(ident: &Ident) -> Error {
181 Error::new(ident.span(), "unsupported type")
182}
183
David Tolnay1fa1ae42020-03-15 23:39:58 -0700184fn unsupported_reference_type(ty: &Ref) -> Error {
185 Error::new_spanned(ty, "unsupported reference type")
186}
187
David Tolnay7db73692019-10-20 14:51:12 -0400188fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
189 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
190}
191
192fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
193 Error::new_spanned(unique_ptr, "unsupported target type of Box")
194}
195
196fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
197 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
198}
199
200fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
201 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
202}
203
204fn field_by_value(field: &Var, types: &Types) -> Error {
205 let desc = describe(&field.ty, types);
206 let message = format!("using {} by value is not supported", desc);
207 Error::new_spanned(field, message)
208}
209
210fn argument_by_value(arg: &Var, types: &Types) -> Error {
211 let desc = describe(&arg.ty, types);
212 let message = format!("passing {} by value is not supported", desc);
213 Error::new_spanned(arg, message)
214}
215
216fn return_by_value(ty: &Type, types: &Types) -> Error {
217 let desc = describe(ty, types);
218 let message = format!("returning {} by value is not supported", desc);
219 Error::new_spanned(ty, message)
220}