blob: be61226cc02921b6655581408694ac2ed236e739 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
David Tolnayebef4a22020-03-17 15:33:47 -07002use crate::syntax::{error, ident, Api, ExternFn, 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 }
72 }
73 _ => {}
74 }
75 }
76
77 for api in apis {
78 if let Api::CxxFunction(efn) = api {
79 errors.extend(check_mut_return_restriction(efn).err());
80 }
81 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
82 errors.extend(check_multiple_arg_lifetimes(efn).err());
83 }
84 }
85
86 ident::check_all(apis, &mut errors);
87
88 let mut iter = errors.into_iter();
89 let mut all_errors = match iter.next() {
90 Some(err) => err,
91 None => return Ok(()),
92 };
93 for err in iter {
94 all_errors.combine(err);
95 }
96 Err(all_errors)
97}
98
99fn is_unsized(ty: &Type, types: &Types) -> bool {
100 let ident = match ty {
101 Type::Ident(ident) => ident,
David Tolnay1fa1ae42020-03-15 23:39:58 -0700102 Type::Void(_) => return true,
David Tolnay7db73692019-10-20 14:51:12 -0400103 _ => return false,
104 };
David Tolnaya52602b2020-03-06 10:24:34 -0800105 ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
David Tolnay7db73692019-10-20 14:51:12 -0400106}
107
108fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
109 match &efn.ret {
110 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
111 _ => return Ok(()),
112 }
113
114 for arg in &efn.args {
115 if let Type::Ref(ty) = &arg.ty {
116 if ty.mutability.is_some() {
117 return Ok(());
118 }
119 }
120 }
121
122 Err(Error::new_spanned(
123 efn,
124 "&mut return type is not allowed unless there is a &mut argument",
125 ))
126}
127
128fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
129 match &efn.ret {
130 Some(Type::Ref(_)) => {}
131 _ => return Ok(()),
132 }
133
134 let mut reference_args = 0;
135 for arg in &efn.args {
136 if let Type::Ref(_) = &arg.ty {
137 reference_args += 1;
138 }
139 }
140
141 if reference_args == 1 {
142 Ok(())
143 } else {
144 Err(Error::new_spanned(
145 efn,
146 "functions that return a reference must take exactly one input reference",
147 ))
148 }
149}
150
151fn describe(ty: &Type, types: &Types) -> String {
152 match ty {
153 Type::Ident(ident) => {
154 if types.structs.contains_key(ident) {
155 "struct".to_owned()
156 } else if types.cxx.contains(ident) {
157 "C++ type".to_owned()
158 } else if types.rust.contains(ident) {
159 "opaque Rust type".to_owned()
160 } else if Atom::from(ident) == Some(CxxString) {
161 "C++ string".to_owned()
162 } else {
163 ident.to_string()
164 }
165 }
166 Type::RustBox(_) => "Box".to_owned(),
167 Type::UniquePtr(_) => "unique_ptr".to_owned(),
168 Type::Ref(_) => "reference".to_owned(),
169 Type::Str(_) => "&str".to_owned(),
David Tolnay417305a2020-03-18 13:54:00 -0700170 Type::Fn(_) => "function pointer".to_owned(),
David Tolnay2fb14e92020-03-15 23:11:38 -0700171 Type::Void(_) => "()".to_owned(),
David Tolnay7db73692019-10-20 14:51:12 -0400172 }
173}
174
175fn unsupported_type(ident: &Ident) -> Error {
176 Error::new(ident.span(), "unsupported type")
177}
178
David Tolnay1fa1ae42020-03-15 23:39:58 -0700179fn unsupported_reference_type(ty: &Ref) -> Error {
180 Error::new_spanned(ty, "unsupported reference type")
181}
182
David Tolnay7db73692019-10-20 14:51:12 -0400183fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
184 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
185}
186
187fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
188 Error::new_spanned(unique_ptr, "unsupported target type of Box")
189}
190
191fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
192 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
193}
194
195fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
196 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
197}
198
199fn field_by_value(field: &Var, types: &Types) -> Error {
200 let desc = describe(&field.ty, types);
201 let message = format!("using {} by value is not supported", desc);
202 Error::new_spanned(field, message)
203}
204
205fn argument_by_value(arg: &Var, types: &Types) -> Error {
206 let desc = describe(&arg.ty, types);
207 let message = format!("passing {} by value is not supported", desc);
208 Error::new_spanned(arg, message)
209}
210
211fn return_by_value(ty: &Type, types: &Types) -> Error {
212 let desc = describe(ty, types);
213 let message = format!("returning {} by value is not supported", desc);
214 Error::new_spanned(ty, message)
215}