Skip to content

Replace stringly-typed item names with proper paths, making things more type-safe - #201

Closed
regexident wants to merge 5 commits into
mozilla:masterfrom
regexident:fix/path-all-the-things
Closed

regexident wants to merge 5 commits into
mozilla:masterfrom
regexident:fix/path-all-the-things

Conversation

@regexident

@regexident regexident commented Aug 22, 2018 •

Copy link
Copy Markdown
Contributor

If we ever want to fix #7 ("Handle fully qualified paths and use aliases"), we need to replace Item's reliance on its self.name field for type resolving with a proper self.path field holding a qualified path.

This PR replaces pub name: String on implementors of Item with pub path: Path, while (for now!) changing Path from type Path = String to struct Path { name: String }.

While this does not fix any existing type resolution bugs (let alone #7), it levels the field for any future fixes thereof and makes cbindgen more robust overall, in regards to type-safety.

Important: It also changes how cbindgen does renaming from changing its actual self.name field to having an auxiliary self.export_name field (that by default is set to be equal to the last path segment) which then is used in fn write(…). This way we don't burn any bridges during the renaming phase and keep all the precious type information intact.

Note: Given that this PR touches pretty much every file in significant ways I'd rather merge all the existing (non-stale and acceptable) PRs, before merging this one. I'll then make sure to rebase this one accordingly.

@regexident regexident changed the title Replaced Items’ String-based names with proper Paths Replace Items’ String-based names with proper Paths Aug 22, 2018
@regexident regexident changed the title Replace Items’ String-based names with proper Paths Replace item names with proper paths, making things more type-safe Aug 22, 2018
@regexident regexident changed the title Replace item names with proper paths, making things more type-safe Replace stringly-typed item names with proper paths, making things more type-safe Aug 22, 2018
@regexident
regexident force-pushed the fix/path-all-the-things branch from d75deb2 to 0ffd0f6 Compare August 22, 2018 22:39
@regexident
regexident force-pushed the fix/path-all-the-things branch 2 times, most recently from 6e17c5b to faab95f Compare September 24, 2018 10:29
@regexident
regexident force-pushed the fix/path-all-the-things branch from faab95f to 1bac2b9 Compare October 5, 2018 07:45
@regexident

Copy link
Copy Markdown
Contributor Author

@eqrion I just did a rebase. Do you think we could merge this in, now that we're basically down to PR inbox-zero?

@eqrion

eqrion commented Oct 5, 2018

Copy link
Copy Markdown
Collaborator

@regexident You may be interested in this.

Bu yes, I will take a look at this PR soon because it'd be good to get it merged either way.

@regexident

Copy link
Copy Markdown
Contributor Author

Looks like my rebase broke cargo fmt --check. Fixed.

Interesting read, @eqrion!

I very much agree with the points you're making. Looking forward to contributing to rust-ffi then!

I'd still like to merge this PR though, as it makes the whole renaming logic more explicit, even if the String to Path transition stays practically ineffective.

@eqrion eqrion left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did a pass through this. Some of my questions might have obvious answers, there are a lot of changes so I didn't have time to look closely at everything.

The changes look good though, it'll be nice to have more typesafety here.

Comment thread src/bindgen/ir/constant.rs Outdated
) -> Self {
let export_name = path.name().to_owned();
Self {
path: path,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it might be more idiomatic rust to change path: path to path in this constructor as they all have the same name. Ditto for all the other field initializers.

Comment thread src/bindgen/ir/generics.rs Outdated
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Generic {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a reason you renamed this from GenericPath to just Generic? I think I prefer the old name and keeping it in path.rs, but it's not super important.

Comment thread src/bindgen/ir/global.rs Outdated
) -> Self {
let export_name = path.name().to_owned();
Self {
path: path,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: drop the duplicated names for field initializers

Comment thread src/bindgen/ir/opaque.rs
impl OpaqueItem {
pub fn new(
name: String,
pub fn load(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a reason this function is needed? Seeing as it just returns Ok(OpaqueItem::new(..))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's merely to make OpaqueItem be more in line with the rest of ir::.

And to hide the …

…
LiteralExpr::load(&item.expr)?,
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
AnnotationSet::load(&item.attrs)?,
Documentation::load(&item.attrs),
…

… shenanigans. Which is what all others are doing in fn load(…), too.

Comment thread src/bindgen/ir/ty.rs Outdated
&syn::Type::Array(syn::TypeArray {
ref elem,
len: syn::Expr::Path(ref path),
len: syn::Expr::Path(syn::ExprPath { ref path, .. }),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I haven't looked at the syn docs, but why is this change needed?

Comment thread src/bindgen/ir/ty.rs Outdated

if let Some(prim) = PrimitiveType::maybe(&path.name) {
if path.generics.len() > 0 {
// FIXME: this `generic_path.name()` seems wrong!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you explain what's wrong about this check?

Comment thread src/bindgen/ir/ty.rs Outdated

let path = GenericPath::load(&path.path)?;
let len = ArrayLength::Name(path.name);
// FIXMe: An array's length cannot really be generic, or can it?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Kind of, I think you can reference associated constants of generic paths.

struct Foo<T> {
  data: T,
}
trait TraitConst {
  const Value: i32;
}
impl<T> TraitConst for Foo<T> {
  const Value: i32 = 1;
}
fn main() {
  let x = [i32; Foo::<i32>::Value as usize];
}

I don't think we support the actual semantic structure of rust paths, which are arbitrary sequences of idents, generic specifiers, and lifetime specifiers. So in the current state of things, it makes sense for this to just use Path.

Comment thread src/bindgen/ir/ty.rs Resolved
Comment thread src/bindgen/ir/ty.rs Outdated
if let Some(items) = library.get_items(&path.name) {
if !out.items.contains(&path.name) {
out.items.insert(path.name.clone());
let path = generic.path().clone();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this copy necessary?

Comment thread src/bindgen/ir/ty.rs Outdated
}

if let Some(items) = library.get_items(&path.name) {
let path = generic.path().clone();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this copy necessary?

@regexident
regexident force-pushed the fix/path-all-the-things branch from a0a1643 to 7477b3b Compare October 19, 2018 15:08
@regexident

regexident commented Oct 19, 2018 •

Copy link
Copy Markdown
Contributor Author

@eqrion I'm having a rather hard time keeping up with all the recently merged PRs.

Could we focus on getting this rebased and merged, before merging any further PRs, or else just abandon it altogether?

With both branches diverging more and more with every PR rebasing is slowly becoming unmanageable. :(

@eqrion

eqrion commented Oct 22, 2018

Copy link
Copy Markdown
Collaborator

@regexident That's been my fault, I'm sorry. Let's get this merged.

I rebased and updated your PR to master. Fixed a couple of test failures from the rebasing, and generally reviewed it. Would you be able to take a look at it and give it a rubber stamp?

I'm not sure if I can update your PR here so I opened a new one. See #233.

@eqrion

eqrion commented Oct 22, 2018

Copy link
Copy Markdown
Collaborator

I'm going to close this in favor of #233.

@eqrion eqrion closed this Oct 22, 2018
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.

Handle fully qualified paths and use aliases

2 participants