fix private fields in #[hdl] pub struct

This commit is contained in:
Jacob Lifshay 2025-11-06 20:23:16 -08:00
parent 26a7090178
commit fbc8ffa5ae
Signed by: programmerjake
SSH key fingerprint: SHA256:HnFTLGpSm4Q4Fj502oCFisjZSoakwEuTsJJMSke63RQ
4 changed files with 42 additions and 26 deletions

View file

@ -224,7 +224,7 @@ impl Builder {
.args
.push_value(match get_field_state(field_index) {
BuilderFieldState::Unfilled => parse_quote_spanned! {self.ident.span()=>
::fayalite::bundle::Unfilled<#ty>
()
},
BuilderFieldState::Generic => {
let type_var = type_var_for_field_name(ident);
@ -383,7 +383,7 @@ impl ToTokens for Builder {
fn default() -> Self {
#ident {
#phantom_field_name: ::fayalite::__std::marker::PhantomData,
#(#field_idents: ::fayalite::__std::default::Default::default(),)*
#(#field_idents: (),)*
}
}
}
@ -395,7 +395,7 @@ impl ToTokens for Builder {
let type_generics = self.generics.split_for_impl().1;
quote_spanned! {self.ident.span()=>
#[automatically_derived]
#[allow(non_camel_case_types, dead_code)]
#[allow(non_camel_case_types, dead_code, private_interfaces)]
impl #filled_impl_generics ::fayalite::expr::ToExpr for #filled_ty
#filled_where_clause
{
@ -499,7 +499,6 @@ impl ToTokens for ParsedBundle {
};
builder.to_tokens(tokens);
let unfilled_builder_ty = builder.builder_struct_ty(|_| BuilderFieldState::Unfilled);
let filled_builder_ty = builder.builder_struct_ty(|_| BuilderFieldState::Filled);
let mut mask_type_fields = FieldsNamed::from(fields.clone());
for Field { ty, .. } in &mut mask_type_fields.named {
*ty = parse_quote_spanned! {span=>
@ -517,8 +516,6 @@ impl ToTokens for ParsedBundle {
mask_type_builder.to_tokens(tokens);
let unfilled_mask_type_builder_ty =
mask_type_builder.builder_struct_ty(|_| BuilderFieldState::Unfilled);
let filled_mask_type_builder_ty =
mask_type_builder.builder_struct_ty(|_| BuilderFieldState::Filled);
ItemStruct {
attrs: vec![
common_derives(span),
@ -785,7 +782,6 @@ impl ToTokens for ParsedBundle {
#where_clause
{
type Builder = #unfilled_mask_type_builder_ty;
type FilledBuilder = #filled_mask_type_builder_ty;
fn fields(&#self_token) -> ::fayalite::intern::Interned<[::fayalite::bundle::BundleField]> {
::fayalite::intern::Intern::intern(&[#(#fields_body_fields)*][..])
}
@ -935,7 +931,6 @@ impl ToTokens for ParsedBundle {
#where_clause
{
type Builder = #unfilled_builder_ty;
type FilledBuilder = #filled_builder_ty;
fn fields(&#self_token) -> ::fayalite::intern::Interned<[::fayalite::bundle::BundleField]> {
::fayalite::intern::Intern::intern(&[#(#fields_body_fields)*][..])
}

View file

@ -271,7 +271,6 @@ impl Type for Bundle {
pub trait BundleType: Type<BaseType = Bundle> {
type Builder: Default;
type FilledBuilder: ToExpr<Type = Self>;
fn fields(&self) -> Interned<[BundleField]>;
}
@ -374,17 +373,8 @@ impl<'a> BundleSimValueToOpaque<'a> {
#[derive(Default)]
pub struct NoBuilder;
pub struct Unfilled<T: Type>(PhantomData<T>);
impl<T: Type> Default for Unfilled<T> {
fn default() -> Self {
Self(PhantomData)
}
}
impl BundleType for Bundle {
type Builder = NoBuilder;
type FilledBuilder = Expr<Bundle>;
fn fields(&self) -> Interned<[BundleField]> {
self.0.fields
}
@ -420,15 +410,14 @@ macro_rules! impl_tuple_builder_fields {
) => {
impl<
$($head_type_var,)*
$cur_type_var: Type,
$($tail_type_var,)*
> TupleBuilder<(
$($head_type_var,)*
Unfilled<$cur_type_var>,
(),
$($tail_type_var,)*
)>
{
pub fn $cur_field(self, $cur_var: impl ToExpr<Type = $cur_type_var>) -> TupleBuilder<(
pub fn $cur_field<$cur_type_var: Type>(self, $cur_var: impl ToExpr<Type = $cur_type_var>) -> TupleBuilder<(
$($head_type_var,)*
Expr<$cur_type_var>,
$($tail_type_var,)*
@ -452,6 +441,12 @@ macro_rules! impl_tuple_builder_fields {
($global:tt []) => {};
}
macro_rules! get_unit_ty {
($($tt:tt)*) => {
()
};
}
macro_rules! impl_tuples {
(
[$({
@ -545,8 +540,7 @@ macro_rules! impl_tuples {
}
}
impl<$($T: Type,)*> BundleType for ($($T,)*) {
type Builder = TupleBuilder<($(Unfilled<$T>,)*)>;
type FilledBuilder = TupleBuilder<($(Expr<$T>,)*)>;
type Builder = TupleBuilder<($(get_unit_ty!($T),)*)>;
fn fields(&self) -> Interned<[BundleField]> {
let ($($var,)*) = self;
[$(BundleField { name: stringify!($num).intern(), flipped: false, ty: $var.canonical() }),*].intern_slice()
@ -791,7 +785,6 @@ impl<T: ?Sized + Send + Sync + 'static> ToExpr for PhantomDataBuilder<T> {
impl<T: ?Sized + Send + Sync + 'static> BundleType for PhantomData<T> {
type Builder = PhantomDataBuilder<T>;
type FilledBuilder = PhantomDataBuilder<T>;
fn fields(&self) -> Interned<[BundleField]> {
Interned::default()
}

View file

@ -96,7 +96,6 @@ impl Type for UIntInRangeMaskType {
impl BundleType for UIntInRangeMaskType {
type Builder = NoBuilder;
type FilledBuilder = Expr<UIntInRangeMaskType>;
fn fields(&self) -> Interned<[BundleField]> {
let [value_name, range_name] = UINT_IN_RANGE_TYPE_FIELD_NAMES;
@ -400,7 +399,6 @@ macro_rules! define_uint_in_range_type {
impl<Start: Size, End: Size> BundleType for $UIntInRangeType<Start, End> {
type Builder = NoBuilder;
type FilledBuilder = Expr<Self>;
fn fields(&self) -> Interned<[BundleField]> {
let [value_name, range_name] = UINT_IN_RANGE_TYPE_FIELD_NAMES;

View file

@ -214,3 +214,33 @@ pub struct MyTypeWithPhantomConstParameter<P: PhantomConstGet<MyPhantomConstInne
pub a: ArrayType<Bool, GetA<P>>,
pub b: HdlOption<GetB<P>>,
}
#[hdl(outline_generated)]
struct MyPrivateType {}
#[hdl(outline_generated)]
pub(crate) struct MyPubCrateType {}
#[hdl(outline_generated)]
pub struct MyTypeWithPrivateMembers {
a: MyPrivateType,
pub(crate) b: MyPubCrateType,
pub c: Bool,
}
#[hdl(outline_generated)]
struct MyPrivateTypeWithArg<T> {
v: T,
}
#[hdl(outline_generated)]
pub(crate) struct MyPubCrateTypeWithArg<T> {
v: T,
}
#[hdl(outline_generated)]
pub struct MyTypeWithPrivateMembersWithArg<T> {
a: MyPrivateTypeWithArg<T>,
pub(crate) b: MyPubCrateTypeWithArg<T>,
pub c: T,
}