| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | use crate::syntax::atom::Atom::{self, *}; |
| David Tolnay | 9dcb833 | 2020-04-30 20:37:33 -0700 | [diff] [blame] | 2 | use crate::syntax::namespace::Namespace; |
| David Tolnay | d763f4c | 2020-04-22 16:09:19 -0700 | [diff] [blame] | 3 | use crate::syntax::{ |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 4 | error, ident, Api, Enum, ExternFn, ExternType, Lang, Receiver, Ref, Slice, Struct, Ty1, Type, |
| 5 | Types, |
| David Tolnay | d763f4c | 2020-04-22 16:09:19 -0700 | [diff] [blame] | 6 | }; |
| David Tolnay | 09462ac | 2020-03-20 14:58:41 -0700 | [diff] [blame] | 7 | use proc_macro2::{Delimiter, Group, Ident, TokenStream}; |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 8 | use quote::{quote, ToTokens}; |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 9 | use std::collections::HashSet; |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 10 | use std::fmt::Display; |
| David Tolnay | 8adb223 | 2020-05-01 10:14:27 -0700 | [diff] [blame] | 11 | use std::u32; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 12 | use syn::{Error, Result}; |
| 13 | |
| David Tolnay | a83301c | 2020-04-30 20:32:37 -0700 | [diff] [blame] | 14 | pub(crate) struct Check<'a> { |
| David Tolnay | 9dcb833 | 2020-04-30 20:37:33 -0700 | [diff] [blame] | 15 | namespace: &'a Namespace, |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 16 | apis: &'a [Api], |
| 17 | types: &'a Types<'a>, |
| 18 | errors: &'a mut Vec<Error>, |
| 19 | } |
| 20 | |
| David Tolnay | 9dcb833 | 2020-04-30 20:37:33 -0700 | [diff] [blame] | 21 | pub(crate) fn typecheck(namespace: &Namespace, apis: &[Api], types: &Types) -> Result<()> { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 22 | let mut errors = Vec::new(); |
| 23 | let mut cx = Check { |
| David Tolnay | 9dcb833 | 2020-04-30 20:37:33 -0700 | [diff] [blame] | 24 | namespace, |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 25 | apis, |
| 26 | types, |
| 27 | errors: &mut errors, |
| 28 | }; |
| 29 | do_typecheck(&mut cx); |
| 30 | combine_errors(errors) |
| 31 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 32 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 33 | fn do_typecheck(cx: &mut Check) { |
| David Tolnay | 6b6423e | 2020-04-30 20:46:24 -0700 | [diff] [blame] | 34 | ident::check_all(cx, cx.namespace, cx.apis); |
| David Tolnay | 9dcb833 | 2020-04-30 20:37:33 -0700 | [diff] [blame] | 35 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 36 | for ty in cx.types { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 37 | match ty { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 38 | Type::Ident(ident) => check_type_ident(cx, ident), |
| 39 | Type::RustBox(ptr) => check_type_box(cx, ptr), |
| David Tolnay | c6d891e | 2020-04-25 12:06:34 -0700 | [diff] [blame] | 40 | Type::RustVec(ty) => check_type_rust_vec(cx, ty), |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 41 | Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr), |
| David Tolnay | fff4c8a | 2020-04-25 11:45:20 -0700 | [diff] [blame] | 42 | Type::CxxVector(ptr) => check_type_cxx_vector(cx, ptr), |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 43 | Type::Ref(ty) => check_type_ref(cx, ty), |
| David Tolnay | eebe9b7 | 2020-04-14 16:32:18 -0700 | [diff] [blame] | 44 | Type::Slice(ty) => check_type_slice(cx, ty), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 45 | _ => {} |
| 46 | } |
| 47 | } |
| 48 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 49 | for api in cx.apis { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 50 | match api { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 51 | Api::Struct(strct) => check_api_struct(cx, strct), |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 52 | Api::Enum(enm) => check_api_enum(cx, enm), |
| David Tolnay | 0b368ae | 2020-04-22 17:55:02 -0700 | [diff] [blame] | 53 | Api::CxxType(ty) | Api::RustType(ty) => check_api_type(cx, ty), |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 54 | Api::CxxFunction(efn) | Api::RustFunction(efn) => check_api_fn(cx, efn), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 55 | _ => {} |
| 56 | } |
| 57 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 60 | impl Check<'_> { |
| David Tolnay | a83301c | 2020-04-30 20:32:37 -0700 | [diff] [blame] | 61 | pub(crate) fn error(&mut self, sp: impl ToTokens, msg: impl Display) { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 62 | self.errors.push(Error::new_spanned(sp, msg)); |
| 63 | } |
| 64 | } |
| 65 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 66 | fn check_type_ident(cx: &mut Check, ident: &Ident) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 67 | if Atom::from(ident).is_none() |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 68 | && !cx.types.structs.contains_key(ident) |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 69 | && !cx.types.enums.contains_key(ident) |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 70 | && !cx.types.cxx.contains(ident) |
| 71 | && !cx.types.rust.contains(ident) |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 72 | { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 73 | cx.error(ident, "unsupported type"); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 77 | fn check_type_box(cx: &mut Check, ptr: &Ty1) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 78 | if let Type::Ident(ident) = &ptr.inner { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 79 | if cx.types.cxx.contains(ident) { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 80 | cx.error(ptr, error::BOX_CXX_TYPE.msg); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 81 | } |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 82 | |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 83 | if Atom::from(ident).is_none() { |
| 84 | return; |
| 85 | } |
| 86 | } |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 87 | |
| 88 | cx.error(ptr, "unsupported target type of Box"); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| David Tolnay | c6d891e | 2020-04-25 12:06:34 -0700 | [diff] [blame] | 91 | fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) { |
| 92 | if let Type::Ident(ident) = &ty.inner { |
| 93 | if cx.types.cxx.contains(ident) { |
| 94 | cx.error(ty, "Rust Vec containing C++ type is not supported yet"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 95 | return; |
| David Tolnay | c6d891e | 2020-04-25 12:06:34 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | match Atom::from(ident) { |
| David Tolnay | 6bd63de | 2020-04-25 12:20:09 -0700 | [diff] [blame] | 99 | None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8) |
| 100 | | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return, |
| 101 | Some(Bool) | Some(RustString) => { /* todo */ } |
| 102 | Some(CxxString) => {} |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| David Tolnay | c6d891e | 2020-04-25 12:06:34 -0700 | [diff] [blame] | 106 | cx.error(ty, "unsupported element type of Vec"); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 107 | } |
| 108 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 109 | fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 110 | if let Type::Ident(ident) = &ptr.inner { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 111 | if cx.types.rust.contains(ident) { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 112 | cx.error(ptr, "unique_ptr of a Rust type is not supported yet"); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 113 | } |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 114 | |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 115 | match Atom::from(ident) { |
| 116 | None | Some(CxxString) => return, |
| 117 | _ => {} |
| 118 | } |
| David Tolnay | 4377a9e | 2020-04-24 15:20:26 -0700 | [diff] [blame] | 119 | } else if let Type::CxxVector(_) = &ptr.inner { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 120 | return; |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 121 | } |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 122 | |
| 123 | cx.error(ptr, "unsupported unique_ptr target type"); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| David Tolnay | fff4c8a | 2020-04-25 11:45:20 -0700 | [diff] [blame] | 126 | fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) { |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 127 | if let Type::Ident(ident) = &ptr.inner { |
| 128 | if cx.types.rust.contains(ident) { |
| David Tolnay | c0faaf6 | 2020-04-25 12:18:38 -0700 | [diff] [blame] | 129 | cx.error( |
| 130 | ptr, |
| 131 | "C++ vector containing a Rust type is not supported yet", |
| 132 | ); |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | match Atom::from(ident) { |
| David Tolnay | 6bd63de | 2020-04-25 12:20:09 -0700 | [diff] [blame] | 136 | None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8) |
| 137 | | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) => return, |
| 138 | Some(CxxString) => { /* todo */ } |
| 139 | Some(Bool) | Some(RustString) => {} |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 140 | } |
| 141 | } |
| David Tolnay | c0faaf6 | 2020-04-25 12:18:38 -0700 | [diff] [blame] | 142 | |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 143 | cx.error(ptr, "unsupported vector target type"); |
| 144 | } |
| 145 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 146 | fn check_type_ref(cx: &mut Check, ty: &Ref) { |
| David Tolnay | d763f4c | 2020-04-22 16:09:19 -0700 | [diff] [blame] | 147 | if ty.lifetime.is_some() { |
| 148 | cx.error(ty, "references with explicit lifetimes are not supported"); |
| 149 | } |
| 150 | |
| David Tolnay | d7e1f1e | 2020-03-18 21:06:37 -0700 | [diff] [blame] | 151 | match ty.inner { |
| 152 | Type::Fn(_) | Type::Void(_) => {} |
| David Tolnay | 776fd89 | 2020-04-28 13:38:21 -0700 | [diff] [blame] | 153 | Type::Ref(_) => { |
| 154 | cx.error(ty, "C++ does not allow references to references"); |
| 155 | return; |
| 156 | } |
| David Tolnay | d7e1f1e | 2020-03-18 21:06:37 -0700 | [diff] [blame] | 157 | _ => return, |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 158 | } |
| David Tolnay | d7e1f1e | 2020-03-18 21:06:37 -0700 | [diff] [blame] | 159 | |
| 160 | cx.error(ty, "unsupported reference type"); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| David Tolnay | eebe9b7 | 2020-04-14 16:32:18 -0700 | [diff] [blame] | 163 | fn check_type_slice(cx: &mut Check, ty: &Slice) { |
| 164 | cx.error(ty, "only &[u8] is supported so far, not other slice types"); |
| 165 | } |
| 166 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 167 | fn check_api_struct(cx: &mut Check, strct: &Struct) { |
| David Tolnay | 0b368ae | 2020-04-22 17:55:02 -0700 | [diff] [blame] | 168 | check_reserved_name(cx, &strct.ident); |
| 169 | |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 170 | if strct.fields.is_empty() { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 171 | let span = span_for_struct_error(strct); |
| 172 | cx.error(span, "structs without any fields are not supported"); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 173 | } |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 174 | |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 175 | for field in &strct.fields { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 176 | if is_unsized(cx, &field.ty) { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 177 | let desc = describe(cx, &field.ty); |
| 178 | let msg = format!("using {} by value is not supported", desc); |
| 179 | cx.error(field, msg); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 180 | } |
| David Tolnay | d7e1f1e | 2020-03-18 21:06:37 -0700 | [diff] [blame] | 181 | if let Type::Fn(_) = field.ty { |
| 182 | cx.error( |
| 183 | field, |
| 184 | "function pointers in a struct field are not implemented yet", |
| 185 | ); |
| 186 | } |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 190 | fn check_api_enum(cx: &mut Check, enm: &Enum) { |
| 191 | check_reserved_name(cx, &enm.ident); |
| 192 | |
| 193 | if enm.variants.is_empty() { |
| 194 | let span = span_for_enum_error(enm); |
| 195 | cx.error(span, "enums without any variants are not supported"); |
| 196 | } |
| 197 | |
| 198 | let mut discriminants = HashSet::new(); |
| Joel Galenson | 04fa096 | 2020-05-01 10:00:31 -0700 | [diff] [blame] | 199 | enm.variants |
| 200 | .iter() |
| 201 | .fold(None, |prev_discriminant, variant| { |
| 202 | if variant.discriminant.is_none() && prev_discriminant.unwrap_or(0) == u32::MAX { |
| 203 | let msg = format!("overflowed on value after {}", prev_discriminant.unwrap()); |
| 204 | cx.error(span_for_enum_error(enm), msg); |
| 205 | return None; |
| 206 | } |
| 207 | let discriminant = variant |
| 208 | .discriminant |
| 209 | .unwrap_or_else(|| prev_discriminant.map_or(0, |n| n + 1)); |
| 210 | if !discriminants.insert(discriminant) { |
| 211 | let msg = format!("discriminant value `{}` already exists", discriminant); |
| 212 | cx.error(span_for_enum_error(enm), msg); |
| 213 | } |
| 214 | Some(discriminant) |
| 215 | }); |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| David Tolnay | 0b368ae | 2020-04-22 17:55:02 -0700 | [diff] [blame] | 218 | fn check_api_type(cx: &mut Check, ty: &ExternType) { |
| 219 | check_reserved_name(cx, &ty.ident); |
| 220 | } |
| 221 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 222 | fn check_api_fn(cx: &mut Check, efn: &ExternFn) { |
| David Tolnay | d763f4c | 2020-04-22 16:09:19 -0700 | [diff] [blame] | 223 | if let Some(receiver) = &efn.receiver { |
| David Tolnay | a1f29c4 | 2020-04-22 18:01:38 -0700 | [diff] [blame] | 224 | let ref span = span_for_receiver_error(receiver); |
| 225 | |
| 226 | if receiver.ty == "Self" { |
| 227 | let mutability = match receiver.mutability { |
| 228 | Some(_) => "mut ", |
| 229 | None => "", |
| 230 | }; |
| 231 | let msg = format!( |
| 232 | "unnamed receiver type is only allowed if the surrounding \ |
| 233 | extern block contains exactly one extern type; \ |
| 234 | use `self: &{mutability}TheType`", |
| 235 | mutability = mutability, |
| 236 | ); |
| 237 | cx.error(span, msg); |
| 238 | } else if !cx.types.structs.contains_key(&receiver.ty) |
| David Tolnay | 8b60bf1 | 2020-04-22 16:52:19 -0700 | [diff] [blame] | 239 | && !cx.types.cxx.contains(&receiver.ty) |
| 240 | && !cx.types.rust.contains(&receiver.ty) |
| 241 | { |
| David Tolnay | 8b60bf1 | 2020-04-22 16:52:19 -0700 | [diff] [blame] | 242 | cx.error(span, "unrecognized receiver type"); |
| 243 | } |
| 244 | |
| David Tolnay | d763f4c | 2020-04-22 16:09:19 -0700 | [diff] [blame] | 245 | if receiver.lifetime.is_some() { |
| David Tolnay | d763f4c | 2020-04-22 16:09:19 -0700 | [diff] [blame] | 246 | cx.error(span, "references with explicit lifetimes are not supported"); |
| 247 | } |
| 248 | } |
| 249 | |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 250 | for arg in &efn.args { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 251 | if is_unsized(cx, &arg.ty) { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 252 | let desc = describe(cx, &arg.ty); |
| 253 | let msg = format!("passing {} by value is not supported", desc); |
| 254 | cx.error(arg, msg); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 255 | } |
| David Tolnay | d7e1f1e | 2020-03-18 21:06:37 -0700 | [diff] [blame] | 256 | if let Type::Fn(_) = arg.ty { |
| David Tolnay | 75dca2e | 2020-03-25 20:17:52 -0700 | [diff] [blame] | 257 | if efn.lang == Lang::Rust { |
| 258 | cx.error( |
| 259 | arg, |
| 260 | "passing a function pointer from C++ to Rust is not implemented yet", |
| 261 | ); |
| 262 | } |
| David Tolnay | d7e1f1e | 2020-03-18 21:06:37 -0700 | [diff] [blame] | 263 | } |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 264 | } |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 265 | |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 266 | if let Some(ty) = &efn.ret { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 267 | if is_unsized(cx, ty) { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 268 | let desc = describe(cx, ty); |
| 269 | let msg = format!("returning {} by value is not supported", desc); |
| 270 | cx.error(ty, msg); |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 271 | } |
| David Tolnay | d7e1f1e | 2020-03-18 21:06:37 -0700 | [diff] [blame] | 272 | if let Type::Fn(_) = ty { |
| 273 | cx.error(ty, "returning a function pointer is not implemented yet"); |
| 274 | } |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 275 | } |
| David Tolnay | 96a826b | 2020-05-04 00:17:12 -0700 | [diff] [blame^] | 276 | |
| 277 | if efn.lang == Lang::Cxx { |
| 278 | check_mut_return_restriction(cx, efn); |
| 279 | } |
| 280 | |
| 281 | check_multiple_arg_lifetimes(cx, efn); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 284 | fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 285 | match &efn.ret { |
| 286 | Some(Type::Ref(ty)) if ty.mutability.is_some() => {} |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 287 | _ => return, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | for arg in &efn.args { |
| 291 | if let Type::Ref(ty) = &arg.ty { |
| 292 | if ty.mutability.is_some() { |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 293 | return; |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 298 | cx.error( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 299 | efn, |
| 300 | "&mut return type is not allowed unless there is a &mut argument", |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 301 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 304 | fn check_multiple_arg_lifetimes(cx: &mut Check, efn: &ExternFn) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 305 | match &efn.ret { |
| 306 | Some(Type::Ref(_)) => {} |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 307 | _ => return, |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | let mut reference_args = 0; |
| 311 | for arg in &efn.args { |
| 312 | if let Type::Ref(_) = &arg.ty { |
| 313 | reference_args += 1; |
| 314 | } |
| 315 | } |
| 316 | |
| Joel Galenson | 3d4f612 | 2020-04-07 15:54:05 -0700 | [diff] [blame] | 317 | if efn.receiver.is_some() { |
| 318 | reference_args += 1; |
| 319 | } |
| 320 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 321 | if reference_args != 1 { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 322 | cx.error( |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 323 | efn, |
| 324 | "functions that return a reference must take exactly one input reference", |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 325 | ); |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| David Tolnay | 0b368ae | 2020-04-22 17:55:02 -0700 | [diff] [blame] | 329 | fn check_reserved_name(cx: &mut Check, ident: &Ident) { |
| David Tolnay | 99c93d8 | 2020-04-25 14:07:44 -0700 | [diff] [blame] | 330 | if ident == "Box" |
| 331 | || ident == "UniquePtr" |
| 332 | || ident == "Vec" |
| 333 | || ident == "CxxVector" |
| 334 | || Atom::from(ident).is_some() |
| 335 | { |
| David Tolnay | 0b368ae | 2020-04-22 17:55:02 -0700 | [diff] [blame] | 336 | cx.error(ident, "reserved name"); |
| 337 | } |
| 338 | } |
| 339 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 340 | fn is_unsized(cx: &mut Check, ty: &Type) -> bool { |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 341 | let ident = match ty { |
| 342 | Type::Ident(ident) => ident, |
| David Tolnay | e70303c | 2020-04-25 15:02:37 -0700 | [diff] [blame] | 343 | Type::CxxVector(_) | Type::Slice(_) | Type::Void(_) => return true, |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 344 | _ => return false, |
| 345 | }; |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 346 | ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident) |
| 347 | } |
| 348 | |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 349 | fn span_for_struct_error(strct: &Struct) -> TokenStream { |
| 350 | let struct_token = strct.struct_token; |
| 351 | let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new()); |
| 352 | brace_token.set_span(strct.brace_token.span); |
| 353 | quote!(#struct_token #brace_token) |
| 354 | } |
| 355 | |
| Joel Galenson | c03402a | 2020-04-23 17:31:09 -0700 | [diff] [blame] | 356 | fn span_for_enum_error(enm: &Enum) -> TokenStream { |
| 357 | let enum_token = enm.enum_token; |
| 358 | let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new()); |
| 359 | brace_token.set_span(enm.brace_token.span); |
| 360 | quote!(#enum_token #brace_token) |
| 361 | } |
| 362 | |
| David Tolnay | d763f4c | 2020-04-22 16:09:19 -0700 | [diff] [blame] | 363 | fn span_for_receiver_error(receiver: &Receiver) -> TokenStream { |
| 364 | let ampersand = receiver.ampersand; |
| 365 | let lifetime = &receiver.lifetime; |
| 366 | let mutability = receiver.mutability; |
| 367 | if receiver.shorthand { |
| 368 | let var = receiver.var; |
| 369 | quote!(#ampersand #lifetime #mutability #var) |
| 370 | } else { |
| 371 | let ty = &receiver.ty; |
| 372 | quote!(#ampersand #lifetime #mutability #ty) |
| 373 | } |
| 374 | } |
| 375 | |
| David Tolnay | 26a2a1d | 2020-03-25 17:26:43 -0700 | [diff] [blame] | 376 | fn combine_errors(errors: Vec<Error>) -> Result<()> { |
| 377 | let mut iter = errors.into_iter(); |
| 378 | let mut all_errors = match iter.next() { |
| 379 | Some(err) => err, |
| 380 | None => return Ok(()), |
| 381 | }; |
| 382 | for err in iter { |
| 383 | all_errors.combine(err); |
| 384 | } |
| 385 | Err(all_errors) |
| David Tolnay | d4e6830 | 2020-03-25 12:04:17 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 388 | fn describe(cx: &mut Check, ty: &Type) -> String { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 389 | match ty { |
| 390 | Type::Ident(ident) => { |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 391 | if cx.types.structs.contains_key(ident) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 392 | "struct".to_owned() |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 393 | } else if cx.types.cxx.contains(ident) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 394 | "C++ type".to_owned() |
| David Tolnay | a420f01 | 2020-03-25 17:55:56 -0700 | [diff] [blame] | 395 | } else if cx.types.rust.contains(ident) { |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 396 | "opaque Rust type".to_owned() |
| 397 | } else if Atom::from(ident) == Some(CxxString) { |
| 398 | "C++ string".to_owned() |
| 399 | } else { |
| 400 | ident.to_string() |
| 401 | } |
| 402 | } |
| 403 | Type::RustBox(_) => "Box".to_owned(), |
| Myron Ahn | eba35cf | 2020-02-05 19:41:51 +0700 | [diff] [blame] | 404 | Type::RustVec(_) => "Vec".to_owned(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 405 | Type::UniquePtr(_) => "unique_ptr".to_owned(), |
| 406 | Type::Ref(_) => "reference".to_owned(), |
| 407 | Type::Str(_) => "&str".to_owned(), |
| David Tolnay | fa85fce | 2020-04-25 15:03:25 -0700 | [diff] [blame] | 408 | Type::CxxVector(_) => "C++ vector".to_owned(), |
| Adrian Taylor | f5dd552 | 2020-04-13 16:50:14 -0700 | [diff] [blame] | 409 | Type::Slice(_) => "slice".to_owned(), |
| 410 | Type::SliceRefU8(_) => "&[u8]".to_owned(), |
| David Tolnay | 417305a | 2020-03-18 13:54:00 -0700 | [diff] [blame] | 411 | Type::Fn(_) => "function pointer".to_owned(), |
| David Tolnay | 2fb14e9 | 2020-03-15 23:11:38 -0700 | [diff] [blame] | 412 | Type::Void(_) => "()".to_owned(), |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 413 | } |
| 414 | } |