rust icon indicating copy to clipboard operation
rust copied to clipboard

Allow more `!Copy` impls

Open fmease opened this issue 3 years ago • 4 comments

You can already implement !Copy for a lot of types (with #![feature(negative_impls)]). However, before this PR you could not implement !Copy for ADTs whose fields don't implement Copy which didn't make any sense. Further, you couldn't implement !Copy for types impl'ing Drop (equally nonsensical).

@rustbot label T-types F-negative_impls Fixes #101836.

r? types

fmease avatar Sep 15 '22 22:09 fmease

The job x86_64-gnu-llvm-13 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

6    |
7    = note: positive implementation in crate `core`
8 
- error[E0751]: found both positive and negative implementation of trait `std::marker::Copy` for type `memchr::memmem::genericsimd::Forward`:
+ error[E0751]: found both positive and negative implementation of trait `std::marker::Copy` for type `memchr::memmem::prefilter::PrefilterFn`:
10   --> $DIR/coherence-negative-impls-copy-problematic-non-adt.rs:69:1
11    |
12 LL | impl<T> !Copy for T {}

The actual stderr differed from the expected stderr.
Some tests failed in compiletest suite=ui mode=ui host=x86_64-unknown-linux-gnu target=i686-unknown-linux-gnu
Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt/coherence-negative-impls-copy-problematic-non-adt.stderr
Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt/coherence-negative-impls-copy-problematic-non-adt.stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args coherence/coherence-negative-impls-copy-problematic-non-adt.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs" "-Zthreads=1" "--target=i686-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt" "-A" "unused" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/i686-unknown-linux-gnu/native/rust-test-helpers" "-Clinker=x86_64-linux-gnu-gcc" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt/auxiliary"
stdout: none
--- stderr -------------------------------
error[E0751]: found both positive and negative implementation of trait `std::marker::Copy` for type `!`:
   |
   |
LL | impl !Copy for ! {}
   | ^^^^^^^^^^^^^^^^ negative implementation here
   = note: positive implementation in crate `core`


error[E0751]: found both positive and negative implementation of trait `std::marker::Copy` for type `memchr::memmem::prefilter::PrefilterFn`:
   |
   |
LL | impl<T> !Copy for T {}
   | ^^^^^^^^^^^^^^^^^^^ negative implementation here
   = note: positive implementation in crate `memchr`

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:44:1
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:44:1
   |
LL | impl !Copy for str {}
   | |              |
   | |              `str` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   |
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:47:1
   |
LL | impl !Copy for fn() {}
   | |              |
   | |              |
   | |              `fn()` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:50:1
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:50:1
   |
LL | impl !Copy for ! {}
   | |              |
   | |              `!` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   |
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:54:1
   |
LL | impl !Copy for <Type as Trait>::Result {}
   | |              |
   | |              |
   | |              `<Type as Trait>::Result` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:57:1
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:57:1
   |
LL | impl !Copy for [Type] {}
   | |              |
   | |              this is not defined in the current crate because slices are always foreign
   | impl doesn't use only types from inside the current crate
   |
   |
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:60:1
   |
LL | impl !Copy for &mut std::cmp::Ordering {}
   | |              |
   | |              `std::cmp::Ordering` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   |
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:63:1
   |
LL | impl !Copy for dyn std::any::Any {}
   | |              |
   | |              |
   | |              `dyn Any` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   = note: define and implement a trait or new type instead

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:66:1
  --> /checkout/src/test/ui/coherence/coherence-negative-impls-copy-problematic-non-adt.rs:66:1
   |
LL | impl !Copy for () {}
   | |              |
   | |              this is not defined in the current crate because tuples are always foreign
   | impl doesn't use only types from inside the current crate
   |
   |
   = note: define and implement a trait or new type instead

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)
   |
   |
LL | impl<T> !Copy for T {}
   |      ^ type parameter `T` must be used as the type parameter for some local type
   |
   = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
   = note: only traits defined in the current crate can be implemented for a type parameter
error: aborting due to 11 previous errors

Some errors have detailed explanations: E0117, E0210, E0751.
For more information about an error, try `rustc --explain E0117`.

rust-log-analyzer avatar Sep 16 '22 00:09 rust-log-analyzer

Okay, I've simplified my PR to make it easier to review. Once again, it's an error to impl !Copy for types like &mut Local, dyn Local and other non-ADTs.

fmease avatar Sep 16 '22 10:09 fmease

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    |
200 | ...                   if can_type_implement_copy(
    |                          ^^^^^^^^^^^^^^^^^^^^^^^
...
204 | ...                       traits::ObligationCause::dummy_with_span(span),
    |                           ---------------------------------------------- an argument of type `rustc_ast::ImplPolarity` is missing
note: function defined here
   --> /checkout/compiler/rustc_trait_selection/src/traits/misc.rs:20:8
    |
    |
20  | pub fn can_type_implement_copy<'tcx>(
help: provide the argument
    |
    |
200 |                                 if can_type_implement_copy(cx.tcx, cx.param_env, ty, /* rustc_ast::ImplPolarity */, traits::ObligationCause::dummy_with_span(span)).is_ok() {

For more information about this error, try `rustc --explain E0061`.
error: could not compile `clippy_lints` due to previous error
Build completed unsuccessfully in 0:04:03

rust-log-analyzer avatar Sep 16 '22 10:09 rust-log-analyzer

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

rustbot avatar Sep 16 '22 10:09 rustbot

r? types (am sick right now, can't review much)

compiler-errors avatar Sep 23 '22 02:09 compiler-errors

Okay, now using bug!() (just like in the Drop visitor). I've slightly simplified the implementation (making things less awkward) and now point at the self-type in all branches which is more consistent, too. I hope that's fine. I've blessed the relevant tests.

one nit, then r=me

I don't think I can do [@]bors r=lcnr without delegation.

fmease avatar Sep 23 '22 19:09 fmease

@fmease: :key: Insufficient privileges: Not in reviewers

bors avatar Sep 23 '22 19:09 bors

ready

fmease avatar Sep 23 '22 20:09 fmease

@bors r+ rollup

lcnr avatar Sep 26 '22 08:09 lcnr

:pushpin: Commit 28e0c5aec8335d34cd84f3970d633860d6bd08a9 has been approved by lcnr

It is now in the queue for this repository.

bors avatar Sep 26 '22 08:09 bors