-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add Share kind #11781
Comments
What's the thought behind replacing IIUC the idea, |
Yes, |
cc me. I am increasingly of the opinion that |
@brson: It's not necessary for any semantic reason that I know of, but I was planning on using it as the basis for type-based alias analysis ( @flaper87: A |
See also the post Data Parallelism in Rust. |
I'm interested in working on this! |
@flaper87 OK. You've got it. This may be as easy as renaming @nikomatsakis I am worried that Rust allows some types to be unsafely shared by default. Would making unsafe pointers unsharable by default prevent this? |
Accepted for 1.0, assigning P-high. |
(assigning E-mentor in hopes that @nikomatsakis has spare cycles to act as a mentor here... or someone else could...) |
I will mentor. @brson that's an interesting thought, maybe we can make unsafe sharing opt-in in this way |
|
I think we want Freeze to stay. it's not clear to me that Send and Freeze have any inherent relationship. I think the breakdown is:
I don't know that any of those three traits have any particular relationship to one another:
For many cases, then, multiple bound are probably required:
OK, gotta go, maybe more writeup / thought is needed here than I previously thought! Note: In the past I've considered a fourth builtin bound |
@nikomatsakis While the relationship between them is an interesting question, I think the more significant question is: in what use cases do you want |
On Thu, Feb 20, 2014 at 01:33:27AM -0800, Gábor Lehel wrote:
Hmm, interesting.
At the moment I can't recall but I feel like I've come up with several |
#12432 seems like a clear example |
@glaebhoerl ah yes, thanks. I think that's the rather convincing one I was thinking of. |
Editing the title. I believe that the current def'n of Freeze in |
I've been thinking about this more. Based on some recent IRC conversations, I believe the correct meaning of
The reason for including the clause "via But I'm not sure. Maybe the simpler definitions (without the
Here threadsafe is defined as "accessible from multiple threads without data races". Also, when I say "all data reachable from |
Specifically, Freeze currently excludes |
Actually, it is if it would be valuable to include |
Need more formalism ;) |
I agree with @nikomatsakis on pretty much everything he said in the previous 3 comments. EDIT: Commented here |
Under the expanded definitions of |
@glaebhoerl I think it is not possible without unsafe code. |
@glaebhoerl though of course anything including a |
of course, and I don't think it has any significance, I was just curious |
I've been thinking this over. My current opinion is that
The interior is defined as all content reachable without traversing a pointer. Threadsafe is defined as "exposing no operation that permits a data race if multiple threads have access to a In both cases, when we say "immutable" or "threadsafe", we are concerned with the public API that a type exports. In other words, implementing Freeze or Share (presuming opt-in) is a contract between the module that defines the module and its non-descendants. Therefore, Without using unsafe code, all types are Motivation for these definitions:
Possible variationsWhile writing this, I was wondering if perhaps this definition isn't quite right, particularly concerning Freeze. This is current definition is sufficient to help LLVM optimization, but a stronger definition -- such as ownership or reachability? -- might enable the Rust front-end to perform more aggressive optimization. I am not sure, though. I was contemplating things like reordering function calls or pulling them out of loops, but given thread-local data, side-effects, and so on it's unclear if this would ever be safe unless we've analyzed the body of those calls. And if we've done that, then the Freeze type is probably not particularly helpful. So I'm still leaning towards this being the right set of definitions. |
Some further clarifications and notes:
|
Created #12683 to cover |
@nikomatsakis I agree with pretty much everything you said in your last comment. I just want to put some emphasis in some of the points that had me thinking when I started working on the
Sounds right. One point though, which probably doesn't conflicts with this definition, is that we may want to consider reachability for
This sounds right to me!
This means (as we said earlier on IRC) that |
`Share` implies that all *reachable* content is *threadsafe*. Threadsafe is defined as "exposing no operation that permits a data race if multiple threads have access to a &T pointer simultaneously". (NB: the type system should guarantee that if you have access to memory via a &T pointer, the only other way to gain access to that memory is through another &T pointer)... Fixes #11781 cc #12577 What this PR will do ================ - [x] Add Share kind and - [x] Replace usages of Freeze with Share in bounds. - [x] Add Unsafe<T> #12577 - [x] Forbid taking the address of a immutable static item with `Unsafe<T>` interior What's left to do in a separate PR (after the snapshot)? =========================================== - Remove `Freeze` completely
Verify Borrow<T> semantics for types that implement Hash, Borrow<str> and Borrow<[u8]>. Fixes rust-lang#11710 The essence of the issue is that types that implement Borrow<T> provide a facet or a representation of the underlying type. Under these semantics `hash(a) == hash(a.borrow())`. This is a problem when a type implements `Borrow<str>`, `Borrow<[u8]>` and Hash, it is expected that the hash of all three types is identical. The problem is that the hash of [u8] is not the same as that of a String, even when the byte reference ([u8]) is derived from `.as_bytes()` - [x] Followed [lint naming conventions][lint_naming] - [x] Added passing UI tests (including committed `.stderr` file) - [x] `cargo test` passes locally - [x] Executed `cargo dev update_lints` - [x] Added lint documentation - [x] Run `cargo dev fmt` --- - [x] Explanation of the issue in the code - [x] Tests reproducing the issue - [x] Lint rule and emission --- changelog: New lint: [`impl_hash_borrow_with_str_and_bytes`] [rust-lang#11781](rust-lang/rust-clippy#11781)
The Share kind is a type bound for types that can be safely shared across threads. This is necessary to support fork/join operations while capturing an environment safely. Nominating.
Migration Plan
Unsafe<T>
type to be used as the basis forCell
,RefCell
,Atomic
, etc and removeFreeze
#12577The text was updated successfully, but these errors were encountered: