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

Canonicalize a type (pointer type) first before checking for constness #1311

Merged
merged 4 commits into from
May 14, 2018

Conversation

cynecx
Copy link
Contributor

@cynecx cynecx commented May 10, 2018

This fixes cases like:

int sodium_munlock(int* const addr); // addr itself is const but the pointer points to mutable data

Before fix:

extern "C" {
    pub fn sodium_munlock(
        addr: *const ::std::os::raw::c_int,
    ) -> ::std::os::raw::c_int;
}

After this patch:

extern "C" {
    pub fn sodium_munlock(
        addr: *mut ::std::os::raw::c_int,
    ) -> ::std::os::raw::c_int;
}

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.

Thanks a lot for submitting this patch!

This is one of the bugs that I never got enough time or motivation to look into properly. Just have minor comments and suggestions, the fix is right in any case.

@@ -1194,7 +1194,23 @@ impl Type {
};

let name = if name.is_empty() { None } else { Some(name) };
let is_const = ty.is_const();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this works, but instead the better fix is (I think), changing the following line:

https://github.com/rust-lang-nursery/rust-bindgen/blob/515ca97b462f348b7b3bb9eaea98b2e0bb1b624b/src/codegen/mod.rs#L3057

To not check self.is_const(), pointing out that pointer-constness itself can't be represented in rust.

Or in any case that line could be cleaned up.

I think handling it in codegen is better though, since there are a couple other places where we use libclang's is_const in context.rs.

Also, could you test this with constants? This would solve #511. The test-case in that issue's description is pretty good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mhhh. It's somewhat strange, I tried to apply your suggestion but I am getting test failures. It seems that ctx.resolve_type(inner) resolves to an opaque type? I am not sure though, I didn't have much time to debug this (I guess, I'll give it another shot later today).

But this PR should also fix #511 (Well, it seems like it).

Copy link
Contributor

Choose a reason for hiding this comment

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

If you get stuck let me know, we can merge this for now and I can take a look at that more carefully. Please do file an issue for that if we leave it as a followup and add the tests for #511 if you can though.

Thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think that's best for now. I'll create a new issue after this PR is merged.
I've also pushed the test from issue #511.

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.

Ok, sounds good, thank you!

@emilio
Copy link
Contributor

emilio commented May 14, 2018

@bors-servo r+

@bors-servo
Copy link

📌 Commit 1fc8172 has been approved by emilio

@bors-servo
Copy link

⌛ Testing commit 1fc8172 with merge 74dcb20...

bors-servo pushed a commit that referenced this pull request May 14, 2018
 Canonicalize a type (pointer type) first before checking for constness

This fixes cases like:

```cpp
int sodium_munlock(int* const addr); // addr itself is const but the pointer points to mutable data
```

Before fix:

```rust
extern "C" {
    pub fn sodium_munlock(
        addr: *const ::std::os::raw::c_int,
    ) -> ::std::os::raw::c_int;
}

```

After this patch:
```rust
extern "C" {
    pub fn sodium_munlock(
        addr: *mut ::std::os::raw::c_int,
    ) -> ::std::os::raw::c_int;
}
```
@bors-servo
Copy link

☀️ Test successful - status-travis
Approved by: emilio
Pushing 74dcb20 to master...

@bors-servo bors-servo merged commit 1fc8172 into rust-lang:master May 14, 2018
@emilio
Copy link
Contributor

emilio commented May 14, 2018

I filed #1312 with the followup. It required a few fixes indeed. Thanks for working on this!

bors-servo pushed a commit that referenced this pull request May 15, 2018
ir: Handle *const T at the codegen level.

Followup to #1311.
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.

4 participants