Repository navigation
Replace stringly-typed item names with proper paths, making things more type-safe - #201
regexident wants to merge 5 commits into
Conversation
Items’ String-based names with proper PathsItems’ String-based names with proper Paths
Items’ String-based names with proper Pathsd75deb2 to
0ffd0f6
Compare
6e17c5b to
faab95f
Compare
faab95f to
1bac2b9
Compare
|
@eqrion I just did a rebase. Do you think we could merge this in, now that we're basically down to PR inbox-zero? |
|
@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. |
|
Looks like my rebase broke 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 |
eqrion
left a comment
There was a problem hiding this comment.
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.
| ) -> Self { | ||
| let export_name = path.name().to_owned(); | ||
| Self { | ||
| path: path, |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
| pub struct Generic { |
There was a problem hiding this comment.
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.
| ) -> Self { | ||
| let export_name = path.name().to_owned(); | ||
| Self { | ||
| path: path, |
There was a problem hiding this comment.
nit: drop the duplicated names for field initializers
| impl OpaqueItem { | ||
| pub fn new( | ||
| name: String, | ||
| pub fn load( |
There was a problem hiding this comment.
Is there a reason this function is needed? Seeing as it just returns Ok(OpaqueItem::new(..))
There was a problem hiding this comment.
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.
| &syn::Type::Array(syn::TypeArray { | ||
| ref elem, | ||
| len: syn::Expr::Path(ref path), | ||
| len: syn::Expr::Path(syn::ExprPath { ref path, .. }), |
There was a problem hiding this comment.
I haven't looked at the syn docs, but why is this change needed?
|
|
||
| if let Some(prim) = PrimitiveType::maybe(&path.name) { | ||
| if path.generics.len() > 0 { | ||
| // FIXME: this `generic_path.name()` seems wrong! |
There was a problem hiding this comment.
Could you explain what's wrong about this check?
|
|
||
| let path = GenericPath::load(&path.path)?; | ||
| let len = ArrayLength::Name(path.name); | ||
| // FIXMe: An array's length cannot really be generic, or can it? |
There was a problem hiding this comment.
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.
| 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(); |
| } | ||
|
|
||
| if let Some(items) = library.get_items(&path.name) { | ||
| let path = generic.path().clone(); |
a0a1643 to
7477b3b
Compare
|
@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. :( |
|
@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. |
|
I'm going to close this in favor of #233. |
If we ever want to fix #7 ("Handle fully qualified paths and
usealiases"), we need to replaceItem's reliance on itsself.namefield for type resolving with a properself.pathfield holding a qualified path.This PR replaces
pub name: Stringon implementors ofItemwithpub path: Path, while (for now!) changingPathfromtype Path = Stringtostruct 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.namefield to having an auxiliaryself.export_namefield (that by default is set to be equal to the last path segment) which then is used infn 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.