Skip to content

Make clang::Cursor::specialized return an Option #191

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
merged 3 commits into from
Nov 3, 2016

Conversation

glasserc
Copy link
Contributor

@glasserc glasserc commented Nov 2, 2016

Fixes #122.

pair=@Natim

Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

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

The rest looks great, thanks for doing this! :)

@@ -187,7 +187,7 @@ impl Cursor {

/// Is the referent a template specialization?
pub fn is_template(&self) -> bool {
self.specialized().is_valid()
self.specialized().map_or(false, |c| c.is_valid())
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be self.specialized().is_some() now, given we check if the cursor is valid in specialized.

Copy link
Member

Choose a reason for hiding this comment

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

Checking is_some() will only work after my requested changes to the is_valid() checking. This is a good insight, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Whoops, I misread the implementation, but yeah :)

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Overall, looks good! Need a tweak to the is_valid() checking, though. See comment below, and don't hesitate to ask me for calrification if it doesn't make sense.

pub fn specialized(&self) -> Cursor {
pub fn specialized(&self) -> Option<Cursor> {
if !self.is_valid() {
return None;
Copy link
Member

Choose a reason for hiding this comment

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

Rather than checking if self.is_valid(), we should make the FFI call to clang, and then check if the result of that FFI call .is_valid() and return None/Some based on that.

Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I misread the issue description. I thought the concern was that the existing Cursor was valid, not that the resulting one was valid. Thanks for the correction!

@@ -187,7 +187,7 @@ impl Cursor {

/// Is the referent a template specialization?
pub fn is_template(&self) -> bool {
self.specialized().is_valid()
self.specialized().map_or(false, |c| c.is_valid())
Copy link
Member

Choose a reason for hiding this comment

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

Nice :)

Thanks @fitzgen for the correction.

This also allows us to simplify the is_template method. Thanks @emilio
for the suggestion.
Copy link
Contributor

@emilio emilio left a comment

Choose a reason for hiding this comment

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

Looks good!

x: clang_getSpecializedCursorTemplate(self.x),
})
let clang_specialized = clang_getSpecializedCursorTemplate(self.x);
if clang_isInvalid(clang_getCursorKind(clang_specialized)) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe:

let ret = Cursor {
    x: clang_getSpecializedCursorTemplate(self.x),
};

if ret.is_valid() { Some(ret) } else { None }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about doing that, but it made me feel uncomfortable to have a Cursor object that didn't necessarily correspond to a real cursor. Also, I misunderstood what @fitzgen said in his comment above. But I agree that it's better to reuse the definition of is_valid. Let me make that change.

Thanks @emilio for the suggestion.
@emilio
Copy link
Contributor

emilio commented Nov 3, 2016

@bors-servo r+

@bors-servo
Copy link

📌 Commit 94136d2 has been approved by emilio

@emilio
Copy link
Contributor

emilio commented Nov 3, 2016

Thanks for doing this! :)

@bors-servo
Copy link

⌛ Testing commit 94136d2 with merge b285185...

bors-servo pushed a commit that referenced this pull request Nov 3, 2016
Make clang::Cursor::specialized return an Option

Fixes #122.

pair=@Natim
@bors-servo
Copy link

☀️ Test successful - status-travis

@bors-servo bors-servo merged commit 94136d2 into rust-lang:master Nov 3, 2016
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.

5 participants