Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply input module visibility to output module #284

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion crates/swift-bridge-ir/src/codegen/generate_rust_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod vec;
impl ToTokens for SwiftBridgeModule {
fn to_tokens(&self, tokens: &mut TokenStream) {
let mod_name = &self.name;
let vis = &self.vis;
let swift_bridge_path = &self.swift_bridge_path;

let mut extern_rust_fn_tokens = vec![];
Expand Down Expand Up @@ -298,7 +299,7 @@ impl ToTokens for SwiftBridgeModule {
let t = quote! {
#[allow(non_snake_case)]
#(#module_attributes)*
mod #mod_name {
#vis mod #mod_name {
#module_inner
}
};
Expand Down Expand Up @@ -1045,4 +1046,20 @@ mod tests {
&expected_fn,
);
}

/// Verify that we apply the module's visibility to the output.
#[test]
fn module_visibility() {
let start = quote! {
pub(super) mod foo {
}
};
let expected = quote! {
#[allow(non_snake_case)]
pub(super) mod foo {
}
};

assert_tokens_eq(&parse_ok(start).to_token_stream(), &expected);
}
}
3 changes: 2 additions & 1 deletion crates/swift-bridge-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![deny(missing_docs)]

use proc_macro2::Ident;
use syn::Path;
use syn::{Path, Visibility};

use crate::bridge_module_attributes::CfgAttr;
use crate::parse::TypeDeclarations;
Expand Down Expand Up @@ -58,6 +58,7 @@ const SWIFT_BRIDGE_PREFIX: &'static str = "__swift_bridge__";
/// ```
pub struct SwiftBridgeModule {
name: Ident,
vis: Visibility,
types: TypeDeclarations,
functions: Vec<ParsedExternFn>,
swift_bridge_path: Path,
Expand Down
2 changes: 2 additions & 0 deletions crates/swift-bridge-ir/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Parse for SwiftBridgeModuleAndErrors {

if let Ok(item_mod) = input.parse::<ItemMod>() {
let module_name = item_mod.ident;
let vis = item_mod.vis;

let mut functions = vec![];
let mut type_declarations = TypeDeclarations::default();
Expand Down Expand Up @@ -125,6 +126,7 @@ impl Parse for SwiftBridgeModuleAndErrors {

let module = SwiftBridgeModule {
name: module_name,
vis,
types: type_declarations,
functions,
swift_bridge_path: syn::parse2(quote! { swift_bridge }).unwrap(),
Expand Down
Loading