Skip to content

Commit

Permalink
Remove delegate checking since it might be buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
YuhanLiin committed Jul 14, 2024
1 parent b7b86ce commit 193d021
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 92 deletions.
1 change: 0 additions & 1 deletion micropb-gen/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ impl Generator {
let Some(msg) = Message::from_proto(proto, self, &msg_conf)? else {
return Ok(quote! {});
};
msg.check_delegates(self)?;
let (msg_mod, hazzer_field_attr) = self.generate_msg_mod(&msg, proto, &msg_conf)?;
let unknown_conf = msg_conf.next_conf("_unknown");

Expand Down
19 changes: 1 addition & 18 deletions micropb-gen/src/generator/field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use proc_macro2::{Literal, Span, TokenStream};
use quote::{format_ident, quote};
use syn::ext::IdentExt;
use syn::{Ident, Lifetime};

use crate::config::OptionalRepr;
Expand Down Expand Up @@ -51,7 +50,7 @@ pub(crate) struct Field<'a> {
pub(crate) name: &'a str,
/// Non-sanitized Rust name after renaming, used for accessor names
pub(crate) rust_name: String,
/// Sanitized Rust ident after renaming, used for field name and delegate name
/// Sanitized Rust ident after renaming, used for field name
pub(crate) san_rust_name: Ident,
pub(crate) default: Option<&'a str>,
pub(crate) boxed: bool,
Expand All @@ -67,22 +66,6 @@ impl<'a> Field<'a> {
matches!(self.ftype, FieldType::Optional(_, OptionalRepr::Hazzer))
}

pub(crate) fn delegate(&self) -> Option<&Ident> {
if let FieldType::Custom(CustomField::Delegate(d)) = &self.ftype {
Some(d)
} else {
None
}
}

pub(crate) fn custom_type_field(&self) -> Option<String> {
if let FieldType::Custom(CustomField::Type(_)) = &self.ftype {
Some(self.san_rust_name.unraw().to_string())
} else {
None
}
}

pub(crate) fn find_lifetime(&self) -> Option<&Lifetime> {
match &self.ftype {
FieldType::Custom(CustomField::Type(ty)) => find_lifetime_from_type(ty),
Expand Down
34 changes: 1 addition & 33 deletions micropb-gen/src/generator/message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
collections::{HashMap, HashSet},
io,
};
use std::{collections::HashMap, io};

use proc_macro2::{Literal, Span, TokenStream};
use quote::{format_ident, quote};
Expand Down Expand Up @@ -159,35 +156,6 @@ impl<'a> Message<'a> {
}))
}

pub(crate) fn check_delegates(&self, gen: &Generator) -> io::Result<()> {
let ocustoms = self.oneofs.iter().filter_map(|o| o.custom_type_field());
let fcustoms = self.fields.iter().filter_map(|f| f.custom_type_field());
let customs: HashSet<_> = ocustoms.chain(fcustoms).collect();

let odelegates = self
.oneofs
.iter()
.filter_map(|o| o.delegate().map(|d| (d, o.name)));
let fdelegates = self
.fields
.iter()
.filter_map(|f| f.delegate().map(|d| (d, f.name)));
for (delegate, fname) in odelegates.chain(fdelegates) {
let delegate = delegate.to_string();
if !customs.contains(delegate.as_str()) {
return Err(field_error(
&gen.pkg,
self.name,
fname,
&format!(
"Delegate field refers to custom field of {delegate}, which doesn't exist"
),
));
}
}
Ok(())
}

pub(crate) fn generate_hazzer_decl(
&self,
conf: CurrentConfig,
Expand Down
29 changes: 3 additions & 26 deletions micropb-gen/src/generator/oneof.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use convert_case::{Case, Casing};
use proc_macro2::{Literal, Span, TokenStream};
use quote::quote;
use syn::{ext::IdentExt, Ident, Lifetime};
use syn::{Ident, Lifetime};

use super::{
derive_msg_attr,
Expand Down Expand Up @@ -154,8 +154,9 @@ pub(crate) enum OneofType<'a> {
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
pub(crate) struct Oneof<'a> {
/// Protobuf name
#[allow(unused)]
pub(crate) name: &'a str,
/// Sanitized Rust ident after renaming, used for delegate name and field name
/// Sanitized Rust ident after renaming, used for field name
pub(crate) san_rust_name: Ident,
pub(crate) otype: OneofType<'a>,
pub(crate) field_attrs: Vec<syn::Attribute>,
Expand All @@ -168,30 +169,6 @@ pub(crate) struct Oneof<'a> {
}

impl<'a> Oneof<'a> {
pub(crate) fn delegate(&self) -> Option<&Ident> {
if let OneofType::Custom {
field: CustomField::Delegate(d),
..
} = &self.otype
{
Some(d)
} else {
None
}
}

pub(crate) fn custom_type_field(&self) -> Option<String> {
if let OneofType::Custom {
field: CustomField::Type(_),
..
} = &self.otype
{
Some(self.san_rust_name.unraw().to_string())
} else {
None
}
}

pub(crate) fn find_lifetime(&self) -> Option<&Lifetime> {
match &self.otype {
OneofType::Custom {
Expand Down
14 changes: 0 additions & 14 deletions micropb-gen/tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,6 @@ fn parse_custom_type_delegate() {
assert!(err.contains("Failed to parse custom delegate"));
}

#[test]
fn unfound_delegate() {
let mut gen = Generator::with_warning_callback(warn_panic);
gen.use_container_alloc();
gen.configure(
".test.Msg.st",
Config::new().custom_field(CustomField::Delegate("kv".to_owned())),
);
let err = compile(gen);
dbg!(&err);
assert!(err.contains("(.test.Msg.st)"));
assert!(err.contains("Delegate field refers to custom field of kv"));
}

#[test]
#[should_panic = "Unused configuration path: \".Msg\""]
fn warn_unused_config() {
Expand Down

0 comments on commit 193d021

Please sign in to comment.