Simplify rename checking.
This commit is contained in:
parent
371b7fd00b
commit
0d7d54e21c
@ -183,8 +183,10 @@ pub(crate) fn flatten_type_groups(ty: &syn::Type) -> &syn::Type {
|
||||
}
|
||||
|
||||
pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::Error> {
|
||||
fn make_key(name: String, itemfn: &ExportedFn) -> String {
|
||||
itemfn.arg_list().fold(name, |mut argstr, fnarg| {
|
||||
fn make_key(name: impl ToString, itemfn: &ExportedFn) -> String {
|
||||
itemfn
|
||||
.arg_list()
|
||||
.fold(name.to_string(), |mut argstr, fnarg| {
|
||||
let type_string: String = match fnarg {
|
||||
syn::FnArg::Receiver(_) => unimplemented!("receiver rhai_fns not implemented"),
|
||||
syn::FnArg::Typed(syn::PatType { ref ty, .. }) => {
|
||||
@ -198,8 +200,7 @@ pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::
|
||||
}
|
||||
|
||||
let mut renames = HashMap::<String, proc_macro2::Span>::new();
|
||||
let mut fn_names = HashMap::<String, proc_macro2::Span>::new();
|
||||
let mut fn_sig = HashMap::<String, proc_macro2::Span>::new();
|
||||
let mut fn_defs = HashMap::<String, proc_macro2::Span>::new();
|
||||
|
||||
for itemfn in fns.iter() {
|
||||
if itemfn.params().name.is_some() || itemfn.params().special != FnSpecialAccess::None {
|
||||
@ -216,7 +217,7 @@ pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::
|
||||
|
||||
for (name, fn_name) in names {
|
||||
let current_span = itemfn.params().span.as_ref().unwrap();
|
||||
let key = make_key(name.clone(), itemfn);
|
||||
let key = make_key(&name, itemfn);
|
||||
if let Some(other_span) = renames.insert(key, *current_span) {
|
||||
let mut err = syn::Error::new(
|
||||
*current_span,
|
||||
@ -231,7 +232,7 @@ pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::
|
||||
}
|
||||
} else {
|
||||
let ident = itemfn.name();
|
||||
if let Some(other_span) = fn_names.insert(ident.to_string(), ident.span()) {
|
||||
if let Some(other_span) = fn_defs.insert(ident.to_string(), ident.span()) {
|
||||
let mut err = syn::Error::new(
|
||||
ident.span(),
|
||||
format!("duplicate function '{}'", ident.to_string()),
|
||||
@ -242,24 +243,20 @@ pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::
|
||||
));
|
||||
return Err(err);
|
||||
}
|
||||
let key = make_key(ident.to_string(), itemfn);
|
||||
fn_sig.insert(key, ident.span());
|
||||
}
|
||||
}
|
||||
|
||||
for (new_name, attr_span) in renames.drain() {
|
||||
let fn_name = new_name.split('.').next().unwrap();
|
||||
if let Some(fn_span) = fn_sig.get(&new_name) {
|
||||
let key = make_key(ident, itemfn);
|
||||
if let Some(fn_span) = renames.get(&key) {
|
||||
let mut err = syn::Error::new(
|
||||
attr_span,
|
||||
format!("duplicate Rhai signature for '{}'", &fn_name),
|
||||
ident.span(),
|
||||
format!("duplicate Rhai signature for '{}'", &ident),
|
||||
);
|
||||
err.combine(syn::Error::new(
|
||||
*fn_span,
|
||||
format!("duplicated function '{}'", &fn_name),
|
||||
format!("duplicated function '{}'", &ident),
|
||||
));
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user