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

Lint for the reserved ABI of bool #46176

Closed
wants to merge 1 commit into from
Closed

Conversation

est31
Copy link
Member

@est31 est31 commented Nov 21, 2017

The ABI of bool is reserved, see these links:

Currently the improper_ctypes lint is inconsistent
with that position by treating bools as if they had
a specified ABI.

To fix this inconsistency, this changes treatment of
bools by the lint.

This might break some code, so possibly it has to
be phased in slowly...

@rust-highfive
Copy link
Collaborator

r? @michaelwoerister

(rust_highfive has picked a reviewer for you, use r? to override)

@est31
Copy link
Member Author

est31 commented Nov 21, 2017

I don't neccessarily endorse this PR myself. If the apparent inconsistency gets fixed by having bool finally specified, I would be happier.

The ABI of bool is reserved, see these links:

* rust-lang/rfcs#954 (comment)
* rust-lang#46156 (comment)
* rust-lang/rfcs#992

Currently the improper_ctypes lint is inconsistent
with that position by treating bools as if they had
a specified ABI.

To fix this inconsistency, this changes treatment of
bools by the lint.

This might break some code, so possibly it has to
be phased in slowly...
@hsivonen
Copy link
Member

As someone who has (successfully, AFAICT, on multiple platforms—the ones Firefox 56+ runs on) used bool in FFI with the assumption that it has the same representation as _Bool, I'd much prefer Rust to officially guarantee that instead of saying that booleans aren't a legitimate part of FFI. In other words, I think this shouldn't be merged.

@michaelwoerister
Copy link
Member

Nominating for discussion in the @rust-lang/compiler team meeting.

@michaelwoerister michaelwoerister added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. I-nominated labels Nov 22, 2017
@hsivonen
Copy link
Member

To elaborate: Not having a type that is equivalent to _Bool in FFI is terribly inconvenient in terms of not being able to expose an idiomatic C11 API for a Rust library directly but having to expose a legacy C API and wrap that as a C11 API on the C side. But worse, not having _Bool in FFI means that Rust can't call C11 libraries without a C-side wrapper.

As for Rust bool being the type that's equivalent to _Bool in FFI, that's already the case in practice (AFAICT), so making it not so is a needless inconvenience.

As for weird embedded C compilers having weird _Bool, I think the inconvenience should be on those who wish to link with code from such compilers instead of everyone working on normal (GCC, clang, MSVC) C compilers being inconvenienced. Rust already can't interop with all imaginable unreasonable but C-compliant C implementations due to Rust relying on two's complement representation of signed integers. Likewise, it should be OK for Rust to interop with _Bool for mainstream C only.

@kennytm kennytm added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Nov 22, 2017
@steveklabnik
Copy link
Member

steveklabnik commented Nov 22, 2017

New lints require an RFC. This kind of blurs the line.

@est31
Copy link
Member Author

est31 commented Nov 22, 2017

This is more a bugfix than a new lint addition or something like that ("feature change" etc). Unless I understood it wrongly, the purpose of the improper_ctypes lint is to check for types that have no defined ABI. Right now it seems to have missed the bool type!

@nagisa nagisa added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Nov 23, 2017
@hsivonen
Copy link
Member

hsivonen commented Nov 24, 2017

When bool works in FFI in practice today on stable channel, making it not work or making the compiler complain about it would break Rust's stability promise in practice. (Even if a working stable feature works unintentionally, making it not work or making the compiler complain about it is a breaking change: Rust programmers should be able to rely on the stability of stable Rust without researching which functionality works intentionally and which funtionality works unintentionally. Additionally, I think the existing behavior of bool working in FFI with mainstream C implementations is desirable behavior.)

@est31
Copy link
Member Author

est31 commented Nov 24, 2017

it would break Rust's stability promise in practice

It is established practice that something can be broken if it only works due to a bug. See this search. And the current behaviour is clearly a bug: the ABI of bool is not specified yet, even though the lint assumes that it is specified.

Probably this change has to be phased in slowly, as the other breaking changes, I'll change my PR if the compiler team has deemed that they want to do the change.

@bluss
Copy link
Member

