-
Notifications
You must be signed in to change notification settings - Fork 742
Feature 699 constified enum module #741
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
Merged
bors-servo
merged 9 commits into
rust-lang:master
from
tmfink:feature-699-constified-enum-module
Jun 21, 2017
+776
−17
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5762339
Adds `--constified-enum-module` option per #699
tmfink 0826846
Convert comment to a doc comment
tmfink fb9959a
Add typedef/namespace tests for module const enum
tmfink 977ec6f
Add more constified module enum tests
tmfink de155b9
Add constified module enum template test
tmfink 5f4b730
Refactor namespace code and test
tmfink 4b10529
Fix recursive aliases to const module enum
tmfink 465e242
Test const mod enum variants shadowing "Type"
tmfink 814d28e
Simplify is_constified_enum_module
tmfink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//! Bindgen's core intermediate representation type. | ||
|
||
use super::super::codegen::CONSTIFIED_ENUM_MODULE_REPR_NAME; | ||
use super::annotations::Annotations; | ||
use super::context::{BindgenContext, ItemId, PartialType}; | ||
use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault}; | ||
|
@@ -825,6 +826,50 @@ impl Item { | |
_ => None, | ||
} | ||
} | ||
|
||
/// Returns whether the item is a constified module enum | ||
fn is_constified_enum_module(&self, ctx: &BindgenContext) -> bool { | ||
if let ItemKind::Type(ref type_) = self.kind { | ||
// Do not count an "alias of an alias" as a constified module enum; | ||
// otherwise, we will get: | ||
// pub mod foo { | ||
// pub type Type = ::std::os::raw::c_uint; | ||
// ... | ||
// } | ||
// pub use foo::Type as foo_alias1; | ||
// pub use foo_alias1::Type as foo_alias2; | ||
// pub use foo_alias2::Type as foo_alias3; | ||
// ... | ||
// | ||
// (We do not want the '::Type' appended to the alias types; only the base type) | ||
if let TypeKind::Alias(inner_id) = *type_.kind() { | ||
let inner_item = ctx.resolve_item(inner_id); | ||
if let ItemKind::Type(ref inner_type) = *inner_item.kind() { | ||
match *inner_type.kind() { | ||
TypeKind::Alias(..) => { return false; } | ||
TypeKind::ResolvedTypeRef(resolved_id) => { | ||
// We need to handle: | ||
// Alias -> ResolvedTypeRef -> Alias | ||
let resolved_item = ctx.resolve_item(resolved_id); | ||
if let ItemKind::Type(ref resolved_type) = *resolved_item.kind() { | ||
if let TypeKind::Alias(..) = *resolved_type.kind() { | ||
return false; | ||
} | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
if let Some(ref type_) = type_.safe_canonical_type(ctx) { | ||
if let TypeKind::Enum(ref enum_) = *type_.kind() { | ||
return enum_.is_constified_enum_module(ctx, self); | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
/// A set of items. | ||
|
@@ -1443,14 +1488,25 @@ impl ItemCanonicalPath for Item { | |
fn namespace_aware_canonical_path(&self, | ||
ctx: &BindgenContext) | ||
-> Vec<String> { | ||
let path = self.canonical_path(ctx); | ||
if ctx.options().enable_cxx_namespaces { | ||
return path; | ||
} | ||
let mut path = self.canonical_path(ctx); | ||
|
||
// ASSUMPTION: (disable_name_namespacing && cxx_namespaces) | ||
// is equivalent to | ||
// disable_name_namespacing | ||
if ctx.options().disable_name_namespacing { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this branch is correct. |
||
return vec![path.last().unwrap().clone()]; | ||
// Only keep the last item in path | ||
let split_idx = path.len() - 1; | ||
path = path.split_off(split_idx); | ||
} else if !ctx.options().enable_cxx_namespaces { | ||
// Ignore first item "root" | ||
path = vec![path[1..].join("_")]; | ||
} | ||
return vec![path[1..].join("_")]; | ||
|
||
if self.is_constified_enum_module(ctx) { | ||
path.push(CONSTIFIED_ENUM_MODULE_REPR_NAME.into()); | ||
} | ||
|
||
return path; | ||
} | ||
|
||
fn canonical_path(&self, ctx: &BindgenContext) -> Vec<String> { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what you're trying to do here is resolving through
ResolvedTypeRef
s, but not throughAlias
, right?If so, instead of reinventing the wheel, you can do something like:
And keep the comment on why not through type alias.
We should arguably unify that and the
canonical_type
business, but that also handles some template stuff that may be nontrivial, so it's worth doing on a followup.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check needs to "peel back" one layer of
Alias
es. The issue with theresolve()
function is that it resolves through all of theAlias
es.I think that implementation should remain as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... I see. I guess I'm not sure why would making an alias claim it's a constified enum module be necessary.
I guess I'll poke a bit at the code this evening when I have the chance and see which tests break with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... Ok, so what breaks in that case is the
typedef enum
case... fun. I still think we should be able to do better, though...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about modifying
ItemResolver
to optionally disable recursive resolving? More generally,ItemResolver
could even take a positive integer indicating through how many layers to resolve.This way,
is_constified_enum_module()
could be simplified to useItemResolver
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is about the number of layers to resolve. I think the logic in
is_constified_enum_module
is wrong, and the "alias to resolved type ref to alias" logic is just masking it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what I came up with, I think this is what you're really trying to do with that function:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to address the
TODO
if you want, btw :)