Skip to content

Commit

Permalink
Introduce DiplomatStr16 (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Nov 22, 2023
1 parent 60e78d1 commit 4e1046a
Show file tree
Hide file tree
Showing 44 changed files with 339 additions and 144 deletions.
3 changes: 2 additions & 1 deletion core/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub use enums::Enum;

mod types;
pub use types::{
CustomType, LifetimeOrigin, ModSymbol, Mutability, PathType, PrimitiveType, TypeName,
CustomType, LifetimeOrigin, ModSymbol, Mutability, PathType, PrimitiveType, StringEncoding,
TypeName,
};

mod lifetimes;
Expand Down
52 changes: 40 additions & 12 deletions core/src/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub enum TypeName {
Result(Box<TypeName>, Box<TypeName>, bool),
Writeable,
/// A `&DiplomatStr` type.
StrReference(Lifetime),
StrReference(Lifetime, StringEncoding),
/// A `&[T]` type, where `T` is a primitive.
PrimitiveSlice(Lifetime, Mutability, PrimitiveType),
/// The `()` type.
Expand All @@ -391,6 +391,13 @@ pub enum TypeName {
SelfType(PathType),
}

#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Debug, Copy)]
#[non_exhaustive]
pub enum StringEncoding {
UnvalidatedUtf8,
UnvalidatedUtf16,
}

impl TypeName {
/// Converts the [`TypeName`] back into an AST node that can be spliced into a program.
pub fn to_syn(&self) -> syn::Type {
Expand Down Expand Up @@ -493,11 +500,20 @@ impl TypeName {
TypeName::Writeable => syn::parse_quote! {
diplomat_runtime::DiplomatWriteable
},
TypeName::StrReference(lifetime) => syn::parse_str(&format!(
"{}DiplomatStr",
ReferenceDisplay(lifetime, &Mutability::Immutable)
))
.unwrap(),
TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf8) => {
syn::parse_str(&format!(
"{}DiplomatStr",
ReferenceDisplay(lifetime, &Mutability::Immutable)
))
.unwrap()
}
TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf16) => {
syn::parse_str(&format!(
"{}DiplomatStr16",
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 @@ -532,11 +548,16 @@ impl TypeName {
let lifetime = Lifetime::from(&r.lifetime);
let mutability = Mutability::from_syn(&r.mutability);

if r.elem.to_token_stream().to_string() == "DiplomatStr" {
let name = r.elem.to_token_stream().to_string();
if name.starts_with("DiplomatStr") {
if mutability.is_mutable() {
panic!("mutable `DiplomatStr` references are disallowed");
panic!("mutable `DiplomatStr*` references are disallowed");
}
if name == "DiplomatStr" {
return TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf8);
} else if name == "DiplomatStr16" {
return TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf16);
}
return TypeName::StrReference(lifetime);
}
if let syn::Type::Slice(slice) = &*r.elem {
if let syn::Type::Path(p) = &*slice.elem {
Expand Down Expand Up @@ -657,7 +678,7 @@ impl TypeName {
ok.visit_lifetimes(visit)?;
err.visit_lifetimes(visit)
}
TypeName::StrReference(lt) => visit(lt, LifetimeOrigin::StrReference),
TypeName::StrReference(lt, ..) => visit(lt, LifetimeOrigin::StrReference),
TypeName::PrimitiveSlice(lt, ..) => visit(lt, LifetimeOrigin::PrimitiveSlice),
_ => ControlFlow::Continue(()),
}
Expand Down Expand Up @@ -866,7 +887,7 @@ impl TypeName {
ok.check_lifetime_elision(full_type, in_path, env, errors);
err.check_lifetime_elision(full_type, in_path, env, errors);
}
TypeName::StrReference(Lifetime::Anonymous)
TypeName::StrReference(Lifetime::Anonymous, ..)
| TypeName::PrimitiveSlice(Lifetime::Anonymous, ..) => {
errors.push(ValidityError::LifetimeElisionInReturn {
full_type: full_type.clone(),
Expand Down Expand Up @@ -916,13 +937,20 @@ impl fmt::Display for TypeName {
write!(f, "Result<{ok}, {err}>")
}
TypeName::Writeable => "DiplomatWriteable".fmt(f),
TypeName::StrReference(lifetime) => {
TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf8) => {
write!(
f,
"{}DiplomatStr",
ReferenceDisplay(lifetime, &Mutability::Immutable)
)
}
TypeName::StrReference(lifetime, StringEncoding::UnvalidatedUtf16) => {
write!(
f,
"{}DiplomatStr16",
ReferenceDisplay(lifetime, &Mutability::Immutable)
)
}
TypeName::PrimitiveSlice(lifetime, mutability, typ) => {
write!(f, "{}[{typ}]", ReferenceDisplay(lifetime, mutability))
}
Expand Down
14 changes: 8 additions & 6 deletions core/src/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,10 @@ impl<'ast, 'errors> LoweringContext<'ast, 'errors> {
));
None
}
ast::TypeName::StrReference(lifetime) => {
Some(Type::Slice(Slice::Str(ltl?.lower_lifetime(lifetime))))
}
ast::TypeName::StrReference(lifetime, encoding) => Some(Type::Slice(Slice::Str(
ltl?.lower_lifetime(lifetime),
*encoding,
))),
ast::TypeName::PrimitiveSlice(lifetime, mutability, prim) => {
let borrow = Borrow::new(ltl?.lower_lifetime(lifetime), *mutability);
let prim = PrimitiveType::from_ast(*prim);
Expand Down Expand Up @@ -658,9 +659,10 @@ impl<'ast, 'errors> LoweringContext<'ast, 'errors> {
));
None
}
ast::TypeName::StrReference(lifetime) => {
Some(OutType::Slice(Slice::Str(ltl?.lower_lifetime(lifetime))))
}
ast::TypeName::StrReference(lifetime, encoding) => Some(OutType::Slice(Slice::Str(
ltl?.lower_lifetime(lifetime),
*encoding,
))),
ast::TypeName::PrimitiveSlice(lifetime, mutability, prim) => {
let borrow = Borrow::new(ltl?.lower_lifetime(lifetime), *mutability);
let prim = PrimitiveType::from_ast(*prim);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ TypeContext {
0,
),
),
UnvalidatedUtf8,
),
),
},
Expand Down Expand Up @@ -128,6 +129,7 @@ TypeContext {
0,
),
),
UnvalidatedUtf8,
),
),
},
Expand Down Expand Up @@ -179,6 +181,7 @@ TypeContext {
1,
),
),
UnvalidatedUtf8,
),
),
},
Expand All @@ -193,6 +196,7 @@ TypeContext {
1,
),
),
UnvalidatedUtf8,
),
),
),
Expand Down
7 changes: 4 additions & 3 deletions core/src/hir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::{
};
use crate::ast;
pub use ast::Mutability;
pub use ast::StringEncoding;