bluss commented Nov 24, 2017

What you are saying is that this is not credible as a soundness bug fix, even of a lint. I understand the not credible part. On the other hand, ffi is unsafe code zone and there are many details the compiler can not check for you there.

It should only be good if we get better at this approximate and pragmatic safety work of warning about portability and safety issues in code outside of safe rust.

@hsivonen
Copy link
Member

hsivonen commented Nov 24, 2017

There's a big difference between a theoretical break to fix a soundness bug and withdrawing a practically working behavior that's in use in deployed code over a theoretical concern.

As for FFI being unsafe, the use of a particular type in a signature is not the kind of unsafe that the compiler couldn't have checked, so I don't think FFI being unsafe makes it OK to make a breaking change.

Withdrawing a working feature because it was not specified officially seems like the C approach of blaming the programmer for relying on what wasn't specified as reliable and not like Rust's stability story. ☹️

@hsivonen
Copy link
Member

As for it being just a lint: Either the lint is the first step towards breaking it, in which case it's not just a lint, or the existing stable behavior is going to continue to work in which case the lint would uselessly bother programmers on mainstream platforms.

A lint that warns all programmers that their FFI code might not link with C code produced on some niche platform that Rust doesn't support yet would be a gross misbalancing of presently relevant mainstream needs vs. potential future niche concerns.

@est31
Copy link
Member Author

est31 commented Nov 24, 2017

@hsivonen this PR is only enacting the existing policy on this issue, which is that bool has an unspecified ABI. I think you should better talk to the lang team to get the policy specified. This would be the best outcome, and then this PR can be closed.

@hsivonen
Copy link
Member

What's the right place to discuss the policy?

@hsivonen
Copy link
Member

What's the right place to discuss the policy?

I posted to the internals forum.

@est31
Copy link
Member Author

est31 commented Nov 27, 2017

@hsivonen thanks for the post!

@michaelwoerister michaelwoerister added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 30, 2017
@arielb1
Copy link
Contributor

arielb1 commented Nov 30, 2017

This is being discussed in the internals thread and needs more research.

@kennytm kennytm added S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). and removed S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels Dec 20, 2017
@rfcbot
Copy link

rfcbot commented Jan 22, 2018

Team member @withoutboats has proposed to close this. The next step is review by the rest of the tagged teams:

No concerns currently listed.

Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jan 22, 2018
@hanna-kruppe
Copy link
Contributor

Why that way around and not the alternative? (Specify how bool currently behaves, and document that this matches _Bool on all platforms we support)

@withoutboats
Copy link
Contributor

Why that way around and not the alternative? (Specify how bool currently behaves, and document that this matches _Bool on all platforms we support)

People could come to the conclusion that they need a c_bool type for their FFI to be forward compatible with platforms we don't yet support. I think defining it as the same representation as _Bool / C++ bool makes it the least likely someone does something painful to avoid entirely hypothetical problems.

@est31
Copy link
Member Author

est31 commented Jan 22, 2018

@withoutboats do I understand you correctly that this "close" FCP also functions as a "merge" FCP for #46156 ?

@withoutboats
Copy link
Contributor

I think we should merge that PR and also somewhere (the reference?) clarify precisely that it has the same representation as _Bool.

@nikomatsakis
Copy link
Contributor

@withoutboats agreed; I was going to propose the same.

@cuviper thanks, just what I wanted to know

@est31
Copy link
Member Author

est31 commented Feb 1, 2018

friendly ping @jseyfried , your mark is missing.

@nikomatsakis
Copy link
Contributor

I took the libery of tagging for @jseyfried

@rfcbot
Copy link

rfcbot commented Feb 1, 2018

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Feb 1, 2018
@est31
Copy link
Member Author

est31 commented Feb 2, 2018

Closing in favour of #46156

@luke-jr
Copy link

luke-jr commented Aug 13, 2020

Just to clarify: do we know of any cases where C++ bool and C's _Bool are treated differently?

Just to answer a few years later: It's more of a detail of C++'s std::vector than bool, but std::vector<bool> only uses one bit per bool (though this is optional in the standard).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.