Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding str support back #369

Merged
merged 7 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions core/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ pub enum TypeName {
pub enum StringEncoding {
UnvalidatedUtf8,
UnvalidatedUtf16,
/// The caller guarantees that they're passing valid UTF-8, under penalty of UB
Utf8,
}

impl TypeName {
Expand Down Expand Up @@ -514,6 +516,11 @@ impl TypeName {
))
.unwrap()
}
TypeName::StrReference(lifetime, StringEncoding::Utf8) => syn::parse_str(&format!(
"{}str",
ReferenceDisplay(lifetime, &Mutability::Immutable)
))
.unwrap(),
TypeName::PrimitiveSlice(lifetime, mutability, name) => {
let primitive_name = PRIMITIVE_TO_STRING.get(name).unwrap();
let formatted_str = format!(
Expand Down Expand Up @@ -549,14 +556,16 @@ impl TypeName {
let mutability = Mutability::from_syn(&r.mutability);

let name = r.elem.to_token_stream().to_string();
if name.starts_with("DiplomatStr") {
if name.starts_with("DiplomatStr") || name == "str" {
if mutability.is_mutable() {
panic!("mutable `DiplomatStr*` references are disallowed");
panic!("mutable string references are disallowed");
}
if name == "DiplomatStr" {
return TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf8);
} else if name == "DiplomatStr16" {
return TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf16);
} else if name == "str" {
return TypeName::StrReference(lifetime, StringEncoding::Utf8);
}
}
if let syn::Type::Slice(slice) = &*r.elem {
Expand Down Expand Up @@ -951,6 +960,13 @@ impl fmt::Display for TypeName {
ReferenceDisplay(lifetime, &Mutability::Immutable)
)
}
TypeName::StrReference(lifetime, StringEncoding::Utf8) => {
write!(
f,
"{}str",
ReferenceDisplay(lifetime, &Mutability::Immutable)
)
}
TypeName::PrimitiveSlice(lifetime, mutability, typ) => {
write!(f, "{}[{typ}]", ReferenceDisplay(lifetime, mutability))
}
Expand Down
1 change: 1 addition & 0 deletions feature_tests/c/include/BorrowedFields.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/c2/include/BorrowedFields.d.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions feature_tests/cpp/docs/source/lifetimes_ffi.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp/include/BorrowedFields.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp/include/BorrowedFields.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion feature_tests/cpp/include/Foo.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp2/include/BorrowedFields.d.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/cpp2/include/BorrowedFields.d.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions feature_tests/cpp2/include/BorrowedFields.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion feature_tests/dart/lib/BorrowedFields.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/dotnet/Lib/Generated/RawBorrowedFields.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/js/api/BorrowedFields.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions feature_tests/js/api/BorrowedFields.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion feature_tests/js/api/Foo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions feature_tests/js/docs/source/lifetimes_ffi.rst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions feature_tests/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod ffi {
pub struct BorrowedFields<'a> {
a: &'a DiplomatStr16,
b: &'a DiplomatStr,
c: &'a str,
}

pub struct BorrowedFieldsReturning<'a> {
Expand Down
13 changes: 10 additions & 3 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ fn gen_params_at_boundary(param: &ast::Param, expanded_params: &mut Vec<FnArg>)
match &param.ty {
ast::TypeName::StrReference(
..,
ast::StringEncoding::UnvalidatedUtf8 | ast::StringEncoding::UnvalidatedUtf16,
ast::StringEncoding::UnvalidatedUtf8
| ast::StringEncoding::UnvalidatedUtf16
| ast::StringEncoding::Utf8,
)
| ast::TypeName::PrimitiveSlice(..) => {
let data_type = if let ast::TypeName::PrimitiveSlice(.., prim) = &param.ty {
ast::TypeName::Primitive(*prim).to_syn().to_token_stream()
} else if let ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf8) =
&param.ty
} else if let ast::TypeName::StrReference(
_,
ast::StringEncoding::UnvalidatedUtf8 | ast::StringEncoding::Utf8,
) = &param.ty
{
quote! { u8 }
} else if let ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16) =
Expand Down Expand Up @@ -116,6 +120,9 @@ fn gen_params_invocation(param: &ast::Param, expanded_params: &mut Vec<Expr>) {
}
},
}
} else if let ast::TypeName::StrReference(_, ast::StringEncoding::Utf8) = &param.ty {
// The FFI guarantees this, by either validating, or communicating this requirement to the user.
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
quote! {unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts(#data_ident, #len_ident))} }
} else {
quote! {
if #len_ident == 0 {
Expand Down
9 changes: 7 additions & 2 deletions macro/src/snapshots/diplomat__tests__multilevel_borrows.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ mod ffi {
#[no_mangle]
extern "C" fn Baz_destroy<'x: 'y, 'y>(this: Box<Baz<'x, 'y>>) {}
#[no_mangle]
extern "C" fn Foo_new<'a>(x: &'a str) -> Box<Foo<'a>> {
Foo::new(x)
extern "C" fn Foo_new<'a>(x_diplomat_data: *const u8, x_diplomat_len: usize) -> Box<Foo<'a>> {
Foo::new(unsafe {
core::str::from_utf8_unchecked(core::slice::from_raw_parts(
x_diplomat_data,
x_diplomat_len,
))
})
}
#[no_mangle]
extern "C" fn Foo_get_bar<'a: 'b, 'b>(this: &'b Foo<'a>) -> Box<Bar<'b, 'a>> {
Expand Down
6 changes: 5 additions & 1 deletion tool/src/c/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ pub fn gen_method<W: fmt::Write>(
write!(out, ", ")?;
}

if let ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf8) = &param.ty {
if let ast::TypeName::StrReference(
_,
ast::StringEncoding::UnvalidatedUtf8 | ast::StringEncoding::Utf8,
) = &param.ty
{
write!(out, "const char* {0}_data, size_t {0}_len", param.name)?;
} else if let ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16) =
&param.ty
Expand Down
14 changes: 8 additions & 6 deletions tool/src/c/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ pub fn gen_type<W: fmt::Write>(
}

ast::TypeName::Writeable => write!(out, "DiplomatWriteable")?,
ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf8) => {
write!(out, "DiplomatStringView")?
}
ast::TypeName::StrReference(
_,
ast::StringEncoding::UnvalidatedUtf8 | ast::StringEncoding::Utf8,
) => write!(out, "DiplomatStringView")?,
ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16) => {
write!(out, "DiplomatU16View")?
}
Expand Down Expand Up @@ -102,9 +103,10 @@ pub fn name_for_type(typ: &ast::TypeName) -> ast::Ident {
name_for_type(err)
)),
ast::TypeName::Writeable => ast::Ident::from("writeable"),
ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf8) => {
ast::Ident::from("str_ref8")
}
ast::TypeName::StrReference(
_,
ast::StringEncoding::UnvalidatedUtf8 | ast::StringEncoding::Utf8,
) => ast::Ident::from("str_ref8"),
ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16) => {
ast::Ident::from("str_ref16")
}
Expand Down
1 change: 1 addition & 0 deletions tool/src/c2/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl<'tcx> CFormatter<'tcx> {
Type::Struct(s) => self.fmt_type_name(P::id_for_path(s)),
Type::Enum(e) => self.fmt_type_name(e.tcx_id.into()),
Type::Slice(hir::Slice::Str(_, StringEncoding::UnvalidatedUtf8)) => "str_ref8".into(),
Type::Slice(hir::Slice::Str(_, StringEncoding::Utf8)) => "str_refv8".into(),
Type::Slice(hir::Slice::Str(_, StringEncoding::UnvalidatedUtf16)) => "str_ref16".into(),
Type::Slice(hir::Slice::Primitive(borrow, p)) => {
let constness = borrow.mutability.if_mut_else("", "const_");
Expand Down
10 changes: 8 additions & 2 deletions tool/src/c2/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
) -> Vec<(Cow<'ccx, str>, Cow<'a, str>)> {
let param_name = self.cx.formatter.fmt_param_name(ident);
match ty {
Type::Slice(hir::Slice::Str(_, hir::StringEncoding::UnvalidatedUtf8)) if !is_struct => {
Type::Slice(hir::Slice::Str(
_,
hir::StringEncoding::UnvalidatedUtf8 | hir::StringEncoding::Utf8,
)) if !is_struct => {
vec![
("const char*".into(), format!("{param_name}_data").into()),
("size_t".into(), format!("{param_name}_len").into()),
Expand Down Expand Up @@ -322,7 +325,10 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
}
Type::Slice(ref s) => {
let ptr_ty = match s {
hir::Slice::Str(_, hir::StringEncoding::UnvalidatedUtf8) => "char".into(),
hir::Slice::Str(
_,
hir::StringEncoding::UnvalidatedUtf8 | hir::StringEncoding::Utf8,
) => "char".into(),
hir::Slice::Str(_, hir::StringEncoding::UnvalidatedUtf16) => "wchar_t".into(),
hir::Slice::Primitive(_, prim) => self.cx.formatter.fmt_primitive_as_c(*prim),
&_ => unreachable!("unknown AST/HIR variant"),
Expand Down
5 changes: 4 additions & 1 deletion tool/src/cpp/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ pub fn gen_rust_to_cpp<W: Write>(
todo!("Returning references from Rust to C++ is not currently supported")
}
ast::TypeName::Writeable => panic!("Returning writeables is not supported"),
ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf8) => {
ast::TypeName::StrReference(
_,
ast::StringEncoding::UnvalidatedUtf8 | ast::StringEncoding::Utf8,
) => {
let raw_value_id = format!("diplomat_str_raw_{path}");
writeln!(out, "capi::DiplomatStringView {raw_value_id} = {cpp};").unwrap();

Expand Down
Loading
Loading