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

Rustdoc: No mention of copy trait for primitive types #25893

Closed
azerupi opened this issue May 30, 2015 · 7 comments
Closed

Rustdoc: No mention of copy trait for primitive types #25893

azerupi opened this issue May 30, 2015 · 7 comments
Labels
C-bug Category: This is a bug. T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@azerupi
Copy link
Contributor

azerupi commented May 30, 2015

As a new rust user I have struggled with a little problem: http://stackoverflow.com/questions/30540419/rust-ownership-copy-clone-trait

The ownership and borrowing rules are well explained in the docs but the copy trait and the types that implement them are not!

In the doc http://doc.rust-lang.org/std/marker/trait.Copy.html primitive types are not listed as copy trait implementors. They should at least be mentioned there.

It would be good to also add a little more info here: http://doc.rust-lang.org/book/ownership.html#copy-types Maybe give some examples like in my question on stackoverflow:

The following two snippets of code only compile because i32 and bool types both implement the copy trait. Meaning that the ownership is not moved by default but copied instead

fn main() {
    let a = 5;

    let _y = double(a);
    println!("{}", a);
}

fn double(x: i32) -> i32 {
    x * 2
}
fn main() {
    let a = true;

    let _y = change_truth(a);
    println!("{}", a);
}

fn change_truth(x: bool) -> bool {
    !x
}

This is very important since it is an exception to the most important rule of the language

Let me know if I have to make a pull request with a "change proposal" :)

@Manishearth Manishearth added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. I-papercut labels May 30, 2015
Manishearth added a commit to Manishearth/rust that referenced this issue Jun 9, 2015
As mentioned in rust-lang#25893 the copy trait is not very well explained for beginners. There is no clear mention that all primitive types implement the copy trait and there are not a lot of examples. 

With this change I try to make it more visible and understandable for new users. 

I myself have struggled with this, see [my question on stackoverflow](http://stackoverflow.com/questions/30540419/why-are-booleans-copyable-even-though-the-documentation-doesnt-indicate-that). And I want to make it more transparent for others. 

I filed issue rust-lang#25893 but I thought that I could give it a shot myself to relieve some of the work from the devs :)

If it is not well written or there are some changes to be made before it can be merged, let me know.

Cheers,
Mathieu
@steveklabnik
Copy link
Member

Triage: no real change.

@shepmaster
Copy link
Member

This continues to confuse newcomers.

@steveklabnik steveklabnik added T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue. and removed T-tools labels May 18, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@steveklabnik
Copy link
Member

@scottmcm
Copy link
Member

This just confused me for the stable-soon never type, where I think it's likely that even experienced rustaceans would be wondering about implemented traits.

@shepmaster
Copy link
Member

shepmaster commented Mar 15, 2018

PR #48171 is working on it 😉

FraGag added a commit to FraGag/rust that referenced this issue Mar 27, 2018
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:

* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
  `u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)

These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.

For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.

The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.

For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.

The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.

This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.

Fixes rust-lang#25893
bors added a commit that referenced this issue Apr 4, 2018
Better document the implementors of Clone and Copy

There are two parts to this change. The first part is a change to the compiler and to the standard library (specifically, libcore) to allow implementations of `Clone` and `Copy` to be written for a subset of builtin types. By adding these implementations to libcore, they now show up in the documentation. This is a [breaking-change] for users of `#![no_core]`, because they will now have to supply their own copy of the implementations of `Clone` and `Copy` that were added in libcore.

The second part is purely a documentation change to document the other implementors of `Clone` and `Copy` that cannot be described in Rust code (yet) and are thus provided by the compiler.

Fixes #25893
@vext01
Copy link
Contributor

vext01 commented May 18, 2018

Is it me, or do the docs still not mention the Copy trait in (e.g.) usize and ptr?

@shepmaster
Copy link
Member

do the docs still not mention the Copy

Rust has a release cadence of 6 weeks. That commit was merged on Apr 4, starting on the nightly release. Rust 1.26 stable was released on Thu May 10 2018 and this was promoted to beta. You can see Copy in those docs.

Rust 1.27 stable will be released on Thu Jun 21 2018, at which time this will show up in those docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-dev-tools Relevant to the dev-tools subteam, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants