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

make mod_path() consistent with/without nightly feature #2330

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

bobozaur
Copy link
Contributor

The nightly version of the mod_path() function does not do the same thing as the one reading Cargo.toml.

We'll only ever get the crate name from Cargo.toml, but the core::module_path!() macro will return the module path, with components separated by ::, as the Rust convention goes.

The problem is that the crate name is the first element of core::module_path!() macro, and the current impl does not consider that. This might not have been visible if tested from the lib.rs of a crate, since the module_path == crate_name, but UniFFI objects in nested modules will pose issues.

If the desire is to hold on to the full module path, then we could at least replace the :: with _ as otherwise syn complains that identifiers are not valid. Let me know if that's a better approach (though I think the behavior should be consistent with the non-nightly version).

@bobozaur bobozaur requested a review from a team as a code owner November 26, 2024 15:45
@bobozaur bobozaur requested review from jeddai and removed request for a team November 26, 2024 15:45
@jplatte
Copy link
Collaborator

jplatte commented Nov 26, 2024

The current implementation returns a module path with that feature on purpose, that's why the function is named as it is named. It should not be changed to flatten that into a single identifier.

If the scaffolding generation doesn't work with the nightly feature, that's a bug. If some backend doesn't work with the nightly feature, you can try to adjust the backend to support it.

The intent with this feature was to allow uniffi to generate bindings that consist of multiple modules, mirroring the Rust-side structure.
Maybe there should be an extra codegen-time runtime flag for this though, so users have to opt in to this more explicitly.

@bobozaur
Copy link
Contributor Author

The current implementation returns a module path with that feature on purpose, that's why the function is named as it is named. It should not be changed to flatten that into a single identifier.

That's why I provided the alternative. It's not a backend issue, but a scaffolding issue. As I described, identifiers seem to be created through the syn crate and this will especially fail on function names because the mod_path is used as namespace and something like format!("uniffi_{namespace}_fn_init_callback_vtable_{callback_interface_name}") won't play nice with my_crate::some_module::some_other_module.

I'll adjust the PR to use the proposed alternative, which is replacing the columns with an underscore.

Copy link
Collaborator

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but this still changes the function when the bug is in some of its usages. Specifically it seems like ffi_names.rs is treating the module path like something that can be interpolated into a symbol name verbatim, and the same seems to be true in setup_scaffolding.rs for some format_ident! invocations.

All of the other uses of this function are correct and should stay the way they are.

I think there used to be some code there that replaced :: with __ (double underscore), to make it a little more bullet-proof against name collisions. I'm guessing a refactoring changed that.

@bobozaur
Copy link
Contributor Author

@jplatte This seems to be the commit that changed this. The return type used to be a Vec of path components and was changed to a regular String.

I can take this on but since my ideas do not seem right, what's the expected behavior here? Should mod_path() go back to return a Vec? If so, should the items then be concatenated with underscores to create the namespace?

How certain are you that the other usages of the function are correct?

@jplatte
Copy link
Collaborator

jplatte commented Nov 26, 2024

I don't think it would be necessary to go back to Vec<_>, you could also .replace("::", "__") in the relevant places.

Anyways, let's wait to hear what @bendk thinks. Good thing he authored that change, because he should be the most knowledgable person on the scaffolding generation in general :)

@bobozaur bobozaur force-pushed the fix-mod-path-nightly branch from 2b1a18a to 983fd7a Compare November 26, 2024 17:47
@bobozaur
Copy link
Contributor Author

@jplatte I followed your advice and only normalized the usage of mod_path() in function names. Hopefully this is satisfactory 😄 .

I assume the reason the module path is meant to be used as-is is that the MetadataBuffer is meant to display the module path with the double colon notation. To be fair, I did encounter this for function names, so this makes sense.

Thanks for the guidance! Let me know if there's something else to change.

@jplatte jplatte requested a review from bendk November 26, 2024 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants