From fa1a7b1698d45d03e4acbf57a1974bd9e7061251 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 11 Feb 2021 11:34:16 -0500 Subject: [PATCH] [bindings] Allow resolution of private types in some cases We need this specifically for resolving the features::sealed::Context trait which is inside the non-pub `sealed` module. --- c-bindings-gen/src/main.rs | 3 +++ c-bindings-gen/src/types.rs | 9 ++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 198e0cfdd..f8e5b4849 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1165,9 +1165,12 @@ impl FullLibraryAST { }; self.load_module(modname, m.attrs, m.content.unwrap().1); submods.push(modident); + } else { + non_mod_items.push(syn::Item::Mod(m)); } } }, + syn::Item::Mod(_) => panic!("--pretty=expanded output should never have non-body modules"), _ => { non_mod_items.push(item); } } } diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index cba9407ae..afe719ffe 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -302,6 +302,7 @@ pub struct ImportResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> { module_path: &'mod_lifetime str, imports: HashMap, declared: HashMap>, + priv_modules: HashSet, } impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'crate_lft> { fn process_use_intern(imports: &mut HashMap, u: &syn::UseTree, partial_path: &str, mut path: syn::punctuated::Punctuated) { @@ -368,6 +369,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr Self::insert_primitive(&mut imports, "Option"); let mut declared = HashMap::new(); + let mut priv_modules = HashSet::new(); for item in contents.iter() { match item { @@ -395,11 +397,14 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr declared.insert(t.ident.clone(), DeclType::Trait(t)); } }, + syn::Item::Mod(m) => { + priv_modules.insert(m.ident.clone()); + }, _ => {}, } } - Self { module_path, imports, declared } + Self { module_path, imports, declared, priv_modules } } pub fn get_declared_type(&self, ident: &syn::Ident) -> Option<&DeclType<'crate_lft>> { @@ -458,6 +463,8 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } else { Some(imp.clone()) } + } else if let Some(_) = self.priv_modules.get(&first_seg.ident) { + Some(format!("{}::{}{}", self.module_path, first_seg.ident, remaining)) } else { None } } } -- 2.39.5