blob: df633deb0425602e570168b4977644bb80a27d77 [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use crate::syntax::atom::Atom::{self, *};
2use crate::syntax::{error, ident, Api, ExternFn, Ty1, Type, Types, Var};
3use 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 }
43 _ => {}
44 }
45 }
46
47 for api in apis {
48 match api {
49 Api::Struct(strct) => {
50 for field in &strct.fields {
51 if is_unsized(&field.ty, types) {
52 errors.push(field_by_value(field, types));
53 }
54 }
55 }
56 Api::CxxFunction(efn) | Api::RustFunction(efn) => {
57 for arg in &efn.args {
58 if is_unsized(&arg.ty, types) {
59 errors.push(argument_by_value(arg, types));
60 }
61 }
62 if let Some(ty) = &efn.ret {
63 if is_unsized(ty, types) {
64 errors.push(return_by_value(ty, types));
65 }
66 }
67 }
68 _ => {}
69 }
70 }
71
72 for api in apis {
73 if let Api::CxxFunction(efn) = api {
74 errors.extend(check_mut_return_restriction(efn).err());
75 }
76 if let Api::CxxFunction(efn) | Api::RustFunction(efn) = api {
77 errors.extend(check_multiple_arg_lifetimes(efn).err());
78 }
79 }
80
81 ident::check_all(apis, &mut errors);
82
83 let mut iter = errors.into_iter();
84 let mut all_errors = match iter.next() {
85 Some(err) => err,
86 None => return Ok(()),
87 };
88 for err in iter {
89 all_errors.combine(err);
90 }
91 Err(all_errors)
92}
93
94fn is_unsized(ty: &Type, types: &Types) -> bool {
95 let ident = match ty {
96 Type::Ident(ident) => ident,
97 _ => return false,
98 };
David Tolnaya52602b2020-03-06 10:24:34 -080099 ident == CxxString || types.cxx.contains(ident) || types.rust.contains(ident)
David Tolnay7db73692019-10-20 14:51:12 -0400100}
101
102fn check_mut_return_restriction(efn: &ExternFn) -> Result<()> {
103 match &efn.ret {
104 Some(Type::Ref(ty)) if ty.mutability.is_some() => {}
105 _ => return Ok(()),
106 }
107
108 for arg in &efn.args {
109 if let Type::Ref(ty) = &arg.ty {
110 if ty.mutability.is_some() {
111 return Ok(());
112 }
113 }
114 }
115
116 Err(Error::new_spanned(
117 efn,
118 "&mut return type is not allowed unless there is a &mut argument",
119 ))
120}
121
122fn check_multiple_arg_lifetimes(efn: &ExternFn) -> Result<()> {
123 match &efn.ret {
124 Some(Type::Ref(_)) => {}
125 _ => return Ok(()),
126 }
127
128 let mut reference_args = 0;
129 for arg in &efn.args {
130 if let Type::Ref(_) = &arg.ty {
131 reference_args += 1;
132 }
133 }
134
135 if reference_args == 1 {
136 Ok(())
137 } else {
138 Err(Error::new_spanned(
139 efn,
140 "functions that return a reference must take exactly one input reference",
141 ))
142 }
143}
144
145fn describe(ty: &Type, types: &Types) -> String {
146 match ty {
147 Type::Ident(ident) => {
148 if types.structs.contains_key(ident) {
149 "struct".to_owned()
150 } else if types.cxx.contains(ident) {
151 "C++ type".to_owned()
152 } else if types.rust.contains(ident) {
153 "opaque Rust type".to_owned()
154 } else if Atom::from(ident) == Some(CxxString) {
155 "C++ string".to_owned()
156 } else {
157 ident.to_string()
158 }
159 }
160 Type::RustBox(_) => "Box".to_owned(),
161 Type::UniquePtr(_) => "unique_ptr".to_owned(),
162 Type::Ref(_) => "reference".to_owned(),
163 Type::Str(_) => "&str".to_owned(),
164 }
165}
166
167fn unsupported_type(ident: &Ident) -> Error {
168 Error::new(ident.span(), "unsupported type")
169}
170
171fn unsupported_cxx_type_in_box(unique_ptr: &Ty1) -> Error {
172 Error::new_spanned(unique_ptr, error::BOX_CXX_TYPE.msg)
173}
174
175fn unsupported_box_target(unique_ptr: &Ty1) -> Error {
176 Error::new_spanned(unique_ptr, "unsupported target type of Box")
177}
178
179fn unsupported_rust_type_in_unique_ptr(unique_ptr: &Ty1) -> Error {
180 Error::new_spanned(unique_ptr, "unique_ptr of a Rust type is not supported yet")
181}
182
183fn unsupported_unique_ptr_target(unique_ptr: &Ty1) -> Error {
184 Error::new_spanned(unique_ptr, "unsupported unique_ptr target type")
185}
186
187fn field_by_value(field: &Var, types: &Types) -> Error {
188 let desc = describe(&field.ty, types);
189 let message = format!("using {} by value is not supported", desc);
190 Error::new_spanned(field, message)
191}
192
193fn argument_by_value(arg: &Var, types: &Types) -> Error {
194 let desc = describe(&arg.ty, types);
195 let message = format!("passing {} by value is not supported", desc);
196 Error::new_spanned(arg, message)
197}
198
199fn return_by_value(ty: &Type, types: &Types) -> Error {
200 let desc = describe(ty, types);
201 let message = format!("returning {} by value is not supported", desc);
202 Error::new_spanned(ty, message)
203}