/// Type that can only be used as an output.
pub type OutType = Type<OutputOnly>;
Expand Down Expand Up @@ -34,7 +35,7 @@ pub enum SelfType {
#[non_exhaustive]
pub enum Slice {
/// A string slice, e.g. `&DiplomatStr`.
Str(MaybeStatic<TypeLifetime>),
Str(MaybeStatic<TypeLifetime>, StringEncoding),

/// A primitive slice, e.g. `&mut [u8]`.
Primitive(Borrow, PrimitiveType),
Expand Down Expand Up @@ -89,8 +90,8 @@ impl Slice {
/// variant.
pub fn lifetime(&self) -> &MaybeStatic<TypeLifetime> {
match self {
Slice::Str(lifetime) => lifetime,
Slice::Primitive(reference, _) => &reference.lifetime,
Slice::Str(lifetime, ..) => lifetime,
Slice::Primitive(reference, ..) => &reference.lifetime,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion example/js/lib/api/ICU4XLocale.js

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

23 changes: 18 additions & 5 deletions example/js/lib/api/diplomat-runtime.js

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

14 changes: 7 additions & 7 deletions example/js/lib/api/diplomat-wasm.mjs

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/c2/include/BorrowedFields.d.h

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/cpp2/include/BorrowedFields.d.h

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/cpp2/include/BorrowedFields.d.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/cpp2/include/BorrowedFields.hpp

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

8 changes: 4 additions & 4 deletions feature_tests/dart/lib/BorrowedFields.g.dart

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

Loading

0 comments on commit 4e1046a

Please sign in to comment.