Improve type checking and error messages for slice type
diff --git a/syntax/check.rs b/syntax/check.rs
index c71295e..181c530 100644
--- a/syntax/check.rs
+++ b/syntax/check.rs
@@ -1,5 +1,5 @@
use crate::syntax::atom::Atom::{self, *};
-use crate::syntax::{error, ident, Api, ExternFn, Lang, Ref, Struct, Ty1, Type, Types};
+use crate::syntax::{error, ident, Api, ExternFn, Lang, Ref, Slice, Struct, Ty1, Type, Types};
use proc_macro2::{Delimiter, Group, Ident, TokenStream};
use quote::{quote, ToTokens};
use std::fmt::Display;
@@ -29,6 +29,7 @@
Type::RustBox(ptr) => check_type_box(cx, ptr),
Type::UniquePtr(ptr) => check_type_unique_ptr(cx, ptr),
Type::Ref(ty) => check_type_ref(cx, ty),
+ Type::Slice(ty) => check_type_slice(cx, ty),
_ => {}
}
}
@@ -107,6 +108,10 @@
cx.error(ty, "unsupported reference type");
}
+fn check_type_slice(cx: &mut Check, ty: &Slice) {
+ cx.error(ty, "only &[u8] is supported so far, not other slice types");
+}
+
fn check_api_struct(cx: &mut Check, strct: &Struct) {
if strct.fields.is_empty() {
let span = span_for_struct_error(strct);
@@ -201,7 +206,7 @@
fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
let ident = match ty {
Type::Ident(ident) => ident,
- Type::Void(_) => return true,
+ Type::Slice(_) | Type::Void(_) => return true,
_ => return false,
};
ident == CxxString || cx.types.cxx.contains(ident) || cx.types.rust.contains(ident)
diff --git a/syntax/parse.rs b/syntax/parse.rs
index 95dd67c..6e5b36e 100644
--- a/syntax/parse.rs
+++ b/syntax/parse.rs
@@ -1,3 +1,4 @@
+use crate::syntax::Atom::*;
use crate::syntax::{
attrs, error, Api, Atom, Doc, ExternFn, ExternType, Lang, Receiver, Ref, Signature, Slice,
Struct, Ty1, Type, Var,
@@ -220,25 +221,13 @@
match ty {
RustType::Reference(ty) => parse_type_reference(ty),
RustType::Path(ty) => parse_type_path(ty),
+ RustType::Slice(ty) => parse_type_slice(ty),
RustType::BareFn(ty) => parse_type_fn(ty),
RustType::Tuple(ty) if ty.elems.is_empty() => Ok(Type::Void(ty.paren_token.span)),
- RustType::Slice(ty) => parse_type_slice(ty),
_ => Err(Error::new_spanned(ty, "unsupported type")),
}
}
-fn parse_type_slice(ty: &TypeSlice) -> Result<Type> {
- let inner = parse_type(&ty.elem)?;
- let which = match &inner {
- Type::Ident(ident) if ident == "u8" => Type::Slice,
- _ => return Err(Error::new_spanned(ty, "unsupported type")),
- };
- Ok(which(Box::new(Slice {
- bracket: ty.bracket_token,
- inner,
- })))
-}
-
fn parse_type_reference(ty: &TypeReference) -> Result<Type> {
let inner = parse_type(&ty.elem)?;
let which = match &inner {
@@ -249,9 +238,9 @@
Type::Str
}
}
- Type::Slice(inner2) => match &inner2.inner {
- Type::Ident(ident) if ident == "u8" => Type::SliceRefU8,
- _ => return Err(Error::new_spanned(ty, "unsupported type")),
+ Type::Slice(slice) => match &slice.inner {
+ Type::Ident(ident) if ident == U8 && ty.mutability.is_none() => Type::SliceRefU8,
+ _ => Type::Ref,
},
_ => Type::Ref,
};
@@ -298,6 +287,14 @@
Err(Error::new_spanned(ty, "unsupported type"))
}
+fn parse_type_slice(ty: &TypeSlice) -> Result<Type> {
+ let inner = parse_type(&ty.elem)?;
+ Ok(Type::Slice(Box::new(Slice {
+ bracket: ty.bracket_token,
+ inner,
+ })))
+}
+
fn parse_type_fn(ty: &TypeBareFn) -> Result<Type> {
if ty.lifetimes.is_some() {
return Err(Error::new_spanned(