From 698f8c2f01ea549d77d7dc3338a12e04c11057b9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:02:58 +0200 Subject: Adding upstream version 1.64.0+dfsg1. Signed-off-by: Daniel Baumann --- src/test/ui/traits/alias/ambiguous.rs | 24 + src/test/ui/traits/alias/ambiguous.stderr | 28 + src/test/ui/traits/alias/auxiliary/greeter.rs | 13 + src/test/ui/traits/alias/auxiliary/send_sync.rs | 3 + src/test/ui/traits/alias/basic.rs | 8 + src/test/ui/traits/alias/bounds.rs | 57 ++ src/test/ui/traits/alias/cross-crate.rs | 17 + src/test/ui/traits/alias/cross-crate.stderr | 31 ++ src/test/ui/traits/alias/impl.rs | 7 + src/test/ui/traits/alias/impl.stderr | 9 + src/test/ui/traits/alias/import-cross-crate.rs | 14 + src/test/ui/traits/alias/import.rs | 40 ++ .../alias/issue-60021-assoc-method-resolve.rs | 19 + .../alias/issue-72415-assoc-const-resolve.rs | 14 + src/test/ui/traits/alias/issue-75983.rs | 17 + src/test/ui/traits/alias/issue-83613.rs | 13 + src/test/ui/traits/alias/issue-83613.stderr | 23 + src/test/ui/traits/alias/maybe-bound.rs | 29 ++ src/test/ui/traits/alias/no-duplicates.rs | 126 +++++ src/test/ui/traits/alias/no-duplicates.stderr | 527 +++++++++++++++++++ src/test/ui/traits/alias/no-extra-traits.rs | 121 +++++ src/test/ui/traits/alias/no-extra-traits.stderr | 579 +++++++++++++++++++++ src/test/ui/traits/alias/object-fail.rs | 11 + src/test/ui/traits/alias/object-fail.stderr | 22 + src/test/ui/traits/alias/object-wf.rs | 85 +++ src/test/ui/traits/alias/object.rs | 18 + src/test/ui/traits/alias/only-maybe-bound.rs | 22 + src/test/ui/traits/alias/only-maybe-bound.stderr | 21 + src/test/ui/traits/alias/style_lint.rs | 8 + src/test/ui/traits/alias/style_lint.stderr | 10 + .../suggest-trait-alias-instead-of-type.fixed | 13 + .../alias/suggest-trait-alias-instead-of-type.rs | 13 + .../suggest-trait-alias-instead-of-type.stderr | 14 + src/test/ui/traits/alias/syntax-fail.rs | 10 + src/test/ui/traits/alias/syntax-fail.stderr | 26 + src/test/ui/traits/alias/syntax.rs | 24 + src/test/ui/traits/alias/wf.rs | 7 + src/test/ui/traits/alias/wf.stderr | 19 + src/test/ui/traits/alignment-gep-tup-like-1.rs | 37 ++ src/test/ui/traits/anon-static-method.rs | 15 + src/test/ui/traits/anon_trait_static_method_exe.rs | 12 + src/test/ui/traits/as-struct-constructor.rs | 6 + src/test/ui/traits/as-struct-constructor.stderr | 9 + src/test/ui/traits/assignability-trait.rs | 47 ++ src/test/ui/traits/assoc-type-in-superbad.rs | 16 + src/test/ui/traits/assoc-type-in-superbad.stderr | 15 + src/test/ui/traits/assoc-type-in-supertrait.rs | 23 + .../assoc_type_bound_with_struct.rs | 23 + .../assoc_type_bound_with_struct.stderr | 101 ++++ .../check-trait-object-bounds-1.rs | 14 + .../check-trait-object-bounds-1.stderr | 16 + .../check-trait-object-bounds-2-ok.rs | 15 + .../check-trait-object-bounds-2.rs | 15 + .../check-trait-object-bounds-2.stderr | 16 + .../check-trait-object-bounds-3.rs | 20 + .../check-trait-object-bounds-3.stderr | 15 + .../check-trait-object-bounds-4.rs | 17 + .../check-trait-object-bounds-4.stderr | 16 + .../check-trait-object-bounds-5.rs | 27 + .../check-trait-object-bounds-5.stderr | 20 + .../check-trait-object-bounds-6.rs | 24 + .../check-trait-object-bounds-6.stderr | 20 + .../ui/traits/associated_type_bound/issue-51446.rs | 34 ++ .../ui/traits/astconv-cycle-between-and-type.rs | 29 ++ src/test/ui/traits/augmented-assignments-trait.rs | 12 + .../auxiliary/anon_trait_static_method_lib.rs | 9 + src/test/ui/traits/auxiliary/go_trait.rs | 43 ++ .../auxiliary/issue_89119_intercrate_caching.rs | 60 +++ src/test/ui/traits/auxiliary/trait_safety_lib.rs | 9 + src/test/ui/traits/auxiliary/traitimpl.rs | 7 + src/test/ui/traits/bad-method-typaram-kind.rs | 14 + src/test/ui/traits/bad-method-typaram-kind.stderr | 19 + src/test/ui/traits/bad-sized.rs | 9 + src/test/ui/traits/bad-sized.stderr | 54 ++ .../traits/bound/assoc-fn-bound-root-obligation.rs | 12 + .../bound/assoc-fn-bound-root-obligation.stderr | 22 + src/test/ui/traits/bound/auxiliary/crate_a1.rs | 9 + src/test/ui/traits/bound/auxiliary/crate_a2.rs | 13 + .../bound/auxiliary/on_structs_and_enums_xc.rs | 13 + src/test/ui/traits/bound/basic.rs | 25 + src/test/ui/traits/bound/generic_trait.rs | 30 ++ .../ui/traits/bound/impl-comparison-duplicates.rs | 16 + src/test/ui/traits/bound/in-arc.rs | 108 ++++ src/test/ui/traits/bound/multiple.rs | 9 + src/test/ui/traits/bound/not-on-bare-trait.rs | 13 + src/test/ui/traits/bound/not-on-bare-trait.stderr | 31 ++ src/test/ui/traits/bound/not-on-struct.rs | 38 ++ src/test/ui/traits/bound/not-on-struct.stderr | 175 +++++++ .../ui/traits/bound/on-structs-and-enums-in-fns.rs | 20 + .../bound/on-structs-and-enums-in-fns.stderr | 27 + .../traits/bound/on-structs-and-enums-in-impls.rs | 25 + .../bound/on-structs-and-enums-in-impls.stderr | 15 + .../ui/traits/bound/on-structs-and-enums-locals.rs | 17 + .../bound/on-structs-and-enums-locals.stderr | 27 + .../ui/traits/bound/on-structs-and-enums-rpass.rs | 21 + .../ui/traits/bound/on-structs-and-enums-static.rs | 15 + .../bound/on-structs-and-enums-static.stderr | 15 + .../ui/traits/bound/on-structs-and-enums-xc.rs | 14 + .../ui/traits/bound/on-structs-and-enums-xc.stderr | 27 + .../ui/traits/bound/on-structs-and-enums-xc1.rs | 15 + .../traits/bound/on-structs-and-enums-xc1.stderr | 27 + src/test/ui/traits/bound/on-structs-and-enums.rs | 43 ++ .../ui/traits/bound/on-structs-and-enums.stderr | 99 ++++ src/test/ui/traits/bound/recursion.rs | 20 + src/test/ui/traits/bound/same-crate-name.rs | 55 ++ src/test/ui/traits/bound/same-crate-name.stderr | 75 +++ src/test/ui/traits/bound/sugar.rs | 19 + src/test/ui/traits/bound/sugar.stderr | 19 + src/test/ui/traits/bug-7183-generics.rs | 36 ++ src/test/ui/traits/bug-7295.rs | 14 + src/test/ui/traits/cache-issue-18209.rs | 20 + src/test/ui/traits/cache-reached-depth-ice.rs | 45 ++ src/test/ui/traits/cache-reached-depth-ice.stderr | 11 + src/test/ui/traits/coercion-generic-bad.rs | 19 + src/test/ui/traits/coercion-generic-bad.stderr | 12 + src/test/ui/traits/coercion-generic-regions.rs | 19 + src/test/ui/traits/coercion-generic-regions.stderr | 15 + src/test/ui/traits/coercion-generic.rs | 25 + src/test/ui/traits/coercion.rs | 35 ++ src/test/ui/traits/composition-trivial.rs | 12 + src/test/ui/traits/conditional-dispatch.rs | 34 ++ src/test/ui/traits/conditional-model-fn.rs | 53 ++ src/test/ui/traits/conservative_impl_trait.rs | 8 + src/test/ui/traits/copy-guessing.rs | 38 ++ src/test/ui/traits/copy-impl-cannot-normalize.rs | 25 + .../ui/traits/copy-impl-cannot-normalize.stderr | 14 + src/test/ui/traits/cycle-cache-err-60010.rs | 74 +++ src/test/ui/traits/cycle-cache-err-60010.stderr | 33 ++ src/test/ui/traits/cycle-generic-bound.rs | 11 + src/test/ui/traits/cycle-type-trait.rs | 25 + src/test/ui/traits/default-method/auxiliary/xc.rs | 40 ++ .../ui/traits/default-method/auxiliary/xc_2.rs | 17 + src/test/ui/traits/default-method/bound-subst.rs | 18 + src/test/ui/traits/default-method/bound-subst2.rs | 16 + src/test/ui/traits/default-method/bound-subst3.rs | 17 + src/test/ui/traits/default-method/bound-subst4.rs | 19 + src/test/ui/traits/default-method/bound.rs | 16 + src/test/ui/traits/default-method/macro.rs | 20 + src/test/ui/traits/default-method/mut.rs | 11 + .../default-method/rustc_must_implement_one_of.rs | 44 ++ .../rustc_must_implement_one_of.stderr | 15 + .../rustc_must_implement_one_of_duplicates.rs | 19 + .../rustc_must_implement_one_of_duplicates.stderr | 34 ++ .../rustc_must_implement_one_of_gated.rs | 13 + .../rustc_must_implement_one_of_gated.stderr | 11 + .../rustc_must_implement_one_of_misuse.rs | 46 ++ .../rustc_must_implement_one_of_misuse.stderr | 100 ++++ src/test/ui/traits/default-method/self.rs | 18 + src/test/ui/traits/default-method/supervtable.rs | 28 + src/test/ui/traits/default-method/trivial.rs | 21 + src/test/ui/traits/default-method/xc-2.rs | 26 + src/test/ui/traits/default-method/xc.rs | 81 +++ ...ype-params-by-name-in-suggestion-issue-96292.rs | 20 + ...params-by-name-in-suggestion-issue-96292.stderr | 34 ++ src/test/ui/traits/duplicate-methods.rs | 6 + src/test/ui/traits/duplicate-methods.stderr | 13 + src/test/ui/traits/dyn-trait.rs | 17 + src/test/ui/traits/early-vtbl-resolution.rs | 19 + src/test/ui/traits/elaborate-type-region.rs | 49 ++ .../false-ambiguity-where-clause-builtin-bound.rs | 16 + src/test/ui/traits/fmt-pointer-trait.rs | 24 + src/test/ui/traits/generic.rs | 45 ++ src/test/ui/traits/impl-1.rs | 16 + src/test/ui/traits/impl-1.stderr | 9 + src/test/ui/traits/impl-2.rs | 19 + src/test/ui/traits/impl-bounds-checking.rs | 14 + src/test/ui/traits/impl-bounds-checking.stderr | 15 + .../traits/impl-can-not-have-untraitful-items.rs | 9 + .../impl-can-not-have-untraitful-items.stderr | 22 + src/test/ui/traits/impl-different-num-params.rs | 12 + .../ui/traits/impl-different-num-params.stderr | 12 + src/test/ui/traits/impl-evaluation-order.rs | 39 ++ src/test/ui/traits/impl-for-module.rs | 11 + src/test/ui/traits/impl-for-module.stderr | 12 + src/test/ui/traits/impl-implicit-trait.rs | 26 + .../ui/traits/impl-inherent-prefer-over-trait.rs | 30 ++ src/test/ui/traits/impl-method-mismatch.rs | 13 + src/test/ui/traits/impl-method-mismatch.stderr | 17 + .../ui/traits/impl-object-overlap-issue-23853.rs | 18 + ...-of-supertrait-has-wrong-lifetime-parameters.rs | 31 ++ ...supertrait-has-wrong-lifetime-parameters.stderr | 27 + src/test/ui/traits/impl.rs | 41 ++ .../traits/impl_trait_as_trait_return_position.rs | 17 + src/test/ui/traits/inductive-overflow/lifetime.rs | 32 ++ .../ui/traits/inductive-overflow/lifetime.stderr | 22 + .../ui/traits/inductive-overflow/simultaneous.rs | 20 + .../traits/inductive-overflow/simultaneous.stderr | 20 + .../inductive-overflow/supertrait-auto-trait.rs | 18 + .../supertrait-auto-trait.stderr | 35 ++ .../ui/traits/inductive-overflow/supertrait.rs | 15 + .../ui/traits/inductive-overflow/supertrait.stderr | 20 + .../ui/traits/inductive-overflow/two-traits.rs | 22 + .../ui/traits/inductive-overflow/two-traits.stderr | 32 ++ .../ui/traits/infer-from-object-issue-26952.rs | 26 + src/test/ui/traits/inherent-method-order.rs | 25 + src/test/ui/traits/inheritance/auto-xc-2.rs | 23 + src/test/ui/traits/inheritance/auto-xc.rs | 25 + src/test/ui/traits/inheritance/auto.rs | 29 ++ .../ui/traits/inheritance/auxiliary/auto_xc.rs | 7 + .../ui/traits/inheritance/auxiliary/auto_xc_2.rs | 9 + .../traits/inheritance/auxiliary/overloading_xc.rs | 38 ++ .../ui/traits/inheritance/auxiliary/xc_call.rs | 11 + src/test/ui/traits/inheritance/basic.rs | 26 + .../ui/traits/inheritance/call-bound-inherited.rs | 20 + .../ui/traits/inheritance/call-bound-inherited2.rs | 23 + .../inheritance/cast-without-call-to-supertrait.rs | 33 ++ src/test/ui/traits/inheritance/cast.rs | 33 ++ .../ui/traits/inheritance/cross-trait-call-xc.rs | 20 + src/test/ui/traits/inheritance/cross-trait-call.rs | 19 + src/test/ui/traits/inheritance/diamond.rs | 28 + .../ui/traits/inheritance/multiple-inheritors.rs | 23 + src/test/ui/traits/inheritance/multiple-params.rs | 26 + src/test/ui/traits/inheritance/num.rs | 13 + src/test/ui/traits/inheritance/num0.rs | 24 + src/test/ui/traits/inheritance/num1.rs | 15 + src/test/ui/traits/inheritance/num2.rs | 86 +++ src/test/ui/traits/inheritance/num3.rs | 19 + src/test/ui/traits/inheritance/num5.rs | 26 + .../ui/traits/inheritance/overloading-simple.rs | 27 + .../ui/traits/inheritance/overloading-xc-exe.rs | 20 + src/test/ui/traits/inheritance/overloading.rs | 47 ++ .../inheritance/repeated-supertrait-ambig.rs | 43 ++ .../inheritance/repeated-supertrait-ambig.stderr | 55 ++ .../ui/traits/inheritance/repeated-supertrait.rs | 48 ++ .../ui/traits/inheritance/self-in-supertype.rs | 62 +++ src/test/ui/traits/inheritance/self.rs | 29 ++ src/test/ui/traits/inheritance/simple.rs | 24 + src/test/ui/traits/inheritance/static.rs | 26 + src/test/ui/traits/inheritance/static2.rs | 29 ++ src/test/ui/traits/inheritance/subst.rs | 27 + src/test/ui/traits/inheritance/subst2.rs | 37 ++ src/test/ui/traits/inheritance/visibility.rs | 20 + src/test/ui/traits/issue-18400.rs | 26 + src/test/ui/traits/issue-18400.stderr | 18 + src/test/ui/traits/issue-18412.rs | 26 + src/test/ui/traits/issue-20692.rs | 11 + src/test/ui/traits/issue-20692.stderr | 35 ++ src/test/ui/traits/issue-22019.rs | 34 ++ src/test/ui/traits/issue-22110.rs | 27 + src/test/ui/traits/issue-22655.rs | 23 + src/test/ui/traits/issue-23003-overflow.rs | 29 ++ src/test/ui/traits/issue-23003.rs | 32 ++ src/test/ui/traits/issue-23825.rs | 21 + src/test/ui/traits/issue-24010.rs | 14 + src/test/ui/traits/issue-26339.rs | 31 ++ src/test/ui/traits/issue-28576.rs | 12 + src/test/ui/traits/issue-28576.stderr | 20 + src/test/ui/traits/issue-32963.rs | 11 + src/test/ui/traits/issue-32963.stderr | 27 + src/test/ui/traits/issue-33140-hack-boundaries.rs | 70 +++ .../ui/traits/issue-33140-hack-boundaries.stderr | 68 +++ src/test/ui/traits/issue-33140.rs | 47 ++ src/test/ui/traits/issue-33140.stderr | 31 ++ src/test/ui/traits/issue-35869.rs | 21 + src/test/ui/traits/issue-35869.stderr | 71 +++ src/test/ui/traits/issue-3683.rs | 18 + src/test/ui/traits/issue-38033.rs | 79 +++ src/test/ui/traits/issue-38604.rs | 16 + src/test/ui/traits/issue-38604.stderr | 33 ++ src/test/ui/traits/issue-3973.rs | 25 + src/test/ui/traits/issue-3973.stderr | 22 + src/test/ui/traits/issue-4107.rs | 26 + src/test/ui/traits/issue-43132.rs | 65 +++ src/test/ui/traits/issue-52893.rs | 57 ++ src/test/ui/traits/issue-52893.stderr | 22 + src/test/ui/traits/issue-56202.rs | 17 + src/test/ui/traits/issue-56488.rs | 13 + src/test/ui/traits/issue-59029-1.rs | 9 + src/test/ui/traits/issue-59029-1.stderr | 15 + src/test/ui/traits/issue-59029-2.rs | 8 + src/test/ui/traits/issue-6128.rs | 22 + src/test/ui/traits/issue-6334.rs | 46 ++ .../issue-65284-suggest-generic-trait-bound.rs | 11 + .../issue-65284-suggest-generic-trait-bound.stderr | 17 + src/test/ui/traits/issue-65673.rs | 12 + src/test/ui/traits/issue-65673.stderr | 12 + src/test/ui/traits/issue-68295.rs | 47 ++ src/test/ui/traits/issue-68295.stderr | 17 + src/test/ui/traits/issue-70944.rs | 23 + src/test/ui/traits/issue-71036.rs | 17 + src/test/ui/traits/issue-71036.stderr | 12 + src/test/ui/traits/issue-71136.rs | 8 + src/test/ui/traits/issue-71136.stderr | 19 + src/test/ui/traits/issue-72410.rs | 18 + src/test/ui/traits/issue-72410.stderr | 25 + src/test/ui/traits/issue-72455.rs | 27 + src/test/ui/traits/issue-75627.rs | 6 + src/test/ui/traits/issue-75627.stderr | 11 + src/test/ui/traits/issue-77982.rs | 41 ++ src/test/ui/traits/issue-77982.stderr | 96 ++++ src/test/ui/traits/issue-78372.rs | 13 + src/test/ui/traits/issue-78372.stderr | 62 +++ src/test/ui/traits/issue-78632.rs | 59 +++ src/test/ui/traits/issue-79458.rs | 10 + src/test/ui/traits/issue-79458.stderr | 19 + src/test/ui/traits/issue-8153.rs | 16 + src/test/ui/traits/issue-8153.stderr | 11 + src/test/ui/traits/issue-82830.rs | 16 + src/test/ui/traits/issue-82830.stderr | 15 + .../issue-83538-tainted-cache-after-cycle.rs | 66 +++ .../issue-83538-tainted-cache-after-cycle.stderr | 38 ++ .../ui/traits/issue-84399-bad-fresh-caching.rs | 55 ++ .../ui/traits/issue-85360-eval-obligation-ice.rs | 143 +++++ .../traits/issue-85360-eval-obligation-ice.stderr | 38 ++ src/test/ui/traits/issue-85735.rs | 13 + src/test/ui/traits/issue-85735.stderr | 11 + src/test/ui/traits/issue-87558.rs | 9 + src/test/ui/traits/issue-87558.stderr | 24 + src/test/ui/traits/issue-89119.rs | 11 + src/test/ui/traits/issue-90195-2.rs | 20 + src/test/ui/traits/issue-90195.rs | 21 + .../ui/traits/issue-90662-projection-caching.rs | 34 ++ src/test/ui/traits/issue-91594.rs | 17 + src/test/ui/traits/issue-91594.stderr | 16 + .../ui/traits/issue-91949-hangs-on-recursion.rs | 31 ++ .../traits/issue-91949-hangs-on-recursion.stderr | 24 + src/test/ui/traits/issue-92292.rs | 32 ++ src/test/ui/traits/issue-9394-inherited-calls.rs | 62 +++ src/test/ui/traits/issue-95311.rs | 19 + src/test/ui/traits/issue-95898.rs | 9 + src/test/ui/traits/issue-95898.stderr | 17 + src/test/ui/traits/issue-96664.rs | 16 + src/test/ui/traits/issue-96665.rs | 16 + src/test/ui/traits/issue-97576.rs | 13 + src/test/ui/traits/issue-97576.stderr | 11 + .../ui/traits/issue-97695-double-trivial-bound.rs | 24 + src/test/ui/traits/item-inside-macro.rs | 30 ++ src/test/ui/traits/item-privacy.rs | 134 +++++ src/test/ui/traits/item-privacy.stderr | 180 +++++++ src/test/ui/traits/kindck-owned-contains-1.rs | 21 + src/test/ui/traits/map-types.rs | 19 + src/test/ui/traits/map-types.stderr | 12 + src/test/ui/traits/matching-lifetimes.rs | 20 + src/test/ui/traits/matching-lifetimes.stderr | 41 ++ src/test/ui/traits/method-private.rs | 20 + src/test/ui/traits/method-private.stderr | 18 + src/test/ui/traits/monad.rs | 48 ++ .../monomorphized-callees-with-ty-params-3314.rs | 32 ++ src/test/ui/traits/multidispatch-bad.rs | 22 + src/test/ui/traits/multidispatch-bad.stderr | 21 + ...ultidispatch-conditional-impl-not-considered.rs | 24 + .../ui/traits/multidispatch-convert-ambig-dest.rs | 31 ++ .../traits/multidispatch-convert-ambig-dest.stderr | 41 ++ .../traits/multidispatch-infer-convert-target.rs | 36 ++ src/test/ui/traits/multidispatch1.rs | 33 ++ src/test/ui/traits/multidispatch2.rs | 39 ++ src/test/ui/traits/mutual-recursion-issue-75860.rs | 15 + .../ui/traits/mutual-recursion-issue-75860.stderr | 16 + .../negative-impls/auxiliary/foreign_trait.rs | 7 + .../explicitly-unimplemented-error-message.rs | 53 ++ .../explicitly-unimplemented-error-message.stderr | 52 ++ .../negative-impls/feature-gate-negative_impls.rs | 3 + .../feature-gate-negative_impls.stderr | 12 + .../negative-impls/negated-auto-traits-error.rs | 68 +++ .../negated-auto-traits-error.stderr | 125 +++++ .../negative-impls/negated-auto-traits-rpass.rs | 21 + .../negative-impls/negative-default-impls.rs | 11 + .../negative-impls/negative-default-impls.stderr | 19 + .../traits/negative-impls/negative-impls-basic.rs | 17 + .../negative-specializes-negative.rs | 13 + .../negative-specializes-negative.stderr | 12 + .../negative-specializes-positive-item.rs | 13 + .../negative-specializes-positive-item.stderr | 22 + .../negative-specializes-positive.rs | 14 + .../negative-specializes-positive.stderr | 21 + src/test/ui/traits/negative-impls/no-items.rs | 11 + src/test/ui/traits/negative-impls/no-items.stderr | 9 + .../pin-unsound-issue-66544-clone.rs | 26 + .../pin-unsound-issue-66544-clone.stderr | 11 + .../pin-unsound-issue-66544-derefmut.rs | 33 ++ .../pin-unsound-issue-66544-derefmut.stderr | 11 + .../positive-specializes-negative.rs | 9 + .../positive-specializes-negative.stderr | 21 + .../rely-on-negative-impl-in-coherence.rs | 24 + src/test/ui/traits/no-fallback-multiple-impls.rs | 16 + .../ui/traits/no-fallback-multiple-impls.stderr | 9 + src/test/ui/traits/no_send-struct.rs | 17 + src/test/ui/traits/no_send-struct.stderr | 18 + src/test/ui/traits/normalize-supertrait.rs | 37 ++ ...ot-suggest-non-existing-fully-qualified-path.rs | 24 + ...uggest-non-existing-fully-qualified-path.stderr | 38 ++ src/test/ui/traits/object-does-not-impl-trait.rs | 8 + .../ui/traits/object-does-not-impl-trait.stderr | 17 + src/test/ui/traits/object-one-type-two-traits.rs | 31 ++ src/test/ui/traits/object/auto-dedup-in-impl.rs | 19 + .../ui/traits/object/auto-dedup-in-impl.stderr | 12 + src/test/ui/traits/object/auto-dedup.rs | 46 ++ src/test/ui/traits/object/bounds-cycle-1.rs | 24 + src/test/ui/traits/object/bounds-cycle-2.rs | 28 + src/test/ui/traits/object/bounds-cycle-3.rs | 25 + src/test/ui/traits/object/bounds-cycle-4.rs | 25 + .../traits/object/enforce-supertrait-projection.rs | 24 + .../object/enforce-supertrait-projection.stderr | 26 + src/test/ui/traits/object/exclusion.rs | 19 + src/test/ui/traits/object/generics.rs | 41 ++ .../traits/object/issue-33140-traitobject-crate.rs | 108 ++++ .../object/issue-33140-traitobject-crate.stderr | 42 ++ src/test/ui/traits/object/lifetime-first.rs | 13 + src/test/ui/traits/object/macro-matcher.rs | 12 + src/test/ui/traits/object/macro-matcher.stderr | 19 + src/test/ui/traits/object/safety.rs | 17 + src/test/ui/traits/object/safety.stderr | 49 ++ .../ui/traits/object/supertrait-lifetime-bound.rs | 14 + .../traits/object/supertrait-lifetime-bound.stderr | 11 + src/test/ui/traits/object/vs-lifetime-2.rs | 11 + src/test/ui/traits/object/vs-lifetime-2.stderr | 9 + src/test/ui/traits/object/vs-lifetime.rs | 17 + src/test/ui/traits/object/vs-lifetime.stderr | 52 ++ src/test/ui/traits/object/with-lifetime-bound.rs | 34 ++ .../object/with-self-in-projection-output-bad.rs | 50 ++ .../with-self-in-projection-output-bad.stderr | 21 + .../object/with-self-in-projection-output-good.rs | 28 + ...elf-in-projection-output-repeated-supertrait.rs | 51 ++ ...ects-owned-object-borrowed-method-headerless.rs | 30 ++ .../ui/traits/operator-overloading-issue-52025.rs | 57 ++ .../overlap-not-permitted-for-builtin-trait.rs | 10 + .../overlap-not-permitted-for-builtin-trait.stderr | 11 + .../traits/overlap-permitted-for-marker-traits.rs | 28 + .../ui/traits/param-without-lifetime-constraint.rs | 20 + .../param-without-lifetime-constraint.stderr | 19 + src/test/ui/traits/parameterized-with-bounds.rs | 21 + src/test/ui/traits/pointee-deduction.rs | 22 + .../ui/traits/pointee-tail-is-generic-errors.rs | 22 + .../traits/pointee-tail-is-generic-errors.stderr | 40 ++ src/test/ui/traits/pointee-tail-is-generic.rs | 29 ++ src/test/ui/traits/principal-less-objects.rs | 42 ++ src/test/ui/traits/privacy.rs | 24 + src/test/ui/traits/project-modulo-regions.rs | 55 ++ .../project-modulo-regions.with_clause.stderr | 11 + .../project-modulo-regions.without_clause.stderr | 11 + src/test/ui/traits/region-pointer-simple.rs | 21 + .../traits/reservation-impl/coherence-conflict.rs | 14 + .../reservation-impl/coherence-conflict.stderr | 13 + src/test/ui/traits/reservation-impl/no-use.rs | 12 + src/test/ui/traits/reservation-impl/no-use.stderr | 13 + .../ui/traits/reservation-impl/non-lattice-ok.rs | 59 +++ src/test/ui/traits/reservation-impl/ok.rs | 28 + src/test/ui/traits/resolution-in-overloaded-op.rs | 11 + .../ui/traits/resolution-in-overloaded-op.stderr | 16 + src/test/ui/traits/safety-fn-body.mir.stderr | 11 + src/test/ui/traits/safety-fn-body.rs | 19 + src/test/ui/traits/safety-fn-body.thir.stderr | 11 + src/test/ui/traits/safety-inherent-impl.rs | 9 + src/test/ui/traits/safety-inherent-impl.stderr | 11 + src/test/ui/traits/safety-ok-cc.rs | 24 + src/test/ui/traits/safety-ok.rs | 18 + src/test/ui/traits/safety-trait-impl-cc.rs | 15 + src/test/ui/traits/safety-trait-impl-cc.stderr | 13 + src/test/ui/traits/safety-trait-impl.rs | 18 + src/test/ui/traits/safety-trait-impl.stderr | 16 + .../ui/traits/self-without-lifetime-constraint.rs | 53 ++ .../traits/self-without-lifetime-constraint.stderr | 19 + .../ui/traits/static-method-generic-inference.rs | 28 + .../traits/static-method-generic-inference.stderr | 17 + src/test/ui/traits/static-method-overwriting.rs | 34 ++ .../ui/traits/static-outlives-a-where-clause.rs | 23 + src/test/ui/traits/staticness-mismatch.rs | 11 + src/test/ui/traits/staticness-mismatch.stderr | 12 + .../ui/traits/suggest-deferences/issue-39029.fixed | 18 + .../ui/traits/suggest-deferences/issue-39029.rs | 18 + .../traits/suggest-deferences/issue-39029.stderr | 20 + .../ui/traits/suggest-deferences/issue-62530.fixed | 15 + .../ui/traits/suggest-deferences/issue-62530.rs | 15 + .../traits/suggest-deferences/issue-62530.stderr | 19 + .../ui/traits/suggest-deferences/multiple-0.fixed | 36 ++ .../ui/traits/suggest-deferences/multiple-0.rs | 36 ++ .../ui/traits/suggest-deferences/multiple-0.stderr | 19 + .../ui/traits/suggest-deferences/multiple-1.rs | 54 ++ .../ui/traits/suggest-deferences/multiple-1.stderr | 18 + .../suggest-deferences/root-obligation.fixed | 13 + .../traits/suggest-deferences/root-obligation.rs | 13 + .../suggest-deferences/root-obligation.stderr | 24 + ...suggest-fully-qualified-path-with-adjustment.rs | 60 +++ ...est-fully-qualified-path-with-adjustment.stderr | 186 +++++++ ...gest-fully-qualified-path-without-adjustment.rs | 64 +++ ...-fully-qualified-path-without-adjustment.stderr | 224 ++++++++ src/test/ui/traits/suggest-where-clause.rs | 36 ++ src/test/ui/traits/suggest-where-clause.stderr | 102 ++++ src/test/ui/traits/superdefault-generics.rs | 39 ++ src/test/ui/traits/syntax-polarity.rs | 21 + src/test/ui/traits/syntax-trait-polarity.rs | 25 + src/test/ui/traits/syntax-trait-polarity.stderr | 37 ++ src/test/ui/traits/test-2.rs | 17 + src/test/ui/traits/test-2.stderr | 85 +++ src/test/ui/traits/test.rs | 6 + src/test/ui/traits/test.stderr | 9 + src/test/ui/traits/to-str.rs | 36 ++ src/test/ui/traits/trait-or-new-type-instead.rs | 6 + .../ui/traits/trait-or-new-type-instead.stderr | 14 + src/test/ui/traits/trait-upcasting/basic.rs | 87 ++++ .../correct-supertrait-substitution.rs | 39 ++ .../trait-upcasting/cyclic-trait-resolution.rs | 13 + .../trait-upcasting/cyclic-trait-resolution.stderr | 21 + src/test/ui/traits/trait-upcasting/diamond.rs | 115 ++++ .../ui/traits/trait-upcasting/invalid-upcast.rs | 87 ++++ .../traits/trait-upcasting/invalid-upcast.stderr | 168 ++++++ .../issue-11515-upcast-fn_mut-fn.rs | 13 + src/test/ui/traits/trait-upcasting/lifetime.rs | 95 ++++ .../ui/traits/trait-upcasting/migrate-lint-deny.rs | 25 + .../trait-upcasting/migrate-lint-deny.stderr | 16 + .../multiple-occurence-ambiguousity.rs | 22 + .../multiple-occurence-ambiguousity.stderr | 14 + src/test/ui/traits/trait-upcasting/replace-vptr.rs | 49 ++ src/test/ui/traits/trait-upcasting/struct.rs | 174 +++++++ .../ui/traits/trait-upcasting/subtrait-method.rs | 70 +++ .../traits/trait-upcasting/subtrait-method.stderr | 68 +++ .../traits/trait-upcasting/type-checking-test-1.rs | 27 + .../trait-upcasting/type-checking-test-1.stderr | 23 + .../traits/trait-upcasting/type-checking-test-2.rs | 32 ++ .../trait-upcasting/type-checking-test-2.stderr | 42 ++ .../type-checking-test-3.polonius.stderr | 22 + .../traits/trait-upcasting/type-checking-test-3.rs | 21 + .../trait-upcasting/type-checking-test-3.stderr | 18 + .../type-checking-test-4.polonius.stderr | 33 ++ .../traits/trait-upcasting/type-checking-test-4.rs | 49 ++ .../trait-upcasting/type-checking-test-4.stderr | 52 ++ .../ui/traits/typeclasses-eq-example-static.rs | 68 +++ src/test/ui/traits/typeclasses-eq-example.rs | 64 +++ src/test/ui/traits/ufcs-object.rs | 17 + src/test/ui/traits/use-before-def.rs | 10 + src/test/ui/traits/vtable-res-trait-param.rs | 23 + src/test/ui/traits/vtable-res-trait-param.stderr | 17 + src/test/ui/traits/vtable/issue-91807.rs | 17 + src/test/ui/traits/vtable/issue-97381.rs | 30 ++ src/test/ui/traits/vtable/issue-97381.stderr | 15 + src/test/ui/traits/vtable/vtable-diamond.rs | 44 ++ src/test/ui/traits/vtable/vtable-diamond.stderr | 29 ++ src/test/ui/traits/vtable/vtable-multi-level.rs | 143 +++++ .../ui/traits/vtable/vtable-multi-level.stderr | 203 ++++++++ src/test/ui/traits/vtable/vtable-multiple.rs | 33 ++ src/test/ui/traits/vtable/vtable-multiple.stderr | 27 + .../ui/traits/vtable/vtable-non-object-safe.rs | 18 + .../ui/traits/vtable/vtable-non-object-safe.stderr | 16 + src/test/ui/traits/vtable/vtable-vacant.rs | 31 ++ src/test/ui/traits/vtable/vtable-vacant.stderr | 16 + src/test/ui/traits/wf-object/maybe-bound.rs | 18 + src/test/ui/traits/wf-object/maybe-bound.stderr | 32 ++ src/test/ui/traits/wf-object/no-duplicates.rs | 33 ++ src/test/ui/traits/wf-object/no-duplicates.stderr | 58 +++ src/test/ui/traits/wf-object/only-maybe-bound.rs | 7 + .../ui/traits/wf-object/only-maybe-bound.stderr | 15 + src/test/ui/traits/wf-object/reverse-order.rs | 15 + src/test/ui/traits/where-clause-vs-impl.rs | 45 ++ src/test/ui/traits/with-bounds-default.rs | 32 ++ src/test/ui/traits/with-dst.rs | 22 + 545 files changed, 17167 insertions(+) create mode 100644 src/test/ui/traits/alias/ambiguous.rs create mode 100644 src/test/ui/traits/alias/ambiguous.stderr create mode 100644 src/test/ui/traits/alias/auxiliary/greeter.rs create mode 100644 src/test/ui/traits/alias/auxiliary/send_sync.rs create mode 100644 src/test/ui/traits/alias/basic.rs create mode 100644 src/test/ui/traits/alias/bounds.rs create mode 100644 src/test/ui/traits/alias/cross-crate.rs create mode 100644 src/test/ui/traits/alias/cross-crate.stderr create mode 100644 src/test/ui/traits/alias/impl.rs create mode 100644 src/test/ui/traits/alias/impl.stderr create mode 100644 src/test/ui/traits/alias/import-cross-crate.rs create mode 100644 src/test/ui/traits/alias/import.rs create mode 100644 src/test/ui/traits/alias/issue-60021-assoc-method-resolve.rs create mode 100644 src/test/ui/traits/alias/issue-72415-assoc-const-resolve.rs create mode 100644 src/test/ui/traits/alias/issue-75983.rs create mode 100644 src/test/ui/traits/alias/issue-83613.rs create mode 100644 src/test/ui/traits/alias/issue-83613.stderr create mode 100644 src/test/ui/traits/alias/maybe-bound.rs create mode 100644 src/test/ui/traits/alias/no-duplicates.rs create mode 100644 src/test/ui/traits/alias/no-duplicates.stderr create mode 100644 src/test/ui/traits/alias/no-extra-traits.rs create mode 100644 src/test/ui/traits/alias/no-extra-traits.stderr create mode 100644 src/test/ui/traits/alias/object-fail.rs create mode 100644 src/test/ui/traits/alias/object-fail.stderr create mode 100644 src/test/ui/traits/alias/object-wf.rs create mode 100644 src/test/ui/traits/alias/object.rs create mode 100644 src/test/ui/traits/alias/only-maybe-bound.rs create mode 100644 src/test/ui/traits/alias/only-maybe-bound.stderr create mode 100644 src/test/ui/traits/alias/style_lint.rs create mode 100644 src/test/ui/traits/alias/style_lint.stderr create mode 100644 src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.fixed create mode 100644 src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.rs create mode 100644 src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.stderr create mode 100644 src/test/ui/traits/alias/syntax-fail.rs create mode 100644 src/test/ui/traits/alias/syntax-fail.stderr create mode 100644 src/test/ui/traits/alias/syntax.rs create mode 100644 src/test/ui/traits/alias/wf.rs create mode 100644 src/test/ui/traits/alias/wf.stderr create mode 100644 src/test/ui/traits/alignment-gep-tup-like-1.rs create mode 100644 src/test/ui/traits/anon-static-method.rs create mode 100644 src/test/ui/traits/anon_trait_static_method_exe.rs create mode 100644 src/test/ui/traits/as-struct-constructor.rs create mode 100644 src/test/ui/traits/as-struct-constructor.stderr create mode 100644 src/test/ui/traits/assignability-trait.rs create mode 100644 src/test/ui/traits/assoc-type-in-superbad.rs create mode 100644 src/test/ui/traits/assoc-type-in-superbad.stderr create mode 100644 src/test/ui/traits/assoc-type-in-supertrait.rs create mode 100644 src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.rs create mode 100644 src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.stderr create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.rs create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.stderr create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2-ok.rs create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.rs create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.stderr create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.rs create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.stderr create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.rs create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.stderr create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.rs create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.stderr create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.rs create mode 100644 src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.stderr create mode 100644 src/test/ui/traits/associated_type_bound/issue-51446.rs create mode 100644 src/test/ui/traits/astconv-cycle-between-and-type.rs create mode 100644 src/test/ui/traits/augmented-assignments-trait.rs create mode 100644 src/test/ui/traits/auxiliary/anon_trait_static_method_lib.rs create mode 100644 src/test/ui/traits/auxiliary/go_trait.rs create mode 100644 src/test/ui/traits/auxiliary/issue_89119_intercrate_caching.rs create mode 100644 src/test/ui/traits/auxiliary/trait_safety_lib.rs create mode 100644 src/test/ui/traits/auxiliary/traitimpl.rs create mode 100644 src/test/ui/traits/bad-method-typaram-kind.rs create mode 100644 src/test/ui/traits/bad-method-typaram-kind.stderr create mode 100644 src/test/ui/traits/bad-sized.rs create mode 100644 src/test/ui/traits/bad-sized.stderr create mode 100644 src/test/ui/traits/bound/assoc-fn-bound-root-obligation.rs create mode 100644 src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr create mode 100644 src/test/ui/traits/bound/auxiliary/crate_a1.rs create mode 100644 src/test/ui/traits/bound/auxiliary/crate_a2.rs create mode 100644 src/test/ui/traits/bound/auxiliary/on_structs_and_enums_xc.rs create mode 100644 src/test/ui/traits/bound/basic.rs create mode 100644 src/test/ui/traits/bound/generic_trait.rs create mode 100644 src/test/ui/traits/bound/impl-comparison-duplicates.rs create mode 100644 src/test/ui/traits/bound/in-arc.rs create mode 100644 src/test/ui/traits/bound/multiple.rs create mode 100644 src/test/ui/traits/bound/not-on-bare-trait.rs create mode 100644 src/test/ui/traits/bound/not-on-bare-trait.stderr create mode 100644 src/test/ui/traits/bound/not-on-struct.rs create mode 100644 src/test/ui/traits/bound/not-on-struct.stderr create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-in-fns.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-in-fns.stderr create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-in-impls.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-in-impls.stderr create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-locals.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-locals.stderr create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-rpass.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-static.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-static.stderr create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-xc.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-xc.stderr create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-xc1.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums-xc1.stderr create mode 100644 src/test/ui/traits/bound/on-structs-and-enums.rs create mode 100644 src/test/ui/traits/bound/on-structs-and-enums.stderr create mode 100644 src/test/ui/traits/bound/recursion.rs create mode 100644 src/test/ui/traits/bound/same-crate-name.rs create mode 100644 src/test/ui/traits/bound/same-crate-name.stderr create mode 100644 src/test/ui/traits/bound/sugar.rs create mode 100644 src/test/ui/traits/bound/sugar.stderr create mode 100644 src/test/ui/traits/bug-7183-generics.rs create mode 100644 src/test/ui/traits/bug-7295.rs create mode 100644 src/test/ui/traits/cache-issue-18209.rs create mode 100644 src/test/ui/traits/cache-reached-depth-ice.rs create mode 100644 src/test/ui/traits/cache-reached-depth-ice.stderr create mode 100644 src/test/ui/traits/coercion-generic-bad.rs create mode 100644 src/test/ui/traits/coercion-generic-bad.stderr create mode 100644 src/test/ui/traits/coercion-generic-regions.rs create mode 100644 src/test/ui/traits/coercion-generic-regions.stderr create mode 100644 src/test/ui/traits/coercion-generic.rs create mode 100644 src/test/ui/traits/coercion.rs create mode 100644 src/test/ui/traits/composition-trivial.rs create mode 100644 src/test/ui/traits/conditional-dispatch.rs create mode 100644 src/test/ui/traits/conditional-model-fn.rs create mode 100644 src/test/ui/traits/conservative_impl_trait.rs create mode 100644 src/test/ui/traits/copy-guessing.rs create mode 100644 src/test/ui/traits/copy-impl-cannot-normalize.rs create mode 100644 src/test/ui/traits/copy-impl-cannot-normalize.stderr create mode 100644 src/test/ui/traits/cycle-cache-err-60010.rs create mode 100644 src/test/ui/traits/cycle-cache-err-60010.stderr create mode 100644 src/test/ui/traits/cycle-generic-bound.rs create mode 100644 src/test/ui/traits/cycle-type-trait.rs create mode 100644 src/test/ui/traits/default-method/auxiliary/xc.rs create mode 100644 src/test/ui/traits/default-method/auxiliary/xc_2.rs create mode 100644 src/test/ui/traits/default-method/bound-subst.rs create mode 100644 src/test/ui/traits/default-method/bound-subst2.rs create mode 100644 src/test/ui/traits/default-method/bound-subst3.rs create mode 100644 src/test/ui/traits/default-method/bound-subst4.rs create mode 100644 src/test/ui/traits/default-method/bound.rs create mode 100644 src/test/ui/traits/default-method/macro.rs create mode 100644 src/test/ui/traits/default-method/mut.rs create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of.rs create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of.stderr create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.stderr create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.rs create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.stderr create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs create mode 100644 src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr create mode 100644 src/test/ui/traits/default-method/self.rs create mode 100644 src/test/ui/traits/default-method/supervtable.rs create mode 100644 src/test/ui/traits/default-method/trivial.rs create mode 100644 src/test/ui/traits/default-method/xc-2.rs create mode 100644 src/test/ui/traits/default-method/xc.rs create mode 100644 src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs create mode 100644 src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.stderr create mode 100644 src/test/ui/traits/duplicate-methods.rs create mode 100644 src/test/ui/traits/duplicate-methods.stderr create mode 100644 src/test/ui/traits/dyn-trait.rs create mode 100644 src/test/ui/traits/early-vtbl-resolution.rs create mode 100644 src/test/ui/traits/elaborate-type-region.rs create mode 100644 src/test/ui/traits/false-ambiguity-where-clause-builtin-bound.rs create mode 100644 src/test/ui/traits/fmt-pointer-trait.rs create mode 100644 src/test/ui/traits/generic.rs create mode 100644 src/test/ui/traits/impl-1.rs create mode 100644 src/test/ui/traits/impl-1.stderr create mode 100644 src/test/ui/traits/impl-2.rs create mode 100644 src/test/ui/traits/impl-bounds-checking.rs create mode 100644 src/test/ui/traits/impl-bounds-checking.stderr create mode 100644 src/test/ui/traits/impl-can-not-have-untraitful-items.rs create mode 100644 src/test/ui/traits/impl-can-not-have-untraitful-items.stderr create mode 100644 src/test/ui/traits/impl-different-num-params.rs create mode 100644 src/test/ui/traits/impl-different-num-params.stderr create mode 100644 src/test/ui/traits/impl-evaluation-order.rs create mode 100644 src/test/ui/traits/impl-for-module.rs create mode 100644 src/test/ui/traits/impl-for-module.stderr create mode 100644 src/test/ui/traits/impl-implicit-trait.rs create mode 100644 src/test/ui/traits/impl-inherent-prefer-over-trait.rs create mode 100644 src/test/ui/traits/impl-method-mismatch.rs create mode 100644 src/test/ui/traits/impl-method-mismatch.stderr create mode 100644 src/test/ui/traits/impl-object-overlap-issue-23853.rs create mode 100644 src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.rs create mode 100644 src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.stderr create mode 100644 src/test/ui/traits/impl.rs create mode 100644 src/test/ui/traits/impl_trait_as_trait_return_position.rs create mode 100644 src/test/ui/traits/inductive-overflow/lifetime.rs create mode 100644 src/test/ui/traits/inductive-overflow/lifetime.stderr create mode 100644 src/test/ui/traits/inductive-overflow/simultaneous.rs create mode 100644 src/test/ui/traits/inductive-overflow/simultaneous.stderr create mode 100644 src/test/ui/traits/inductive-overflow/supertrait-auto-trait.rs create mode 100644 src/test/ui/traits/inductive-overflow/supertrait-auto-trait.stderr create mode 100644 src/test/ui/traits/inductive-overflow/supertrait.rs create mode 100644 src/test/ui/traits/inductive-overflow/supertrait.stderr create mode 100644 src/test/ui/traits/inductive-overflow/two-traits.rs create mode 100644 src/test/ui/traits/inductive-overflow/two-traits.stderr create mode 100644 src/test/ui/traits/infer-from-object-issue-26952.rs create mode 100644 src/test/ui/traits/inherent-method-order.rs create mode 100644 src/test/ui/traits/inheritance/auto-xc-2.rs create mode 100644 src/test/ui/traits/inheritance/auto-xc.rs create mode 100644 src/test/ui/traits/inheritance/auto.rs create mode 100644 src/test/ui/traits/inheritance/auxiliary/auto_xc.rs create mode 100644 src/test/ui/traits/inheritance/auxiliary/auto_xc_2.rs create mode 100644 src/test/ui/traits/inheritance/auxiliary/overloading_xc.rs create mode 100644 src/test/ui/traits/inheritance/auxiliary/xc_call.rs create mode 100644 src/test/ui/traits/inheritance/basic.rs create mode 100644 src/test/ui/traits/inheritance/call-bound-inherited.rs create mode 100644 src/test/ui/traits/inheritance/call-bound-inherited2.rs create mode 100644 src/test/ui/traits/inheritance/cast-without-call-to-supertrait.rs create mode 100644 src/test/ui/traits/inheritance/cast.rs create mode 100644 src/test/ui/traits/inheritance/cross-trait-call-xc.rs create mode 100644 src/test/ui/traits/inheritance/cross-trait-call.rs create mode 100644 src/test/ui/traits/inheritance/diamond.rs create mode 100644 src/test/ui/traits/inheritance/multiple-inheritors.rs create mode 100644 src/test/ui/traits/inheritance/multiple-params.rs create mode 100644 src/test/ui/traits/inheritance/num.rs create mode 100644 src/test/ui/traits/inheritance/num0.rs create mode 100644 src/test/ui/traits/inheritance/num1.rs create mode 100644 src/test/ui/traits/inheritance/num2.rs create mode 100644 src/test/ui/traits/inheritance/num3.rs create mode 100644 src/test/ui/traits/inheritance/num5.rs create mode 100644 src/test/ui/traits/inheritance/overloading-simple.rs create mode 100644 src/test/ui/traits/inheritance/overloading-xc-exe.rs create mode 100644 src/test/ui/traits/inheritance/overloading.rs create mode 100644 src/test/ui/traits/inheritance/repeated-supertrait-ambig.rs create mode 100644 src/test/ui/traits/inheritance/repeated-supertrait-ambig.stderr create mode 100644 src/test/ui/traits/inheritance/repeated-supertrait.rs create mode 100644 src/test/ui/traits/inheritance/self-in-supertype.rs create mode 100644 src/test/ui/traits/inheritance/self.rs create mode 100644 src/test/ui/traits/inheritance/simple.rs create mode 100644 src/test/ui/traits/inheritance/static.rs create mode 100644 src/test/ui/traits/inheritance/static2.rs create mode 100644 src/test/ui/traits/inheritance/subst.rs create mode 100644 src/test/ui/traits/inheritance/subst2.rs create mode 100644 src/test/ui/traits/inheritance/visibility.rs create mode 100644 src/test/ui/traits/issue-18400.rs create mode 100644 src/test/ui/traits/issue-18400.stderr create mode 100644 src/test/ui/traits/issue-18412.rs create mode 100644 src/test/ui/traits/issue-20692.rs create mode 100644 src/test/ui/traits/issue-20692.stderr create mode 100644 src/test/ui/traits/issue-22019.rs create mode 100644 src/test/ui/traits/issue-22110.rs create mode 100644 src/test/ui/traits/issue-22655.rs create mode 100644 src/test/ui/traits/issue-23003-overflow.rs create mode 100644 src/test/ui/traits/issue-23003.rs create mode 100644 src/test/ui/traits/issue-23825.rs create mode 100644 src/test/ui/traits/issue-24010.rs create mode 100644 src/test/ui/traits/issue-26339.rs create mode 100644 src/test/ui/traits/issue-28576.rs create mode 100644 src/test/ui/traits/issue-28576.stderr create mode 100644 src/test/ui/traits/issue-32963.rs create mode 100644 src/test/ui/traits/issue-32963.stderr create mode 100644 src/test/ui/traits/issue-33140-hack-boundaries.rs create mode 100644 src/test/ui/traits/issue-33140-hack-boundaries.stderr create mode 100644 src/test/ui/traits/issue-33140.rs create mode 100644 src/test/ui/traits/issue-33140.stderr create mode 100644 src/test/ui/traits/issue-35869.rs create mode 100644 src/test/ui/traits/issue-35869.stderr create mode 100644 src/test/ui/traits/issue-3683.rs create mode 100644 src/test/ui/traits/issue-38033.rs create mode 100644 src/test/ui/traits/issue-38604.rs create mode 100644 src/test/ui/traits/issue-38604.stderr create mode 100644 src/test/ui/traits/issue-3973.rs create mode 100644 src/test/ui/traits/issue-3973.stderr create mode 100644 src/test/ui/traits/issue-4107.rs create mode 100644 src/test/ui/traits/issue-43132.rs create mode 100644 src/test/ui/traits/issue-52893.rs create mode 100644 src/test/ui/traits/issue-52893.stderr create mode 100644 src/test/ui/traits/issue-56202.rs create mode 100644 src/test/ui/traits/issue-56488.rs create mode 100644 src/test/ui/traits/issue-59029-1.rs create mode 100644 src/test/ui/traits/issue-59029-1.stderr create mode 100644 src/test/ui/traits/issue-59029-2.rs create mode 100644 src/test/ui/traits/issue-6128.rs create mode 100644 src/test/ui/traits/issue-6334.rs create mode 100644 src/test/ui/traits/issue-65284-suggest-generic-trait-bound.rs create mode 100644 src/test/ui/traits/issue-65284-suggest-generic-trait-bound.stderr create mode 100644 src/test/ui/traits/issue-65673.rs create mode 100644 src/test/ui/traits/issue-65673.stderr create mode 100644 src/test/ui/traits/issue-68295.rs create mode 100644 src/test/ui/traits/issue-68295.stderr create mode 100644 src/test/ui/traits/issue-70944.rs create mode 100644 src/test/ui/traits/issue-71036.rs create mode 100644 src/test/ui/traits/issue-71036.stderr create mode 100644 src/test/ui/traits/issue-71136.rs create mode 100644 src/test/ui/traits/issue-71136.stderr create mode 100644 src/test/ui/traits/issue-72410.rs create mode 100644 src/test/ui/traits/issue-72410.stderr create mode 100644 src/test/ui/traits/issue-72455.rs create mode 100644 src/test/ui/traits/issue-75627.rs create mode 100644 src/test/ui/traits/issue-75627.stderr create mode 100644 src/test/ui/traits/issue-77982.rs create mode 100644 src/test/ui/traits/issue-77982.stderr create mode 100644 src/test/ui/traits/issue-78372.rs create mode 100644 src/test/ui/traits/issue-78372.stderr create mode 100644 src/test/ui/traits/issue-78632.rs create mode 100644 src/test/ui/traits/issue-79458.rs create mode 100644 src/test/ui/traits/issue-79458.stderr create mode 100644 src/test/ui/traits/issue-8153.rs create mode 100644 src/test/ui/traits/issue-8153.stderr create mode 100644 src/test/ui/traits/issue-82830.rs create mode 100644 src/test/ui/traits/issue-82830.stderr create mode 100644 src/test/ui/traits/issue-83538-tainted-cache-after-cycle.rs create mode 100644 src/test/ui/traits/issue-83538-tainted-cache-after-cycle.stderr create mode 100644 src/test/ui/traits/issue-84399-bad-fresh-caching.rs create mode 100644 src/test/ui/traits/issue-85360-eval-obligation-ice.rs create mode 100644 src/test/ui/traits/issue-85360-eval-obligation-ice.stderr create mode 100644 src/test/ui/traits/issue-85735.rs create mode 100644 src/test/ui/traits/issue-85735.stderr create mode 100644 src/test/ui/traits/issue-87558.rs create mode 100644 src/test/ui/traits/issue-87558.stderr create mode 100644 src/test/ui/traits/issue-89119.rs create mode 100644 src/test/ui/traits/issue-90195-2.rs create mode 100644 src/test/ui/traits/issue-90195.rs create mode 100644 src/test/ui/traits/issue-90662-projection-caching.rs create mode 100644 src/test/ui/traits/issue-91594.rs create mode 100644 src/test/ui/traits/issue-91594.stderr create mode 100644 src/test/ui/traits/issue-91949-hangs-on-recursion.rs create mode 100644 src/test/ui/traits/issue-91949-hangs-on-recursion.stderr create mode 100644 src/test/ui/traits/issue-92292.rs create mode 100644 src/test/ui/traits/issue-9394-inherited-calls.rs create mode 100644 src/test/ui/traits/issue-95311.rs create mode 100644 src/test/ui/traits/issue-95898.rs create mode 100644 src/test/ui/traits/issue-95898.stderr create mode 100644 src/test/ui/traits/issue-96664.rs create mode 100644 src/test/ui/traits/issue-96665.rs create mode 100644 src/test/ui/traits/issue-97576.rs create mode 100644 src/test/ui/traits/issue-97576.stderr create mode 100644 src/test/ui/traits/issue-97695-double-trivial-bound.rs create mode 100644 src/test/ui/traits/item-inside-macro.rs create mode 100644 src/test/ui/traits/item-privacy.rs create mode 100644 src/test/ui/traits/item-privacy.stderr create mode 100644 src/test/ui/traits/kindck-owned-contains-1.rs create mode 100644 src/test/ui/traits/map-types.rs create mode 100644 src/test/ui/traits/map-types.stderr create mode 100644 src/test/ui/traits/matching-lifetimes.rs create mode 100644 src/test/ui/traits/matching-lifetimes.stderr create mode 100644 src/test/ui/traits/method-private.rs create mode 100644 src/test/ui/traits/method-private.stderr create mode 100644 src/test/ui/traits/monad.rs create mode 100644 src/test/ui/traits/monomorphized-callees-with-ty-params-3314.rs create mode 100644 src/test/ui/traits/multidispatch-bad.rs create mode 100644 src/test/ui/traits/multidispatch-bad.stderr create mode 100644 src/test/ui/traits/multidispatch-conditional-impl-not-considered.rs create mode 100644 src/test/ui/traits/multidispatch-convert-ambig-dest.rs create mode 100644 src/test/ui/traits/multidispatch-convert-ambig-dest.stderr create mode 100644 src/test/ui/traits/multidispatch-infer-convert-target.rs create mode 100644 src/test/ui/traits/multidispatch1.rs create mode 100644 src/test/ui/traits/multidispatch2.rs create mode 100644 src/test/ui/traits/mutual-recursion-issue-75860.rs create mode 100644 src/test/ui/traits/mutual-recursion-issue-75860.stderr create mode 100644 src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs create mode 100644 src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.rs create mode 100644 src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.stderr create mode 100644 src/test/ui/traits/negative-impls/feature-gate-negative_impls.rs create mode 100644 src/test/ui/traits/negative-impls/feature-gate-negative_impls.stderr create mode 100644 src/test/ui/traits/negative-impls/negated-auto-traits-error.rs create mode 100644 src/test/ui/traits/negative-impls/negated-auto-traits-error.stderr create mode 100644 src/test/ui/traits/negative-impls/negated-auto-traits-rpass.rs create mode 100644 src/test/ui/traits/negative-impls/negative-default-impls.rs create mode 100644 src/test/ui/traits/negative-impls/negative-default-impls.stderr create mode 100644 src/test/ui/traits/negative-impls/negative-impls-basic.rs create mode 100644 src/test/ui/traits/negative-impls/negative-specializes-negative.rs create mode 100644 src/test/ui/traits/negative-impls/negative-specializes-negative.stderr create mode 100644 src/test/ui/traits/negative-impls/negative-specializes-positive-item.rs create mode 100644 src/test/ui/traits/negative-impls/negative-specializes-positive-item.stderr create mode 100644 src/test/ui/traits/negative-impls/negative-specializes-positive.rs create mode 100644 src/test/ui/traits/negative-impls/negative-specializes-positive.stderr create mode 100644 src/test/ui/traits/negative-impls/no-items.rs create mode 100644 src/test/ui/traits/negative-impls/no-items.stderr create mode 100644 src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs create mode 100644 src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.stderr create mode 100644 src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs create mode 100644 src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.stderr create mode 100644 src/test/ui/traits/negative-impls/positive-specializes-negative.rs create mode 100644 src/test/ui/traits/negative-impls/positive-specializes-negative.stderr create mode 100644 src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs create mode 100644 src/test/ui/traits/no-fallback-multiple-impls.rs create mode 100644 src/test/ui/traits/no-fallback-multiple-impls.stderr create mode 100644 src/test/ui/traits/no_send-struct.rs create mode 100644 src/test/ui/traits/no_send-struct.stderr create mode 100644 src/test/ui/traits/normalize-supertrait.rs create mode 100644 src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.rs create mode 100644 src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.stderr create mode 100644 src/test/ui/traits/object-does-not-impl-trait.rs create mode 100644 src/test/ui/traits/object-does-not-impl-trait.stderr create mode 100644 src/test/ui/traits/object-one-type-two-traits.rs create mode 100644 src/test/ui/traits/object/auto-dedup-in-impl.rs create mode 100644 src/test/ui/traits/object/auto-dedup-in-impl.stderr create mode 100644 src/test/ui/traits/object/auto-dedup.rs create mode 100644 src/test/ui/traits/object/bounds-cycle-1.rs create mode 100644 src/test/ui/traits/object/bounds-cycle-2.rs create mode 100644 src/test/ui/traits/object/bounds-cycle-3.rs create mode 100644 src/test/ui/traits/object/bounds-cycle-4.rs create mode 100644 src/test/ui/traits/object/enforce-supertrait-projection.rs create mode 100644 src/test/ui/traits/object/enforce-supertrait-projection.stderr create mode 100644 src/test/ui/traits/object/exclusion.rs create mode 100644 src/test/ui/traits/object/generics.rs create mode 100644 src/test/ui/traits/object/issue-33140-traitobject-crate.rs create mode 100644 src/test/ui/traits/object/issue-33140-traitobject-crate.stderr create mode 100644 src/test/ui/traits/object/lifetime-first.rs create mode 100644 src/test/ui/traits/object/macro-matcher.rs create mode 100644 src/test/ui/traits/object/macro-matcher.stderr create mode 100644 src/test/ui/traits/object/safety.rs create mode 100644 src/test/ui/traits/object/safety.stderr create mode 100644 src/test/ui/traits/object/supertrait-lifetime-bound.rs create mode 100644 src/test/ui/traits/object/supertrait-lifetime-bound.stderr create mode 100644 src/test/ui/traits/object/vs-lifetime-2.rs create mode 100644 src/test/ui/traits/object/vs-lifetime-2.stderr create mode 100644 src/test/ui/traits/object/vs-lifetime.rs create mode 100644 src/test/ui/traits/object/vs-lifetime.stderr create mode 100644 src/test/ui/traits/object/with-lifetime-bound.rs create mode 100644 src/test/ui/traits/object/with-self-in-projection-output-bad.rs create mode 100644 src/test/ui/traits/object/with-self-in-projection-output-bad.stderr create mode 100644 src/test/ui/traits/object/with-self-in-projection-output-good.rs create mode 100644 src/test/ui/traits/object/with-self-in-projection-output-repeated-supertrait.rs create mode 100644 src/test/ui/traits/objects-owned-object-borrowed-method-headerless.rs create mode 100644 src/test/ui/traits/operator-overloading-issue-52025.rs create mode 100644 src/test/ui/traits/overlap-not-permitted-for-builtin-trait.rs create mode 100644 src/test/ui/traits/overlap-not-permitted-for-builtin-trait.stderr create mode 100644 src/test/ui/traits/overlap-permitted-for-marker-traits.rs create mode 100644 src/test/ui/traits/param-without-lifetime-constraint.rs create mode 100644 src/test/ui/traits/param-without-lifetime-constraint.stderr create mode 100644 src/test/ui/traits/parameterized-with-bounds.rs create mode 100644 src/test/ui/traits/pointee-deduction.rs create mode 100644 src/test/ui/traits/pointee-tail-is-generic-errors.rs create mode 100644 src/test/ui/traits/pointee-tail-is-generic-errors.stderr create mode 100644 src/test/ui/traits/pointee-tail-is-generic.rs create mode 100644 src/test/ui/traits/principal-less-objects.rs create mode 100644 src/test/ui/traits/privacy.rs create mode 100644 src/test/ui/traits/project-modulo-regions.rs create mode 100644 src/test/ui/traits/project-modulo-regions.with_clause.stderr create mode 100644 src/test/ui/traits/project-modulo-regions.without_clause.stderr create mode 100644 src/test/ui/traits/region-pointer-simple.rs create mode 100644 src/test/ui/traits/reservation-impl/coherence-conflict.rs create mode 100644 src/test/ui/traits/reservation-impl/coherence-conflict.stderr create mode 100644 src/test/ui/traits/reservation-impl/no-use.rs create mode 100644 src/test/ui/traits/reservation-impl/no-use.stderr create mode 100644 src/test/ui/traits/reservation-impl/non-lattice-ok.rs create mode 100644 src/test/ui/traits/reservation-impl/ok.rs create mode 100644 src/test/ui/traits/resolution-in-overloaded-op.rs create mode 100644 src/test/ui/traits/resolution-in-overloaded-op.stderr create mode 100644 src/test/ui/traits/safety-fn-body.mir.stderr create mode 100644 src/test/ui/traits/safety-fn-body.rs create mode 100644 src/test/ui/traits/safety-fn-body.thir.stderr create mode 100644 src/test/ui/traits/safety-inherent-impl.rs create mode 100644 src/test/ui/traits/safety-inherent-impl.stderr create mode 100644 src/test/ui/traits/safety-ok-cc.rs create mode 100644 src/test/ui/traits/safety-ok.rs create mode 100644 src/test/ui/traits/safety-trait-impl-cc.rs create mode 100644 src/test/ui/traits/safety-trait-impl-cc.stderr create mode 100644 src/test/ui/traits/safety-trait-impl.rs create mode 100644 src/test/ui/traits/safety-trait-impl.stderr create mode 100644 src/test/ui/traits/self-without-lifetime-constraint.rs create mode 100644 src/test/ui/traits/self-without-lifetime-constraint.stderr create mode 100644 src/test/ui/traits/static-method-generic-inference.rs create mode 100644 src/test/ui/traits/static-method-generic-inference.stderr create mode 100644 src/test/ui/traits/static-method-overwriting.rs create mode 100644 src/test/ui/traits/static-outlives-a-where-clause.rs create mode 100644 src/test/ui/traits/staticness-mismatch.rs create mode 100644 src/test/ui/traits/staticness-mismatch.stderr create mode 100644 src/test/ui/traits/suggest-deferences/issue-39029.fixed create mode 100644 src/test/ui/traits/suggest-deferences/issue-39029.rs create mode 100644 src/test/ui/traits/suggest-deferences/issue-39029.stderr create mode 100644 src/test/ui/traits/suggest-deferences/issue-62530.fixed create mode 100644 src/test/ui/traits/suggest-deferences/issue-62530.rs create mode 100644 src/test/ui/traits/suggest-deferences/issue-62530.stderr create mode 100644 src/test/ui/traits/suggest-deferences/multiple-0.fixed create mode 100644 src/test/ui/traits/suggest-deferences/multiple-0.rs create mode 100644 src/test/ui/traits/suggest-deferences/multiple-0.stderr create mode 100644 src/test/ui/traits/suggest-deferences/multiple-1.rs create mode 100644 src/test/ui/traits/suggest-deferences/multiple-1.stderr create mode 100644 src/test/ui/traits/suggest-deferences/root-obligation.fixed create mode 100644 src/test/ui/traits/suggest-deferences/root-obligation.rs create mode 100644 src/test/ui/traits/suggest-deferences/root-obligation.stderr create mode 100644 src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.rs create mode 100644 src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.stderr create mode 100644 src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.rs create mode 100644 src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.stderr create mode 100644 src/test/ui/traits/suggest-where-clause.rs create mode 100644 src/test/ui/traits/suggest-where-clause.stderr create mode 100644 src/test/ui/traits/superdefault-generics.rs create mode 100644 src/test/ui/traits/syntax-polarity.rs create mode 100644 src/test/ui/traits/syntax-trait-polarity.rs create mode 100644 src/test/ui/traits/syntax-trait-polarity.stderr create mode 100644 src/test/ui/traits/test-2.rs create mode 100644 src/test/ui/traits/test-2.stderr create mode 100644 src/test/ui/traits/test.rs create mode 100644 src/test/ui/traits/test.stderr create mode 100644 src/test/ui/traits/to-str.rs create mode 100644 src/test/ui/traits/trait-or-new-type-instead.rs create mode 100644 src/test/ui/traits/trait-or-new-type-instead.stderr create mode 100644 src/test/ui/traits/trait-upcasting/basic.rs create mode 100644 src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs create mode 100644 src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.rs create mode 100644 src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr create mode 100644 src/test/ui/traits/trait-upcasting/diamond.rs create mode 100644 src/test/ui/traits/trait-upcasting/invalid-upcast.rs create mode 100644 src/test/ui/traits/trait-upcasting/invalid-upcast.stderr create mode 100644 src/test/ui/traits/trait-upcasting/issue-11515-upcast-fn_mut-fn.rs create mode 100644 src/test/ui/traits/trait-upcasting/lifetime.rs create mode 100644 src/test/ui/traits/trait-upcasting/migrate-lint-deny.rs create mode 100644 src/test/ui/traits/trait-upcasting/migrate-lint-deny.stderr create mode 100644 src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.rs create mode 100644 src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.stderr create mode 100644 src/test/ui/traits/trait-upcasting/replace-vptr.rs create mode 100644 src/test/ui/traits/trait-upcasting/struct.rs create mode 100644 src/test/ui/traits/trait-upcasting/subtrait-method.rs create mode 100644 src/test/ui/traits/trait-upcasting/subtrait-method.stderr create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-1.rs create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-1.stderr create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-2.rs create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-2.stderr create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-3.polonius.stderr create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-3.rs create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-3.stderr create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-4.polonius.stderr create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-4.rs create mode 100644 src/test/ui/traits/trait-upcasting/type-checking-test-4.stderr create mode 100644 src/test/ui/traits/typeclasses-eq-example-static.rs create mode 100644 src/test/ui/traits/typeclasses-eq-example.rs create mode 100644 src/test/ui/traits/ufcs-object.rs create mode 100644 src/test/ui/traits/use-before-def.rs create mode 100644 src/test/ui/traits/vtable-res-trait-param.rs create mode 100644 src/test/ui/traits/vtable-res-trait-param.stderr create mode 100644 src/test/ui/traits/vtable/issue-91807.rs create mode 100644 src/test/ui/traits/vtable/issue-97381.rs create mode 100644 src/test/ui/traits/vtable/issue-97381.stderr create mode 100644 src/test/ui/traits/vtable/vtable-diamond.rs create mode 100644 src/test/ui/traits/vtable/vtable-diamond.stderr create mode 100644 src/test/ui/traits/vtable/vtable-multi-level.rs create mode 100644 src/test/ui/traits/vtable/vtable-multi-level.stderr create mode 100644 src/test/ui/traits/vtable/vtable-multiple.rs create mode 100644 src/test/ui/traits/vtable/vtable-multiple.stderr create mode 100644 src/test/ui/traits/vtable/vtable-non-object-safe.rs create mode 100644 src/test/ui/traits/vtable/vtable-non-object-safe.stderr create mode 100644 src/test/ui/traits/vtable/vtable-vacant.rs create mode 100644 src/test/ui/traits/vtable/vtable-vacant.stderr create mode 100644 src/test/ui/traits/wf-object/maybe-bound.rs create mode 100644 src/test/ui/traits/wf-object/maybe-bound.stderr create mode 100644 src/test/ui/traits/wf-object/no-duplicates.rs create mode 100644 src/test/ui/traits/wf-object/no-duplicates.stderr create mode 100644 src/test/ui/traits/wf-object/only-maybe-bound.rs create mode 100644 src/test/ui/traits/wf-object/only-maybe-bound.stderr create mode 100644 src/test/ui/traits/wf-object/reverse-order.rs create mode 100644 src/test/ui/traits/where-clause-vs-impl.rs create mode 100644 src/test/ui/traits/with-bounds-default.rs create mode 100644 src/test/ui/traits/with-dst.rs (limited to 'src/test/ui/traits') diff --git a/src/test/ui/traits/alias/ambiguous.rs b/src/test/ui/traits/alias/ambiguous.rs new file mode 100644 index 000000000..28409e0c6 --- /dev/null +++ b/src/test/ui/traits/alias/ambiguous.rs @@ -0,0 +1,24 @@ +#![feature(trait_alias)] + +mod inner { + pub trait A { fn foo(&self); } + pub trait B { fn foo(&self); } + + impl A for u8 { + fn foo(&self) {} + } + impl B for u8 { + fn foo(&self) {} + } + + pub trait C = A + B; +} + +use inner::C; + +fn main() { + let t = 1u8; + t.foo(); //~ ERROR E0034 + + inner::A::foo(&t); // ok +} diff --git a/src/test/ui/traits/alias/ambiguous.stderr b/src/test/ui/traits/alias/ambiguous.stderr new file mode 100644 index 000000000..0fe1a7967 --- /dev/null +++ b/src/test/ui/traits/alias/ambiguous.stderr @@ -0,0 +1,28 @@ +error[E0034]: multiple applicable items in scope + --> $DIR/ambiguous.rs:21:7 + | +LL | t.foo(); + | ^^^ multiple `foo` found + | +note: candidate #1 is defined in an impl of the trait `A` for the type `u8` + --> $DIR/ambiguous.rs:8:9 + | +LL | fn foo(&self) {} + | ^^^^^^^^^^^^^ +note: candidate #2 is defined in an impl of the trait `B` for the type `u8` + --> $DIR/ambiguous.rs:11:9 + | +LL | fn foo(&self) {} + | ^^^^^^^^^^^^^ +help: disambiguate the associated function for candidate #1 + | +LL | A::foo(&t); + | ~~~~~~~~~~ +help: disambiguate the associated function for candidate #2 + | +LL | B::foo(&t); + | ~~~~~~~~~~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0034`. diff --git a/src/test/ui/traits/alias/auxiliary/greeter.rs b/src/test/ui/traits/alias/auxiliary/greeter.rs new file mode 100644 index 000000000..9e4122155 --- /dev/null +++ b/src/test/ui/traits/alias/auxiliary/greeter.rs @@ -0,0 +1,13 @@ +#![feature(trait_alias)] + +pub trait Hello { + fn hello(&self); +} + +pub struct Hi; + +impl Hello for Hi { + fn hello(&self) {} +} + +pub trait Greet = Hello; diff --git a/src/test/ui/traits/alias/auxiliary/send_sync.rs b/src/test/ui/traits/alias/auxiliary/send_sync.rs new file mode 100644 index 000000000..9e56b87e0 --- /dev/null +++ b/src/test/ui/traits/alias/auxiliary/send_sync.rs @@ -0,0 +1,3 @@ +#![feature(trait_alias)] + +pub trait SendSync = Send + Sync; diff --git a/src/test/ui/traits/alias/basic.rs b/src/test/ui/traits/alias/basic.rs new file mode 100644 index 000000000..d8168f299 --- /dev/null +++ b/src/test/ui/traits/alias/basic.rs @@ -0,0 +1,8 @@ +// run-pass + +#![feature(trait_alias)] + +pub trait Foo {} +pub trait FooAlias = Foo; + +fn main() {} diff --git a/src/test/ui/traits/alias/bounds.rs b/src/test/ui/traits/alias/bounds.rs new file mode 100644 index 000000000..b97eb38c5 --- /dev/null +++ b/src/test/ui/traits/alias/bounds.rs @@ -0,0 +1,57 @@ +// run-pass + +#![feature(trait_alias)] + +use std::marker::PhantomData; + +trait Empty {} +trait EmptyAlias = Empty; +trait CloneDefault = Clone + Default; +trait SendSyncAlias = Send + Sync; +trait WhereSendAlias = where Self: Send; +trait SendEqAlias = Send where T: PartialEq; +trait I32Iterator = Iterator; + +#[allow(dead_code)] +struct Foo(PhantomData); +#[allow(dead_code)] +struct Bar(PhantomData) where T: SendSyncAlias; + +impl dyn EmptyAlias {} + +impl Empty for T {} + +fn a() -> (T, T) { + let one = T::default(); + let two = one.clone(); + (one, two) +} + +fn b(x: &impl SendEqAlias) -> bool { + 22_i32 == *x +} + +fn c(x: &mut T) -> Option { + x.next() +} + +fn d() { + is_send_and_sync::(); +} + +fn is_send_and_sync() {} + +fn main() { + let both = a::(); + assert_eq!(both.0, 0); + assert_eq!(both.1, 0); + let both: (i32, i32) = a(); + assert_eq!(both.0, 0); + assert_eq!(both.1, 0); + + assert!(b(&22)); + + assert_eq!(c(&mut vec![22].into_iter()), Some(22)); + + d::(); +} diff --git a/src/test/ui/traits/alias/cross-crate.rs b/src/test/ui/traits/alias/cross-crate.rs new file mode 100644 index 000000000..8919c6434 --- /dev/null +++ b/src/test/ui/traits/alias/cross-crate.rs @@ -0,0 +1,17 @@ +// aux-build:send_sync.rs + +#![feature(trait_alias)] + +extern crate send_sync; + +use std::rc::Rc; +use send_sync::SendSync; + +fn use_alias() {} + +fn main() { + use_alias::(); + use_alias::>(); + //~^ ERROR `Rc` cannot be sent between threads safely [E0277] + //~^^ ERROR `Rc` cannot be shared between threads safely [E0277] +} diff --git a/src/test/ui/traits/alias/cross-crate.stderr b/src/test/ui/traits/alias/cross-crate.stderr new file mode 100644 index 000000000..d0d00c97e --- /dev/null +++ b/src/test/ui/traits/alias/cross-crate.stderr @@ -0,0 +1,31 @@ +error[E0277]: `Rc` cannot be sent between threads safely + --> $DIR/cross-crate.rs:14:17 + | +LL | use_alias::>(); + | ^^^^^^^ `Rc` cannot be sent between threads safely + | + = help: the trait `Send` is not implemented for `Rc` + = note: required because of the requirements on the impl of `SendSync` for `Rc` +note: required by a bound in `use_alias` + --> $DIR/cross-crate.rs:10:17 + | +LL | fn use_alias() {} + | ^^^^^^^^ required by this bound in `use_alias` + +error[E0277]: `Rc` cannot be shared between threads safely + --> $DIR/cross-crate.rs:14:17 + | +LL | use_alias::>(); + | ^^^^^^^ `Rc` cannot be shared between threads safely + | + = help: the trait `Sync` is not implemented for `Rc` + = note: required because of the requirements on the impl of `SendSync` for `Rc` +note: required by a bound in `use_alias` + --> $DIR/cross-crate.rs:10:17 + | +LL | fn use_alias() {} + | ^^^^^^^^ required by this bound in `use_alias` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/alias/impl.rs b/src/test/ui/traits/alias/impl.rs new file mode 100644 index 000000000..6e35793a0 --- /dev/null +++ b/src/test/ui/traits/alias/impl.rs @@ -0,0 +1,7 @@ +#![feature(trait_alias)] + +trait DefaultAlias = Default; + +impl DefaultAlias for () {} //~ ERROR expected trait, found trait alias + +fn main() {} diff --git a/src/test/ui/traits/alias/impl.stderr b/src/test/ui/traits/alias/impl.stderr new file mode 100644 index 000000000..cedcd1021 --- /dev/null +++ b/src/test/ui/traits/alias/impl.stderr @@ -0,0 +1,9 @@ +error[E0404]: expected trait, found trait alias `DefaultAlias` + --> $DIR/impl.rs:5:6 + | +LL | impl DefaultAlias for () {} + | ^^^^^^^^^^^^ not a trait + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0404`. diff --git a/src/test/ui/traits/alias/import-cross-crate.rs b/src/test/ui/traits/alias/import-cross-crate.rs new file mode 100644 index 000000000..868585cd0 --- /dev/null +++ b/src/test/ui/traits/alias/import-cross-crate.rs @@ -0,0 +1,14 @@ +// run-pass +// aux-build:greeter.rs + +#![feature(trait_alias)] + +extern crate greeter; + +// Import only the alias, not the real trait. +use greeter::{Greet, Hi}; + +fn main() { + let hi = Hi; + hi.hello(); // From `Hello`, via `Greet` alias. +} diff --git a/src/test/ui/traits/alias/import.rs b/src/test/ui/traits/alias/import.rs new file mode 100644 index 000000000..802a8f156 --- /dev/null +++ b/src/test/ui/traits/alias/import.rs @@ -0,0 +1,40 @@ +// run-pass + +#![feature(trait_alias)] + +mod inner { + pub trait Foo { + fn foo(&self); + } + + pub struct Qux; + + impl Foo for Qux { + fn foo(&self) {} + } + + pub trait Bar = Foo; +} + +mod two { + pub trait A { + fn foo(); + } + + impl A for u8 { + fn foo() {} + } +} + +// Import only the alias, not the `Foo` trait. +use inner::{Bar, Qux}; + +// Declaring an alias also brings in aliased methods. +trait Two = two::A; + +fn main() { + let q = Qux; + q.foo(); // From Bar. + + u8::foo(); // From A. +} diff --git a/src/test/ui/traits/alias/issue-60021-assoc-method-resolve.rs b/src/test/ui/traits/alias/issue-60021-assoc-method-resolve.rs new file mode 100644 index 000000000..5e27ed3c6 --- /dev/null +++ b/src/test/ui/traits/alias/issue-60021-assoc-method-resolve.rs @@ -0,0 +1,19 @@ +// check-pass + +#![feature(trait_alias)] + +trait SomeTrait { + fn map(&self) {} +} + +impl SomeTrait for Option {} + +trait SomeAlias = SomeTrait; + +fn main() { + let x = Some(123); + // This should resolve to the trait impl for Option + Option::map(x, |z| z); + // This should resolve to the trait impl for SomeTrait + SomeTrait::map(&x); +} diff --git a/src/test/ui/traits/alias/issue-72415-assoc-const-resolve.rs b/src/test/ui/traits/alias/issue-72415-assoc-const-resolve.rs new file mode 100644 index 000000000..e49125d10 --- /dev/null +++ b/src/test/ui/traits/alias/issue-72415-assoc-const-resolve.rs @@ -0,0 +1,14 @@ +// check-pass + +#![feature(trait_alias)] + +trait Bounded { const MAX: Self; } + +impl Bounded for u32 { + // This should correctly resolve to the associated const in the inherent impl of u32. + const MAX: Self = u32::MAX; +} + +trait Num = Bounded + Copy; + +fn main() {} diff --git a/src/test/ui/traits/alias/issue-75983.rs b/src/test/ui/traits/alias/issue-75983.rs new file mode 100644 index 000000000..f9a7f36de --- /dev/null +++ b/src/test/ui/traits/alias/issue-75983.rs @@ -0,0 +1,17 @@ +// check-pass + +#![feature(trait_alias)] + +struct Bar; +trait Foo {} +impl Foo for Bar {} + +trait Baz = Foo where Bar: Foo; + +fn new() -> impl Baz { + Bar +} + +fn main() { + let _ = new(); +} diff --git a/src/test/ui/traits/alias/issue-83613.rs b/src/test/ui/traits/alias/issue-83613.rs new file mode 100644 index 000000000..04320e720 --- /dev/null +++ b/src/test/ui/traits/alias/issue-83613.rs @@ -0,0 +1,13 @@ +#![feature(type_alias_impl_trait)] +trait OpaqueTrait {} +impl OpaqueTrait for T {} +type OpaqueType = impl OpaqueTrait; +fn mk_opaque() -> OpaqueType { + || 0 +} +trait AnotherTrait {} +impl AnotherTrait for T {} +impl AnotherTrait for OpaqueType {} +//~^ ERROR conflicting implementations of trait `AnotherTrait` for type `OpaqueType` +//~| ERROR cannot implement trait on type alias impl trait +fn main() {} diff --git a/src/test/ui/traits/alias/issue-83613.stderr b/src/test/ui/traits/alias/issue-83613.stderr new file mode 100644 index 000000000..b9d931601 --- /dev/null +++ b/src/test/ui/traits/alias/issue-83613.stderr @@ -0,0 +1,23 @@ +error[E0119]: conflicting implementations of trait `AnotherTrait` for type `OpaqueType` + --> $DIR/issue-83613.rs:10:1 + | +LL | impl AnotherTrait for T {} + | -------------------------------- first implementation here +LL | impl AnotherTrait for OpaqueType {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `OpaqueType` + +error: cannot implement trait on type alias impl trait + --> $DIR/issue-83613.rs:10:23 + | +LL | impl AnotherTrait for OpaqueType {} + | ^^^^^^^^^^ + | +note: type alias impl trait defined here + --> $DIR/issue-83613.rs:4:19 + | +LL | type OpaqueType = impl OpaqueTrait; + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0119`. diff --git a/src/test/ui/traits/alias/maybe-bound.rs b/src/test/ui/traits/alias/maybe-bound.rs new file mode 100644 index 000000000..284baa481 --- /dev/null +++ b/src/test/ui/traits/alias/maybe-bound.rs @@ -0,0 +1,29 @@ +// build-pass (FIXME(62277): could be check-pass?) + +// Test that `dyn ... + ?Sized + ...` resulting from the expansion of trait aliases is okay. + +#![feature(trait_alias)] + +trait Foo {} + +trait S = ?Sized; + +// Nest a couple of levels deep: +trait _0 = S; +trait _1 = _0; + +// Straight list expansion: +type _T0 = dyn _1 + Foo; + +// In second position: +type _T1 = dyn Foo + _1; + +// ... and with an auto trait: +type _T2 = dyn Foo + Send + _1; + +// Twice: +trait _2 = _1 + _1; + +type _T3 = dyn _2 + Foo; + +fn main() {} diff --git a/src/test/ui/traits/alias/no-duplicates.rs b/src/test/ui/traits/alias/no-duplicates.rs new file mode 100644 index 000000000..88feb8917 --- /dev/null +++ b/src/test/ui/traits/alias/no-duplicates.rs @@ -0,0 +1,126 @@ +// The purpose of this test is to demonstrate that duplicating object safe traits +// that are not auto traits is rejected with trait aliases even though one could +// reasonably accept this. + +#![feature(trait_alias)] + +use std::marker::Unpin; + +// Some arbitrary object-safe trait: +trait Obj {} + +// Nest a few levels deep: +trait _0 = Obj; +trait _1 = _0; + +type _T00 = dyn _0 + _0; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T01 = dyn _1 + _0; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T02 = dyn _1 + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T03 = dyn Obj + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T04 = dyn _1 + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Nest some more and in weird ways: + +trait _2 = _0 + _1; +trait _3 = Obj; +trait _4 = _3; + +type _T10 = dyn _2 + _3; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T11 = dyn _3 + _2; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T12 = dyn Obj + _2; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T13 = dyn _2 + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T14 = dyn _1 + _3; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T15 = dyn _3 + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T16 = dyn _1 + _4; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T17 = dyn _4 + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Include auto traits: + +trait _5 = Obj + Send; + +type _T20 = dyn _5 + _5; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T21 = dyn Obj + _5; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T22 = dyn _5 + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T23 = dyn _5 + Send + Sync + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Also nest: + +trait _6 = _5 + _5; // ==> Obj + Send + Obj + Send + +type _T30 = dyn _6; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T31 = dyn _6 + Send; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T32 = dyn Send + _6; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Nest some more: + +trait _7 = _5 + Sync; +trait _8 = Unpin + _7; + +type _T40 = dyn _8 + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T41 = dyn Obj + _8; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T42 = dyn _8 + _4; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T43 = dyn _4 + _8; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T44 = dyn _4 + Send + Sync + _8; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Take higher ranked types into account. + +// Note that `'a` and `'b` are intentionally different to make sure we consider +// them semantically the same. +trait ObjL<'l> {} +trait _9 = for<'a> ObjL<'a>; +trait _10 = for<'b> ObjL<'b>; +type _T50 = dyn _9 + _10; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +trait ObjT {} +trait _11 = ObjT fn(&'a u8)>; +trait _12 = ObjT fn(&'b u8)>; +type _T60 = dyn _11 + _12; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +fn main() {} diff --git a/src/test/ui/traits/alias/no-duplicates.stderr b/src/test/ui/traits/alias/no-duplicates.stderr new file mode 100644 index 000000000..bf244b97e --- /dev/null +++ b/src/test/ui/traits/alias/no-duplicates.stderr @@ -0,0 +1,527 @@ +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:16:22 + | +LL | trait _0 = Obj; + | --- + | | + | additional non-auto trait + | first non-auto trait +... +LL | type _T00 = dyn _0 + _0; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:19:22 + | +LL | trait _0 = Obj; + | --- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | type _T01 = dyn _1 + _0; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:22:22 + | +LL | trait _0 = Obj; + | --- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _1 = _0; + | -- + | | + | referenced here (additional use) + | referenced here (first use) +... +LL | type _T02 = dyn _1 + _1; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:25:23 + | +LL | trait _0 = Obj; + | --- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | type _T03 = dyn Obj + _1; + | --- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:28:22 + | +LL | trait _0 = Obj; + | --- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | type _T04 = dyn _1 + Obj; + | -- ^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:37:17 + | +LL | trait _0 = Obj; + | --- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _2 = _0 + _1; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +... +LL | type _T10 = dyn _2 + _3; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:40:22 + | +LL | trait _0 = Obj; + | --- additional non-auto trait +... +LL | trait _2 = _0 + _1; + | -- referenced here (additional use) +LL | trait _3 = Obj; + | --- first non-auto trait +... +LL | type _T11 = dyn _3 + _2; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:43:23 + | +LL | trait _0 = Obj; + | --- additional non-auto trait +... +LL | trait _2 = _0 + _1; + | -- referenced here (additional use) +... +LL | type _T12 = dyn Obj + _2; + | --- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:46:17 + | +LL | trait _0 = Obj; + | --- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _2 = _0 + _1; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +... +LL | type _T13 = dyn _2 + Obj; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:49:22 + | +LL | trait _0 = Obj; + | --- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _3 = Obj; + | --- additional non-auto trait +... +LL | type _T14 = dyn _1 + _3; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:52:22 + | +LL | trait _0 = Obj; + | --- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _3 = Obj; + | --- first non-auto trait +... +LL | type _T15 = dyn _3 + _1; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:55:22 + | +LL | trait _0 = Obj; + | --- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _3 = Obj; + | --- additional non-auto trait +LL | trait _4 = _3; + | -- referenced here (additional use) +... +LL | type _T16 = dyn _1 + _4; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:58:22 + | +LL | trait _0 = Obj; + | --- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _3 = Obj; + | --- first non-auto trait +LL | trait _4 = _3; + | -- referenced here (first use) +... +LL | type _T17 = dyn _4 + _1; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:65:22 + | +LL | trait _5 = Obj + Send; + | --- + | | + | additional non-auto trait + | first non-auto trait +LL | +LL | type _T20 = dyn _5 + _5; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:68:23 + | +LL | trait _5 = Obj + Send; + | --- additional non-auto trait +... +LL | type _T21 = dyn Obj + _5; + | --- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:71:22 + | +LL | trait _5 = Obj + Send; + | --- first non-auto trait +... +LL | type _T22 = dyn _5 + Obj; + | -- ^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:74:36 + | +LL | trait _5 = Obj + Send; + | --- first non-auto trait +... +LL | type _T23 = dyn _5 + Send + Sync + Obj; + | -- ^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:81:17 + | +LL | trait _5 = Obj + Send; + | --- + | | + | additional non-auto trait + | first non-auto trait +... +LL | trait _6 = _5 + _5; // ==> Obj + Send + Obj + Send + | -- -- referenced here (additional use) + | | + | referenced here (first use) +LL | +LL | type _T30 = dyn _6; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:84:17 + | +LL | trait _5 = Obj + Send; + | --- + | | + | additional non-auto trait + | first non-auto trait +... +LL | trait _6 = _5 + _5; // ==> Obj + Send + Obj + Send + | -- -- referenced here (additional use) + | | + | referenced here (first use) +... +LL | type _T31 = dyn _6 + Send; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:87:24 + | +LL | trait _5 = Obj + Send; + | --- + | | + | additional non-auto trait + | first non-auto trait +... +LL | trait _6 = _5 + _5; // ==> Obj + Send + Obj + Send + | -- -- referenced here (additional use) + | | + | referenced here (first use) +... +LL | type _T32 = dyn Send + _6; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:95:22 + | +LL | trait _5 = Obj + Send; + | --- first non-auto trait +... +LL | trait _7 = _5 + Sync; + | -- referenced here (first use) +LL | trait _8 = Unpin + _7; + | -- referenced here (first use) +LL | +LL | type _T40 = dyn _8 + Obj; + | -- ^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:98:23 + | +LL | trait _5 = Obj + Send; + | --- additional non-auto trait +... +LL | trait _7 = _5 + Sync; + | -- referenced here (additional use) +LL | trait _8 = Unpin + _7; + | -- referenced here (additional use) +... +LL | type _T41 = dyn Obj + _8; + | --- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:101:22 + | +LL | trait _3 = Obj; + | --- additional non-auto trait +LL | trait _4 = _3; + | -- referenced here (additional use) +... +LL | trait _5 = Obj + Send; + | --- first non-auto trait +... +LL | trait _7 = _5 + Sync; + | -- referenced here (first use) +LL | trait _8 = Unpin + _7; + | -- referenced here (first use) +... +LL | type _T42 = dyn _8 + _4; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:104:22 + | +LL | trait _3 = Obj; + | --- first non-auto trait +LL | trait _4 = _3; + | -- referenced here (first use) +... +LL | trait _5 = Obj + Send; + | --- additional non-auto trait +... +LL | trait _7 = _5 + Sync; + | -- referenced here (additional use) +LL | trait _8 = Unpin + _7; + | -- referenced here (additional use) +... +LL | type _T43 = dyn _4 + _8; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:107:36 + | +LL | trait _3 = Obj; + | --- first non-auto trait +LL | trait _4 = _3; + | -- referenced here (first use) +... +LL | trait _5 = Obj + Send; + | --- additional non-auto trait +... +LL | trait _7 = _5 + Sync; + | -- referenced here (additional use) +LL | trait _8 = Unpin + _7; + | -- referenced here (additional use) +... +LL | type _T44 = dyn _4 + Send + Sync + _8; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:117:22 + | +LL | trait _9 = for<'a> ObjL<'a>; + | ---------------- first non-auto trait +LL | trait _10 = for<'b> ObjL<'b>; + | ---------------- additional non-auto trait +LL | type _T50 = dyn _9 + _10; + | -- ^^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: for<'a> ObjL<'a> + for<'b> ObjL<'b> {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:123:23 + | +LL | trait _11 = ObjT fn(&'a u8)>; + | ------------------------ first non-auto trait +LL | trait _12 = ObjT fn(&'b u8)>; + | ------------------------ additional non-auto trait +LL | type _T60 = dyn _11 + _12; + | --- ^^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjT fn(&'a u8)> + ObjT fn(&'b u8)> {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error: aborting due to 27 previous errors + +For more information about this error, try `rustc --explain E0225`. diff --git a/src/test/ui/traits/alias/no-extra-traits.rs b/src/test/ui/traits/alias/no-extra-traits.rs new file mode 100644 index 000000000..4dad8c0f8 --- /dev/null +++ b/src/test/ui/traits/alias/no-extra-traits.rs @@ -0,0 +1,121 @@ +// The purpose of this test is to demonstrate that trait alias expansion +// preserves the rule that `dyn Trait` may only reference one non-auto trait. + +#![feature(trait_alias)] + +use std::marker::Unpin; + +// Some arbitrary object-safe traits: +trait ObjA {} +trait ObjB {} + +// Nest a few levels deep: +trait _0 = ObjA; +trait _1 = _0; + +type _T00 = dyn _0 + ObjB; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T01 = dyn ObjB + _0; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T02 = dyn ObjB + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T03 = dyn _1 + ObjB; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Nest some more and in weird ways: + +trait _2 = ObjB; +trait _3 = _2; +trait _4 = _3; + +type _T10 = dyn _2 + _3; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T11 = dyn _3 + _2; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T12 = dyn _2 + _4; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T13 = dyn _4 + _2; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Include auto traits: + +trait _5 = Sync + ObjB + Send; + +type _T20 = dyn _5 + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T21 = dyn _1 + _5; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T22 = dyn _5 + ObjA; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T23 = dyn ObjA + _5; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T24 = dyn Send + _5 + _1 + Sync; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T25 = dyn _1 + Sync + _5 + Send; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T26 = dyn Sync + Send + _5 + ObjA; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T27 = dyn Send + Sync + ObjA + _5; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Also nest: + +trait _6 = _1 + _5; +trait _7 = _6; +trait _8 = _7; + +type _T30 = dyn _6; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T31 = dyn _6 + Send; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T32 = dyn Send + _6; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T33 = dyn _8; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T34 = dyn _8 + Send; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T35 = dyn Send + _8; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Nest some more: + +trait _9 = _5 + Sync; +trait _10 = Unpin + _9; + +type _T40 = dyn _10 + ObjA; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T41 = dyn ObjA + _10; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T42 = dyn _10 + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T43 = dyn Send + _10 + Sync + ObjA; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T44 = dyn ObjA + _10 + Send + Sync; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _T45 = dyn Sync + Send + _10 + _1; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +fn main() {} diff --git a/src/test/ui/traits/alias/no-extra-traits.stderr b/src/test/ui/traits/alias/no-extra-traits.stderr new file mode 100644 index 000000000..4b1ddf684 --- /dev/null +++ b/src/test/ui/traits/alias/no-extra-traits.stderr @@ -0,0 +1,579 @@ +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:16:22 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +... +LL | type _T00 = dyn _0 + ObjB; + | -- ^^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:19:24 + | +LL | trait _0 = ObjA; + | ---- additional non-auto trait +... +LL | type _T01 = dyn ObjB + _0; + | ---- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:22:24 + | +LL | trait _0 = ObjA; + | ---- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | type _T02 = dyn ObjB + _1; + | ---- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:25:22 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | type _T03 = dyn _1 + ObjB; + | -- ^^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:34:22 + | +LL | trait _2 = ObjB; + | ---- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _3 = _2; + | -- referenced here (additional use) +... +LL | type _T10 = dyn _2 + _3; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:37:22 + | +LL | trait _2 = ObjB; + | ---- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _3 = _2; + | -- referenced here (first use) +... +LL | type _T11 = dyn _3 + _2; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:40:22 + | +LL | trait _2 = ObjB; + | ---- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _3 = _2; + | -- referenced here (additional use) +LL | trait _4 = _3; + | -- referenced here (additional use) +... +LL | type _T12 = dyn _2 + _4; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:43:22 + | +LL | trait _2 = ObjB; + | ---- + | | + | additional non-auto trait + | first non-auto trait +LL | trait _3 = _2; + | -- referenced here (first use) +LL | trait _4 = _3; + | -- referenced here (first use) +... +LL | type _T13 = dyn _4 + _2; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:50:22 + | +LL | trait _0 = ObjA; + | ---- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +LL | +LL | type _T20 = dyn _5 + _1; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:53:22 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | type _T21 = dyn _1 + _5; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:56:22 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +... +LL | type _T22 = dyn _5 + ObjA; + | -- ^^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:59:24 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | type _T23 = dyn ObjA + _5; + | ---- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:62:29 + | +LL | trait _0 = ObjA; + | ---- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +... +LL | type _T24 = dyn Send + _5 + _1 + Sync; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:65:29 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | type _T25 = dyn _1 + Sync + _5 + Send; + | -- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:68:36 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +... +LL | type _T26 = dyn Sync + Send + _5 + ObjA; + | -- ^^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:71:38 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | type _T27 = dyn Send + Sync + ObjA + _5; + | ---- ^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:80:17 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _6 = _1 + _5; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +... +LL | type _T30 = dyn _6; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:83:17 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _6 = _1 + _5; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +... +LL | type _T31 = dyn _6 + Send; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:86:24 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _6 = _1 + _5; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +... +LL | type _T32 = dyn Send + _6; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:89:17 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _6 = _1 + _5; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +LL | trait _7 = _6; + | -- + | | + | referenced here (additional use) + | referenced here (first use) +LL | trait _8 = _7; + | -- + | | + | referenced here (additional use) + | referenced here (first use) +... +LL | type _T33 = dyn _8; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:92:17 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _6 = _1 + _5; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +LL | trait _7 = _6; + | -- + | | + | referenced here (additional use) + | referenced here (first use) +LL | trait _8 = _7; + | -- + | | + | referenced here (additional use) + | referenced here (first use) +... +LL | type _T34 = dyn _8 + Send; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:95:24 + | +LL | trait _0 = ObjA; + | ---- first non-auto trait +LL | trait _1 = _0; + | -- referenced here (first use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _6 = _1 + _5; + | -- -- referenced here (additional use) + | | + | referenced here (first use) +LL | trait _7 = _6; + | -- + | | + | referenced here (additional use) + | referenced here (first use) +LL | trait _8 = _7; + | -- + | | + | referenced here (additional use) + | referenced here (first use) +... +LL | type _T35 = dyn Send + _8; + | ^^ + | | + | trait alias used in trait object type (additional use) + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:103:23 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +... +LL | trait _9 = _5 + Sync; + | -- referenced here (first use) +LL | trait _10 = Unpin + _9; + | -- referenced here (first use) +LL | +LL | type _T40 = dyn _10 + ObjA; + | --- ^^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:106:24 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _9 = _5 + Sync; + | -- referenced here (additional use) +LL | trait _10 = Unpin + _9; + | -- referenced here (additional use) +... +LL | type _T41 = dyn ObjA + _10; + | ---- ^^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:109:23 + | +LL | trait _0 = ObjA; + | ---- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +... +LL | trait _9 = _5 + Sync; + | -- referenced here (first use) +LL | trait _10 = Unpin + _9; + | -- referenced here (first use) +... +LL | type _T42 = dyn _10 + _1; + | --- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:112:37 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +... +LL | trait _9 = _5 + Sync; + | -- referenced here (first use) +LL | trait _10 = Unpin + _9; + | -- referenced here (first use) +... +LL | type _T43 = dyn Send + _10 + Sync + ObjA; + | --- ^^^^ additional non-auto trait + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:115:24 + | +LL | trait _5 = Sync + ObjB + Send; + | ---- additional non-auto trait +... +LL | trait _9 = _5 + Sync; + | -- referenced here (additional use) +LL | trait _10 = Unpin + _9; + | -- referenced here (additional use) +... +LL | type _T44 = dyn ObjA + _10 + Send + Sync; + | ---- ^^^ trait alias used in trait object type (additional use) + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjA + ObjB {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-extra-traits.rs:118:37 + | +LL | trait _0 = ObjA; + | ---- additional non-auto trait +LL | trait _1 = _0; + | -- referenced here (additional use) +... +LL | trait _5 = Sync + ObjB + Send; + | ---- first non-auto trait +... +LL | trait _9 = _5 + Sync; + | -- referenced here (first use) +LL | trait _10 = Unpin + _9; + | -- referenced here (first use) +... +LL | type _T45 = dyn Sync + Send + _10 + _1; + | --- ^^ trait alias used in trait object type (additional use) + | | + | trait alias used in trait object type (first use) + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjB + ObjA {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error: aborting due to 28 previous errors + +For more information about this error, try `rustc --explain E0225`. diff --git a/src/test/ui/traits/alias/object-fail.rs b/src/test/ui/traits/alias/object-fail.rs new file mode 100644 index 000000000..5c753ff20 --- /dev/null +++ b/src/test/ui/traits/alias/object-fail.rs @@ -0,0 +1,11 @@ +#![feature(trait_alias)] + +trait EqAlias = Eq; +trait IteratorAlias = Iterator; + +fn main() { + let _: &dyn EqAlias = &123; + //~^ ERROR the trait `Eq` cannot be made into an object [E0038] + let _: &dyn IteratorAlias = &vec![123].into_iter(); + //~^ ERROR must be specified +} diff --git a/src/test/ui/traits/alias/object-fail.stderr b/src/test/ui/traits/alias/object-fail.stderr new file mode 100644 index 000000000..325bc6d28 --- /dev/null +++ b/src/test/ui/traits/alias/object-fail.stderr @@ -0,0 +1,22 @@ +error[E0038]: the trait `Eq` cannot be made into an object + --> $DIR/object-fail.rs:7:13 + | +LL | let _: &dyn EqAlias = &123; + | ^^^^^^^^^^^ `Eq` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $SRC_DIR/core/src/cmp.rs:LL:COL + | +LL | pub trait Eq: PartialEq { + | ^^^^^^^^^^^^^^^ the trait cannot be made into an object because it uses `Self` as a type parameter + +error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified + --> $DIR/object-fail.rs:9:17 + | +LL | let _: &dyn IteratorAlias = &vec![123].into_iter(); + | ^^^^^^^^^^^^^ help: specify the associated type: `IteratorAlias` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0038, E0191. +For more information about an error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/alias/object-wf.rs b/src/test/ui/traits/alias/object-wf.rs new file mode 100644 index 000000000..1440f02df --- /dev/null +++ b/src/test/ui/traits/alias/object-wf.rs @@ -0,0 +1,85 @@ +// check-pass + +// This test checks that trait objects involving trait aliases are well-formed. + +#![feature(trait_alias)] + +trait Obj {} + +trait _0 = Send + Sync; + +// Just auto traits: + +trait _1 = _0 + Send + Sync; + +use std::marker::Unpin; + +fn _f0() { + let _: Box; + let _: Box; + let _: Box; +} + +// Include object safe traits: + +fn _f1() { + let _: Box; + let _: Box; + let _: Box; +} + +// And when the object safe trait is in a trait alias: + +trait _2 = Obj; + +fn _f2() { + let _: Box; + let _: Box; + let _: Box; +} + +// And it should also work when that trait is has auto traits to the right of it. + +trait _3 = Obj + Unpin; + +fn _f3() { + let _: Box; + let _: Box; + let _: Box; +} + +// Nest the trait deeply: + +trait _4 = _3; +trait _5 = _4 + Sync + _0 + Send; +trait _6 = _5 + Send + _1 + Sync; + +fn _f4() { + let _: Box; + let _: Box; + let _: Box; +} + +// Just nest the trait alone: + +trait _7 = _2; +trait _8 = _7; +trait _9 = _8; + +fn _f5() { + let _: Box; +} + +// First bound is auto trait: + +trait _10 = Send + Obj; +trait _11 = Obj + Send; +trait _12 = Sync + _11; +trait _13 = Send + _12; + +fn f6() { + let _: Box; + let _: Box; +} + +fn main() {} diff --git a/src/test/ui/traits/alias/object.rs b/src/test/ui/traits/alias/object.rs new file mode 100644 index 000000000..12177cd82 --- /dev/null +++ b/src/test/ui/traits/alias/object.rs @@ -0,0 +1,18 @@ +// run-pass + +#![feature(trait_alias)] + +trait Foo = PartialEq + Send; +trait Bar = Foo + Sync; + +trait I32Iterator = Iterator; + +pub fn main() { + let a: &dyn Bar = &123; + assert!(*a == 123); + let b = Box::new(456) as Box; + assert!(*b == 456); + + let c: &mut dyn I32Iterator = &mut vec![123].into_iter(); + assert_eq!(c.next(), Some(123)); +} diff --git a/src/test/ui/traits/alias/only-maybe-bound.rs b/src/test/ui/traits/alias/only-maybe-bound.rs new file mode 100644 index 000000000..e4abf314e --- /dev/null +++ b/src/test/ui/traits/alias/only-maybe-bound.rs @@ -0,0 +1,22 @@ +// Test that `dyn ?Sized` (i.e., a trait object with only a maybe buond) is not allowed, when just +// `?Sized` results from trait alias expansion. + +#![feature(trait_alias)] + +trait S = ?Sized; + +// Nest a couple of levels deep: +trait _0 = S; +trait _1 = _0; + +// Straight list expansion: +type _T0 = dyn _1; +//~^ ERROR at least one trait is required for an object type [E0224] + +// Twice: +trait _2 = _1 + _1; + +type _T1 = dyn _2; +//~^ ERROR at least one trait is required for an object type [E0224] + +fn main() {} diff --git a/src/test/ui/traits/alias/only-maybe-bound.stderr b/src/test/ui/traits/alias/only-maybe-bound.stderr new file mode 100644 index 000000000..175ec8120 --- /dev/null +++ b/src/test/ui/traits/alias/only-maybe-bound.stderr @@ -0,0 +1,21 @@ +error[E0224]: at least one trait is required for an object type + --> $DIR/only-maybe-bound.rs:13:12 + | +LL | trait _1 = _0; + | -------- this alias does not contain a trait +... +LL | type _T0 = dyn _1; + | ^^^^^^ + +error[E0224]: at least one trait is required for an object type + --> $DIR/only-maybe-bound.rs:19:12 + | +LL | trait _2 = _1 + _1; + | -------- this alias does not contain a trait +LL | +LL | type _T1 = dyn _2; + | ^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/traits/alias/style_lint.rs b/src/test/ui/traits/alias/style_lint.rs new file mode 100644 index 000000000..33be20054 --- /dev/null +++ b/src/test/ui/traits/alias/style_lint.rs @@ -0,0 +1,8 @@ +// check-pass + +#![feature(trait_alias)] + +trait Foo = std::fmt::Display + std::fmt::Debug; +trait bar = std::fmt::Display + std::fmt::Debug; //~WARN trait alias `bar` should have an upper camel case name + +fn main() {} diff --git a/src/test/ui/traits/alias/style_lint.stderr b/src/test/ui/traits/alias/style_lint.stderr new file mode 100644 index 000000000..91e2ea90e --- /dev/null +++ b/src/test/ui/traits/alias/style_lint.stderr @@ -0,0 +1,10 @@ +warning: trait alias `bar` should have an upper camel case name + --> $DIR/style_lint.rs:6:7 + | +LL | trait bar = std::fmt::Display + std::fmt::Debug; + | ^^^ help: convert the identifier to upper camel case: `Bar` + | + = note: `#[warn(non_camel_case_types)]` on by default + +warning: 1 warning emitted + diff --git a/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.fixed b/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.fixed new file mode 100644 index 000000000..8a94abaeb --- /dev/null +++ b/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.fixed @@ -0,0 +1,13 @@ +// Regression test of #43913. + +// run-rustfix + +#![feature(trait_alias)] +#![allow(bare_trait_objects, dead_code)] + +trait Strings = Iterator; + +struct Struct(S); +//~^ ERROR: expected trait, found type alias `Strings` + +fn main() {} diff --git a/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.rs b/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.rs new file mode 100644 index 000000000..40c678c28 --- /dev/null +++ b/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.rs @@ -0,0 +1,13 @@ +// Regression test of #43913. + +// run-rustfix + +#![feature(trait_alias)] +#![allow(bare_trait_objects, dead_code)] + +type Strings = Iterator; + +struct Struct(S); +//~^ ERROR: expected trait, found type alias `Strings` + +fn main() {} diff --git a/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.stderr b/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.stderr new file mode 100644 index 000000000..6e03eeada --- /dev/null +++ b/src/test/ui/traits/alias/suggest-trait-alias-instead-of-type.stderr @@ -0,0 +1,14 @@ +error[E0404]: expected trait, found type alias `Strings` + --> $DIR/suggest-trait-alias-instead-of-type.rs:10:18 + | +LL | struct Struct(S); + | ^^^^^^^ type aliases cannot be used as traits + | +help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias + | +LL | trait Strings = Iterator; + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0404`. diff --git a/src/test/ui/traits/alias/syntax-fail.rs b/src/test/ui/traits/alias/syntax-fail.rs new file mode 100644 index 000000000..039bbce8c --- /dev/null +++ b/src/test/ui/traits/alias/syntax-fail.rs @@ -0,0 +1,10 @@ +#![feature(trait_alias)] + +trait Foo {} +auto trait A = Foo; //~ ERROR trait aliases cannot be `auto` +unsafe trait B = Foo; //~ ERROR trait aliases cannot be `unsafe` + +trait C: Ord = Eq; //~ ERROR bounds are not allowed on trait aliases +trait D: = Eq; //~ ERROR bounds are not allowed on trait aliases + +fn main() {} diff --git a/src/test/ui/traits/alias/syntax-fail.stderr b/src/test/ui/traits/alias/syntax-fail.stderr new file mode 100644 index 000000000..748b92056 --- /dev/null +++ b/src/test/ui/traits/alias/syntax-fail.stderr @@ -0,0 +1,26 @@ +error: trait aliases cannot be `auto` + --> $DIR/syntax-fail.rs:4:1 + | +LL | auto trait A = Foo; + | ^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `auto` + +error: trait aliases cannot be `unsafe` + --> $DIR/syntax-fail.rs:5:1 + | +LL | unsafe trait B = Foo; + | ^^^^^^^^^^^^^^^^^^^^^ trait aliases cannot be `unsafe` + +error: bounds are not allowed on trait aliases + --> $DIR/syntax-fail.rs:7:8 + | +LL | trait C: Ord = Eq; + | ^^^^^ + +error: bounds are not allowed on trait aliases + --> $DIR/syntax-fail.rs:8:8 + | +LL | trait D: = Eq; + | ^ + +error: aborting due to 4 previous errors + diff --git a/src/test/ui/traits/alias/syntax.rs b/src/test/ui/traits/alias/syntax.rs new file mode 100644 index 000000000..17557a51a --- /dev/null +++ b/src/test/ui/traits/alias/syntax.rs @@ -0,0 +1,24 @@ +// run-pass + +#![feature(trait_alias)] + +trait SimpleAlias = Default; +trait GenericAlias = Iterator; +trait Partial = IntoIterator; +trait SpecificAlias = GenericAlias; +trait PartialEqRef<'a, T: 'a> = PartialEq<&'a T>; +trait StaticAlias = 'static; + +trait Things {} +trait Romeo {} +#[allow(dead_code)] +struct The(T); +#[allow(dead_code)] +struct Fore(T); +impl Things for The {} +impl Romeo for Fore {} + +trait WithWhere = Romeo + Romeo where Fore<(Art, Thou)>: Romeo; +trait BareWhere = where The: Things; + +fn main() {} diff --git a/src/test/ui/traits/alias/wf.rs b/src/test/ui/traits/alias/wf.rs new file mode 100644 index 000000000..d10e2abb0 --- /dev/null +++ b/src/test/ui/traits/alias/wf.rs @@ -0,0 +1,7 @@ +#![feature(trait_alias)] + +trait Foo {} +trait A {} +trait B = A; //~ ERROR `T: Foo` is not satisfied + +fn main() {} diff --git a/src/test/ui/traits/alias/wf.stderr b/src/test/ui/traits/alias/wf.stderr new file mode 100644 index 000000000..7172008d3 --- /dev/null +++ b/src/test/ui/traits/alias/wf.stderr @@ -0,0 +1,19 @@ +error[E0277]: the trait bound `T: Foo` is not satisfied + --> $DIR/wf.rs:5:14 + | +LL | trait B = A; + | ^^^^ the trait `Foo` is not implemented for `T` + | +note: required by a bound in `A` + --> $DIR/wf.rs:4:12 + | +LL | trait A {} + | ^^^ required by this bound in `A` +help: consider restricting type parameter `T` + | +LL | trait B = A; + | +++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/alignment-gep-tup-like-1.rs b/src/test/ui/traits/alignment-gep-tup-like-1.rs new file mode 100644 index 000000000..eb503dcf3 --- /dev/null +++ b/src/test/ui/traits/alignment-gep-tup-like-1.rs @@ -0,0 +1,37 @@ +// run-pass + +#![allow(non_camel_case_types)] +#![allow(dead_code)] + +struct pair { + a: A, b: B +} + +trait Invokable { + fn f(&self) -> (A, u16); +} + +struct Invoker { + a: A, + b: u16, +} + +impl Invokable for Invoker { + fn f(&self) -> (A, u16) { + (self.a.clone(), self.b) + } +} + +fn f(a: A, b: u16) -> Box+'static> { + Box::new(Invoker { + a: a, + b: b, + }) as Box+'static> +} + +pub fn main() { + let (a, b) = f(22_u64, 44u16).f(); + println!("a={} b={}", a, b); + assert_eq!(a, 22u64); + assert_eq!(b, 44u16); +} diff --git a/src/test/ui/traits/anon-static-method.rs b/src/test/ui/traits/anon-static-method.rs new file mode 100644 index 000000000..ede01afae --- /dev/null +++ b/src/test/ui/traits/anon-static-method.rs @@ -0,0 +1,15 @@ +// run-pass +struct Foo { + x: isize +} + +impl Foo { + pub fn new() -> Foo { + Foo { x: 3 } + } +} + +pub fn main() { + let x = Foo::new(); + println!("{}", x.x); +} diff --git a/src/test/ui/traits/anon_trait_static_method_exe.rs b/src/test/ui/traits/anon_trait_static_method_exe.rs new file mode 100644 index 000000000..b49302954 --- /dev/null +++ b/src/test/ui/traits/anon_trait_static_method_exe.rs @@ -0,0 +1,12 @@ +// run-pass +#![allow(non_camel_case_types)] + +// aux-build:anon_trait_static_method_lib.rs + +extern crate anon_trait_static_method_lib; +use anon_trait_static_method_lib::Foo; + +pub fn main() { + let x = Foo::new(); + println!("{}", x.x); +} diff --git a/src/test/ui/traits/as-struct-constructor.rs b/src/test/ui/traits/as-struct-constructor.rs new file mode 100644 index 000000000..13ebf9d8d --- /dev/null +++ b/src/test/ui/traits/as-struct-constructor.rs @@ -0,0 +1,6 @@ +trait TraitNotAStruct {} + +fn main() { + TraitNotAStruct{ value: 0 }; + //~^ ERROR expected struct, variant or union type, found trait `TraitNotAStruct` +} diff --git a/src/test/ui/traits/as-struct-constructor.stderr b/src/test/ui/traits/as-struct-constructor.stderr new file mode 100644 index 000000000..d06e85f3a --- /dev/null +++ b/src/test/ui/traits/as-struct-constructor.stderr @@ -0,0 +1,9 @@ +error[E0574]: expected struct, variant or union type, found trait `TraitNotAStruct` + --> $DIR/as-struct-constructor.rs:4:5 + | +LL | TraitNotAStruct{ value: 0 }; + | ^^^^^^^^^^^^^^^ not a struct, variant or union type + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0574`. diff --git a/src/test/ui/traits/assignability-trait.rs b/src/test/ui/traits/assignability-trait.rs new file mode 100644 index 000000000..a8547c1d2 --- /dev/null +++ b/src/test/ui/traits/assignability-trait.rs @@ -0,0 +1,47 @@ +// run-pass +#![allow(non_camel_case_types)] + +// Tests that type assignability is used to search for instances when +// making method calls, but only if there aren't any matches without +// it. + +trait iterable { + fn iterate(&self, blk: F) -> bool where F: FnMut(&A) -> bool; +} + +impl<'a,A> iterable for &'a [A] { + fn iterate(&self, f: F) -> bool where F: FnMut(&A) -> bool { + self.iter().all(f) + } +} + +impl iterable for Vec { + fn iterate(&self, f: F) -> bool where F: FnMut(&A) -> bool { + self.iter().all(f) + } +} + +fn length>(x: T) -> usize { + let mut len = 0; + x.iterate(|_y| { + len += 1; + true + }); + return len; +} + +pub fn main() { + let x: Vec = vec![0,1,2,3]; + // Call a method + x.iterate(|y| { assert_eq!(x[*y as usize], *y); true }); + // Call a parameterized function + assert_eq!(length(x.clone()), x.len()); + // Call a parameterized function, with type arguments that require + // a borrow + assert_eq!(length::(&*x), x.len()); + + // Now try it with a type that *needs* to be borrowed + let z = [0,1,2,3]; + // Call a parameterized function + assert_eq!(length::(&z), z.len()); +} diff --git a/src/test/ui/traits/assoc-type-in-superbad.rs b/src/test/ui/traits/assoc-type-in-superbad.rs new file mode 100644 index 000000000..579ce7cf7 --- /dev/null +++ b/src/test/ui/traits/assoc-type-in-superbad.rs @@ -0,0 +1,16 @@ +// Test case where an associated type is referenced from within the +// supertrait definition, and the impl makes the wrong +// associations. Issue #20220. + +use std::vec::IntoIter; + +pub trait Foo: Iterator::Key> { + type Key; +} + +impl Foo for IntoIter { + type Key = u32; //~ ERROR type mismatch +} + +fn main() { +} diff --git a/src/test/ui/traits/assoc-type-in-superbad.stderr b/src/test/ui/traits/assoc-type-in-superbad.stderr new file mode 100644 index 000000000..f36947914 --- /dev/null +++ b/src/test/ui/traits/assoc-type-in-superbad.stderr @@ -0,0 +1,15 @@ +error[E0271]: type mismatch resolving ` as Iterator>::Item == u32` + --> $DIR/assoc-type-in-superbad.rs:12:16 + | +LL | type Key = u32; + | ^^^ expected `u32`, found `i32` + | +note: required by a bound in `Foo` + --> $DIR/assoc-type-in-superbad.rs:7:25 + | +LL | pub trait Foo: Iterator::Key> { + | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/src/test/ui/traits/assoc-type-in-supertrait.rs b/src/test/ui/traits/assoc-type-in-supertrait.rs new file mode 100644 index 000000000..7d6a754cc --- /dev/null +++ b/src/test/ui/traits/assoc-type-in-supertrait.rs @@ -0,0 +1,23 @@ +// run-pass +// Test case where an associated type is referenced from within the +// supertrait definition. Issue #20220. + + +use std::vec::IntoIter; + +pub trait Foo: Iterator::Key> { + type Key; +} + +impl Foo for IntoIter { + type Key = i32; +} + +fn sum_foo>(f: F) -> i32 { + f.fold(0, |a,b| a + b) +} + +fn main() { + let x = sum_foo(vec![11, 10, 1].into_iter()); + assert_eq!(x, 22); +} diff --git a/src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.rs b/src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.rs new file mode 100644 index 000000000..471a6b836 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.rs @@ -0,0 +1,23 @@ +trait Bar { + type Baz; +} + +struct Foo where T: Bar, ::Baz: String { //~ ERROR expected trait, found struct + t: T, +} + +struct Qux<'a, T> where T: Bar, <&'a T as Bar>::Baz: String { //~ ERROR expected trait, found struct + t: &'a T, +} + +fn foo(_: T) where ::Baz: String { //~ ERROR expected trait, found struct +} + +fn qux<'a, T: Bar>(_: &'a T) where <&'a T as Bar>::Baz: String { //~ ERROR expected trait, found +} + +fn issue_95327() where ::Assoc: String {} +//~^ ERROR expected trait, found struct +//~| ERROR use of undeclared type `Unresolved` + +fn main() {} diff --git a/src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.stderr b/src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.stderr new file mode 100644 index 000000000..9ca446a0a --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/assoc_type_bound_with_struct.stderr @@ -0,0 +1,101 @@ +error[E0433]: failed to resolve: use of undeclared type `Unresolved` + --> $DIR/assoc_type_bound_with_struct.rs:19:31 + | +LL | fn issue_95327() where ::Assoc: String {} + | ^^^^^^^^^^ use of undeclared type `Unresolved` + +error[E0404]: expected trait, found struct `String` + --> $DIR/assoc_type_bound_with_struct.rs:5:46 + | +LL | struct Foo where T: Bar, ::Baz: String { + | ^^^^^^ not a trait + | + ::: $SRC_DIR/alloc/src/string.rs:LL:COL + | +LL | pub trait ToString { + | ------------------ similarly named trait `ToString` defined here + | +help: constrain the associated type to `String` + | +LL | struct Foo where T: Bar, T: Bar { + | ~~~~~~~~~~~~~~~~~~~~ +help: a trait with a similar name exists + | +LL | struct Foo where T: Bar, ::Baz: ToString { + | ~~~~~~~~ + +error[E0404]: expected trait, found struct `String` + --> $DIR/assoc_type_bound_with_struct.rs:9:54 + | +LL | struct Qux<'a, T> where T: Bar, <&'a T as Bar>::Baz: String { + | ^^^^^^ not a trait + | + ::: $SRC_DIR/alloc/src/string.rs:LL:COL + | +LL | pub trait ToString { + | ------------------ similarly named trait `ToString` defined here + | +help: constrain the associated type to `String` + | +LL | struct Qux<'a, T> where T: Bar, &'a T: Bar { + | ~~~~~~~~~~~~~~~~~~~~~~~~ +help: a trait with a similar name exists + | +LL | struct Qux<'a, T> where T: Bar, <&'a T as Bar>::Baz: ToString { + | ~~~~~~~~ + +error[E0404]: expected trait, found struct `String` + --> $DIR/assoc_type_bound_with_struct.rs:13:45 + | +LL | fn foo(_: T) where ::Baz: String { + | ^^^^^^ not a trait + | + ::: $SRC_DIR/alloc/src/string.rs:LL:COL + | +LL | pub trait ToString { + | ------------------ similarly named trait `ToString` defined here + | +help: constrain the associated type to `String` + | +LL | fn foo(_: T) where T: Bar { + | ~~~~~~~~~~~~~~~~~~~~ +help: a trait with a similar name exists + | +LL | fn foo(_: T) where ::Baz: ToString { + | ~~~~~~~~ + +error[E0404]: expected trait, found struct `String` + --> $DIR/assoc_type_bound_with_struct.rs:16:57 + | +LL | fn qux<'a, T: Bar>(_: &'a T) where <&'a T as Bar>::Baz: String { + | ^^^^^^ not a trait + | + ::: $SRC_DIR/alloc/src/string.rs:LL:COL + | +LL | pub trait ToString { + | ------------------ similarly named trait `ToString` defined here + | +help: constrain the associated type to `String` + | +LL | fn qux<'a, T: Bar>(_: &'a T) where &'a T: Bar { + | ~~~~~~~~~~~~~~~~~~~~~~~~ +help: a trait with a similar name exists + | +LL | fn qux<'a, T: Bar>(_: &'a T) where <&'a T as Bar>::Baz: ToString { + | ~~~~~~~~ + +error[E0404]: expected trait, found struct `String` + --> $DIR/assoc_type_bound_with_struct.rs:19:51 + | +LL | fn issue_95327() where ::Assoc: String {} + | ^^^^^^ help: a trait with a similar name exists: `ToString` + | + ::: $SRC_DIR/alloc/src/string.rs:LL:COL + | +LL | pub trait ToString { + | ------------------ similarly named trait `ToString` defined here + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0404, E0433. +For more information about an error, try `rustc --explain E0404`. diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.rs b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.rs new file mode 100644 index 000000000..b1f124c7e --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.rs @@ -0,0 +1,14 @@ +// Check that we validate associated type bounds for trait objects + +trait X { + type Y: Clone; +} + +fn f() { + None::.clone(); +} + +fn main() { + f::>(); + //~^ ERROR the trait bound `str: Clone` is not satisfied +} diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.stderr b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.stderr new file mode 100644 index 000000000..bfbbe7fd2 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-1.stderr @@ -0,0 +1,16 @@ +error[E0277]: the trait bound `str: Clone` is not satisfied + --> $DIR/check-trait-object-bounds-1.rs:12:5 + | +LL | f::>(); + | ^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `str` + | + = help: the trait `Clone` is implemented for `String` +note: required by a bound in `f` + --> $DIR/check-trait-object-bounds-1.rs:7:9 + | +LL | fn f() { + | ^ required by this bound in `f` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2-ok.rs b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2-ok.rs new file mode 100644 index 000000000..1422dda27 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2-ok.rs @@ -0,0 +1,15 @@ +// Make sure that we're handling bound lifetimes correctly when validating trait +// bounds. +// run-pass + +trait X<'a> { + type F: FnOnce(&i32) -> &'a i32; +} + +fn f X<'r> + ?Sized>() { + None::.map(|f| f(&0)); +} + +fn main() { + f:: X<'x, F = fn(&i32) -> &'x i32>>(); +} diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.rs b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.rs new file mode 100644 index 000000000..eb2fb6e84 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.rs @@ -0,0 +1,15 @@ +// Check that we validate associated type bounds for trait objects when they +// have bound lifetimes + +trait X<'a> { + type F: FnOnce(&i32) -> &'a i32; +} + +fn f X<'r> + ?Sized>() { + None::.map(|f| f(&0)); +} + +fn main() { + f:: X<'x, F = i32>>(); + //~^ expected a `FnOnce<(&i32,)>` closure, found `i32` +} diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.stderr b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.stderr new file mode 100644 index 000000000..46e8ce788 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-2.stderr @@ -0,0 +1,16 @@ +error[E0277]: expected a `FnOnce<(&i32,)>` closure, found `i32` + --> $DIR/check-trait-object-bounds-2.rs:13:5 + | +LL | f:: X<'x, F = i32>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected an `FnOnce<(&i32,)>` closure, found `i32` + | + = help: the trait `for<'r> FnOnce<(&'r i32,)>` is not implemented for `i32` +note: required by a bound in `f` + --> $DIR/check-trait-object-bounds-2.rs:8:9 + | +LL | fn f X<'r> + ?Sized>() { + | ^^^^^^^^^^^^^ required by this bound in `f` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.rs b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.rs new file mode 100644 index 000000000..ba04fd93a --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.rs @@ -0,0 +1,20 @@ +// Check that we validate associated type bounds for trait objects + +trait X<'a> { + type Y: Into<&'static str> + From<&'a str>; +} + +fn f<'a, T: X<'a> + ?Sized>(s: &'a str) -> &'static str { + T::Y::from(s).into() +} + +pub fn main() { + let z; + { + let s = String::from("abcdef"); + z = f::>(&s); + //~^ ERROR `s` does not live long enough + } + + println!("{}", z) +} diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.stderr b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.stderr new file mode 100644 index 000000000..ade552c4b --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-3.stderr @@ -0,0 +1,15 @@ +error[E0597]: `s` does not live long enough + --> $DIR/check-trait-object-bounds-3.rs:15:34 + | +LL | z = f::>(&s); + | ---------------------^^- + | | | + | | borrowed value does not live long enough + | argument requires that `s` is borrowed for `'static` +LL | +LL | } + | - `s` dropped here while still borrowed + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`. diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.rs b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.rs new file mode 100644 index 000000000..e9ca1563f --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.rs @@ -0,0 +1,17 @@ +// Check that we validate associated type bounds on super traits for trait +// objects + +trait Super { + type Y: Clone; +} + +trait X: Super {} + +fn f() { + None::.clone(); +} + +fn main() { + f::>(); + //~^ ERROR the trait bound `str: Clone` is not satisfied +} diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.stderr b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.stderr new file mode 100644 index 000000000..3ca36d5d2 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-4.stderr @@ -0,0 +1,16 @@ +error[E0277]: the trait bound `str: Clone` is not satisfied + --> $DIR/check-trait-object-bounds-4.rs:15:5 + | +LL | f::>(); + | ^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `str` + | + = help: the trait `Clone` is implemented for `String` +note: required by a bound in `f` + --> $DIR/check-trait-object-bounds-4.rs:10:9 + | +LL | fn f() { + | ^ required by this bound in `f` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.rs b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.rs new file mode 100644 index 000000000..7d733ad26 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.rs @@ -0,0 +1,27 @@ +// Check that we validate associated type bounds on super traits for trait +// objects + +trait Is { + type T; +} + +impl Is for U { + type T = U; +} + +trait Super { + type V; +} + +trait Obj: Super { + type U: Is; +} + +fn is_obj(_: &T) {} + +fn f(x: &dyn Obj) { + is_obj(x) + //~^ type mismatch resolving `::T == i64` +} + +fn main() {} diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.stderr b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.stderr new file mode 100644 index 000000000..4251c1a1e --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-5.stderr @@ -0,0 +1,20 @@ +error[E0271]: type mismatch resolving `::T == i64` + --> $DIR/check-trait-object-bounds-5.rs:23:5 + | +LL | is_obj(x) + | ^^^^^^ type mismatch resolving `::T == i64` + | +note: expected this to be `i64` + --> $DIR/check-trait-object-bounds-5.rs:9:14 + | +LL | type T = U; + | ^ +note: required by a bound in `is_obj` + --> $DIR/check-trait-object-bounds-5.rs:20:23 + | +LL | fn is_obj(_: &T) {} + | ^^^ required by this bound in `is_obj` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.rs b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.rs new file mode 100644 index 000000000..cb196d67f --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.rs @@ -0,0 +1,24 @@ +// Check that we validate associated type bounds on super traits for trait +// objects + +trait Is { + type T; +} + +impl Is for U { + type T = U; +} + +trait Obj { + type U: Is; + type V; +} + +fn is_obj(_: &T) {} + +fn f(x: &dyn Obj) { + is_obj(x) + //~^ ERROR type mismatch resolving `::T == i64` +} + +fn main() {} diff --git a/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.stderr b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.stderr new file mode 100644 index 000000000..5b23a513e --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/check-trait-object-bounds-6.stderr @@ -0,0 +1,20 @@ +error[E0271]: type mismatch resolving `::T == i64` + --> $DIR/check-trait-object-bounds-6.rs:20:5 + | +LL | is_obj(x) + | ^^^^^^ type mismatch resolving `::T == i64` + | +note: expected this to be `i64` + --> $DIR/check-trait-object-bounds-6.rs:9:14 + | +LL | type T = U; + | ^ +note: required by a bound in `is_obj` + --> $DIR/check-trait-object-bounds-6.rs:17:23 + | +LL | fn is_obj(_: &T) {} + | ^^^ required by this bound in `is_obj` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/src/test/ui/traits/associated_type_bound/issue-51446.rs b/src/test/ui/traits/associated_type_bound/issue-51446.rs new file mode 100644 index 000000000..7dd95de73 --- /dev/null +++ b/src/test/ui/traits/associated_type_bound/issue-51446.rs @@ -0,0 +1,34 @@ +// Regression test for #51446. +// check-pass + +trait Foo { + type Item; + fn get(&self) -> Self::Item; +} + +fn blah(x: T, f: F) -> B +where + T: Foo, + F: Fn(T::Item), +{ + B { x: x.get(), f } +} + +pub struct B +where + F: Fn(T), +{ + pub x: T, + pub f: F, +} + +impl Foo for i32 { + type Item = i32; + fn get(&self) -> i32 { + *self + } +} + +fn main() { + let _ = blah(0, |_| ()); +} diff --git a/src/test/ui/traits/astconv-cycle-between-and-type.rs b/src/test/ui/traits/astconv-cycle-between-and-type.rs new file mode 100644 index 000000000..cc8f9dc51 --- /dev/null +++ b/src/test/ui/traits/astconv-cycle-between-and-type.rs @@ -0,0 +1,29 @@ +// run-pass +// Test that we are able to successfully compile a setup where a trait +// (`Trait1`) references a struct (`SomeType`) which in turn +// carries a predicate that references the trait (`u32 : Trait1`, +// substituted). + +// pretty-expanded FIXME #23616 + +#![allow(dead_code)] + +trait Trait1 : Trait2> { + fn dumb(&self) { } +} + +trait Trait2 { + fn dumber(&self, _: A) { } +} + +struct SomeType + where A : Trait1 +{ + a: A +} + +impl Trait1 for u32 { } + +impl Trait2> for u32 { } + +fn main() { } diff --git a/src/test/ui/traits/augmented-assignments-trait.rs b/src/test/ui/traits/augmented-assignments-trait.rs new file mode 100644 index 000000000..747a5393f --- /dev/null +++ b/src/test/ui/traits/augmented-assignments-trait.rs @@ -0,0 +1,12 @@ +// run-pass +use std::ops::AddAssign; + +struct Int(#[allow(unused_tuple_struct_fields)] i32); + +impl AddAssign for Int { + fn add_assign(&mut self, _: Int) { + unimplemented!() + } +} + +fn main() {} diff --git a/src/test/ui/traits/auxiliary/anon_trait_static_method_lib.rs b/src/test/ui/traits/auxiliary/anon_trait_static_method_lib.rs new file mode 100644 index 000000000..dceec7e3e --- /dev/null +++ b/src/test/ui/traits/auxiliary/anon_trait_static_method_lib.rs @@ -0,0 +1,9 @@ +pub struct Foo { + pub x: isize +} + +impl Foo { + pub fn new() -> Foo { + Foo { x: 3 } + } +} diff --git a/src/test/ui/traits/auxiliary/go_trait.rs b/src/test/ui/traits/auxiliary/go_trait.rs new file mode 100644 index 000000000..aa0ec2289 --- /dev/null +++ b/src/test/ui/traits/auxiliary/go_trait.rs @@ -0,0 +1,43 @@ +#![feature(specialization)] + +// Common code used for tests that model the Fn/FnMut/FnOnce hierarchy. + +pub trait Go { + fn go(&self, arg: isize); +} + +pub fn go(this: &G, arg: isize) { + this.go(arg) +} + +pub trait GoMut { + fn go_mut(&mut self, arg: isize); +} + +pub fn go_mut(this: &mut G, arg: isize) { + this.go_mut(arg) +} + +pub trait GoOnce { + fn go_once(self, arg: isize); +} + +pub fn go_once(this: G, arg: isize) { + this.go_once(arg) +} + +impl GoMut for G + where G : Go +{ + default fn go_mut(&mut self, arg: isize) { + go(&*self, arg) + } +} + +impl GoOnce for G + where G : GoMut +{ + default fn go_once(mut self, arg: isize) { + go_mut(&mut self, arg) + } +} diff --git a/src/test/ui/traits/auxiliary/issue_89119_intercrate_caching.rs b/src/test/ui/traits/auxiliary/issue_89119_intercrate_caching.rs new file mode 100644 index 000000000..769e89731 --- /dev/null +++ b/src/test/ui/traits/auxiliary/issue_89119_intercrate_caching.rs @@ -0,0 +1,60 @@ +// This is the auxiliary crate for the regression test for issue #89119, minimized +// from `zvariant-2.8.0`. + +use std::convert::TryFrom; +use std::borrow::Cow; + +pub struct Str<'a>(Cow<'a, str>); +impl<'a> Str<'a> { + pub fn to_owned(&self) -> Str<'static> { + todo!() + } +} + +pub enum Value<'a> { + Str(Str<'a>), + Value(Box>), +} +impl<'a> Value<'a> { + pub fn to_owned(&self) -> Value<'static> { + match self { + Value::Str(v) => Value::Str(v.to_owned()), + Value::Value(v) => { + let o = OwnedValue::from(&**v); + Value::Value(Box::new(o.into_inner())) + } + } + } +} + +struct OwnedValue(Value<'static>); +impl OwnedValue { + pub(crate) fn into_inner(self) -> Value<'static> { + todo!() + } +} +impl<'a, T> TryFrom for Vec +where + T: TryFrom, Error = ()>, +{ + type Error = (); + fn try_from(_: OwnedValue) -> Result { + todo!() + } +} +impl TryFrom for Vec { + type Error = (); + fn try_from(_: OwnedValue) -> Result { + todo!() + } +} +impl<'a> From> for OwnedValue { + fn from(_: Value<'a>) -> Self { + todo!() + } +} +impl<'a> From<&Value<'a>> for OwnedValue { + fn from(_: &Value<'a>) -> Self { + todo!() + } +} diff --git a/src/test/ui/traits/auxiliary/trait_safety_lib.rs b/src/test/ui/traits/auxiliary/trait_safety_lib.rs new file mode 100644 index 000000000..6fc432ed4 --- /dev/null +++ b/src/test/ui/traits/auxiliary/trait_safety_lib.rs @@ -0,0 +1,9 @@ +// Simple smoke test that unsafe traits can be compiled etc. + +pub unsafe trait Foo { + fn foo(&self) -> isize; +} + +unsafe impl Foo for isize { + fn foo(&self) -> isize { *self } +} diff --git a/src/test/ui/traits/auxiliary/traitimpl.rs b/src/test/ui/traits/auxiliary/traitimpl.rs new file mode 100644 index 000000000..fda5314cd --- /dev/null +++ b/src/test/ui/traits/auxiliary/traitimpl.rs @@ -0,0 +1,7 @@ +// Test inherent trait impls work cross-crait. + +pub trait Bar<'a> : 'a {} + +impl<'a> Bar<'a> { + pub fn bar(&self) {} +} diff --git a/src/test/ui/traits/bad-method-typaram-kind.rs b/src/test/ui/traits/bad-method-typaram-kind.rs new file mode 100644 index 000000000..b088eae1c --- /dev/null +++ b/src/test/ui/traits/bad-method-typaram-kind.rs @@ -0,0 +1,14 @@ +fn foo() { + 1.bar::(); //~ ERROR `T` cannot be sent between threads safely +} + +trait Bar { + fn bar(&self); +} + +impl Bar for usize { + fn bar(&self) { + } +} + +fn main() {} diff --git a/src/test/ui/traits/bad-method-typaram-kind.stderr b/src/test/ui/traits/bad-method-typaram-kind.stderr new file mode 100644 index 000000000..8befa4c5f --- /dev/null +++ b/src/test/ui/traits/bad-method-typaram-kind.stderr @@ -0,0 +1,19 @@ +error[E0277]: `T` cannot be sent between threads safely + --> $DIR/bad-method-typaram-kind.rs:2:7 + | +LL | 1.bar::(); + | ^^^ `T` cannot be sent between threads safely + | +note: required by a bound in `Bar::bar` + --> $DIR/bad-method-typaram-kind.rs:6:14 + | +LL | fn bar(&self); + | ^^^^ required by this bound in `Bar::bar` +help: consider further restricting this bound + | +LL | fn foo() { + | +++++++++++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bad-sized.rs b/src/test/ui/traits/bad-sized.rs new file mode 100644 index 000000000..a15219679 --- /dev/null +++ b/src/test/ui/traits/bad-sized.rs @@ -0,0 +1,9 @@ +trait Trait {} + +pub fn main() { + let x: Vec = Vec::new(); + //~^ ERROR only auto traits can be used as additional traits in a trait object + //~| ERROR the size for values of type + //~| ERROR the size for values of type + //~| ERROR the size for values of type +} diff --git a/src/test/ui/traits/bad-sized.stderr b/src/test/ui/traits/bad-sized.stderr new file mode 100644 index 000000000..6f9113fff --- /dev/null +++ b/src/test/ui/traits/bad-sized.stderr @@ -0,0 +1,54 @@ +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/bad-sized.rs:4:28 + | +LL | let x: Vec = Vec::new(); + | ----- ^^^^^ additional non-auto trait + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Trait + Sized {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time + --> $DIR/bad-sized.rs:4:12 + | +LL | let x: Vec = Vec::new(); + | ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Trait` +note: required by a bound in `Vec` + --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + | +LL | pub struct Vec { + | ^ required by this bound in `Vec` + +error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time + --> $DIR/bad-sized.rs:4:37 + | +LL | let x: Vec = Vec::new(); + | ^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Trait` +note: required by a bound in `Vec::::new` + --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + | +LL | impl Vec { + | ^ required by this bound in `Vec::::new` + +error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time + --> $DIR/bad-sized.rs:4:37 + | +LL | let x: Vec = Vec::new(); + | ^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Trait` +note: required by a bound in `Vec` + --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + | +LL | pub struct Vec { + | ^ required by this bound in `Vec` + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0225, E0277. +For more information about an error, try `rustc --explain E0225`. diff --git a/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.rs b/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.rs new file mode 100644 index 000000000..1d2345180 --- /dev/null +++ b/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.rs @@ -0,0 +1,12 @@ +fn strip_lf(s: &str) -> &str { + s.strip_suffix(b'\n').unwrap_or(s) + //~^ ERROR expected a `FnMut<(char,)>` closure, found `u8` + //~| NOTE expected an `FnMut<(char,)>` closure, found `u8` + //~| NOTE required by a bound introduced by this call + //~| HELP the trait `FnMut<(char,)>` is not implemented for `u8` + //~| HELP the following other types implement trait `Pattern<'a>`: + //~| NOTE required because of the requirements on the impl of `Pattern<'_>` for `u8` + +} + +fn main() {} diff --git a/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr b/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr new file mode 100644 index 000000000..6ce57b626 --- /dev/null +++ b/src/test/ui/traits/bound/assoc-fn-bound-root-obligation.stderr @@ -0,0 +1,22 @@ +error[E0277]: expected a `FnMut<(char,)>` closure, found `u8` + --> $DIR/assoc-fn-bound-root-obligation.rs:2:20 + | +LL | s.strip_suffix(b'\n').unwrap_or(s) + | ------------ ^^^^^ expected an `FnMut<(char,)>` closure, found `u8` + | | + | required by a bound introduced by this call + | + = help: the trait `FnMut<(char,)>` is not implemented for `u8` + = help: the following other types implement trait `Pattern<'a>`: + &'b String + &'b [char; N] + &'b [char] + &'b str + &'c &'b str + [char; N] + char + = note: required because of the requirements on the impl of `Pattern<'_>` for `u8` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/auxiliary/crate_a1.rs b/src/test/ui/traits/bound/auxiliary/crate_a1.rs new file mode 100644 index 000000000..6aa010258 --- /dev/null +++ b/src/test/ui/traits/bound/auxiliary/crate_a1.rs @@ -0,0 +1,9 @@ +pub trait Bar {} + +pub fn try_foo(x: impl Bar) {} + +pub struct ImplementsTraitForUsize { + _marker: std::marker::PhantomData, +} + +impl Bar for ImplementsTraitForUsize {} diff --git a/src/test/ui/traits/bound/auxiliary/crate_a2.rs b/src/test/ui/traits/bound/auxiliary/crate_a2.rs new file mode 100644 index 000000000..d6057db5e --- /dev/null +++ b/src/test/ui/traits/bound/auxiliary/crate_a2.rs @@ -0,0 +1,13 @@ +pub struct Foo; + +pub trait Bar {} + +impl Bar for Foo {} + +pub struct DoesNotImplementTrait; + +pub struct ImplementsWrongTraitConditionally { + _marker: std::marker::PhantomData, +} + +impl Bar for ImplementsWrongTraitConditionally {} diff --git a/src/test/ui/traits/bound/auxiliary/on_structs_and_enums_xc.rs b/src/test/ui/traits/bound/auxiliary/on_structs_and_enums_xc.rs new file mode 100644 index 000000000..7e9592eee --- /dev/null +++ b/src/test/ui/traits/bound/auxiliary/on_structs_and_enums_xc.rs @@ -0,0 +1,13 @@ +pub trait Trait { + fn dummy(&self) { } +} + +pub struct Foo { + pub x: T, +} + +pub enum Bar { + ABar(isize), + BBar(T), + CBar(usize), +} diff --git a/src/test/ui/traits/bound/basic.rs b/src/test/ui/traits/bound/basic.rs new file mode 100644 index 000000000..8c8a7eb7d --- /dev/null +++ b/src/test/ui/traits/bound/basic.rs @@ -0,0 +1,25 @@ +// run-pass +#![allow(dead_code)] +#![allow(unconditional_recursion)] + +// pretty-expanded FIXME #23616 + +trait Foo { +} + +fn b(_x: Box) { +} + +fn c(x: Box) { + e(x); +} + +fn d(x: Box) { + e(x); +} + +fn e(x: Box) { + e(x); +} + +pub fn main() { } diff --git a/src/test/ui/traits/bound/generic_trait.rs b/src/test/ui/traits/bound/generic_trait.rs new file mode 100644 index 000000000..18382bb59 --- /dev/null +++ b/src/test/ui/traits/bound/generic_trait.rs @@ -0,0 +1,30 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +trait connection { + fn read(&self) -> isize; +} + +trait connection_factory { + fn create(&self) -> C; +} + +type my_connection = (); +type my_connection_factory = (); + +impl connection for () { + fn read(&self) -> isize { 43 } +} + +impl connection_factory for my_connection_factory { + fn create(&self) -> my_connection { () } +} + +pub fn main() { + let factory = (); + let connection = factory.create(); + let result = connection.read(); + assert_eq!(result, 43); +} diff --git a/src/test/ui/traits/bound/impl-comparison-duplicates.rs b/src/test/ui/traits/bound/impl-comparison-duplicates.rs new file mode 100644 index 000000000..de6c2afa2 --- /dev/null +++ b/src/test/ui/traits/bound/impl-comparison-duplicates.rs @@ -0,0 +1,16 @@ +// run-pass +// Tests that type parameter bounds on an implementation need not match the +// trait exactly, as long as the implementation doesn't demand *more* bounds +// than the trait. + +// pretty-expanded FIXME #23616 + +trait A { + fn foo(&self); +} + +impl A for isize { + fn foo(&self) {} // Ord implies Eq, so this is ok. +} + +fn main() {} diff --git a/src/test/ui/traits/bound/in-arc.rs b/src/test/ui/traits/bound/in-arc.rs new file mode 100644 index 000000000..a1492c0b9 --- /dev/null +++ b/src/test/ui/traits/bound/in-arc.rs @@ -0,0 +1,108 @@ +// run-pass +#![allow(unused_must_use)] +// Tests that a heterogeneous list of existential `dyn` types can be put inside an Arc +// and shared between threads as long as all types fulfill Send. + +// ignore-emscripten no threads support + +use std::sync::Arc; +use std::sync::mpsc::channel; +use std::thread; + +trait Pet { + fn name(&self, blk: Box); + fn num_legs(&self) -> usize; + fn of_good_pedigree(&self) -> bool; +} + +struct Catte { + num_whiskers: usize, + name: String, +} + +struct Dogge { + bark_decibels: usize, + tricks_known: usize, + name: String, +} + +struct Goldfyshe { + swim_speed: usize, + name: String, +} + +impl Pet for Catte { + fn name(&self, mut blk: Box) { blk(&self.name) } + fn num_legs(&self) -> usize { 4 } + fn of_good_pedigree(&self) -> bool { self.num_whiskers >= 4 } +} +impl Pet for Dogge { + fn name(&self, mut blk: Box) { blk(&self.name) } + fn num_legs(&self) -> usize { 4 } + fn of_good_pedigree(&self) -> bool { + self.bark_decibels < 70 || self.tricks_known > 20 + } +} +impl Pet for Goldfyshe { + fn name(&self, mut blk: Box) { blk(&self.name) } + fn num_legs(&self) -> usize { 0 } + fn of_good_pedigree(&self) -> bool { self.swim_speed >= 500 } +} + +pub fn main() { + let catte = Catte { num_whiskers: 7, name: "alonzo_church".to_string() }; + let dogge1 = Dogge { + bark_decibels: 100, + tricks_known: 42, + name: "alan_turing".to_string(), + }; + let dogge2 = Dogge { + bark_decibels: 55, + tricks_known: 11, + name: "albert_einstein".to_string(), + }; + let fishe = Goldfyshe { + swim_speed: 998, + name: "alec_guinness".to_string(), + }; + let arc = Arc::new(vec![ + Box::new(catte) as Box, + Box::new(dogge1) as Box, + Box::new(fishe) as Box, + Box::new(dogge2) as Box]); + let (tx1, rx1) = channel(); + let arc1 = arc.clone(); + let t1 = thread::spawn(move|| { check_legs(arc1); tx1.send(()); }); + let (tx2, rx2) = channel(); + let arc2 = arc.clone(); + let t2 = thread::spawn(move|| { check_names(arc2); tx2.send(()); }); + let (tx3, rx3) = channel(); + let arc3 = arc.clone(); + let t3 = thread::spawn(move|| { check_pedigree(arc3); tx3.send(()); }); + rx1.recv(); + rx2.recv(); + rx3.recv(); + t1.join(); + t2.join(); + t3.join(); +} + +fn check_legs(arc: Arc>>) { + let mut legs = 0; + for pet in arc.iter() { + legs += pet.num_legs(); + } + assert!(legs == 12); +} +fn check_names(arc: Arc>>) { + for pet in arc.iter() { + pet.name(Box::new(|name| { + assert!(name.as_bytes()[0] == 'a' as u8 && name.as_bytes()[1] == 'l' as u8); + })) + } +} +fn check_pedigree(arc: Arc>>) { + for pet in arc.iter() { + assert!(pet.of_good_pedigree()); + } +} diff --git a/src/test/ui/traits/bound/multiple.rs b/src/test/ui/traits/bound/multiple.rs new file mode 100644 index 000000000..868b33407 --- /dev/null +++ b/src/test/ui/traits/bound/multiple.rs @@ -0,0 +1,9 @@ +// run-pass +// pretty-expanded FIXME #23616 + +fn f(_: T) { +} + +pub fn main() { + f(3); +} diff --git a/src/test/ui/traits/bound/not-on-bare-trait.rs b/src/test/ui/traits/bound/not-on-bare-trait.rs new file mode 100644 index 000000000..daf18c670 --- /dev/null +++ b/src/test/ui/traits/bound/not-on-bare-trait.rs @@ -0,0 +1,13 @@ +trait Foo { + fn dummy(&self) {} +} + +// This should emit the less confusing error, not the more confusing one. + +fn foo(_x: Foo + Send) { + //~^ ERROR the size for values of type + //~| WARN trait objects without an explicit `dyn` are deprecated + //~| WARN this is accepted in the current edition +} + +fn main() {} diff --git a/src/test/ui/traits/bound/not-on-bare-trait.stderr b/src/test/ui/traits/bound/not-on-bare-trait.stderr new file mode 100644 index 000000000..8a92dd118 --- /dev/null +++ b/src/test/ui/traits/bound/not-on-bare-trait.stderr @@ -0,0 +1,31 @@ +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/not-on-bare-trait.rs:7:12 + | +LL | fn foo(_x: Foo + Send) { + | ^^^^^^^^^^ + | + = note: `#[warn(bare_trait_objects)]` on by default + = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = note: for more information, see +help: use `dyn` + | +LL - fn foo(_x: Foo + Send) { +LL + fn foo(_x: dyn Foo + Send) { + | + +error[E0277]: the size for values of type `(dyn Foo + Send + 'static)` cannot be known at compilation time + --> $DIR/not-on-bare-trait.rs:7:8 + | +LL | fn foo(_x: Foo + Send) { + | ^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn Foo + Send + 'static)` + = help: unsized fn params are gated as an unstable feature +help: function arguments must have a statically known size, borrowed types always have a known size + | +LL | fn foo(_x: &Foo + Send) { + | + + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/not-on-struct.rs b/src/test/ui/traits/bound/not-on-struct.rs new file mode 100644 index 000000000..8633e9d7a --- /dev/null +++ b/src/test/ui/traits/bound/not-on-struct.rs @@ -0,0 +1,38 @@ +// We don't need those errors. Ideally we would silence them, but to do so we need to move the +// lint from being an early-lint during parsing to a late-lint, because it needs to be aware of +// the types involved. +#![allow(bare_trait_objects)] + +struct Foo; + +fn foo(_x: Box) { } //~ ERROR expected trait, found struct `Foo` + +type TypeAlias = Box>; //~ ERROR expected trait, found struct `Vec` + +struct A; +fn a() -> A + 'static { //~ ERROR expected trait, found + A +} +fn b<'a,T,E>(iter: Iterator + 'a>) { //~ ERROR expected trait, found + panic!() +} +fn c() -> 'static + A { //~ ERROR expected trait, found + A +} +fn d<'a,T,E>(iter: Iterator>) { //~ ERROR expected trait, found + panic!() +} +fn e() -> 'static + A + 'static { //~ ERROR expected trait, found +//~^ ERROR only a single explicit lifetime bound is permitted + A +} +fn f<'a,T,E>(iter: Iterator + 'a>) { //~ ERROR expected trait, found +//~^ ERROR only a single explicit lifetime bound is permitted + panic!() +} +struct Traitor; +trait Trait {} +fn g() -> Traitor + 'static { //~ ERROR expected trait, found struct `Traitor` + A +} +fn main() {} diff --git a/src/test/ui/traits/bound/not-on-struct.stderr b/src/test/ui/traits/bound/not-on-struct.stderr new file mode 100644 index 000000000..2de35dc7f --- /dev/null +++ b/src/test/ui/traits/bound/not-on-struct.stderr @@ -0,0 +1,175 @@ +error[E0226]: only a single explicit lifetime bound is permitted + --> $DIR/not-on-struct.rs:25:25 + | +LL | fn e() -> 'static + A + 'static { + | ^^^^^^^ + +error[E0226]: only a single explicit lifetime bound is permitted + --> $DIR/not-on-struct.rs:29:53 + | +LL | fn f<'a,T,E>(iter: Iterator + 'a>) { + | ^^ + +error[E0404]: expected trait, found struct `Foo` + --> $DIR/not-on-struct.rs:8:16 + | +LL | fn foo(_x: Box) { } + | ^^^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:8:22 + | +LL | fn foo(_x: Box) { } + | --- ^^^^ ...because of this bound + | | + | expected this type to be a trait... + +error[E0404]: expected trait, found struct `Vec` + --> $DIR/not-on-struct.rs:10:29 + | +LL | type TypeAlias = Box>; + | ^^^^^^ not a trait + +error[E0404]: expected trait, found struct `A` + --> $DIR/not-on-struct.rs:13:11 + | +LL | fn a() -> A + 'static { + | ^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:13:15 + | +LL | fn a() -> A + 'static { + | - ^^^^^^^ ...because of this bound + | | + | expected this type to be a trait... +help: if you meant to use a type and not a trait here, remove the bounds + | +LL - fn a() -> A + 'static { +LL + fn a() -> A { + | + +error[E0404]: expected trait, found enum `Result` + --> $DIR/not-on-struct.rs:16:34 + | +LL | fn b<'a,T,E>(iter: Iterator + 'a>) { + | ^^^^^^^^^^^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:16:48 + | +LL | fn b<'a,T,E>(iter: Iterator + 'a>) { + | ----------- ^^ ...because of this bound + | | + | expected this type to be a trait... +help: if you meant to use a type and not a trait here, remove the bounds + | +LL - fn b<'a,T,E>(iter: Iterator + 'a>) { +LL + fn b<'a,T,E>(iter: Iterator>) { + | + +error[E0404]: expected trait, found struct `A` + --> $DIR/not-on-struct.rs:19:21 + | +LL | fn c() -> 'static + A { + | ^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:19:11 + | +LL | fn c() -> 'static + A { + | ^^^^^^^ - expected this type to be a trait... + | | + | ...because of this bound +help: if you meant to use a type and not a trait here, remove the bounds + | +LL - fn c() -> 'static + A { +LL + fn c() -> A { + | + +error[E0404]: expected trait, found enum `Result` + --> $DIR/not-on-struct.rs:22:39 + | +LL | fn d<'a,T,E>(iter: Iterator>) { + | ^^^^^^^^^^^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:22:34 + | +LL | fn d<'a,T,E>(iter: Iterator>) { + | ^^ ----------- expected this type to be a trait... + | | + | ...because of this bound +help: if you meant to use a type and not a trait here, remove the bounds + | +LL - fn d<'a,T,E>(iter: Iterator>) { +LL + fn d<'a,T,E>(iter: Iterator>) { + | + +error[E0404]: expected trait, found struct `A` + --> $DIR/not-on-struct.rs:25:21 + | +LL | fn e() -> 'static + A + 'static { + | ^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:25:11 + | +LL | fn e() -> 'static + A + 'static { + | ^^^^^^^ - ^^^^^^^ ...because of these bounds + | | + | expected this type to be a trait... +help: if you meant to use a type and not a trait here, remove the bounds + | +LL - fn e() -> 'static + A + 'static { +LL + fn e() -> A { + | + +error[E0404]: expected trait, found enum `Result` + --> $DIR/not-on-struct.rs:29:39 + | +LL | fn f<'a,T,E>(iter: Iterator + 'a>) { + | ^^^^^^^^^^^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:29:34 + | +LL | fn f<'a,T,E>(iter: Iterator + 'a>) { + | ^^ ----------- ^^ ...because of these bounds + | | + | expected this type to be a trait... +help: if you meant to use a type and not a trait here, remove the bounds + | +LL - fn f<'a,T,E>(iter: Iterator + 'a>) { +LL + fn f<'a,T,E>(iter: Iterator>) { + | + +error[E0404]: expected trait, found struct `Traitor` + --> $DIR/not-on-struct.rs:35:11 + | +LL | trait Trait {} + | ----------- similarly named trait `Trait` defined here +LL | fn g() -> Traitor + 'static { + | ^^^^^^^ not a trait + | +help: `+` is used to constrain a "trait object" type with lifetimes or auto-traits; structs and enums can't be bound in that way + --> $DIR/not-on-struct.rs:35:21 + | +LL | fn g() -> Traitor + 'static { + | ------- ^^^^^^^ ...because of this bound + | | + | expected this type to be a trait... +help: if you meant to use a type and not a trait here, remove the bounds + | +LL - fn g() -> Traitor + 'static { +LL + fn g() -> Traitor { + | +help: a trait with a similar name exists + | +LL | fn g() -> Trait + 'static { + | ~~~~~ + +error: aborting due to 11 previous errors + +Some errors have detailed explanations: E0226, E0404. +For more information about an error, try `rustc --explain E0226`. diff --git a/src/test/ui/traits/bound/on-structs-and-enums-in-fns.rs b/src/test/ui/traits/bound/on-structs-and-enums-in-fns.rs new file mode 100644 index 000000000..6a6fcf530 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-in-fns.rs @@ -0,0 +1,20 @@ +trait Trait {} + +struct Foo { + x: T, +} + +enum Bar { + ABar(isize), + BBar(T), + CBar(usize), +} + +fn explode(x: Foo) {} +//~^ ERROR E0277 + +fn kaboom(y: Bar) {} +//~^ ERROR E0277 + +fn main() { +} diff --git a/src/test/ui/traits/bound/on-structs-and-enums-in-fns.stderr b/src/test/ui/traits/bound/on-structs-and-enums-in-fns.stderr new file mode 100644 index 000000000..61237a63e --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-in-fns.stderr @@ -0,0 +1,27 @@ +error[E0277]: the trait bound `u32: Trait` is not satisfied + --> $DIR/on-structs-and-enums-in-fns.rs:13:15 + | +LL | fn explode(x: Foo) {} + | ^^^^^^^^ the trait `Trait` is not implemented for `u32` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums-in-fns.rs:3:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` + +error[E0277]: the trait bound `f32: Trait` is not satisfied + --> $DIR/on-structs-and-enums-in-fns.rs:16:14 + | +LL | fn kaboom(y: Bar) {} + | ^^^^^^^^ the trait `Trait` is not implemented for `f32` + | +note: required by a bound in `Bar` + --> $DIR/on-structs-and-enums-in-fns.rs:7:12 + | +LL | enum Bar { + | ^^^^^ required by this bound in `Bar` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/on-structs-and-enums-in-impls.rs b/src/test/ui/traits/bound/on-structs-and-enums-in-impls.rs new file mode 100644 index 000000000..d37949952 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-in-impls.rs @@ -0,0 +1,25 @@ +trait Trait {} + +struct Foo { + x: T, +} + +enum Bar { + ABar(isize), + BBar(T), + CBar(usize), +} + +trait PolyTrait +{ + fn whatever(&self, t: T) {} +} + +struct Struct; + +impl PolyTrait> for Struct { +//~^ ERROR E0277 +} + +fn main() { +} diff --git a/src/test/ui/traits/bound/on-structs-and-enums-in-impls.stderr b/src/test/ui/traits/bound/on-structs-and-enums-in-impls.stderr new file mode 100644 index 000000000..8a4374226 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-in-impls.stderr @@ -0,0 +1,15 @@ +error[E0277]: the trait bound `u16: Trait` is not satisfied + --> $DIR/on-structs-and-enums-in-impls.rs:20:6 + | +LL | impl PolyTrait> for Struct { + | ^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `u16` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums-in-impls.rs:3:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/on-structs-and-enums-locals.rs b/src/test/ui/traits/bound/on-structs-and-enums-locals.rs new file mode 100644 index 000000000..21c0ce80f --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-locals.rs @@ -0,0 +1,17 @@ +trait Trait { + fn dummy(&self) { } +} + +struct Foo { + x: T, +} + +fn main() { + let foo = Foo { + //~^ ERROR E0277 + x: 3 + }; + + let baz: Foo = loop { }; + //~^ ERROR E0277 +} diff --git a/src/test/ui/traits/bound/on-structs-and-enums-locals.stderr b/src/test/ui/traits/bound/on-structs-and-enums-locals.stderr new file mode 100644 index 000000000..c9068a270 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-locals.stderr @@ -0,0 +1,27 @@ +error[E0277]: the trait bound `usize: Trait` is not satisfied + --> $DIR/on-structs-and-enums-locals.rs:15:14 + | +LL | let baz: Foo = loop { }; + | ^^^^^^^^^^ the trait `Trait` is not implemented for `usize` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums-locals.rs:5:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` + +error[E0277]: the trait bound `{integer}: Trait` is not satisfied + --> $DIR/on-structs-and-enums-locals.rs:10:15 + | +LL | let foo = Foo { + | ^^^ the trait `Trait` is not implemented for `{integer}` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums-locals.rs:5:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/on-structs-and-enums-rpass.rs b/src/test/ui/traits/bound/on-structs-and-enums-rpass.rs new file mode 100644 index 000000000..4dc4fecc9 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-rpass.rs @@ -0,0 +1,21 @@ +// run-pass +#![allow(dead_code)] +#![allow(unused_variables)] +// pretty-expanded FIXME #23616 + +trait U {} +trait T { fn get(self) -> X; } + +trait S2 { + fn m(x: Box+'static>) {} +} + +struct St { + f: Box+'static>, +} + +impl St { + fn blah() {} +} + +fn main() {} diff --git a/src/test/ui/traits/bound/on-structs-and-enums-static.rs b/src/test/ui/traits/bound/on-structs-and-enums-static.rs new file mode 100644 index 000000000..df3f8b8a5 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-static.rs @@ -0,0 +1,15 @@ +trait Trait { + fn dummy(&self) { } +} + +struct Foo { + x: T, +} + +static X: Foo = Foo { +//~^ ERROR E0277 + x: 1, +}; + +fn main() { +} diff --git a/src/test/ui/traits/bound/on-structs-and-enums-static.stderr b/src/test/ui/traits/bound/on-structs-and-enums-static.stderr new file mode 100644 index 000000000..fda734e85 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-static.stderr @@ -0,0 +1,15 @@ +error[E0277]: the trait bound `usize: Trait` is not satisfied + --> $DIR/on-structs-and-enums-static.rs:9:11 + | +LL | static X: Foo = Foo { + | ^^^^^^^^^^ the trait `Trait` is not implemented for `usize` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums-static.rs:5:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/on-structs-and-enums-xc.rs b/src/test/ui/traits/bound/on-structs-and-enums-xc.rs new file mode 100644 index 000000000..94316d240 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-xc.rs @@ -0,0 +1,14 @@ +// aux-build:on_structs_and_enums_xc.rs + +extern crate on_structs_and_enums_xc; + +use on_structs_and_enums_xc::{Bar, Foo, Trait}; + +fn explode(x: Foo) {} +//~^ ERROR E0277 + +fn kaboom(y: Bar) {} +//~^ ERROR E0277 + +fn main() { +} diff --git a/src/test/ui/traits/bound/on-structs-and-enums-xc.stderr b/src/test/ui/traits/bound/on-structs-and-enums-xc.stderr new file mode 100644 index 000000000..5064b60bf --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-xc.stderr @@ -0,0 +1,27 @@ +error[E0277]: the trait bound `usize: Trait` is not satisfied + --> $DIR/on-structs-and-enums-xc.rs:7:15 + | +LL | fn explode(x: Foo) {} + | ^^^^^^^^^^ the trait `Trait` is not implemented for `usize` + | +note: required by a bound in `Foo` + --> $DIR/auxiliary/on_structs_and_enums_xc.rs:5:18 + | +LL | pub struct Foo { + | ^^^^^ required by this bound in `Foo` + +error[E0277]: the trait bound `f32: Trait` is not satisfied + --> $DIR/on-structs-and-enums-xc.rs:10:14 + | +LL | fn kaboom(y: Bar) {} + | ^^^^^^^^ the trait `Trait` is not implemented for `f32` + | +note: required by a bound in `Bar` + --> $DIR/auxiliary/on_structs_and_enums_xc.rs:9:16 + | +LL | pub enum Bar { + | ^^^^^ required by this bound in `Bar` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/on-structs-and-enums-xc1.rs b/src/test/ui/traits/bound/on-structs-and-enums-xc1.rs new file mode 100644 index 000000000..8156868e0 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-xc1.rs @@ -0,0 +1,15 @@ +// aux-build:on_structs_and_enums_xc.rs + +extern crate on_structs_and_enums_xc; + +use on_structs_and_enums_xc::{Bar, Foo, Trait}; + +fn main() { + let foo = Foo { + //~^ ERROR E0277 + x: 3 + }; + let bar: Bar = return; + //~^ ERROR E0277 + let _ = bar; +} diff --git a/src/test/ui/traits/bound/on-structs-and-enums-xc1.stderr b/src/test/ui/traits/bound/on-structs-and-enums-xc1.stderr new file mode 100644 index 000000000..f4cc64af9 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums-xc1.stderr @@ -0,0 +1,27 @@ +error[E0277]: the trait bound `f64: Trait` is not satisfied + --> $DIR/on-structs-and-enums-xc1.rs:12:14 + | +LL | let bar: Bar = return; + | ^^^^^^^^ the trait `Trait` is not implemented for `f64` + | +note: required by a bound in `Bar` + --> $DIR/auxiliary/on_structs_and_enums_xc.rs:9:16 + | +LL | pub enum Bar { + | ^^^^^ required by this bound in `Bar` + +error[E0277]: the trait bound `{integer}: Trait` is not satisfied + --> $DIR/on-structs-and-enums-xc1.rs:8:15 + | +LL | let foo = Foo { + | ^^^ the trait `Trait` is not implemented for `{integer}` + | +note: required by a bound in `Foo` + --> $DIR/auxiliary/on_structs_and_enums_xc.rs:5:18 + | +LL | pub struct Foo { + | ^^^^^ required by this bound in `Foo` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/on-structs-and-enums.rs b/src/test/ui/traits/bound/on-structs-and-enums.rs new file mode 100644 index 000000000..024084fa7 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums.rs @@ -0,0 +1,43 @@ +trait Trait {} + +struct Foo { + x: T, +} + +enum Bar { + ABar(isize), + BBar(T), + CBar(usize), +} + +impl Foo { +//~^ ERROR `T: Trait` is not satisfied + fn uhoh() {} +} + +struct Baz { + a: Foo, //~ ERROR E0277 +} + +enum Boo { + Quux(Bar), //~ ERROR E0277 +} + +struct Badness { + b: Foo, //~ ERROR E0277 +} + +enum MoreBadness { + EvenMoreBadness(Bar), //~ ERROR E0277 +} + +struct TupleLike( + Foo, //~ ERROR E0277 +); + +enum Enum { + DictionaryLike { field: Bar }, //~ ERROR E0277 +} + +fn main() { +} diff --git a/src/test/ui/traits/bound/on-structs-and-enums.stderr b/src/test/ui/traits/bound/on-structs-and-enums.stderr new file mode 100644 index 000000000..fe05b8634 --- /dev/null +++ b/src/test/ui/traits/bound/on-structs-and-enums.stderr @@ -0,0 +1,99 @@ +error[E0277]: the trait bound `T: Trait` is not satisfied + --> $DIR/on-structs-and-enums.rs:13:9 + | +LL | impl Foo { + | ^^^^^^ the trait `Trait` is not implemented for `T` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums.rs:3:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` +help: consider restricting type parameter `T` + | +LL | impl Foo { + | +++++++ + +error[E0277]: the trait bound `isize: Trait` is not satisfied + --> $DIR/on-structs-and-enums.rs:19:8 + | +LL | a: Foo, + | ^^^^^^^^^^ the trait `Trait` is not implemented for `isize` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums.rs:3:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` + +error[E0277]: the trait bound `usize: Trait` is not satisfied + --> $DIR/on-structs-and-enums.rs:23:10 + | +LL | Quux(Bar), + | ^^^^^^^^^^ the trait `Trait` is not implemented for `usize` + | +note: required by a bound in `Bar` + --> $DIR/on-structs-and-enums.rs:7:12 + | +LL | enum Bar { + | ^^^^^ required by this bound in `Bar` + +error[E0277]: the trait bound `U: Trait` is not satisfied + --> $DIR/on-structs-and-enums.rs:27:8 + | +LL | b: Foo, + | ^^^^^^ the trait `Trait` is not implemented for `U` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums.rs:3:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` +help: consider restricting type parameter `U` + | +LL | struct Badness { + | +++++++ + +error[E0277]: the trait bound `V: Trait` is not satisfied + --> $DIR/on-structs-and-enums.rs:31:21 + | +LL | EvenMoreBadness(Bar), + | ^^^^^^ the trait `Trait` is not implemented for `V` + | +note: required by a bound in `Bar` + --> $DIR/on-structs-and-enums.rs:7:12 + | +LL | enum Bar { + | ^^^^^ required by this bound in `Bar` +help: consider restricting type parameter `V` + | +LL | enum MoreBadness { + | +++++++ + +error[E0277]: the trait bound `i32: Trait` is not satisfied + --> $DIR/on-structs-and-enums.rs:35:5 + | +LL | Foo, + | ^^^^^^^^ the trait `Trait` is not implemented for `i32` + | +note: required by a bound in `Foo` + --> $DIR/on-structs-and-enums.rs:3:14 + | +LL | struct Foo { + | ^^^^^ required by this bound in `Foo` + +error[E0277]: the trait bound `u8: Trait` is not satisfied + --> $DIR/on-structs-and-enums.rs:39:29 + | +LL | DictionaryLike { field: Bar }, + | ^^^^^^^ the trait `Trait` is not implemented for `u8` + | +note: required by a bound in `Bar` + --> $DIR/on-structs-and-enums.rs:7:12 + | +LL | enum Bar { + | ^^^^^ required by this bound in `Bar` + +error: aborting due to 7 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/recursion.rs b/src/test/ui/traits/bound/recursion.rs new file mode 100644 index 000000000..0023ff654 --- /dev/null +++ b/src/test/ui/traits/bound/recursion.rs @@ -0,0 +1,20 @@ +// run-pass +// pretty-expanded FIXME #23616 + +trait I { fn i(&self) -> Self; } + +trait A { + fn id(x:T) -> T { x.i() } +} + +trait J { fn j(&self) -> T; } + +trait B> { + fn id(x:T) -> T { x.j() } +} + +trait C { + fn id>(x:T) -> T { x.j() } +} + +pub fn main() { } diff --git a/src/test/ui/traits/bound/same-crate-name.rs b/src/test/ui/traits/bound/same-crate-name.rs new file mode 100644 index 000000000..8d646a414 --- /dev/null +++ b/src/test/ui/traits/bound/same-crate-name.rs @@ -0,0 +1,55 @@ +// aux-build:crate_a1.rs +// aux-build:crate_a2.rs + +// Issue 22750 +// This tests the extra help message reported when a trait bound +// is not met but the struct implements a trait with the same path. + +fn main() { + let foo = { + extern crate crate_a2 as a; + a::Foo + }; + + let implements_no_traits = { + extern crate crate_a2 as a; + a::DoesNotImplementTrait + }; + + let other_variant_implements_mismatched_trait = { + extern crate crate_a2 as a; + a::ImplementsWrongTraitConditionally { _marker: std::marker::PhantomData:: } + }; + + let other_variant_implements_correct_trait = { + extern crate crate_a1 as a; + a::ImplementsTraitForUsize { _marker: std::marker::PhantomData:: } + }; + + { + extern crate crate_a1 as a; + a::try_foo(foo); + //~^ ERROR E0277 + //~| trait impl with same name found + //~| perhaps two different versions of crate `crate_a2` + + // We don't want to see the "version mismatch" help message here + // because `implements_no_traits` has no impl for `Foo` + a::try_foo(implements_no_traits); + //~^ ERROR E0277 + + // We don't want to see the "version mismatch" help message here + // because `other_variant_implements_mismatched_trait` + // does not have an impl for its `` variant, + // only for its `` variant. + a::try_foo(other_variant_implements_mismatched_trait); + //~^ ERROR E0277 + + // We don't want to see the "version mismatch" help message here + // because `ImplementsTraitForUsize` only has + // impls for the correct trait where the path is not misleading. + a::try_foo(other_variant_implements_correct_trait); + //~^ ERROR E0277 + //~| the trait `main::a::Bar` is implemented for `ImplementsTraitForUsize` + } +} diff --git a/src/test/ui/traits/bound/same-crate-name.stderr b/src/test/ui/traits/bound/same-crate-name.stderr new file mode 100644 index 000000000..f66cad77f --- /dev/null +++ b/src/test/ui/traits/bound/same-crate-name.stderr @@ -0,0 +1,75 @@ +error[E0277]: the trait bound `Foo: main::a::Bar` is not satisfied + --> $DIR/same-crate-name.rs:31:20 + | +LL | a::try_foo(foo); + | ---------- ^^^ the trait `main::a::Bar` is not implemented for `Foo` + | | + | required by a bound introduced by this call + | +help: trait impl with same name found + --> $DIR/auxiliary/crate_a2.rs:5:1 + | +LL | impl Bar for Foo {} + | ^^^^^^^^^^^^^^^^ + = note: perhaps two different versions of crate `crate_a2` are being used? + = help: the trait `main::a::Bar` is implemented for `ImplementsTraitForUsize` +note: required by a bound in `try_foo` + --> $DIR/auxiliary/crate_a1.rs:3:24 + | +LL | pub fn try_foo(x: impl Bar) {} + | ^^^ required by this bound in `try_foo` + +error[E0277]: the trait bound `DoesNotImplementTrait: main::a::Bar` is not satisfied + --> $DIR/same-crate-name.rs:38:20 + | +LL | a::try_foo(implements_no_traits); + | ---------- ^^^^^^^^^^^^^^^^^^^^ the trait `main::a::Bar` is not implemented for `DoesNotImplementTrait` + | | + | required by a bound introduced by this call + | + = help: the trait `main::a::Bar` is implemented for `ImplementsTraitForUsize` +note: required by a bound in `try_foo` + --> $DIR/auxiliary/crate_a1.rs:3:24 + | +LL | pub fn try_foo(x: impl Bar) {} + | ^^^ required by this bound in `try_foo` + +error[E0277]: the trait bound `ImplementsWrongTraitConditionally: main::a::Bar` is not satisfied + --> $DIR/same-crate-name.rs:45:20 + | +LL | a::try_foo(other_variant_implements_mismatched_trait); + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `main::a::Bar` is not implemented for `ImplementsWrongTraitConditionally` + | | + | required by a bound introduced by this call + | +help: trait impl with same name found + --> $DIR/auxiliary/crate_a2.rs:13:1 + | +LL | impl Bar for ImplementsWrongTraitConditionally {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: perhaps two different versions of crate `crate_a2` are being used? + = help: the trait `main::a::Bar` is implemented for `ImplementsTraitForUsize` +note: required by a bound in `try_foo` + --> $DIR/auxiliary/crate_a1.rs:3:24 + | +LL | pub fn try_foo(x: impl Bar) {} + | ^^^ required by this bound in `try_foo` + +error[E0277]: the trait bound `ImplementsTraitForUsize: main::a::Bar` is not satisfied + --> $DIR/same-crate-name.rs:51:20 + | +LL | a::try_foo(other_variant_implements_correct_trait); + | ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `main::a::Bar` is not implemented for `ImplementsTraitForUsize` + | | + | required by a bound introduced by this call + | + = help: the trait `main::a::Bar` is implemented for `ImplementsTraitForUsize` +note: required by a bound in `try_foo` + --> $DIR/auxiliary/crate_a1.rs:3:24 + | +LL | pub fn try_foo(x: impl Bar) {} + | ^^^ required by this bound in `try_foo` + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/bound/sugar.rs b/src/test/ui/traits/bound/sugar.rs new file mode 100644 index 000000000..65b6f6faa --- /dev/null +++ b/src/test/ui/traits/bound/sugar.rs @@ -0,0 +1,19 @@ +// Tests for "default" bounds inferred for traits with no bounds list. + +trait Foo {} + +fn a(_x: Box) { +} + +fn b(_x: &'static (dyn Foo + 'static)) { +} + +fn c(x: Box) { + a(x); //~ ERROR mismatched types +} + +fn d(x: &'static (dyn Foo + Sync)) { + b(x); +} + +fn main() {} diff --git a/src/test/ui/traits/bound/sugar.stderr b/src/test/ui/traits/bound/sugar.stderr new file mode 100644 index 000000000..b67648c7b --- /dev/null +++ b/src/test/ui/traits/bound/sugar.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/sugar.rs:12:7 + | +LL | a(x); + | - ^ expected trait `Foo + Send`, found trait `Foo + Sync` + | | + | arguments to this function are incorrect + | + = note: expected struct `Box<(dyn Foo + Send + 'static)>` + found struct `Box<(dyn Foo + Sync + 'static)>` +note: function defined here + --> $DIR/sugar.rs:5:4 + | +LL | fn a(_x: Box) { + | ^ ----------------------- + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/traits/bug-7183-generics.rs b/src/test/ui/traits/bug-7183-generics.rs new file mode 100644 index 000000000..f53a17361 --- /dev/null +++ b/src/test/ui/traits/bug-7183-generics.rs @@ -0,0 +1,36 @@ +// run-pass + +trait Speak : Sized { + fn say(&self, s:&str) -> String; + fn hi(&self) -> String { hello(self) } +} + +fn hello(s:&S) -> String{ + s.say("hello") +} + +impl Speak for isize { + fn say(&self, s:&str) -> String { + format!("{}: {}", s, *self) + } +} + +impl Speak for Option { + fn say(&self, s:&str) -> String { + match *self { + None => format!("{} - none", s), + Some(ref x) => { format!("something!{}", x.say(s)) } + } + } +} + + +pub fn main() { + assert_eq!(3.hi(), "hello: 3".to_string()); + assert_eq!(Some(Some(3)).hi(), + "something!something!hello: 3".to_string()); + assert_eq!(None::.hi(), "hello - none".to_string()); + + assert_eq!(Some(None::).hi(), "something!hello - none".to_string()); + assert_eq!(Some(3).hi(), "something!hello: 3".to_string()); +} diff --git a/src/test/ui/traits/bug-7295.rs b/src/test/ui/traits/bug-7295.rs new file mode 100644 index 000000000..156ff2ee8 --- /dev/null +++ b/src/test/ui/traits/bug-7295.rs @@ -0,0 +1,14 @@ +// run-pass +// pretty-expanded FIXME #23616 + +pub trait Foo { + fn func1(&self, t: U, w: T); + + fn func2(&self, t: U, w: T) { + self.func1(t, w); + } +} + +pub fn main() { + +} diff --git a/src/test/ui/traits/cache-issue-18209.rs b/src/test/ui/traits/cache-issue-18209.rs new file mode 100644 index 000000000..15676e455 --- /dev/null +++ b/src/test/ui/traits/cache-issue-18209.rs @@ -0,0 +1,20 @@ +// run-pass +// Test that the cache results from the default method do not pollute +// the cache for the later call in `load()`. +// +// See issue #18209. + +// pretty-expanded FIXME #23616 + +pub trait Foo { + fn load_from() -> Box; + fn load() -> Box { + Foo::load_from() + } +} + +pub fn load() -> Box { + Foo::load() +} + +fn main() { } diff --git a/src/test/ui/traits/cache-reached-depth-ice.rs b/src/test/ui/traits/cache-reached-depth-ice.rs new file mode 100644 index 000000000..c36ac0857 --- /dev/null +++ b/src/test/ui/traits/cache-reached-depth-ice.rs @@ -0,0 +1,45 @@ +#![feature(rustc_attrs)] + +// Test for a particular corner case where the evaluation +// cache can get out of date. The problem here is that +// when we cache C, we have observed that it reaches +// to depth 2 (the node for B), but we later realize +// that B itself depends on A (reached depth 0). We +// failed to update the depth for C transitively, which +// resulted in an assertion failure when it was referenced +// from D. +// +// A (reached depth 0) +// E +// B // depth 2 -- reached depth = 0 +// C // depth 3 -- reached depth = 2 (should be 0) +// B +// A // depth 0 +// D (depth 1) +// C (cache -- reached depth = 2) + +struct A { + e: E, + d: C, +} + +struct E { + b: B, +} + +struct B { + a: Option>, + c: C, +} + +struct C { + b: Option>, +} + +#[rustc_evaluate_where_clauses] +fn test() {} + +fn main() { + test::(); + //~^ ERROR evaluate(Binder(TraitPredicate(, polarity:Positive), [])) = Ok(EvaluatedToOk) +} diff --git a/src/test/ui/traits/cache-reached-depth-ice.stderr b/src/test/ui/traits/cache-reached-depth-ice.stderr new file mode 100644 index 000000000..082aa0f5c --- /dev/null +++ b/src/test/ui/traits/cache-reached-depth-ice.stderr @@ -0,0 +1,11 @@ +error: evaluate(Binder(TraitPredicate(, polarity:Positive), [])) = Ok(EvaluatedToOk) + --> $DIR/cache-reached-depth-ice.rs:43:5 + | +LL | fn test() {} + | ---- predicate +... +LL | test::(); + | ^^^^^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/traits/coercion-generic-bad.rs b/src/test/ui/traits/coercion-generic-bad.rs new file mode 100644 index 000000000..2e115c732 --- /dev/null +++ b/src/test/ui/traits/coercion-generic-bad.rs @@ -0,0 +1,19 @@ +struct Struct { + person: &'static str +} + +trait Trait { + fn f(&self, x: T); +} + +impl Trait<&'static str> for Struct { + fn f(&self, x: &'static str) { + println!("Hello, {}!", x); + } +} + +fn main() { + let s: Box> = Box::new(Struct { person: "Fred" }); + //~^ ERROR `Struct: Trait` is not satisfied + s.f(1); +} diff --git a/src/test/ui/traits/coercion-generic-bad.stderr b/src/test/ui/traits/coercion-generic-bad.stderr new file mode 100644 index 000000000..93d6770eb --- /dev/null +++ b/src/test/ui/traits/coercion-generic-bad.stderr @@ -0,0 +1,12 @@ +error[E0277]: the trait bound `Struct: Trait` is not satisfied + --> $DIR/coercion-generic-bad.rs:16:36 + | +LL | let s: Box> = Box::new(Struct { person: "Fred" }); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `Struct` + | + = help: the trait `Trait<&'static str>` is implemented for `Struct` + = note: required for the cast from `Struct` to the object type `dyn Trait` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/coercion-generic-regions.rs b/src/test/ui/traits/coercion-generic-regions.rs new file mode 100644 index 000000000..af478df6d --- /dev/null +++ b/src/test/ui/traits/coercion-generic-regions.rs @@ -0,0 +1,19 @@ +struct Struct { + person: &'static str +} + +trait Trait { + fn f(&self, x: T); +} + +impl Trait<&'static str> for Struct { + fn f(&self, x: &'static str) { + println!("Hello, {}!", x); + } +} + +fn main() { + let person = "Fred".to_string(); + let person: &str = &person; //~ ERROR `person` does not live long enough + let s: Box> = Box::new(Struct { person: person }); +} diff --git a/src/test/ui/traits/coercion-generic-regions.stderr b/src/test/ui/traits/coercion-generic-regions.stderr new file mode 100644 index 000000000..5cfb64901 --- /dev/null +++ b/src/test/ui/traits/coercion-generic-regions.stderr @@ -0,0 +1,15 @@ +error[E0597]: `person` does not live long enough + --> $DIR/coercion-generic-regions.rs:17:24 + | +LL | let person: &str = &person; + | ^^^^^^^ + | | + | borrowed value does not live long enough + | assignment requires that `person` is borrowed for `'static` +LL | let s: Box> = Box::new(Struct { person: person }); +LL | } + | - `person` dropped here while still borrowed + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0597`. diff --git a/src/test/ui/traits/coercion-generic.rs b/src/test/ui/traits/coercion-generic.rs new file mode 100644 index 000000000..bf4dda495 --- /dev/null +++ b/src/test/ui/traits/coercion-generic.rs @@ -0,0 +1,25 @@ +// run-pass +#![allow(dead_code)] +trait Trait { + fn f(&self, x: T); +} + +#[derive(Copy, Clone)] +struct Struct { + x: isize, + y: isize, +} + +impl Trait<&'static str> for Struct { + fn f(&self, x: &'static str) { + println!("Hi, {}!", x); + } +} + +pub fn main() { + let a = Struct { x: 1, y: 2 }; + let b: Box> = Box::new(a); + b.f("Mary"); + let c: &dyn Trait<&'static str> = &a; + c.f("Joe"); +} diff --git a/src/test/ui/traits/coercion.rs b/src/test/ui/traits/coercion.rs new file mode 100644 index 000000000..e62742bac --- /dev/null +++ b/src/test/ui/traits/coercion.rs @@ -0,0 +1,35 @@ +// run-pass +#![allow(dead_code)] +#![allow(unused_mut)] +#![allow(unused_variables)] + +use std::io::{self, Write}; + +trait Trait { + fn f(&self); +} + +#[derive(Copy, Clone)] +struct Struct { + x: isize, + y: isize, +} + +impl Trait for Struct { + fn f(&self) { + println!("Hi!"); + } +} + +fn foo(mut a: Box) {} + +pub fn main() { + let a = Struct { x: 1, y: 2 }; + let b: Box = Box::new(a); + b.f(); + let c: &dyn Trait = &a; + c.f(); + + let out = io::stdout(); + foo(Box::new(out)); +} diff --git a/src/test/ui/traits/composition-trivial.rs b/src/test/ui/traits/composition-trivial.rs new file mode 100644 index 000000000..90e5dcd68 --- /dev/null +++ b/src/test/ui/traits/composition-trivial.rs @@ -0,0 +1,12 @@ +// run-pass +// pretty-expanded FIXME #23616 + +trait Foo { + fn foo(&self); +} + +trait Bar : Foo { + fn bar(&self); +} + +pub fn main() {} diff --git a/src/test/ui/traits/conditional-dispatch.rs b/src/test/ui/traits/conditional-dispatch.rs new file mode 100644 index 000000000..dd882dce6 --- /dev/null +++ b/src/test/ui/traits/conditional-dispatch.rs @@ -0,0 +1,34 @@ +// run-pass +// Test that we are able to resolve conditional dispatch. Here, the +// blanket impl for T:Copy coexists with an impl for Box, because +// Box does not impl Copy. + + +trait Get { + fn get(&self) -> Self; +} + +trait MyCopy { fn copy(&self) -> Self; } +impl MyCopy for u16 { fn copy(&self) -> Self { *self } } +impl MyCopy for u32 { fn copy(&self) -> Self { *self } } +impl MyCopy for i32 { fn copy(&self) -> Self { *self } } +impl MyCopy for Option { fn copy(&self) -> Self { *self } } + +impl Get for T { + fn get(&self) -> T { self.copy() } +} + +impl Get for Box { + fn get(&self) -> Box { Box::new(get_it(&**self)) } +} + +fn get_it(t: &T) -> T { + (*t).get() +} + +fn main() { + assert_eq!(get_it(&1_u32), 1_u32); + assert_eq!(get_it(&1_u16), 1_u16); + assert_eq!(get_it(&Some(1_u16)), Some(1_u16)); + assert_eq!(get_it(&Box::new(1)), Box::new(1)); +} diff --git a/src/test/ui/traits/conditional-model-fn.rs b/src/test/ui/traits/conditional-model-fn.rs new file mode 100644 index 000000000..ba8867003 --- /dev/null +++ b/src/test/ui/traits/conditional-model-fn.rs @@ -0,0 +1,53 @@ +// run-pass +#![allow(unused_imports)] +// A model for how the `Fn` traits could work. You can implement at +// most one of `Go`, `GoMut`, or `GoOnce`, and then the others follow +// automatically. + +// aux-build:go_trait.rs + +extern crate go_trait; + +use go_trait::{Go, GoMut, GoOnce, go, go_mut, go_once}; + +use std::rc::Rc; +use std::cell::Cell; + +struct SomeGoableThing { + counter: Rc> +} + +impl Go for SomeGoableThing { + fn go(&self, arg: isize) { + self.counter.set(self.counter.get() + arg); + } +} + +struct SomeGoOnceableThing { + counter: Rc> +} + +impl GoOnce for SomeGoOnceableThing { + fn go_once(self, arg: isize) { + self.counter.set(self.counter.get() + arg); + } +} + +fn main() { + let counter = Rc::new(Cell::new(0)); + let mut x = SomeGoableThing { counter: counter.clone() }; + + go(&x, 10); + assert_eq!(counter.get(), 10); + + go_mut(&mut x, 100); + assert_eq!(counter.get(), 110); + + go_once(x, 1_000); + assert_eq!(counter.get(), 1_110); + + let x = SomeGoOnceableThing { counter: counter.clone() }; + + go_once(x, 10_000); + assert_eq!(counter.get(), 11_110); +} diff --git a/src/test/ui/traits/conservative_impl_trait.rs b/src/test/ui/traits/conservative_impl_trait.rs new file mode 100644 index 000000000..4f25e57be --- /dev/null +++ b/src/test/ui/traits/conservative_impl_trait.rs @@ -0,0 +1,8 @@ +// build-pass (FIXME(62277): could be check-pass?) +// #39665 + +fn batches(n: &u32) -> impl Iterator { + std::iter::once(n) +} + +fn main() {} diff --git a/src/test/ui/traits/copy-guessing.rs b/src/test/ui/traits/copy-guessing.rs new file mode 100644 index 000000000..f031dd9ca --- /dev/null +++ b/src/test/ui/traits/copy-guessing.rs @@ -0,0 +1,38 @@ +// run-pass +#![allow(dead_code)] +// "guessing" in trait selection can affect `copy_or_move`. Check that this +// is correctly handled. I am not sure what is the "correct" behaviour, +// but we should at least not ICE. + +use std::mem; + +struct U([u8; 1337]); + +struct S<'a,T:'a>(&'a T); +impl<'a, T> Clone for S<'a, T> { fn clone(&self) -> Self { S(self.0) } } +/// This impl triggers inference "guessing" - S<_>: Copy => _ = U +impl<'a> Copy for S<'a, Option> {} + +fn assert_impls_fnR>(_: &T){} + +fn main() { + let n = None; + let e = S(&n); + let f = || { + // S being copy is critical for this to work + drop(e); + mem::size_of_val(e.0) + }; + assert_impls_fn(&f); + assert_eq!(f(), 1337+1); + + assert_eq!((|| { + // S being Copy is not critical here, but + // we check it anyway. + let n = None; + let e = S(&n); + let ret = mem::size_of_val(e.0); + drop(e); + ret + })(), 1337+1); +} diff --git a/src/test/ui/traits/copy-impl-cannot-normalize.rs b/src/test/ui/traits/copy-impl-cannot-normalize.rs new file mode 100644 index 000000000..a78ff046e --- /dev/null +++ b/src/test/ui/traits/copy-impl-cannot-normalize.rs @@ -0,0 +1,25 @@ +trait TraitFoo { + type Bar; +} + +struct Foo +where + T: TraitFoo, +{ + inner: T::Bar, +} + +impl Clone for Foo +where + T: TraitFoo, + T::Bar: Clone, +{ + fn clone(&self) -> Self { + Self { inner: self.inner.clone() } + } +} + +impl Copy for Foo {} +//~^ ERROR the trait bound `T: TraitFoo` is not satisfied + +fn main() {} diff --git a/src/test/ui/traits/copy-impl-cannot-normalize.stderr b/src/test/ui/traits/copy-impl-cannot-normalize.stderr new file mode 100644 index 000000000..afdad1609 --- /dev/null +++ b/src/test/ui/traits/copy-impl-cannot-normalize.stderr @@ -0,0 +1,14 @@ +error[E0277]: the trait bound `T: TraitFoo` is not satisfied + --> $DIR/copy-impl-cannot-normalize.rs:22:1 + | +LL | impl Copy for Foo {} + | ^^^^^^^^^^^^^^^^^^^^^^^ the trait `TraitFoo` is not implemented for `T` + | +help: consider restricting type parameter `T` + | +LL | impl Copy for Foo {} + | ++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/cycle-cache-err-60010.rs b/src/test/ui/traits/cycle-cache-err-60010.rs new file mode 100644 index 000000000..94e718317 --- /dev/null +++ b/src/test/ui/traits/cycle-cache-err-60010.rs @@ -0,0 +1,74 @@ +// Test that we properly detect the cycle amongst the traits +// here and report an error. + +use std::panic::RefUnwindSafe; + +trait Database { + type Storage; +} +trait HasQueryGroup {} +trait Query { + type Data; +} +trait SourceDatabase { + fn parse(&self) { + loop {} + } +} + +struct ParseQuery; +struct RootDatabase { + _runtime: Runtime, +} +struct Runtime { + _storage: Box, +} +struct SalsaStorage { + _parse: >::Data, + //~^ ERROR overflow +} + +impl Database for RootDatabase { + // This would also be an error if we didn't abort compilation on the error + // above. + type Storage = SalsaStorage; +} +impl HasQueryGroup for RootDatabase {} +impl Query for ParseQuery +where + DB: SourceDatabase, + DB: Database, +{ + type Data = RootDatabase; +} +impl SourceDatabase for T +where + T: RefUnwindSafe, + T: HasQueryGroup, +{ +} + +pub(crate) fn goto_implementation(db: &RootDatabase) -> u32 { + // This is not satisfied: + // + // - `RootDatabase: SourceDatabase` + // - requires `RootDatabase: RefUnwindSafe` + `RootDatabase: HasQueryGroup` + // - `RootDatabase: RefUnwindSafe` + // - requires `Runtime: RefUnwindSafe` + // - `Runtime: RefUnwindSafe` + // - requires `DB::Storage: RefUnwindSafe` (`SalsaStorage: RefUnwindSafe`) + // - `SalsaStorage: RefUnwindSafe` + // - requires `>::Data: RefUnwindSafe`, + // which means `ParseQuery: Query` + // - `ParseQuery: Query` + // - requires `RootDatabase: SourceDatabase`, + // - `RootDatabase: SourceDatabase` is already on the stack, so we have a + // cycle with non-coinductive participants + // + // we used to fail to report an error here because we got the + // caching wrong. + SourceDatabase::parse(db); + 22 +} + +fn main() {} diff --git a/src/test/ui/traits/cycle-cache-err-60010.stderr b/src/test/ui/traits/cycle-cache-err-60010.stderr new file mode 100644 index 000000000..859403f20 --- /dev/null +++ b/src/test/ui/traits/cycle-cache-err-60010.stderr @@ -0,0 +1,33 @@ +error[E0275]: overflow evaluating the requirement `SalsaStorage: RefUnwindSafe` + --> $DIR/cycle-cache-err-60010.rs:27:13 + | +LL | _parse: >::Data, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: required because it appears within the type `PhantomData` + = note: required because it appears within the type `Unique` + = note: required because it appears within the type `Box` +note: required because it appears within the type `Runtime` + --> $DIR/cycle-cache-err-60010.rs:23:8 + | +LL | struct Runtime { + | ^^^^^^^ +note: required because it appears within the type `RootDatabase` + --> $DIR/cycle-cache-err-60010.rs:20:8 + | +LL | struct RootDatabase { + | ^^^^^^^^^^^^ +note: required because of the requirements on the impl of `SourceDatabase` for `RootDatabase` + --> $DIR/cycle-cache-err-60010.rs:44:9 + | +LL | impl SourceDatabase for T + | ^^^^^^^^^^^^^^ ^ +note: required because of the requirements on the impl of `Query` for `ParseQuery` + --> $DIR/cycle-cache-err-60010.rs:37:10 + | +LL | impl Query for ParseQuery + | ^^^^^^^^^ ^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/cycle-generic-bound.rs b/src/test/ui/traits/cycle-generic-bound.rs new file mode 100644 index 000000000..9241f3789 --- /dev/null +++ b/src/test/ui/traits/cycle-generic-bound.rs @@ -0,0 +1,11 @@ +// run-pass +// Regression test for #15477. This test just needs to compile. + +// pretty-expanded FIXME #23616 + +trait Chromosome> { +} + +impl Chromosome for i32 { } + +fn main() { } diff --git a/src/test/ui/traits/cycle-type-trait.rs b/src/test/ui/traits/cycle-type-trait.rs new file mode 100644 index 000000000..c62d01403 --- /dev/null +++ b/src/test/ui/traits/cycle-type-trait.rs @@ -0,0 +1,25 @@ +// run-pass +#![allow(dead_code)] +// Test a case where a supertrait references a type that references +// the original trait. This poses no problem at the moment. + +// pretty-expanded FIXME #23616 + +trait Chromosome: Get> { +} + +trait Get { + fn get(&self) -> A; +} + +struct Struct { c: C } + +impl Chromosome for i32 { } + +impl Get> for i32 { + fn get(&self) -> Struct { + Struct { c: *self } + } +} + +fn main() { } diff --git a/src/test/ui/traits/default-method/auxiliary/xc.rs b/src/test/ui/traits/default-method/auxiliary/xc.rs new file mode 100644 index 000000000..0fb26af80 --- /dev/null +++ b/src/test/ui/traits/default-method/auxiliary/xc.rs @@ -0,0 +1,40 @@ +pub struct Something { pub x: isize } + +pub trait A { + fn f(&self) -> isize; + fn g(&self) -> isize { 10 } + fn h(&self) -> isize { 11 } + fn lurr(x: &Self, y: &Self) -> isize { x.g() + y.h() } +} + + +impl A for isize { + fn f(&self) -> isize { 10 } +} + +impl A for Something { + fn f(&self) -> isize { 10 } +} + +pub trait B { + fn thing(&self, x: T, y: U) -> (T, U) { (x, y) } + fn staticthing(_z: &Self, x: T, y: U) -> (T, U) { (x, y) } +} + +impl B for isize { } +impl B for bool { } + + + +pub trait TestEquality { + fn test_eq(&self, rhs: &Self) -> bool; + fn test_neq(&self, rhs: &Self) -> bool { + !self.test_eq(rhs) + } +} + +impl TestEquality for isize { + fn test_eq(&self, rhs: &isize) -> bool { + *self == *rhs + } +} diff --git a/src/test/ui/traits/default-method/auxiliary/xc_2.rs b/src/test/ui/traits/default-method/auxiliary/xc_2.rs new file mode 100644 index 000000000..979233820 --- /dev/null +++ b/src/test/ui/traits/default-method/auxiliary/xc_2.rs @@ -0,0 +1,17 @@ +// aux-build:xc.rs + +extern crate xc as aux; +use aux::A; + +pub struct a_struct { pub x: isize } + +impl A for a_struct { + fn f(&self) -> isize { 10 } +} + +// This function will need to get inlined, and badness may result. +pub fn welp(x: A) -> A { + let a = a_struct { x: 0 }; + a.g(); + x +} diff --git a/src/test/ui/traits/default-method/bound-subst.rs b/src/test/ui/traits/default-method/bound-subst.rs new file mode 100644 index 000000000..6a5d5c8ba --- /dev/null +++ b/src/test/ui/traits/default-method/bound-subst.rs @@ -0,0 +1,18 @@ +// run-pass + + +trait A { + fn g(&self, x: T, y: U) -> (T, U) { (x, y) } +} + +impl A for i32 { } +impl A for u32 { } + +fn f>(i: V, j: T, k: U) -> (T, U) { + i.g(j, k) +} + +pub fn main () { + assert_eq!(f(0, 1, 2), (1, 2)); + assert_eq!(f(0, 1, 2), (1, 2)); +} diff --git a/src/test/ui/traits/default-method/bound-subst2.rs b/src/test/ui/traits/default-method/bound-subst2.rs new file mode 100644 index 000000000..78eabba2d --- /dev/null +++ b/src/test/ui/traits/default-method/bound-subst2.rs @@ -0,0 +1,16 @@ +// run-pass + + +trait A { + fn g(&self, x: T) -> T { x } +} + +impl A for isize { } + +fn f>(i: V, j: T) -> T { + i.g(j) +} + +pub fn main () { + assert_eq!(f(0, 2), 2); +} diff --git a/src/test/ui/traits/default-method/bound-subst3.rs b/src/test/ui/traits/default-method/bound-subst3.rs new file mode 100644 index 000000000..dd39dec4b --- /dev/null +++ b/src/test/ui/traits/default-method/bound-subst3.rs @@ -0,0 +1,17 @@ +// run-pass + + +trait A { + fn g(&self, x: T, y: T) -> (T, T) { (x, y) } +} + +impl A for isize { } + +fn f(i: V, j: T, k: T) -> (T, T) { + i.g(j, k) +} + +pub fn main () { + assert_eq!(f(0, 1, 2), (1, 2)); + assert_eq!(f(0, 1u8, 2u8), (1u8, 2u8)); +} diff --git a/src/test/ui/traits/default-method/bound-subst4.rs b/src/test/ui/traits/default-method/bound-subst4.rs new file mode 100644 index 000000000..ef1330645 --- /dev/null +++ b/src/test/ui/traits/default-method/bound-subst4.rs @@ -0,0 +1,19 @@ +// run-pass +#![allow(unused_variables)] + + +trait A { + fn g(&self, x: usize) -> usize { x } + fn h(&self, x: T) { } +} + +impl A for isize { } + +fn f>(i: V, j: usize) -> usize { + i.g(j) +} + +pub fn main () { + assert_eq!(f::(0, 2), 2); + assert_eq!(f::(0, 2), 2); +} diff --git a/src/test/ui/traits/default-method/bound.rs b/src/test/ui/traits/default-method/bound.rs new file mode 100644 index 000000000..0855a9db8 --- /dev/null +++ b/src/test/ui/traits/default-method/bound.rs @@ -0,0 +1,16 @@ +// run-pass + + +trait A { + fn g(&self) -> isize { 10 } +} + +impl A for isize { } + +fn f(i: T) { + assert_eq!(i.g(), 10); +} + +pub fn main () { + f(0); +} diff --git a/src/test/ui/traits/default-method/macro.rs b/src/test/ui/traits/default-method/macro.rs new file mode 100644 index 000000000..2b50ee9b4 --- /dev/null +++ b/src/test/ui/traits/default-method/macro.rs @@ -0,0 +1,20 @@ +// run-pass + + +trait Foo { + fn bar(&self) -> String { + format!("test") + } +} + +enum Baz { + Quux +} + +impl Foo for Baz { +} + +pub fn main() { + let q = Baz::Quux; + assert_eq!(q.bar(), "test".to_string()); +} diff --git a/src/test/ui/traits/default-method/mut.rs b/src/test/ui/traits/default-method/mut.rs new file mode 100644 index 000000000..5f8e983b0 --- /dev/null +++ b/src/test/ui/traits/default-method/mut.rs @@ -0,0 +1,11 @@ +// run-pass +#![allow(unused_assignments)] +// pretty-expanded FIXME #23616 + +#![allow(unused_variables)] + +trait Foo { + fn foo(&self, mut v: isize) { v = 1; } +} + +pub fn main() {} diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of.rs b/src/test/ui/traits/default-method/rustc_must_implement_one_of.rs new file mode 100644 index 000000000..5ba2f5ce3 --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of.rs @@ -0,0 +1,44 @@ +#![feature(rustc_attrs)] + +#[rustc_must_implement_one_of(eq, neq)] +trait Equal { + fn eq(&self, other: &Self) -> bool { + !self.neq(other) + } + + fn neq(&self, other: &Self) -> bool { + !self.eq(other) + } +} + +struct T0; +struct T1; +struct T2; +struct T3; + +impl Equal for T0 { + fn eq(&self, _other: &Self) -> bool { + true + } +} + +impl Equal for T1 { + fn neq(&self, _other: &Self) -> bool { + false + } +} + +impl Equal for T2 { + fn eq(&self, _other: &Self) -> bool { + true + } + + fn neq(&self, _other: &Self) -> bool { + false + } +} + +impl Equal for T3 {} +//~^ not all trait items implemented, missing one of: `eq`, `neq` + +fn main() {} diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of.stderr b/src/test/ui/traits/default-method/rustc_must_implement_one_of.stderr new file mode 100644 index 000000000..5a4dd1388 --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of.stderr @@ -0,0 +1,15 @@ +error[E0046]: not all trait items implemented, missing one of: `eq`, `neq` + --> $DIR/rustc_must_implement_one_of.rs:41:1 + | +LL | impl Equal for T3 {} + | ^^^^^^^^^^^^^^^^^ missing one of `eq`, `neq` in implementation + | +note: required because of this annotation + --> $DIR/rustc_must_implement_one_of.rs:3:1 + | +LL | #[rustc_must_implement_one_of(eq, neq)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0046`. diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs b/src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs new file mode 100644 index 000000000..56e8fcff0 --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs @@ -0,0 +1,19 @@ +#![feature(rustc_attrs)] + +#[rustc_must_implement_one_of(a, a)] +//~^ Functions names are duplicated +trait Trait { + fn a() {} +} + +#[rustc_must_implement_one_of(b, a, a, c, b, c)] +//~^ Functions names are duplicated +//~| Functions names are duplicated +//~| Functions names are duplicated +trait Trait1 { + fn a() {} + fn b() {} + fn c() {} +} + +fn main() {} diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.stderr b/src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.stderr new file mode 100644 index 000000000..777beba61 --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_duplicates.stderr @@ -0,0 +1,34 @@ +error: Functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:3:31 + | +LL | #[rustc_must_implement_one_of(a, a)] + | ^ ^ + | + = note: All `#[rustc_must_implement_one_of]` arguments must be unique + +error: Functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:9:34 + | +LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)] + | ^ ^ + | + = note: All `#[rustc_must_implement_one_of]` arguments must be unique + +error: Functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:9:31 + | +LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)] + | ^ ^ + | + = note: All `#[rustc_must_implement_one_of]` arguments must be unique + +error: Functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:9:40 + | +LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)] + | ^ ^ + | + = note: All `#[rustc_must_implement_one_of]` arguments must be unique + +error: aborting due to 4 previous errors + diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.rs b/src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.rs new file mode 100644 index 000000000..ec2995872 --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.rs @@ -0,0 +1,13 @@ +#[rustc_must_implement_one_of(eq, neq)] +//~^ the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete definition of a trait, it's currently in experimental form and should be changed before being exposed outside of the std +trait Equal { + fn eq(&self, other: &Self) -> bool { + !self.neq(other) + } + + fn neq(&self, other: &Self) -> bool { + !self.eq(other) + } +} + +fn main() {} diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.stderr b/src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.stderr new file mode 100644 index 000000000..228bc3e35 --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_gated.stderr @@ -0,0 +1,11 @@ +error[E0658]: the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete definition of a trait, it's currently in experimental form and should be changed before being exposed outside of the std + --> $DIR/rustc_must_implement_one_of_gated.rs:1:1 + | +LL | #[rustc_must_implement_one_of(eq, neq)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add `#![feature(rustc_attrs)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs new file mode 100644 index 000000000..1f896da94 --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs @@ -0,0 +1,46 @@ +#![feature(rustc_attrs)] + +#[rustc_must_implement_one_of(a, b)] +//~^ Function not found in this trait +//~| Function not found in this trait +trait Tr0 {} + +#[rustc_must_implement_one_of(a, b)] +//~^ Function not found in this trait +trait Tr1 { + fn a() {} +} + +#[rustc_must_implement_one_of(a)] +//~^ the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args +trait Tr2 { + fn a() {} +} + +#[rustc_must_implement_one_of] +//~^ malformed `rustc_must_implement_one_of` attribute input +trait Tr3 {} + +#[rustc_must_implement_one_of(A, B)] +trait Tr4 { + const A: u8 = 1; //~ Not a function + + type B; //~ Not a function +} + +#[rustc_must_implement_one_of(a, b)] +trait Tr5 { + fn a(); //~ This function doesn't have a default implementation + + fn b(); //~ This function doesn't have a default implementation +} + +#[rustc_must_implement_one_of(abc, xyz)] +//~^ attribute should be applied to a trait +fn function() {} + +#[rustc_must_implement_one_of(abc, xyz)] +//~^ attribute should be applied to a trait +struct Struct {} + +fn main() {} diff --git a/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr new file mode 100644 index 000000000..869184f0d --- /dev/null +++ b/src/test/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr @@ -0,0 +1,100 @@ +error: malformed `rustc_must_implement_one_of` attribute input + --> $DIR/rustc_must_implement_one_of_misuse.rs:20:1 + | +LL | #[rustc_must_implement_one_of] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_must_implement_one_of(function1, function2, ...)]` + +error: attribute should be applied to a trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:38:1 + | +LL | #[rustc_must_implement_one_of(abc, xyz)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | +LL | fn function() {} + | ---------------- not a trait + +error: attribute should be applied to a trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:42:1 + | +LL | #[rustc_must_implement_one_of(abc, xyz)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | +LL | struct Struct {} + | ---------------- not a trait + +error: Function not found in this trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:3:31 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^ + +error: Function not found in this trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:3:34 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^ + +error: Function not found in this trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:8:34 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^ + +error: the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args + --> $DIR/rustc_must_implement_one_of_misuse.rs:14:1 + | +LL | #[rustc_must_implement_one_of(a)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: Not a function + --> $DIR/rustc_must_implement_one_of_misuse.rs:26:5 + | +LL | const A: u8 = 1; + | ^^^^^^^^^^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:24:1 + | +LL | #[rustc_must_implement_one_of(A, B)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: All `#[rustc_must_implement_one_of]` arguments must be associated function names + +error: Not a function + --> $DIR/rustc_must_implement_one_of_misuse.rs:28:5 + | +LL | type B; + | ^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:24:1 + | +LL | #[rustc_must_implement_one_of(A, B)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: All `#[rustc_must_implement_one_of]` arguments must be associated function names + +error: This function doesn't have a default implementation + --> $DIR/rustc_must_implement_one_of_misuse.rs:33:5 + | +LL | fn a(); + | ^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:31:1 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: This function doesn't have a default implementation + --> $DIR/rustc_must_implement_one_of_misuse.rs:35:5 + | +LL | fn b(); + | ^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:31:1 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 11 previous errors + diff --git a/src/test/ui/traits/default-method/self.rs b/src/test/ui/traits/default-method/self.rs new file mode 100644 index 000000000..cdf4d1e14 --- /dev/null +++ b/src/test/ui/traits/default-method/self.rs @@ -0,0 +1,18 @@ +// run-pass + + +trait Cat { + fn meow(&self) -> bool; + fn scratch(&self) -> bool { self.purr() } + fn purr(&self) -> bool { true } +} + +impl Cat for isize { + fn meow(&self) -> bool { + self.scratch() + } +} + +pub fn main() { + assert!(5.meow()); +} diff --git a/src/test/ui/traits/default-method/supervtable.rs b/src/test/ui/traits/default-method/supervtable.rs new file mode 100644 index 000000000..939ad5135 --- /dev/null +++ b/src/test/ui/traits/default-method/supervtable.rs @@ -0,0 +1,28 @@ +// run-pass + + +// Tests that we can call a function bounded over a supertrait from +// a default method + +fn require_y(x: T) -> isize { x.y() } + +trait Y { + fn y(self) -> isize; +} + + +trait Z: Y + Sized { + fn x(self) -> isize { + require_y(self) + } +} + +impl Y for isize { + fn y(self) -> isize { self } +} + +impl Z for isize {} + +pub fn main() { + assert_eq!(12.x(), 12); +} diff --git a/src/test/ui/traits/default-method/trivial.rs b/src/test/ui/traits/default-method/trivial.rs new file mode 100644 index 000000000..dc41938ec --- /dev/null +++ b/src/test/ui/traits/default-method/trivial.rs @@ -0,0 +1,21 @@ +// run-pass + + +trait Cat { + fn meow(&self) -> bool; + fn scratch(&self) -> bool; + fn purr(&self) -> bool { true } +} + +impl Cat for isize { + fn meow(&self) -> bool { + self.scratch() + } + fn scratch(&self) -> bool { + self.purr() + } +} + +pub fn main() { + assert!(5.meow()); +} diff --git a/src/test/ui/traits/default-method/xc-2.rs b/src/test/ui/traits/default-method/xc-2.rs new file mode 100644 index 000000000..1de61dcf8 --- /dev/null +++ b/src/test/ui/traits/default-method/xc-2.rs @@ -0,0 +1,26 @@ +// run-pass +// aux-build:xc.rs +// aux-build:xc_2.rs + + + +extern crate xc as aux; +extern crate xc_2 as aux2; +use aux::A; +use aux2::{a_struct, welp}; + + +pub fn main () { + + let a = a_struct { x: 0 }; + let b = a_struct { x: 1 }; + + assert_eq!(0.g(), 10); + assert_eq!(a.g(), 10); + assert_eq!(a.h(), 11); + assert_eq!(b.g(), 10); + assert_eq!(b.h(), 11); + assert_eq!(A::lurr(&a, &b), 21); + + welp(&0); +} diff --git a/src/test/ui/traits/default-method/xc.rs b/src/test/ui/traits/default-method/xc.rs new file mode 100644 index 000000000..76a1573d6 --- /dev/null +++ b/src/test/ui/traits/default-method/xc.rs @@ -0,0 +1,81 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_camel_case_types)] + +// aux-build:xc.rs + + +extern crate xc as aux; +use aux::{A, TestEquality, Something}; +use aux::B; + +fn f(i: T) { + assert_eq!(i.g(), 10); +} + +fn welp(i: isize, _x: &T) -> isize { + i.g() +} + +mod stuff { + pub struct thing { pub x: isize } +} + +impl A for stuff::thing { + fn f(&self) -> isize { 10 } +} + +fn g>(i: V, j: T, k: U) -> (T, U) { + i.thing(j, k) +} + +fn eq(lhs: &T, rhs: &T) -> bool { + lhs.test_eq(rhs) +} +fn neq(lhs: &T, rhs: &T) -> bool { + lhs.test_neq(rhs) +} + + +impl TestEquality for stuff::thing { + fn test_eq(&self, rhs: &stuff::thing) -> bool { + //self.x.test_eq(&rhs.x) + eq(&self.x, &rhs.x) + } +} + + +pub fn main() { + // Some tests of random things + f(0); + + assert_eq!(A::lurr(&0, &1), 21); + + let a = stuff::thing { x: 0 }; + let b = stuff::thing { x: 1 }; + let c = Something { x: 1 }; + + assert_eq!(0.g(), 10); + assert_eq!(a.g(), 10); + assert_eq!(a.h(), 11); + assert_eq!(c.h(), 11); + + assert_eq!(0.thing(3.14f64, 1), (3.14f64, 1)); + assert_eq!(B::staticthing(&0, 3.14f64, 1), (3.14f64, 1)); + assert_eq!(B::::staticthing::(&0, 3.14, 1), (3.14, 1)); + + assert_eq!(g(0, 3.14f64, 1), (3.14f64, 1)); + assert_eq!(g(false, 3.14f64, 1), (3.14, 1)); + + + // Trying out a real one + assert!(12.test_neq(&10)); + assert!(!10.test_neq(&10)); + assert!(a.test_neq(&b)); + assert!(!a.test_neq(&a)); + + assert!(neq(&12, &10)); + assert!(!neq(&10, &10)); + assert!(neq(&a, &b)); + assert!(!neq(&a, &a)); +} diff --git a/src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs b/src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs new file mode 100644 index 000000000..9a444be50 --- /dev/null +++ b/src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs @@ -0,0 +1,20 @@ +struct Thing(X); + +trait Method { + fn method(self, _: i32) -> T; +} + +impl Method for Thing { + fn method(self, _: i32) -> i32 { 0 } +} + +impl Method for Thing { + fn method(self, _: i32) -> u32 { 0 } +} + +fn main() { + let thing = Thing(true); + thing.method(42); + //~^ ERROR type annotations needed + //~| ERROR type annotations needed +} diff --git a/src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.stderr b/src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.stderr new file mode 100644 index 000000000..57b2587ae --- /dev/null +++ b/src/test/ui/traits/do-not-mention-type-params-by-name-in-suggestion-issue-96292.stderr @@ -0,0 +1,34 @@ +error[E0282]: type annotations needed + --> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:17:11 + | +LL | thing.method(42); + | ^^^^^^ + | +help: try using a fully qualified path to specify the expected types + | +LL | as Method>::method(thing, 42); + | +++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:17:11 + | +LL | thing.method(42); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/do-not-mention-type-params-by-name-in-suggestion-issue-96292.rs:7:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | as Method>::method(thing, 42); + | +++++++++++++++++++++++++++++++++++ ~ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0282, E0283. +For more information about an error, try `rustc --explain E0282`. diff --git a/src/test/ui/traits/duplicate-methods.rs b/src/test/ui/traits/duplicate-methods.rs new file mode 100644 index 000000000..1f9bdf56f --- /dev/null +++ b/src/test/ui/traits/duplicate-methods.rs @@ -0,0 +1,6 @@ +trait Foo { + fn orange(&self); + fn orange(&self); //~ ERROR the name `orange` is defined multiple times +} + +fn main() {} diff --git a/src/test/ui/traits/duplicate-methods.stderr b/src/test/ui/traits/duplicate-methods.stderr new file mode 100644 index 000000000..6aa88d0df --- /dev/null +++ b/src/test/ui/traits/duplicate-methods.stderr @@ -0,0 +1,13 @@ +error[E0428]: the name `orange` is defined multiple times + --> $DIR/duplicate-methods.rs:3:5 + | +LL | fn orange(&self); + | ----------------- previous definition of the value `orange` here +LL | fn orange(&self); + | ^^^^^^^^^^^^^^^^^ `orange` redefined here + | + = note: `orange` must be defined only once in the value namespace of this trait + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0428`. diff --git a/src/test/ui/traits/dyn-trait.rs b/src/test/ui/traits/dyn-trait.rs new file mode 100644 index 000000000..e1c1a8de5 --- /dev/null +++ b/src/test/ui/traits/dyn-trait.rs @@ -0,0 +1,17 @@ +// run-pass +// ignore-pretty `dyn ::foo` parses differently in the current edition + +use std::fmt::Display; + +static BYTE: u8 = 33; + +fn main() { + let x: &(dyn 'static + Display) = &BYTE; + let y: Box = Box::new(BYTE); + let _: &dyn (Display) = &BYTE; + let _: &dyn (::std::fmt::Display) = &BYTE; + let xstr = format!("{}", x); + let ystr = format!("{}", y); + assert_eq!(xstr, "33"); + assert_eq!(ystr, "33"); +} diff --git a/src/test/ui/traits/early-vtbl-resolution.rs b/src/test/ui/traits/early-vtbl-resolution.rs new file mode 100644 index 000000000..f4b69c140 --- /dev/null +++ b/src/test/ui/traits/early-vtbl-resolution.rs @@ -0,0 +1,19 @@ +// run-pass + +#![allow(non_camel_case_types)] +#![allow(dead_code)] +// pretty-expanded FIXME #23616 + +trait thing { + fn foo(&self) -> Option; +} +impl thing for isize { + fn foo(&self) -> Option { None } +} +fn foo_func>(x: B) -> Option { x.foo() } + +struct A { a: isize } + +pub fn main() { + let _x: Option = foo_func(0); +} diff --git a/src/test/ui/traits/elaborate-type-region.rs b/src/test/ui/traits/elaborate-type-region.rs new file mode 100644 index 000000000..03aef0184 --- /dev/null +++ b/src/test/ui/traits/elaborate-type-region.rs @@ -0,0 +1,49 @@ +// run-pass +#![allow(dead_code)] + +// Test that we elaborate `Type: 'region` constraints and infer various important things. + +trait Master<'a, T: ?Sized> { + fn foo() where T: 'a; +} + +// [U]: 'a => U: 'a +impl<'a, U> Master<'a, [U]> for () { + fn foo() where U: 'a { } +} + +// &'b U: 'a => 'b: 'a, U: 'a +impl<'a, 'b, U> Master<'a, &'b U> for () { + fn foo() where 'b: 'a, U: 'a { } +} + +// &'b [U]: 'a => 'b: 'a, U: 'a +impl<'a, 'b, U> Master<'a, &'b [U]> for () { + fn foo() where 'b: 'a, U: 'a { } +} + +// Foo<'b>: 'a => 'b: 'a +struct Foo<'a> { x: &'a () } +impl<'a, 'b> Master<'a, Foo<'b>> for () { + fn foo() where 'b: 'a { } +} + +// Bar<'b, T>: 'a => 'b: 'a, T: 'a +struct Bar<'a, T: 'a> { x: &'a T } +impl<'a, 'b, T> Master<'a, Bar<'b, T>> for () { + fn foo() where 'b: 'a, T: 'a { } +} + +// fn(T): 'a => T: 'a +impl<'a, T> Master<'a, fn(T)> for () { + fn foo() where T: 'a { } +} + +// fn() -> T: 'a => T: 'a +impl<'a, T> Master<'a, fn() -> T> for () { + fn foo() where T: 'a { } +} + +fn main() { + println!("Hello, world!"); +} diff --git a/src/test/ui/traits/false-ambiguity-where-clause-builtin-bound.rs b/src/test/ui/traits/false-ambiguity-where-clause-builtin-bound.rs new file mode 100644 index 000000000..3413db6a6 --- /dev/null +++ b/src/test/ui/traits/false-ambiguity-where-clause-builtin-bound.rs @@ -0,0 +1,16 @@ +// run-pass +// Test that we do not error out because of a (False) ambiguity +// between the builtin rules for Sized and the where clause. Issue +// #20959. + +// pretty-expanded FIXME #23616 + +fn foo(x: Option) + where Option : Sized +{ + let _y = x; +} + +fn main() { + foo(Some(22)); +} diff --git a/src/test/ui/traits/fmt-pointer-trait.rs b/src/test/ui/traits/fmt-pointer-trait.rs new file mode 100644 index 000000000..b7876b9bd --- /dev/null +++ b/src/test/ui/traits/fmt-pointer-trait.rs @@ -0,0 +1,24 @@ +// run-pass +use std::ptr; +use std::rc::Rc; +use std::sync::Arc; + +fn main() { + let p: *const u8 = ptr::null(); + let rc = Rc::new(1usize); + let arc = Arc::new(1usize); + let b = Box::new("hi"); + + let _ = format!("{:p}{:p}{:p}", + rc, arc, b); + + if cfg!(target_pointer_width = "32") { + assert_eq!(format!("{:#p}", p), + "0x00000000"); + } else { + assert_eq!(format!("{:#p}", p), + "0x0000000000000000"); + } + assert_eq!(format!("{:p}", p), + "0x0"); +} diff --git a/src/test/ui/traits/generic.rs b/src/test/ui/traits/generic.rs new file mode 100644 index 000000000..80efe1c93 --- /dev/null +++ b/src/test/ui/traits/generic.rs @@ -0,0 +1,45 @@ +// run-pass +#![allow(non_camel_case_types)] + + + +trait to_str { + fn to_string_(&self) -> String; +} +impl to_str for isize { + fn to_string_(&self) -> String { self.to_string() } +} +impl to_str for String { + fn to_string_(&self) -> String { self.clone() } +} +impl to_str for () { + fn to_string_(&self) -> String { "()".to_string() } +} + +trait map { + fn map(&self, f: F) -> Vec where F: FnMut(&T) -> U; +} +impl map for Vec { + fn map(&self, mut f: F) -> Vec where F: FnMut(&T) -> U { + let mut r = Vec::new(); + for i in self { + r.push(f(i)); + } + r + } +} + +fn foo>(x: T) -> Vec { + x.map(|_e| "hi".to_string() ) +} +fn bar>(x: T) -> Vec { + x.map(|_e| _e.to_string_() ) +} + +pub fn main() { + assert_eq!(foo(vec![1]), ["hi".to_string()]); + assert_eq!(bar:: >(vec![4, 5]), ["4".to_string(), "5".to_string()]); + assert_eq!(bar:: >(vec!["x".to_string(), "y".to_string()]), + ["x".to_string(), "y".to_string()]); + assert_eq!(bar::<(), Vec<()>>(vec![()]), ["()".to_string()]); +} diff --git a/src/test/ui/traits/impl-1.rs b/src/test/ui/traits/impl-1.rs new file mode 100644 index 000000000..d22ac72d1 --- /dev/null +++ b/src/test/ui/traits/impl-1.rs @@ -0,0 +1,16 @@ +// Test calling methods on an impl for a bare trait. This test checks that the +// trait impl is only applied to a trait object, not concrete types which implement +// the trait. + +trait T {} + +impl<'a> dyn T + 'a { + fn foo(&self) {} +} + +impl T for i32 {} + +fn main() { + let x = &42i32; + x.foo(); //~ERROR: no method named `foo` found +} diff --git a/src/test/ui/traits/impl-1.stderr b/src/test/ui/traits/impl-1.stderr new file mode 100644 index 000000000..7694e3f5c --- /dev/null +++ b/src/test/ui/traits/impl-1.stderr @@ -0,0 +1,9 @@ +error[E0599]: no method named `foo` found for reference `&i32` in the current scope + --> $DIR/impl-1.rs:15:7 + | +LL | x.foo(); + | ^^^ method not found in `&i32` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/traits/impl-2.rs b/src/test/ui/traits/impl-2.rs new file mode 100644 index 000000000..804ffec12 --- /dev/null +++ b/src/test/ui/traits/impl-2.rs @@ -0,0 +1,19 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_snake_case)] + +// pretty-expanded FIXME #23616 + +pub mod Foo { + pub trait Trait { + fn foo(&self); + } +} + +mod Bar { + impl<'a> dyn (::Foo::Trait) + 'a { + fn bar(&self) { self.foo() } + } +} + +fn main() {} diff --git a/src/test/ui/traits/impl-bounds-checking.rs b/src/test/ui/traits/impl-bounds-checking.rs new file mode 100644 index 000000000..5e65a27bf --- /dev/null +++ b/src/test/ui/traits/impl-bounds-checking.rs @@ -0,0 +1,14 @@ +pub trait Clone2 { + fn clone(&self) -> Self; +} + + +trait Getter { + fn get(&self) -> T; +} + +impl Getter for isize { //~ ERROR `isize: Clone2` is not satisfied + fn get(&self) -> isize { *self } +} + +fn main() { } diff --git a/src/test/ui/traits/impl-bounds-checking.stderr b/src/test/ui/traits/impl-bounds-checking.stderr new file mode 100644 index 000000000..b01bacdb8 --- /dev/null +++ b/src/test/ui/traits/impl-bounds-checking.stderr @@ -0,0 +1,15 @@ +error[E0277]: the trait bound `isize: Clone2` is not satisfied + --> $DIR/impl-bounds-checking.rs:10:6 + | +LL | impl Getter for isize { + | ^^^^^^^^^^^^^ the trait `Clone2` is not implemented for `isize` + | +note: required by a bound in `Getter` + --> $DIR/impl-bounds-checking.rs:6:17 + | +LL | trait Getter { + | ^^^^^^ required by this bound in `Getter` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/impl-can-not-have-untraitful-items.rs b/src/test/ui/traits/impl-can-not-have-untraitful-items.rs new file mode 100644 index 000000000..0c885447b --- /dev/null +++ b/src/test/ui/traits/impl-can-not-have-untraitful-items.rs @@ -0,0 +1,9 @@ +trait A { } + +impl A for isize { + const BAR: () = (); //~ ERROR const `BAR` is not a member of trait `A` + type Baz = (); //~ ERROR type `Baz` is not a member of trait `A` + fn foo(&self) { } //~ ERROR method `foo` is not a member of trait `A` +} + +fn main() { } diff --git a/src/test/ui/traits/impl-can-not-have-untraitful-items.stderr b/src/test/ui/traits/impl-can-not-have-untraitful-items.stderr new file mode 100644 index 000000000..7f56f34ea --- /dev/null +++ b/src/test/ui/traits/impl-can-not-have-untraitful-items.stderr @@ -0,0 +1,22 @@ +error[E0438]: const `BAR` is not a member of trait `A` + --> $DIR/impl-can-not-have-untraitful-items.rs:4:5 + | +LL | const BAR: () = (); + | ^^^^^^^^^^^^^^^^^^^ not a member of trait `A` + +error[E0437]: type `Baz` is not a member of trait `A` + --> $DIR/impl-can-not-have-untraitful-items.rs:5:5 + | +LL | type Baz = (); + | ^^^^^^^^^^^^^^ not a member of trait `A` + +error[E0407]: method `foo` is not a member of trait `A` + --> $DIR/impl-can-not-have-untraitful-items.rs:6:5 + | +LL | fn foo(&self) { } + | ^^^^^^^^^^^^^^^^^ not a member of trait `A` + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0407, E0437, E0438. +For more information about an error, try `rustc --explain E0407`. diff --git a/src/test/ui/traits/impl-different-num-params.rs b/src/test/ui/traits/impl-different-num-params.rs new file mode 100644 index 000000000..53400622d --- /dev/null +++ b/src/test/ui/traits/impl-different-num-params.rs @@ -0,0 +1,12 @@ +trait Foo { + fn bar(&self, x: usize) -> Self; +} +impl Foo for isize { + fn bar(&self) -> isize { + //~^ ERROR method `bar` has 1 parameter but the declaration in trait `Foo::bar` has 2 + *self + } +} + +fn main() { +} diff --git a/src/test/ui/traits/impl-different-num-params.stderr b/src/test/ui/traits/impl-different-num-params.stderr new file mode 100644 index 000000000..910ba3510 --- /dev/null +++ b/src/test/ui/traits/impl-different-num-params.stderr @@ -0,0 +1,12 @@ +error[E0050]: method `bar` has 1 parameter but the declaration in trait `Foo::bar` has 2 + --> $DIR/impl-different-num-params.rs:5:12 + | +LL | fn bar(&self, x: usize) -> Self; + | --------------- trait requires 2 parameters +... +LL | fn bar(&self) -> isize { + | ^^^^^ expected 2 parameters, found 1 + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0050`. diff --git a/src/test/ui/traits/impl-evaluation-order.rs b/src/test/ui/traits/impl-evaluation-order.rs new file mode 100644 index 000000000..57809d89a --- /dev/null +++ b/src/test/ui/traits/impl-evaluation-order.rs @@ -0,0 +1,39 @@ +// Regression test for #79902 + +// Check that evaluation (which is used to determine whether to copy a type in +// MIR building) evaluates bounds from normalizing an impl after evaluating +// any bounds on the impl. + +// check-pass + +trait A { + type B; +} +trait M {} + +struct G(*const T, *const U); + +impl Clone for G { + fn clone(&self) -> Self { + G { ..*self } + } +} + +impl Copy for G +where + T: A, + U: A, +{ +} + +impl A for () { + type B = (); +} + +fn is_m(_: T) {} + +fn main() { + let x = G(&(), &()); + drop(x); + drop(x); +} diff --git a/src/test/ui/traits/impl-for-module.rs b/src/test/ui/traits/impl-for-module.rs new file mode 100644 index 000000000..9004627c5 --- /dev/null +++ b/src/test/ui/traits/impl-for-module.rs @@ -0,0 +1,11 @@ +mod a { +} + +trait A { +} + +impl A for a { //~ ERROR expected type, found module +} + +fn main() { +} diff --git a/src/test/ui/traits/impl-for-module.stderr b/src/test/ui/traits/impl-for-module.stderr new file mode 100644 index 000000000..6ec4083b5 --- /dev/null +++ b/src/test/ui/traits/impl-for-module.stderr @@ -0,0 +1,12 @@ +error[E0573]: expected type, found module `a` + --> $DIR/impl-for-module.rs:7:12 + | +LL | trait A { + | ------- similarly named trait `A` defined here +... +LL | impl A for a { + | ^ help: a trait with a similar name exists: `A` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0573`. diff --git a/src/test/ui/traits/impl-implicit-trait.rs b/src/test/ui/traits/impl-implicit-trait.rs new file mode 100644 index 000000000..fac2bcce2 --- /dev/null +++ b/src/test/ui/traits/impl-implicit-trait.rs @@ -0,0 +1,26 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_camel_case_types)] + +// pretty-expanded FIXME #23616 + +enum option_ { + none_, + some_(T), +} + +impl option_ { + pub fn foo(&self) -> bool { true } +} + +enum option__ { + none__, + some__(isize) +} + +impl option__ { + pub fn foo(&self) -> bool { true } +} + +pub fn main() { +} diff --git a/src/test/ui/traits/impl-inherent-prefer-over-trait.rs b/src/test/ui/traits/impl-inherent-prefer-over-trait.rs new file mode 100644 index 000000000..827607888 --- /dev/null +++ b/src/test/ui/traits/impl-inherent-prefer-over-trait.rs @@ -0,0 +1,30 @@ +// run-pass + +struct Foo; + +trait Trait { + fn bar(&self); +} + +// Inherent impls should be preferred over trait ones. +impl Foo { + fn bar(&self) {} +} + +impl dyn Trait { + fn baz(_: &Foo) {} +} + +impl Trait for Foo { + fn bar(&self) { panic!("wrong method called!") } +} + +fn main() { + Foo.bar(); + Foo::bar(&Foo); + ::bar(&Foo); + + // Should work even if Trait::baz doesn't exist. + // N.B: `::bar` would be ambiguous. + ::baz(&Foo); +} diff --git a/src/test/ui/traits/impl-method-mismatch.rs b/src/test/ui/traits/impl-method-mismatch.rs new file mode 100644 index 000000000..683b1c1aa --- /dev/null +++ b/src/test/ui/traits/impl-method-mismatch.rs @@ -0,0 +1,13 @@ +trait Mumbo { + fn jumbo(&self, x: &usize) -> usize; +} + +impl Mumbo for usize { + // Cannot have a larger effect than the trait: + unsafe fn jumbo(&self, x: &usize) { *self + *x; } + //~^ ERROR method `jumbo` has an incompatible type for trait + //~| expected fn pointer `fn + //~| found fn pointer `unsafe fn +} + +fn main() {} diff --git a/src/test/ui/traits/impl-method-mismatch.stderr b/src/test/ui/traits/impl-method-mismatch.stderr new file mode 100644 index 000000000..30aa97d29 --- /dev/null +++ b/src/test/ui/traits/impl-method-mismatch.stderr @@ -0,0 +1,17 @@ +error[E0053]: method `jumbo` has an incompatible type for trait + --> $DIR/impl-method-mismatch.rs:7:5 + | +LL | unsafe fn jumbo(&self, x: &usize) { *self + *x; } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected normal fn, found unsafe fn + | +note: type in trait + --> $DIR/impl-method-mismatch.rs:2:5 + | +LL | fn jumbo(&self, x: &usize) -> usize; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: expected fn pointer `fn(&usize, &usize) -> usize` + found fn pointer `unsafe fn(&usize, &usize)` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0053`. diff --git a/src/test/ui/traits/impl-object-overlap-issue-23853.rs b/src/test/ui/traits/impl-object-overlap-issue-23853.rs new file mode 100644 index 000000000..e490967b6 --- /dev/null +++ b/src/test/ui/traits/impl-object-overlap-issue-23853.rs @@ -0,0 +1,18 @@ +// run-pass +// Test that we are able to compile the case where both a blanket impl +// and the object type itself supply the required trait obligation. +// In this case, the blanket impl for `Foo` applies to any type, +// including `Bar`, but the object type `Bar` also implicitly supplies +// this context. + +trait Foo { fn dummy(&self) { } } + +trait Bar: Foo { } + +impl Foo for T { } + +fn want_foo() { } + +fn main() { + want_foo::(); +} diff --git a/src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.rs b/src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.rs new file mode 100644 index 000000000..05b9db972 --- /dev/null +++ b/src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.rs @@ -0,0 +1,31 @@ +// Check that when we test the supertrait we ensure consistent use of +// lifetime parameters. In this case, implementing T2<'a,'b> requires +// an impl of T1<'a>, but we have an impl of T1<'b>. + +trait T1<'x> { + fn x(&self) -> &'x isize; +} + +trait T2<'x, 'y> : T1<'x> { + fn y(&self) -> &'y isize; +} + +struct S<'a, 'b> { + a: &'a isize, + b: &'b isize +} + +impl<'a,'b> T1<'b> for S<'a, 'b> { + fn x(&self) -> &'b isize { + self.b + } +} + +impl<'a,'b> T2<'a, 'b> for S<'a, 'b> { //~ ERROR cannot infer an appropriate lifetime + fn y(&self) -> &'b isize { + self.b + } +} + +pub fn main() { +} diff --git a/src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.stderr b/src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.stderr new file mode 100644 index 000000000..5572c6515 --- /dev/null +++ b/src/test/ui/traits/impl-of-supertrait-has-wrong-lifetime-parameters.stderr @@ -0,0 +1,27 @@ +error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements + --> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:13 + | +LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> { + | ^^^^^^^^^^ + | +note: first, the lifetime cannot outlive the lifetime `'a` as defined here... + --> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:6 + | +LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> { + | ^^ +note: ...but the lifetime must also be valid for the lifetime `'b` as defined here... + --> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:9 + | +LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> { + | ^^ +note: ...so that the types are compatible + --> $DIR/impl-of-supertrait-has-wrong-lifetime-parameters.rs:24:13 + | +LL | impl<'a,'b> T2<'a, 'b> for S<'a, 'b> { + | ^^^^^^^^^^ + = note: expected `T1<'a>` + found `T1<'_>` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0495`. diff --git a/src/test/ui/traits/impl.rs b/src/test/ui/traits/impl.rs new file mode 100644 index 000000000..f512d91eb --- /dev/null +++ b/src/test/ui/traits/impl.rs @@ -0,0 +1,41 @@ +// run-pass +// Test calling methods on an impl for a bare trait. + +// aux-build:traitimpl.rs + +extern crate traitimpl; +use traitimpl::Bar; + +static mut COUNT: usize = 1; + +trait T { + fn t(&self) {} +} + +impl<'a> dyn T+'a { + fn foo(&self) { + unsafe { COUNT *= 2; } + } + fn bar() { + unsafe { COUNT *= 3; } + } +} + +impl T for isize {} + +struct Foo; +impl<'a> Bar<'a> for Foo {} + +fn main() { + let x: &dyn T = &42; + + x.foo(); + ::foo(x); + ::bar(); + + unsafe { assert_eq!(COUNT, 12); } + + // Cross-crait case + let x: &dyn Bar = &Foo; + x.bar(); +} diff --git a/src/test/ui/traits/impl_trait_as_trait_return_position.rs b/src/test/ui/traits/impl_trait_as_trait_return_position.rs new file mode 100644 index 000000000..c3325fd80 --- /dev/null +++ b/src/test/ui/traits/impl_trait_as_trait_return_position.rs @@ -0,0 +1,17 @@ +// check-pass + +trait A { + type Foo; +} + +impl A for T { + type Foo = (); +} + +fn foo() -> impl std::borrow::Borrow<::Foo> { + () +} + +fn main() { + foo(); +} diff --git a/src/test/ui/traits/inductive-overflow/lifetime.rs b/src/test/ui/traits/inductive-overflow/lifetime.rs new file mode 100644 index 000000000..c36c17d3d --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/lifetime.rs @@ -0,0 +1,32 @@ +// Test that we don't hit the recursion limit for short cycles involving lifetimes. + +// Shouldn't hit this, we should realize that we're in a cycle sooner. +#![recursion_limit="20"] + +trait NotAuto {} +trait Y { + type P; +} + +impl<'a> Y for C<'a> { + type P = Box>>; +} + +struct C<'a>(&'a ()); +struct X(T::P); + +impl NotAuto for Box {} //~ NOTE: required +impl NotAuto for X where T::P: NotAuto {} +impl<'a> NotAuto for C<'a> {} + +fn is_send() {} +//~^ NOTE: required +//~| NOTE: required + +fn main() { + // Should only be a few notes. + is_send::>>(); + //~^ ERROR overflow evaluating + //~| 3 redundant requirements hidden + //~| required because of +} diff --git a/src/test/ui/traits/inductive-overflow/lifetime.stderr b/src/test/ui/traits/inductive-overflow/lifetime.stderr new file mode 100644 index 000000000..9ca615aac --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/lifetime.stderr @@ -0,0 +1,22 @@ +error[E0275]: overflow evaluating the requirement `X>: NotAuto` + --> $DIR/lifetime.rs:28:5 + | +LL | is_send::>>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: required because of the requirements on the impl of `NotAuto` for `Box>>` + --> $DIR/lifetime.rs:18:18 + | +LL | impl NotAuto for Box {} + | ^^^^^^^ ^^^^^^ + = note: 3 redundant requirements hidden + = note: required because of the requirements on the impl of `NotAuto` for `X>` +note: required by a bound in `is_send` + --> $DIR/lifetime.rs:22:15 + | +LL | fn is_send() {} + | ^^^^^^^ required by this bound in `is_send` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/inductive-overflow/simultaneous.rs b/src/test/ui/traits/inductive-overflow/simultaneous.rs new file mode 100644 index 000000000..40ac92146 --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/simultaneous.rs @@ -0,0 +1,20 @@ +// Regression test for #33344, initial version. This example allowed +// arbitrary trait bounds to be synthesized. + +trait Tweedledum: IntoIterator {} +trait Tweedledee: IntoIterator {} + +impl Tweedledee for T {} +impl Tweedledum for T {} + +trait Combo: IntoIterator {} +impl Combo for T {} + +fn is_ee(t: T) { + t.into_iter(); +} + +fn main() { + is_ee(4); + //~^ ERROR overflow evaluating the requirement `{integer}: Tweedle +} diff --git a/src/test/ui/traits/inductive-overflow/simultaneous.stderr b/src/test/ui/traits/inductive-overflow/simultaneous.stderr new file mode 100644 index 000000000..230c2638c --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/simultaneous.stderr @@ -0,0 +1,20 @@ +error[E0275]: overflow evaluating the requirement `{integer}: Tweedledum` + --> $DIR/simultaneous.rs:18:5 + | +LL | is_ee(4); + | ^^^^^ + | +note: required because of the requirements on the impl of `Combo` for `{integer}` + --> $DIR/simultaneous.rs:11:34 + | +LL | impl Combo for T {} + | ^^^^^ ^ +note: required by a bound in `is_ee` + --> $DIR/simultaneous.rs:13:13 + | +LL | fn is_ee(t: T) { + | ^^^^^ required by this bound in `is_ee` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/inductive-overflow/supertrait-auto-trait.rs b/src/test/ui/traits/inductive-overflow/supertrait-auto-trait.rs new file mode 100644 index 000000000..5fea47a1b --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/supertrait-auto-trait.rs @@ -0,0 +1,18 @@ +// Auto-trait-based version of #29859, supertrait version. Test that using +// a simple auto trait `..` impl alone still doesn't allow arbitrary bounds +// to be synthesized. + +#![feature(auto_traits)] +#![feature(negative_impls)] + +auto trait Magic: Copy {} //~ ERROR E0568 + +fn copy(x: T) -> (T, T) { (x, x) } + +#[derive(Debug)] +struct NoClone; + +fn main() { + let (a, b) = copy(NoClone); //~ ERROR + println!("{:?} {:?}", a, b); +} diff --git a/src/test/ui/traits/inductive-overflow/supertrait-auto-trait.stderr b/src/test/ui/traits/inductive-overflow/supertrait-auto-trait.stderr new file mode 100644 index 000000000..d7697dcc6 --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/supertrait-auto-trait.stderr @@ -0,0 +1,35 @@ +error[E0568]: auto traits cannot have super traits or lifetime bounds + --> $DIR/supertrait-auto-trait.rs:8:17 + | +LL | auto trait Magic: Copy {} + | -----^^^^^^ help: remove the super traits or lifetime bounds + | | + | auto trait cannot have super traits or lifetime bounds + +error[E0277]: the trait bound `NoClone: Copy` is not satisfied + --> $DIR/supertrait-auto-trait.rs:16:23 + | +LL | let (a, b) = copy(NoClone); + | ---- ^^^^^^^ the trait `Copy` is not implemented for `NoClone` + | | + | required by a bound introduced by this call + | +note: required because of the requirements on the impl of `Magic` for `NoClone` + --> $DIR/supertrait-auto-trait.rs:8:12 + | +LL | auto trait Magic: Copy {} + | ^^^^^ +note: required by a bound in `copy` + --> $DIR/supertrait-auto-trait.rs:10:12 + | +LL | fn copy(x: T) -> (T, T) { (x, x) } + | ^^^^^ required by this bound in `copy` +help: consider annotating `NoClone` with `#[derive(Copy)]` + | +LL | #[derive(Copy)] + | + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0277, E0568. +For more information about an error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/inductive-overflow/supertrait.rs b/src/test/ui/traits/inductive-overflow/supertrait.rs new file mode 100644 index 000000000..c7aa4d90f --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/supertrait.rs @@ -0,0 +1,15 @@ +// Regression test for #29859, supertrait version. This example +// allowed arbitrary trait bounds to be synthesized. + +trait Magic: Copy {} +impl Magic for T {} + +fn copy(x: T) -> (T, T) { (x, x) } + +#[derive(Debug)] +struct NoClone; + +fn main() { + let (a, b) = copy(NoClone); //~ ERROR E0275 + println!("{:?} {:?}", a, b); +} diff --git a/src/test/ui/traits/inductive-overflow/supertrait.stderr b/src/test/ui/traits/inductive-overflow/supertrait.stderr new file mode 100644 index 000000000..95325a534 --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/supertrait.stderr @@ -0,0 +1,20 @@ +error[E0275]: overflow evaluating the requirement `NoClone: Magic` + --> $DIR/supertrait.rs:13:18 + | +LL | let (a, b) = copy(NoClone); + | ^^^^ + | +note: required because of the requirements on the impl of `Magic` for `NoClone` + --> $DIR/supertrait.rs:5:16 + | +LL | impl Magic for T {} + | ^^^^^ ^ +note: required by a bound in `copy` + --> $DIR/supertrait.rs:7:12 + | +LL | fn copy(x: T) -> (T, T) { (x, x) } + | ^^^^^ required by this bound in `copy` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/inductive-overflow/two-traits.rs b/src/test/ui/traits/inductive-overflow/two-traits.rs new file mode 100644 index 000000000..463b55d85 --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/two-traits.rs @@ -0,0 +1,22 @@ +// Regression test for #29859, initial version. This example allowed +// arbitrary trait bounds to be synthesized. + +// Trait that you want all types to implement. +use std::marker::{Sync as Trait}; + +pub trait Magic { + type X: Trait; +} +impl Magic for T { + type X = Self; + //~^ ERROR E0277 +} + +fn check() {} + +fn wizard() { check::<::X>(); } + +fn main() { + wizard::<*mut ()>(); //~ ERROR E0275 + // check::<*mut ()>(); +} diff --git a/src/test/ui/traits/inductive-overflow/two-traits.stderr b/src/test/ui/traits/inductive-overflow/two-traits.stderr new file mode 100644 index 000000000..0d0bf8861 --- /dev/null +++ b/src/test/ui/traits/inductive-overflow/two-traits.stderr @@ -0,0 +1,32 @@ +error[E0277]: `T` cannot be shared between threads safely + --> $DIR/two-traits.rs:11:14 + | +LL | type X = Self; + | ^^^^ `T` cannot be shared between threads safely + | +note: required by a bound in `Magic::X` + --> $DIR/two-traits.rs:8:13 + | +LL | type X: Trait; + | ^^^^^ required by this bound in `Magic::X` +help: consider further restricting this bound + | +LL | impl Magic for T { + | +++++++++++++++++++ + +error[E0275]: overflow evaluating the requirement `*mut (): Magic` + --> $DIR/two-traits.rs:20:5 + | +LL | wizard::<*mut ()>(); + | ^^^^^^^^^^^^^^^^^ + | +note: required by a bound in `wizard` + --> $DIR/two-traits.rs:17:14 + | +LL | fn wizard() { check::<::X>(); } + | ^^^^^ required by this bound in `wizard` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0275, E0277. +For more information about an error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/infer-from-object-issue-26952.rs b/src/test/ui/traits/infer-from-object-issue-26952.rs new file mode 100644 index 000000000..ed258dbb2 --- /dev/null +++ b/src/test/ui/traits/infer-from-object-issue-26952.rs @@ -0,0 +1,26 @@ +// run-pass +#![allow(dead_code)] +#![allow(unused_variables)] +// Test that when we match a trait reference like `Foo: Foo<_#0t>`, +// we unify with `_#0t` with `A`. In this code, if we failed to do +// that, then you get an unconstrained type-variable in `call`. +// +// Also serves as a regression test for issue #26952, though the test +// was derived from another reported regression with the same cause. + +use std::marker::PhantomData; + +trait Trait { fn foo(&self); } + +struct Type { a: PhantomData } + +fn as_trait(t: &Type) -> &dyn Trait { loop { } } + +fn want+?Sized>(t: &T) { } + +fn call(p: Type) { + let q = as_trait(&p); + want(q); // parameter A to `want` *would* be unconstrained +} + +fn main() { } diff --git a/src/test/ui/traits/inherent-method-order.rs b/src/test/ui/traits/inherent-method-order.rs new file mode 100644 index 000000000..f632ae8a9 --- /dev/null +++ b/src/test/ui/traits/inherent-method-order.rs @@ -0,0 +1,25 @@ +// run-pass + +struct Foo; + +impl Foo { + #[allow(dead_code)] + fn foo(self) { + panic!("wrong method!") + } +} + +trait Trait { + fn foo(self); +} + +impl<'a,'b,'c> Trait for &'a &'b &'c Foo { + fn foo(self) { + // ok + } +} + +fn main() { + let x = &(&(&Foo)); + x.foo(); +} diff --git a/src/test/ui/traits/inheritance/auto-xc-2.rs b/src/test/ui/traits/inheritance/auto-xc-2.rs new file mode 100644 index 000000000..f2130228d --- /dev/null +++ b/src/test/ui/traits/inheritance/auto-xc-2.rs @@ -0,0 +1,23 @@ +// run-pass +// aux-build:auto_xc_2.rs + + +extern crate auto_xc_2 as aux; + +// aux defines impls of Foo, Bar and Baz for A +use aux::{Foo, Bar, Baz, A}; + +// We want to extend all Foo, Bar, Bazes to Quuxes +pub trait Quux: Foo + Bar + Baz { } +impl Quux for T { } + +fn f(a: &T) { + assert_eq!(a.f(), 10); + assert_eq!(a.g(), 20); + assert_eq!(a.h(), 30); +} + +pub fn main() { + let a = &A { x: 3 }; + f(a); +} diff --git a/src/test/ui/traits/inheritance/auto-xc.rs b/src/test/ui/traits/inheritance/auto-xc.rs new file mode 100644 index 000000000..3d5ae182a --- /dev/null +++ b/src/test/ui/traits/inheritance/auto-xc.rs @@ -0,0 +1,25 @@ +// run-pass +#![allow(dead_code)] +// aux-build:auto_xc.rs + + +extern crate auto_xc as aux; + +use aux::{Foo, Bar, Baz, Quux}; + +struct A { x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } +impl Bar for A { fn g(&self) -> isize { 20 } } +impl Baz for A { fn h(&self) -> isize { 30 } } + +fn f(a: &T) { + assert_eq!(a.f(), 10); + assert_eq!(a.g(), 20); + assert_eq!(a.h(), 30); +} + +pub fn main() { + let a = &A { x: 3 }; + f(a); +} diff --git a/src/test/ui/traits/inheritance/auto.rs b/src/test/ui/traits/inheritance/auto.rs new file mode 100644 index 000000000..0be67a55e --- /dev/null +++ b/src/test/ui/traits/inheritance/auto.rs @@ -0,0 +1,29 @@ +// run-pass +#![allow(dead_code)] +// Testing that this impl turns A into a Quux, because +// A is already a Foo Bar Baz + +impl Quux for T { } + +trait Foo { fn f(&self) -> isize; } +trait Bar { fn g(&self) -> isize; } +trait Baz { fn h(&self) -> isize; } + +trait Quux: Foo + Bar + Baz { } + +struct A { x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } +impl Bar for A { fn g(&self) -> isize { 20 } } +impl Baz for A { fn h(&self) -> isize { 30 } } + +fn f(a: &T) { + assert_eq!(a.f(), 10); + assert_eq!(a.g(), 20); + assert_eq!(a.h(), 30); +} + +pub fn main() { + let a = &A { x: 3 }; + f(a); +} diff --git a/src/test/ui/traits/inheritance/auxiliary/auto_xc.rs b/src/test/ui/traits/inheritance/auxiliary/auto_xc.rs new file mode 100644 index 000000000..9af26cb2e --- /dev/null +++ b/src/test/ui/traits/inheritance/auxiliary/auto_xc.rs @@ -0,0 +1,7 @@ +pub trait Foo { fn f(&self) -> isize; } +pub trait Bar { fn g(&self) -> isize; } +pub trait Baz { fn h(&self) -> isize; } + +pub trait Quux: Foo + Bar + Baz { } + +impl Quux for T { } diff --git a/src/test/ui/traits/inheritance/auxiliary/auto_xc_2.rs b/src/test/ui/traits/inheritance/auxiliary/auto_xc_2.rs new file mode 100644 index 000000000..e9327676d --- /dev/null +++ b/src/test/ui/traits/inheritance/auxiliary/auto_xc_2.rs @@ -0,0 +1,9 @@ +pub trait Foo { fn f(&self) -> isize; } +pub trait Bar { fn g(&self) -> isize; } +pub trait Baz { fn h(&self) -> isize; } + +pub struct A { pub x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } +impl Bar for A { fn g(&self) -> isize { 20 } } +impl Baz for A { fn h(&self) -> isize { 30 } } diff --git a/src/test/ui/traits/inheritance/auxiliary/overloading_xc.rs b/src/test/ui/traits/inheritance/auxiliary/overloading_xc.rs new file mode 100644 index 000000000..a25704412 --- /dev/null +++ b/src/test/ui/traits/inheritance/auxiliary/overloading_xc.rs @@ -0,0 +1,38 @@ +use std::cmp::PartialEq; +use std::ops::{Add, Sub, Mul}; + +pub trait MyNum : Add + Sub + Mul + PartialEq + Clone { +} + +#[derive(Clone, Debug)] +pub struct MyInt { + pub val: isize +} + +impl Add for MyInt { + type Output = MyInt; + + fn add(self, other: MyInt) -> MyInt { mi(self.val + other.val) } +} + +impl Sub for MyInt { + type Output = MyInt; + + fn sub(self, other: MyInt) -> MyInt { mi(self.val - other.val) } +} + +impl Mul for MyInt { + type Output = MyInt; + + fn mul(self, other: MyInt) -> MyInt { mi(self.val * other.val) } +} + +impl PartialEq for MyInt { + fn eq(&self, other: &MyInt) -> bool { self.val == other.val } + + fn ne(&self, other: &MyInt) -> bool { !self.eq(other) } +} + +impl MyNum for MyInt {} + +fn mi(v: isize) -> MyInt { MyInt { val: v } } diff --git a/src/test/ui/traits/inheritance/auxiliary/xc_call.rs b/src/test/ui/traits/inheritance/auxiliary/xc_call.rs new file mode 100644 index 000000000..b76c52e62 --- /dev/null +++ b/src/test/ui/traits/inheritance/auxiliary/xc_call.rs @@ -0,0 +1,11 @@ +pub trait Foo { + fn f(&self) -> isize; +} + +pub struct A { + pub x: isize +} + +impl Foo for A { + fn f(&self) -> isize { 10 } +} diff --git a/src/test/ui/traits/inheritance/basic.rs b/src/test/ui/traits/inheritance/basic.rs new file mode 100644 index 000000000..5bfa60b1a --- /dev/null +++ b/src/test/ui/traits/inheritance/basic.rs @@ -0,0 +1,26 @@ +// run-pass +#![allow(dead_code)] + +trait Foo { fn f(&self) -> isize; } +trait Bar { fn g(&self) -> isize; } +trait Baz { fn h(&self) -> isize; } + +trait Quux: Foo + Bar + Baz { } + +struct A { x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } +impl Bar for A { fn g(&self) -> isize { 20 } } +impl Baz for A { fn h(&self) -> isize { 30 } } +impl Quux for A {} + +fn f(a: &T) { + assert_eq!(a.f(), 10); + assert_eq!(a.g(), 20); + assert_eq!(a.h(), 30); +} + +pub fn main() { + let a = &A { x: 3 }; + f(a); +} diff --git a/src/test/ui/traits/inheritance/call-bound-inherited.rs b/src/test/ui/traits/inheritance/call-bound-inherited.rs new file mode 100644 index 000000000..37c2ff63c --- /dev/null +++ b/src/test/ui/traits/inheritance/call-bound-inherited.rs @@ -0,0 +1,20 @@ +// run-pass +#![allow(dead_code)] + +trait Foo { fn f(&self) -> isize; } +trait Bar : Foo { fn g(&self) -> isize; } + +struct A { x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } +impl Bar for A { fn g(&self) -> isize { 20 } } + +// Call a function on Foo, given a T: Bar +fn gg(a: &T) -> isize { + a.f() +} + +pub fn main() { + let a = &A { x: 3 }; + assert_eq!(gg(a), 10); +} diff --git a/src/test/ui/traits/inheritance/call-bound-inherited2.rs b/src/test/ui/traits/inheritance/call-bound-inherited2.rs new file mode 100644 index 000000000..8576d29f2 --- /dev/null +++ b/src/test/ui/traits/inheritance/call-bound-inherited2.rs @@ -0,0 +1,23 @@ +// run-pass +#![allow(dead_code)] + +trait Foo { fn f(&self) -> isize; } +trait Bar : Foo { fn g(&self) -> isize; } +trait Baz : Bar { fn h(&self) -> isize; } + +struct A { x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } +impl Bar for A { fn g(&self) -> isize { 20 } } +impl Baz for A { fn h(&self) -> isize { 30 } } + +// Call a function on Foo, given a T: Baz, +// which is inherited via Bar +fn gg(a: &T) -> isize { + a.f() +} + +pub fn main() { + let a = &A { x: 3 }; + assert_eq!(gg(a), 10); +} diff --git a/src/test/ui/traits/inheritance/cast-without-call-to-supertrait.rs b/src/test/ui/traits/inheritance/cast-without-call-to-supertrait.rs new file mode 100644 index 000000000..25159c1ad --- /dev/null +++ b/src/test/ui/traits/inheritance/cast-without-call-to-supertrait.rs @@ -0,0 +1,33 @@ +// run-pass +#![allow(dead_code)] +// Testing that we can cast to a subtrait and call subtrait +// methods. Not testing supertrait methods + + +trait Foo { + fn f(&self) -> isize; +} + +trait Bar : Foo { + fn g(&self) -> isize; +} + +struct A { + x: isize +} + +impl Foo for A { + fn f(&self) -> isize { 10 } +} + +impl Bar for A { + fn g(&self) -> isize { 20 } +} + +pub fn main() { + let a = &A { x: 3 }; + let afoo = a as &dyn Foo; + let abar = a as &dyn Bar; + assert_eq!(afoo.f(), 10); + assert_eq!(abar.g(), 20); +} diff --git a/src/test/ui/traits/inheritance/cast.rs b/src/test/ui/traits/inheritance/cast.rs new file mode 100644 index 000000000..9070b9d1f --- /dev/null +++ b/src/test/ui/traits/inheritance/cast.rs @@ -0,0 +1,33 @@ +// run-pass +#![allow(dead_code)] +// Testing that supertrait methods can be called on subtrait object types + + +trait Foo { + fn f(&self) -> isize; +} + +trait Bar : Foo { + fn g(&self) -> isize; +} + +struct A { + x: isize +} + +impl Foo for A { + fn f(&self) -> isize { 10 } +} + +impl Bar for A { + fn g(&self) -> isize { 20 } +} + +pub fn main() { + let a = &A { x: 3 }; + let afoo = a as &dyn Foo; + let abar = a as &dyn Bar; + assert_eq!(afoo.f(), 10); + assert_eq!(abar.g(), 20); + assert_eq!(abar.f(), 10); +} diff --git a/src/test/ui/traits/inheritance/cross-trait-call-xc.rs b/src/test/ui/traits/inheritance/cross-trait-call-xc.rs new file mode 100644 index 000000000..99fbb5c61 --- /dev/null +++ b/src/test/ui/traits/inheritance/cross-trait-call-xc.rs @@ -0,0 +1,20 @@ +// run-pass +// aux-build:xc_call.rs + + +extern crate xc_call as aux; + +use aux::Foo; + +trait Bar : Foo { + fn g(&self) -> isize; +} + +impl Bar for aux::A { + fn g(&self) -> isize { self.f() } +} + +pub fn main() { + let a = &aux::A { x: 3 }; + assert_eq!(a.g(), 10); +} diff --git a/src/test/ui/traits/inheritance/cross-trait-call.rs b/src/test/ui/traits/inheritance/cross-trait-call.rs new file mode 100644 index 000000000..512c928ca --- /dev/null +++ b/src/test/ui/traits/inheritance/cross-trait-call.rs @@ -0,0 +1,19 @@ +// run-pass +#![allow(dead_code)] + +trait Foo { fn f(&self) -> isize; } +trait Bar : Foo { fn g(&self) -> isize; } + +struct A { x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } + +impl Bar for A { + // Testing that this impl can call the impl of Foo + fn g(&self) -> isize { self.f() } +} + +pub fn main() { + let a = &A { x: 3 }; + assert_eq!(a.g(), 10); +} diff --git a/src/test/ui/traits/inheritance/diamond.rs b/src/test/ui/traits/inheritance/diamond.rs new file mode 100644 index 000000000..32ad0fb4d --- /dev/null +++ b/src/test/ui/traits/inheritance/diamond.rs @@ -0,0 +1,28 @@ +// run-pass +#![allow(dead_code)] +// B and C both require A, so D does as well, twice, but that's just fine + + +trait A { fn a(&self) -> isize; } +trait B: A { fn b(&self) -> isize; } +trait C: A { fn c(&self) -> isize; } +trait D: B + C { fn d(&self) -> isize; } + +struct S { bogus: () } + +impl A for S { fn a(&self) -> isize { 10 } } +impl B for S { fn b(&self) -> isize { 20 } } +impl C for S { fn c(&self) -> isize { 30 } } +impl D for S { fn d(&self) -> isize { 40 } } + +fn f(x: &T) { + assert_eq!(x.a(), 10); + assert_eq!(x.b(), 20); + assert_eq!(x.c(), 30); + assert_eq!(x.d(), 40); +} + +pub fn main() { + let value = &S { bogus: () }; + f(value); +} diff --git a/src/test/ui/traits/inheritance/multiple-inheritors.rs b/src/test/ui/traits/inheritance/multiple-inheritors.rs new file mode 100644 index 000000000..77ecbd8eb --- /dev/null +++ b/src/test/ui/traits/inheritance/multiple-inheritors.rs @@ -0,0 +1,23 @@ +// run-pass +#![allow(dead_code)] + +trait A { fn a(&self) -> isize; } +trait B: A { fn b(&self) -> isize; } +trait C: A { fn c(&self) -> isize; } + +struct S { bogus: () } + +impl A for S { fn a(&self) -> isize { 10 } } +impl B for S { fn b(&self) -> isize { 20 } } +impl C for S { fn c(&self) -> isize { 30 } } + +// Both B and C inherit from A +fn f(x: &T) { + assert_eq!(x.a(), 10); + assert_eq!(x.b(), 20); + assert_eq!(x.c(), 30); +} + +pub fn main() { + f(&S { bogus: () }) +} diff --git a/src/test/ui/traits/inheritance/multiple-params.rs b/src/test/ui/traits/inheritance/multiple-params.rs new file mode 100644 index 000000000..8ff5ba541 --- /dev/null +++ b/src/test/ui/traits/inheritance/multiple-params.rs @@ -0,0 +1,26 @@ +// run-pass +#![allow(dead_code)] + +trait A { fn a(&self) -> isize; } +trait B: A { fn b(&self) -> isize; } +trait C: A { fn c(&self) -> isize; } + +struct S { bogus: () } + +impl A for S { fn a(&self) -> isize { 10 } } +impl B for S { fn b(&self) -> isize { 20 } } +impl C for S { fn c(&self) -> isize { 30 } } + +// Multiple type params, multiple levels of inheritance +fn f(x: &X, y: &Y, z: &Z) { + assert_eq!(x.a(), 10); + assert_eq!(y.a(), 10); + assert_eq!(y.b(), 20); + assert_eq!(z.a(), 10); + assert_eq!(z.c(), 30); +} + +pub fn main() { + let s = &S { bogus: () }; + f(s, s, s); +} diff --git a/src/test/ui/traits/inheritance/num.rs b/src/test/ui/traits/inheritance/num.rs new file mode 100644 index 000000000..3d63d78ca --- /dev/null +++ b/src/test/ui/traits/inheritance/num.rs @@ -0,0 +1,13 @@ +// run-pass +#![allow(dead_code)] +#![allow(unused_variables)] +// pretty-expanded FIXME #23616 + +pub trait NumExt: PartialEq + PartialOrd {} + +pub trait FloatExt: NumExt {} + +fn greater_than_one(n: &T) -> bool { loop {} } +fn greater_than_one_float(n: &T) -> bool { loop {} } + +pub fn main() {} diff --git a/src/test/ui/traits/inheritance/num0.rs b/src/test/ui/traits/inheritance/num0.rs new file mode 100644 index 000000000..cee52542d --- /dev/null +++ b/src/test/ui/traits/inheritance/num0.rs @@ -0,0 +1,24 @@ +// run-pass +#![allow(dead_code)] +// Extending Num and using inherited static methods + +// pretty-expanded FIXME #23616 + +use std::cmp::PartialOrd; + +pub trait NumCast: Sized { + fn from(i: i32) -> Option; +} + +pub trait Num { + fn from_int(i: isize) -> Self; + fn gt(&self, other: &Self) -> bool; +} + +pub trait NumExt: NumCast + PartialOrd { } + +fn greater_than_one(n: &T) -> bool { + n.gt(&NumCast::from(1).unwrap()) +} + +pub fn main() {} diff --git a/src/test/ui/traits/inheritance/num1.rs b/src/test/ui/traits/inheritance/num1.rs new file mode 100644 index 000000000..663dd3a5e --- /dev/null +++ b/src/test/ui/traits/inheritance/num1.rs @@ -0,0 +1,15 @@ +// run-pass +#![allow(dead_code)] +// pretty-expanded FIXME #23616 + +pub trait NumCast: Sized { + fn from(i: i32) -> Option; +} + +pub trait NumExt: NumCast + PartialOrd { } + +fn greater_than_one(n: &T) -> bool { + *n > NumCast::from(1).unwrap() +} + +pub fn main() {} diff --git a/src/test/ui/traits/inheritance/num2.rs b/src/test/ui/traits/inheritance/num2.rs new file mode 100644 index 000000000..b713c66a3 --- /dev/null +++ b/src/test/ui/traits/inheritance/num2.rs @@ -0,0 +1,86 @@ +// run-pass +// A more complex example of numeric extensions + +pub trait TypeExt {} + +impl TypeExt for u8 {} +impl TypeExt for u16 {} +impl TypeExt for u32 {} +impl TypeExt for u64 {} +impl TypeExt for usize {} + +impl TypeExt for i8 {} +impl TypeExt for i16 {} +impl TypeExt for i32 {} +impl TypeExt for i64 {} +impl TypeExt for isize {} + +impl TypeExt for f32 {} +impl TypeExt for f64 {} + + +pub trait NumExt: TypeExt + PartialEq + PartialOrd {} + +impl NumExt for u8 {} +impl NumExt for u16 {} +impl NumExt for u32 {} +impl NumExt for u64 {} +impl NumExt for usize {} + +impl NumExt for i8 {} +impl NumExt for i16 {} +impl NumExt for i32 {} +impl NumExt for i64 {} +impl NumExt for isize {} + +impl NumExt for f32 {} +impl NumExt for f64 {} + + +pub trait UnSignedExt: NumExt {} + +impl UnSignedExt for u8 {} +impl UnSignedExt for u16 {} +impl UnSignedExt for u32 {} +impl UnSignedExt for u64 {} +impl UnSignedExt for usize {} + + +pub trait SignedExt: NumExt {} + +impl SignedExt for i8 {} +impl SignedExt for i16 {} +impl SignedExt for i32 {} +impl SignedExt for i64 {} +impl SignedExt for isize {} + +impl SignedExt for f32 {} +impl SignedExt for f64 {} + + +pub trait IntegerExt: NumExt {} + +impl IntegerExt for u8 {} +impl IntegerExt for u16 {} +impl IntegerExt for u32 {} +impl IntegerExt for u64 {} +impl IntegerExt for usize {} + +impl IntegerExt for i8 {} +impl IntegerExt for i16 {} +impl IntegerExt for i32 {} +impl IntegerExt for i64 {} +impl IntegerExt for isize {} + + +pub trait FloatExt: NumExt {} + +impl FloatExt for f32 {} +impl FloatExt for f64 {} + + +fn test_float_ext(n: T) { println!("{}", n < n) } + +pub fn main() { + test_float_ext(1f32); +} diff --git a/src/test/ui/traits/inheritance/num3.rs b/src/test/ui/traits/inheritance/num3.rs new file mode 100644 index 000000000..c40be6f83 --- /dev/null +++ b/src/test/ui/traits/inheritance/num3.rs @@ -0,0 +1,19 @@ +// run-pass +pub trait NumCast: Sized { + fn from(i: i32) -> Option; +} + +pub trait NumExt: PartialEq + PartialOrd + NumCast {} + +impl NumExt for f32 {} +impl NumCast for f32 { + fn from(i: i32) -> Option { Some(i as f32) } +} + +fn num_eq_one(n: T) { + println!("{}", n == NumCast::from(1).unwrap()) +} + +pub fn main() { + num_eq_one(1f32); // you need to actually use the function to trigger the ICE +} diff --git a/src/test/ui/traits/inheritance/num5.rs b/src/test/ui/traits/inheritance/num5.rs new file mode 100644 index 000000000..f478618f7 --- /dev/null +++ b/src/test/ui/traits/inheritance/num5.rs @@ -0,0 +1,26 @@ +// run-pass +// pretty-expanded FIXME #23616 + +pub trait NumCast: Sized { + fn from(i: i32) -> Option; +} + +pub trait NumExt: PartialEq + NumCast {} + +impl NumExt for f32 {} +impl NumExt for isize {} + +impl NumCast for f32 { + fn from(i: i32) -> Option { Some(i as f32) } +} +impl NumCast for isize { + fn from(i: i32) -> Option { Some(i as isize) } +} + +fn num_eq_one() -> T { + NumCast::from(1).unwrap() +} + +pub fn main() { + num_eq_one::(); // you need to actually use the function to trigger the ICE +} diff --git a/src/test/ui/traits/inheritance/overloading-simple.rs b/src/test/ui/traits/inheritance/overloading-simple.rs new file mode 100644 index 000000000..c306aa2cd --- /dev/null +++ b/src/test/ui/traits/inheritance/overloading-simple.rs @@ -0,0 +1,27 @@ +// run-pass +#![allow(dead_code)] +use std::cmp::PartialEq; + +trait MyNum : PartialEq { } + +#[derive(Debug)] +struct MyInt { val: isize } + +impl PartialEq for MyInt { + fn eq(&self, other: &MyInt) -> bool { self.val == other.val } + fn ne(&self, other: &MyInt) -> bool { !self.eq(other) } +} + +impl MyNum for MyInt {} + +fn f(x: T, y: T) -> bool { + return x == y; +} + +fn mi(v: isize) -> MyInt { MyInt { val: v } } + +pub fn main() { + let (x, y, z) = (mi(3), mi(5), mi(3)); + assert!(x != y); + assert_eq!(x, z); +} diff --git a/src/test/ui/traits/inheritance/overloading-xc-exe.rs b/src/test/ui/traits/inheritance/overloading-xc-exe.rs new file mode 100644 index 000000000..08778061b --- /dev/null +++ b/src/test/ui/traits/inheritance/overloading-xc-exe.rs @@ -0,0 +1,20 @@ +// run-pass +// aux-build:overloading_xc.rs + + +extern crate overloading_xc; +use overloading_xc::{MyNum, MyInt}; + +fn f(x: T, y: T) -> (T, T, T) { + return (x.clone() + y.clone(), x.clone() - y.clone(), x * y); +} + +fn mi(v: isize) -> MyInt { MyInt { val: v } } + +pub fn main() { + let (x, y) = (mi(3), mi(5)); + let (a, b, c) = f(x, y); + assert_eq!(a, mi(8)); + assert_eq!(b, mi(-2)); + assert_eq!(c, mi(15)); +} diff --git a/src/test/ui/traits/inheritance/overloading.rs b/src/test/ui/traits/inheritance/overloading.rs new file mode 100644 index 000000000..083643e82 --- /dev/null +++ b/src/test/ui/traits/inheritance/overloading.rs @@ -0,0 +1,47 @@ +// run-pass +use std::cmp::PartialEq; +use std::ops::{Add, Sub, Mul}; + +trait MyNum : Add + Sub + Mul + PartialEq + Clone { } + +#[derive(Clone, Debug)] +struct MyInt { val: isize } + +impl Add for MyInt { + type Output = MyInt; + + fn add(self, other: MyInt) -> MyInt { mi(self.val + other.val) } +} + +impl Sub for MyInt { + type Output = MyInt; + + fn sub(self, other: MyInt) -> MyInt { mi(self.val - other.val) } +} + +impl Mul for MyInt { + type Output = MyInt; + + fn mul(self, other: MyInt) -> MyInt { mi(self.val * other.val) } +} + +impl PartialEq for MyInt { + fn eq(&self, other: &MyInt) -> bool { self.val == other.val } + fn ne(&self, other: &MyInt) -> bool { !self.eq(other) } +} + +impl MyNum for MyInt {} + +fn f(x: T, y: T) -> (T, T, T) { + return (x.clone() + y.clone(), x.clone() - y.clone(), x * y); +} + +fn mi(v: isize) -> MyInt { MyInt { val: v } } + +pub fn main() { + let (x, y) = (mi(3), mi(5)); + let (a, b, c) = f(x, y); + assert_eq!(a, mi(8)); + assert_eq!(b, mi(-2)); + assert_eq!(c, mi(15)); +} diff --git a/src/test/ui/traits/inheritance/repeated-supertrait-ambig.rs b/src/test/ui/traits/inheritance/repeated-supertrait-ambig.rs new file mode 100644 index 000000000..727897d20 --- /dev/null +++ b/src/test/ui/traits/inheritance/repeated-supertrait-ambig.rs @@ -0,0 +1,43 @@ +// Test a case of a trait which extends the same supertrait twice, but +// with difference type parameters. Test then that when we don't give +// enough information to pick between these, no selection is made. In +// this particular case, the two choices are i64/u64 -- so when we use +// an integer literal, we wind up falling this literal back to i32. +// See also `run-pass/trait-repeated-supertrait.rs`. + +trait CompareTo { + fn same_as(&self, t: T) -> bool; +} + +trait CompareToInts : CompareTo + CompareTo { +} + +impl CompareTo for i64 { + fn same_as(&self, t: i64) -> bool { *self == t } +} + +impl CompareTo for i64 { + fn same_as(&self, t: u64) -> bool { *self == (t as i64) } +} + +impl CompareToInts for i64 { } + +fn with_obj(c: &dyn CompareToInts) -> bool { + c.same_as(22) //~ ERROR `dyn CompareToInts: CompareTo` is not satisfied +} + +fn with_trait(c: &C) -> bool { + c.same_as(22) //~ ERROR `C: CompareTo` is not satisfied +} + +fn with_ufcs1(c: &C) -> bool { + ::same_as(c, 22) //~ ERROR `dyn CompareToInts: CompareTo` is not satisfi +} + +fn with_ufcs2(c: &C) -> bool { + CompareTo::same_as(c, 22) //~ ERROR `C: CompareTo` is not satisfied +} + +fn main() { + assert_eq!(22_i64.same_as(22), true); //~ ERROR `i64: CompareTo` is not satisfied +} diff --git a/src/test/ui/traits/inheritance/repeated-supertrait-ambig.stderr b/src/test/ui/traits/inheritance/repeated-supertrait-ambig.stderr new file mode 100644 index 000000000..cab0ccdf7 --- /dev/null +++ b/src/test/ui/traits/inheritance/repeated-supertrait-ambig.stderr @@ -0,0 +1,55 @@ +error[E0277]: the trait bound `dyn CompareToInts: CompareTo` is not satisfied + --> $DIR/repeated-supertrait-ambig.rs:26:7 + | +LL | c.same_as(22) + | ^^^^^^^ the trait `CompareTo` is not implemented for `dyn CompareToInts` + | + = help: the following other types implement trait `CompareTo`: + > + > + +error[E0277]: the trait bound `C: CompareTo` is not satisfied + --> $DIR/repeated-supertrait-ambig.rs:30:7 + | +LL | c.same_as(22) + | ^^^^^^^ the trait `CompareTo` is not implemented for `C` + | +help: consider further restricting this bound + | +LL | fn with_trait>(c: &C) -> bool { + | ++++++++++++++++ + +error[E0277]: the trait bound `dyn CompareToInts: CompareTo` is not satisfied + --> $DIR/repeated-supertrait-ambig.rs:34:5 + | +LL | ::same_as(c, 22) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `CompareTo` is not implemented for `dyn CompareToInts` + | + = help: the following other types implement trait `CompareTo`: + > + > + +error[E0277]: the trait bound `C: CompareTo` is not satisfied + --> $DIR/repeated-supertrait-ambig.rs:38:5 + | +LL | CompareTo::same_as(c, 22) + | ^^^^^^^^^^^^^^^^^^ the trait `CompareTo` is not implemented for `C` + | +help: consider further restricting this bound + | +LL | fn with_ufcs2>(c: &C) -> bool { + | ++++++++++++++++ + +error[E0277]: the trait bound `i64: CompareTo` is not satisfied + --> $DIR/repeated-supertrait-ambig.rs:42:23 + | +LL | assert_eq!(22_i64.same_as(22), true); + | ^^^^^^^ the trait `CompareTo` is not implemented for `i64` + | + = help: the following other types implement trait `CompareTo`: + > + > + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/inheritance/repeated-supertrait.rs b/src/test/ui/traits/inheritance/repeated-supertrait.rs new file mode 100644 index 000000000..cb2581ffa --- /dev/null +++ b/src/test/ui/traits/inheritance/repeated-supertrait.rs @@ -0,0 +1,48 @@ +// run-pass +// Test a case of a trait which extends the same supertrait twice, but +// with difference type parameters. Test that we can invoke the +// various methods in various ways successfully. +// See also `ui/traits/trait-repeated-supertrait-ambig.rs`. + + +trait CompareTo { + fn same_as(&self, t: T) -> bool; +} + +trait CompareToInts : CompareTo + CompareTo { +} + +impl CompareTo for i64 { + fn same_as(&self, t: i64) -> bool { *self == t } +} + +impl CompareTo for i64 { + fn same_as(&self, t: u64) -> bool { *self == (t as i64) } +} + +impl CompareToInts for i64 { } + +fn with_obj(c: &dyn CompareToInts) -> bool { + c.same_as(22_i64) && c.same_as(22_u64) +} + +fn with_trait(c: &C) -> bool { + c.same_as(22_i64) && c.same_as(22_u64) +} + +fn with_ufcs1(c: &C) -> bool { + ::same_as(c, 22_i64) && ::same_as(c, 22_u64) +} + +fn with_ufcs2(c: &C) -> bool { + CompareTo::same_as(c, 22_i64) && CompareTo::same_as(c, 22_u64) +} + +fn main() { + assert_eq!(22_i64.same_as(22_i64), true); + assert_eq!(22_i64.same_as(22_u64), true); + assert_eq!(with_trait(&22), true); + assert_eq!(with_obj(&22), true); + assert_eq!(with_ufcs1(&22), true); + assert_eq!(with_ufcs2(&22), true); +} diff --git a/src/test/ui/traits/inheritance/self-in-supertype.rs b/src/test/ui/traits/inheritance/self-in-supertype.rs new file mode 100644 index 000000000..e8a2bd791 --- /dev/null +++ b/src/test/ui/traits/inheritance/self-in-supertype.rs @@ -0,0 +1,62 @@ +// run-pass +// Test for issue #4183: use of Self in supertraits. + +pub static FUZZY_EPSILON: f64 = 0.1; + +pub trait FuzzyEq { + fn fuzzy_eq(&self, other: &Self) -> bool; + fn fuzzy_eq_eps(&self, other: &Self, epsilon: &Eps) -> bool; +} + +trait Float: Sized+FuzzyEq { + fn two_pi() -> Self; +} + +impl FuzzyEq for f32 { + fn fuzzy_eq(&self, other: &f32) -> bool { + self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f32)) + } + + fn fuzzy_eq_eps(&self, other: &f32, epsilon: &f32) -> bool { + (*self - *other).abs() < *epsilon + } +} + +impl Float for f32 { + fn two_pi() -> f32 { 6.28318530717958647692528676655900576_f32 } +} + +impl FuzzyEq for f64 { + fn fuzzy_eq(&self, other: &f64) -> bool { + self.fuzzy_eq_eps(other, &(FUZZY_EPSILON as f64)) + } + + fn fuzzy_eq_eps(&self, other: &f64, epsilon: &f64) -> bool { + (*self - *other).abs() < *epsilon + } +} + +impl Float for f64 { + fn two_pi() -> f64 { 6.28318530717958647692528676655900576_f64 } +} + +fn compare(f1: F) -> bool { + let f2 = Float::two_pi(); + f1.fuzzy_eq(&f2) +} + +pub fn main() { + assert!(compare::(6.28318530717958647692528676655900576)); + assert!(compare::(6.29)); + assert!(compare::(6.3)); + assert!(compare::(6.19)); + assert!(!compare::(7.28318530717958647692528676655900576)); + assert!(!compare::(6.18)); + + assert!(compare::(6.28318530717958647692528676655900576)); + assert!(compare::(6.29)); + assert!(compare::(6.3)); + assert!(compare::(6.19)); + assert!(!compare::(7.28318530717958647692528676655900576)); + assert!(!compare::(6.18)); +} diff --git a/src/test/ui/traits/inheritance/self.rs b/src/test/ui/traits/inheritance/self.rs new file mode 100644 index 000000000..5f2559f48 --- /dev/null +++ b/src/test/ui/traits/inheritance/self.rs @@ -0,0 +1,29 @@ +// run-pass +trait Foo { + fn f(&self, x: &T); +} + +trait Bar : Sized + Foo { + fn g(&self); +} + +struct S { + x: isize +} + +impl Foo for S { + fn f(&self, x: &S) { + println!("{}", x.x); + } +} + +impl Bar for S { + fn g(&self) { + self.f(self); + } +} + +pub fn main() { + let s = S { x: 1 }; + s.g(); +} diff --git a/src/test/ui/traits/inheritance/simple.rs b/src/test/ui/traits/inheritance/simple.rs new file mode 100644 index 000000000..ca3a284e5 --- /dev/null +++ b/src/test/ui/traits/inheritance/simple.rs @@ -0,0 +1,24 @@ +// run-pass +#![allow(dead_code)] + +trait Foo { fn f(&self) -> isize; } +trait Bar : Foo { fn g(&self) -> isize; } + +struct A { x: isize } + +impl Foo for A { fn f(&self) -> isize { 10 } } +impl Bar for A { fn g(&self) -> isize { 20 } } + +fn ff(a: &T) -> isize { + a.f() +} + +fn gg(a: &T) -> isize { + a.g() +} + +pub fn main() { + let a = &A { x: 3 }; + assert_eq!(ff(a), 10); + assert_eq!(gg(a), 20); +} diff --git a/src/test/ui/traits/inheritance/static.rs b/src/test/ui/traits/inheritance/static.rs new file mode 100644 index 000000000..16218fbd2 --- /dev/null +++ b/src/test/ui/traits/inheritance/static.rs @@ -0,0 +1,26 @@ +// run-pass + +pub trait MyNum { + fn from_int(_: isize) -> Self; +} + +pub trait NumExt: MyNum { } + +struct S { v: isize } + +impl MyNum for S { + fn from_int(i: isize) -> S { + S { + v: i + } + } +} + +impl NumExt for S { } + +fn greater_than_one() -> T { MyNum::from_int(1) } + +pub fn main() { + let v: S = greater_than_one(); + assert_eq!(v.v, 1); +} diff --git a/src/test/ui/traits/inheritance/static2.rs b/src/test/ui/traits/inheritance/static2.rs new file mode 100644 index 000000000..bc78e1e23 --- /dev/null +++ b/src/test/ui/traits/inheritance/static2.rs @@ -0,0 +1,29 @@ +// run-pass +pub trait MyEq {} + +pub trait MyNum { + fn from_int(_: isize) -> Self; +} + +pub trait NumExt: MyEq + MyNum { } + +struct S { v: isize } + +impl MyEq for S { } + +impl MyNum for S { + fn from_int(i: isize) -> S { + S { + v: i + } + } +} + +impl NumExt for S { } + +fn greater_than_one() -> T { MyNum::from_int(1) } + +pub fn main() { + let v: S = greater_than_one(); + assert_eq!(v.v, 1); +} diff --git a/src/test/ui/traits/inheritance/subst.rs b/src/test/ui/traits/inheritance/subst.rs new file mode 100644 index 000000000..b2b650366 --- /dev/null +++ b/src/test/ui/traits/inheritance/subst.rs @@ -0,0 +1,27 @@ +// run-pass + +pub trait Add { + fn add(&self, rhs: &RHS) -> Result; +} + +trait MyNum : Sized + Add { } + +struct MyInt { val: isize } + +impl Add for MyInt { + fn add(&self, other: &MyInt) -> MyInt { mi(self.val + other.val) } +} + +impl MyNum for MyInt {} + +fn f(x: T, y: T) -> T { + return x.add(&y); +} + +fn mi(v: isize) -> MyInt { MyInt { val: v } } + +pub fn main() { + let (x, y) = (mi(3), mi(5)); + let z = f(x, y); + assert_eq!(z.val, 8) +} diff --git a/src/test/ui/traits/inheritance/subst2.rs b/src/test/ui/traits/inheritance/subst2.rs new file mode 100644 index 000000000..ccc9628c7 --- /dev/null +++ b/src/test/ui/traits/inheritance/subst2.rs @@ -0,0 +1,37 @@ +// run-pass + +trait Panda { + fn chomp(&self, bamboo: &T) -> T; +} + +trait Add: Panda { + fn add(&self, rhs: &RHS) -> Result; +} + +trait MyNum : Sized + Add { } + +struct MyInt { val: isize } + +impl Panda for MyInt { + fn chomp(&self, bamboo: &MyInt) -> MyInt { + mi(self.val + bamboo.val) + } +} + +impl Add for MyInt { + fn add(&self, other: &MyInt) -> MyInt { self.chomp(other) } +} + +impl MyNum for MyInt {} + +fn f(x: T, y: T) -> T { + return x.add(&y).chomp(&y); +} + +fn mi(v: isize) -> MyInt { MyInt { val: v } } + +pub fn main() { + let (x, y) = (mi(3), mi(5)); + let z = f(x, y); + assert_eq!(z.val, 13); +} diff --git a/src/test/ui/traits/inheritance/visibility.rs b/src/test/ui/traits/inheritance/visibility.rs new file mode 100644 index 000000000..6ad864926 --- /dev/null +++ b/src/test/ui/traits/inheritance/visibility.rs @@ -0,0 +1,20 @@ +// run-pass + +mod traits { + pub trait Foo { fn f(&self) -> isize; } + + impl Foo for isize { fn f(&self) -> isize { 10 } } +} + +trait Quux: traits::Foo { } +impl Quux for T { } + +// Foo is not in scope but because Quux is we can still access +// Foo's methods on a Quux bound typaram +fn f(x: &T) { + assert_eq!(x.f(), 10); +} + +pub fn main() { + f(&0) +} diff --git a/src/test/ui/traits/issue-18400.rs b/src/test/ui/traits/issue-18400.rs new file mode 100644 index 000000000..fdd11512d --- /dev/null +++ b/src/test/ui/traits/issue-18400.rs @@ -0,0 +1,26 @@ +trait Set { + fn contains(&self, _: T) -> bool; + fn set(&mut self, _: T); +} + +impl<'a, T, S> Set<&'a [T]> for S where + T: Copy, + S: Set, +{ + fn contains(&self, bits: &[T]) -> bool { + bits.iter().all(|&bit| self.contains(bit)) + } + + fn set(&mut self, bits: &[T]) { + for &bit in bits { + self.set(bit) + } + } +} + +fn main() { + let bits: &[_] = &[0, 1]; + + 0.contains(bits); + //~^ ERROR overflow +} diff --git a/src/test/ui/traits/issue-18400.stderr b/src/test/ui/traits/issue-18400.stderr new file mode 100644 index 000000000..92e0f6007 --- /dev/null +++ b/src/test/ui/traits/issue-18400.stderr @@ -0,0 +1,18 @@ +error[E0275]: overflow evaluating the requirement `_: Sized` + --> $DIR/issue-18400.rs:24:7 + | +LL | 0.contains(bits); + | ^^^^^^^^ + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`issue_18400`) +note: required because of the requirements on the impl of `Set<&[_]>` for `{integer}` + --> $DIR/issue-18400.rs:6:16 + | +LL | impl<'a, T, S> Set<&'a [T]> for S where + | ^^^^^^^^^^^^ ^ + = note: 128 redundant requirements hidden + = note: required because of the requirements on the impl of `Set<&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[&[_]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]>` for `{integer}` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/issue-18412.rs b/src/test/ui/traits/issue-18412.rs new file mode 100644 index 000000000..fe1cfb3df --- /dev/null +++ b/src/test/ui/traits/issue-18412.rs @@ -0,0 +1,26 @@ +// run-pass +// Test that non-static methods can be assigned to local variables as +// function pointers. + + +trait Foo { + fn foo(&self) -> usize; +} + +struct A(usize); + +impl A { + fn bar(&self) -> usize { self.0 } +} + +impl Foo for A { + fn foo(&self) -> usize { self.bar() } +} + +fn main() { + let f = A::bar; + let g = Foo::foo; + let a = A(42); + + assert_eq!(f(&a), g(&a)); +} diff --git a/src/test/ui/traits/issue-20692.rs b/src/test/ui/traits/issue-20692.rs new file mode 100644 index 000000000..1cb2d8c73 --- /dev/null +++ b/src/test/ui/traits/issue-20692.rs @@ -0,0 +1,11 @@ +trait Array: Sized + Copy {} + +fn f(x: &T) { + let _ = x + //~^ ERROR `Array` cannot be made into an object + as + &dyn Array; + //~^ ERROR `Array` cannot be made into an object +} + +fn main() {} diff --git a/src/test/ui/traits/issue-20692.stderr b/src/test/ui/traits/issue-20692.stderr new file mode 100644 index 000000000..1d7f252e5 --- /dev/null +++ b/src/test/ui/traits/issue-20692.stderr @@ -0,0 +1,35 @@ +error[E0038]: the trait `Array` cannot be made into an object + --> $DIR/issue-20692.rs:7:5 + | +LL | &dyn Array; + | ^^^^^^^^^^ `Array` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-20692.rs:1:14 + | +LL | trait Array: Sized + Copy {} + | ----- ^^^^^ ^^^^ ...because it requires `Self: Sized` + | | | + | | ...because it requires `Self: Sized` + | this trait cannot be made into an object... + +error[E0038]: the trait `Array` cannot be made into an object + --> $DIR/issue-20692.rs:4:13 + | +LL | let _ = x + | ^ `Array` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-20692.rs:1:14 + | +LL | trait Array: Sized + Copy {} + | ----- ^^^^^ ^^^^ ...because it requires `Self: Sized` + | | | + | | ...because it requires `Self: Sized` + | this trait cannot be made into an object... + = note: required because of the requirements on the impl of `CoerceUnsized<&dyn Array>` for `&T` + = note: required by cast to type `&dyn Array` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/issue-22019.rs b/src/test/ui/traits/issue-22019.rs new file mode 100644 index 000000000..1a887f0f3 --- /dev/null +++ b/src/test/ui/traits/issue-22019.rs @@ -0,0 +1,34 @@ +// run-pass +// Test an issue where global caching was causing free regions from +// distinct scopes to be compared (`'g` and `'h`). The only important +// thing is that compilation succeeds here. + +// pretty-expanded FIXME #23616 + +#![allow(missing_copy_implementations)] +#![allow(unused_variables)] + +use std::borrow::ToOwned; + +pub struct CFGNode; + +pub type Node<'a> = &'a CFGNode; + +pub trait GraphWalk<'c, N> { + /// Returns all the nodes in this graph. + fn nodes(&'c self) where [N]:ToOwned>; +} + +impl<'g> GraphWalk<'g, Node<'g>> for u32 +{ + fn nodes(&'g self) where [Node<'g>]:ToOwned>> + { loop { } } +} + +impl<'h> GraphWalk<'h, Node<'h>> for u64 +{ + fn nodes(&'h self) where [Node<'h>]:ToOwned>> + { loop { } } +} + +fn main() { } diff --git a/src/test/ui/traits/issue-22110.rs b/src/test/ui/traits/issue-22110.rs new file mode 100644 index 000000000..bdbfee799 --- /dev/null +++ b/src/test/ui/traits/issue-22110.rs @@ -0,0 +1,27 @@ +// run-pass +// Test an issue where we reported ambiguity between the where-clause +// and the blanket impl. The only important thing is that compilation +// succeeds here. Issue #22110. + +// pretty-expanded FIXME #23616 + +#![allow(dead_code)] + +trait Foo { + fn foo(&self, a: A); +} + +impl Foo for F { + fn foo(&self, _: A) { } +} + +fn baz Foo<(&'a A,)>>(_: F) { } + +fn components(t: fn(&A)) + where fn(&A) : for<'a> Foo<(&'a A,)>, +{ + baz(t) +} + +fn main() { +} diff --git a/src/test/ui/traits/issue-22655.rs b/src/test/ui/traits/issue-22655.rs new file mode 100644 index 000000000..bc08ca0a2 --- /dev/null +++ b/src/test/ui/traits/issue-22655.rs @@ -0,0 +1,23 @@ +// run-pass +#![allow(dead_code)] +// Regression test for issue #22655: This test should not lead to +// infinite recursion. + +// pretty-expanded FIXME #23616 + +unsafe impl Send for Unique { } + +pub struct Unique { + pointer: *const T, +} + +pub struct Node { + vals: V, + edges: Unique>, +} + +fn is_send() {} + +fn main() { + is_send::>(); +} diff --git a/src/test/ui/traits/issue-23003-overflow.rs b/src/test/ui/traits/issue-23003-overflow.rs new file mode 100644 index 000000000..c5f471f23 --- /dev/null +++ b/src/test/ui/traits/issue-23003-overflow.rs @@ -0,0 +1,29 @@ +// A variant of traits-issue-23003 in which an infinite series of +// types are required. This test now just compiles fine, since the +// relevant rules that triggered the overflow were removed. + +// check-pass +#![allow(dead_code)] + +use std::marker::PhantomData; + +trait Async { + type Cancel; +} + +struct Receipt { + marker: PhantomData, +} + +struct Complete { + core: Option, +} + +impl Async for Complete { + type Cancel = Receipt>>; +} + +fn foo(_: Receipt>) { } + + +fn main() { } diff --git a/src/test/ui/traits/issue-23003.rs b/src/test/ui/traits/issue-23003.rs new file mode 100644 index 000000000..24c2b2ad6 --- /dev/null +++ b/src/test/ui/traits/issue-23003.rs @@ -0,0 +1,32 @@ +// run-pass +// Test stack overflow triggered by evaluating the implications. To be +// WF, the type `Receipt` would require that `::Cancel` be WF. This normalizes to `Receipt` +// again, leading to an infinite cycle. Issue #23003. + +// pretty-expanded FIXME #23616 + +#![allow(dead_code)] +#![allow(unused_variables)] + +use std::marker::PhantomData; + +trait Async { + type Cancel; +} + +struct Receipt { + marker: PhantomData, +} + +struct Complete { + core: Option<()>, +} + +impl Async for Complete { + type Cancel = Receipt; +} + +fn foo(r: Receipt) { } + +fn main() { } diff --git a/src/test/ui/traits/issue-23825.rs b/src/test/ui/traits/issue-23825.rs new file mode 100644 index 000000000..a9f0095d2 --- /dev/null +++ b/src/test/ui/traits/issue-23825.rs @@ -0,0 +1,21 @@ +// run-pass +trait Stringify { + fn to_string(&self) -> String; +} + +impl Stringify for u32 { + fn to_string(&self) -> String { format!("u32: {}", *self) } +} + +impl Stringify for f32 { + fn to_string(&self) -> String { format!("f32: {}", *self) } +} + +fn print(x: T) -> String { + x.to_string() +} + +fn main() { + assert_eq!(&print(5), "u32: 5"); + assert_eq!(&print(5.0), "f32: 5"); +} diff --git a/src/test/ui/traits/issue-24010.rs b/src/test/ui/traits/issue-24010.rs new file mode 100644 index 000000000..f18185334 --- /dev/null +++ b/src/test/ui/traits/issue-24010.rs @@ -0,0 +1,14 @@ +// run-pass + +trait Foo: Fn(i32) -> i32 + Send {} + +impl i32 + Send> Foo for T {} + +fn wants_foo(f: Box) -> i32 { + f(42) +} + +fn main() { + let f = Box::new(|x| x); + assert_eq!(wants_foo(f), 42); +} diff --git a/src/test/ui/traits/issue-26339.rs b/src/test/ui/traits/issue-26339.rs new file mode 100644 index 000000000..bedd87cc4 --- /dev/null +++ b/src/test/ui/traits/issue-26339.rs @@ -0,0 +1,31 @@ +// run-pass +// Test that the right implementation is called through a trait +// object when supertraits include multiple references to the +// same trait, with different type parameters. + +trait A: PartialEq + PartialEq { } + +struct Foo; +struct Bar; + +struct Aimpl; + +impl PartialEq for Aimpl { + fn eq(&self, _rhs: &Foo) -> bool { + true + } +} + +impl PartialEq for Aimpl { + fn eq(&self, _rhs: &Bar) -> bool { + false + } +} + +impl A for Aimpl { } + +fn main() { + let a = &Aimpl as &dyn A; + + assert!(*a == Foo); +} diff --git a/src/test/ui/traits/issue-28576.rs b/src/test/ui/traits/issue-28576.rs new file mode 100644 index 000000000..972c839b6 --- /dev/null +++ b/src/test/ui/traits/issue-28576.rs @@ -0,0 +1,12 @@ +pub trait Foo { + type Assoc; +} + +pub trait Bar: Foo { + fn new(&self, b: & + dyn Bar //~ ERROR the trait `Bar` cannot be made into an object + + ); +} + +fn main() {} diff --git a/src/test/ui/traits/issue-28576.stderr b/src/test/ui/traits/issue-28576.stderr new file mode 100644 index 000000000..203cd0630 --- /dev/null +++ b/src/test/ui/traits/issue-28576.stderr @@ -0,0 +1,20 @@ +error[E0038]: the trait `Bar` cannot be made into an object + --> $DIR/issue-28576.rs:7:12 + | +LL | / dyn Bar +LL | | + | |________________________^ `Bar` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-28576.rs:5:16 + | +LL | pub trait Bar: Foo { + | --- ^^^^^^^^^^^^^ + | | | | + | | | ...because it uses `Self` as a type parameter + | | ...because it uses `Self` as a type parameter + | this trait cannot be made into an object... + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/issue-32963.rs b/src/test/ui/traits/issue-32963.rs new file mode 100644 index 000000000..56a68f3a2 --- /dev/null +++ b/src/test/ui/traits/issue-32963.rs @@ -0,0 +1,11 @@ +use std::mem; + +trait Misc {} + +fn size_of_copy() -> usize { mem::size_of::() } + +fn main() { + size_of_copy::(); + //~^ ERROR only auto traits can be used as additional traits in a trait object + //~| ERROR the trait bound `dyn Misc: Copy` is not satisfied +} diff --git a/src/test/ui/traits/issue-32963.stderr b/src/test/ui/traits/issue-32963.stderr new file mode 100644 index 000000000..bad45e54d --- /dev/null +++ b/src/test/ui/traits/issue-32963.stderr @@ -0,0 +1,27 @@ +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/issue-32963.rs:8:31 + | +LL | size_of_copy::(); + | ---- ^^^^ additional non-auto trait + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Misc + Copy {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0277]: the trait bound `dyn Misc: Copy` is not satisfied + --> $DIR/issue-32963.rs:8:20 + | +LL | size_of_copy::(); + | ^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `dyn Misc` + | +note: required by a bound in `size_of_copy` + --> $DIR/issue-32963.rs:5:20 + | +LL | fn size_of_copy() -> usize { mem::size_of::() } + | ^^^^ required by this bound in `size_of_copy` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0225, E0277. +For more information about an error, try `rustc --explain E0225`. diff --git a/src/test/ui/traits/issue-33140-hack-boundaries.rs b/src/test/ui/traits/issue-33140-hack-boundaries.rs new file mode 100644 index 000000000..d091162fc --- /dev/null +++ b/src/test/ui/traits/issue-33140-hack-boundaries.rs @@ -0,0 +1,70 @@ +#![feature(negative_impls)] +#![allow(order_dependent_trait_objects)] + +// Check that the issue #33140 hack does not allow unintended things. + +// OK +trait Trait0 {} + +impl Trait0 for dyn Send {} +impl Trait0 for dyn Send {} + +// Problem 1: associated types +trait Trait1 { + fn my_fn(&self) {} +} + +impl Trait1 for dyn Send {} +impl Trait1 for dyn Send {} +//~^ ERROR E0119 + +// Problem 2: negative impl +trait Trait2 {} + +impl Trait2 for dyn Send {} +impl !Trait2 for dyn Send {} +//~^ ERROR E0751 + +// Problem 3: type parameter +trait Trait3 {} + +impl Trait3 for dyn Send {} +impl Trait3 for dyn Send {} +//~^ ERROR E0119 + +// Problem 4a: not a trait object - generic +trait Trait4a {} + +impl Trait4a for T {} +impl Trait4a for dyn Send {} +//~^ ERROR E0119 + +// Problem 4b: not a trait object - misc +trait Trait4b {} + +impl Trait4b for () {} +impl Trait4b for () {} +//~^ ERROR E0119 + +// Problem 4c: not a principal-less trait object +trait Trait4c {} + +impl Trait4c for dyn Trait1 + Send {} +impl Trait4c for dyn Trait1 + Send {} +//~^ ERROR E0119 + +// Problem 4d: lifetimes +trait Trait4d {} + +impl<'a> Trait4d for dyn Send + 'a {} +impl<'a> Trait4d for dyn Send + 'a {} +//~^ ERROR E0119 + +// Problem 5: where-clauses +trait Trait5 {} + +impl Trait5 for dyn Send {} +impl Trait5 for dyn Send where u32: Copy {} +//~^ ERROR E0119 + +fn main() {} diff --git a/src/test/ui/traits/issue-33140-hack-boundaries.stderr b/src/test/ui/traits/issue-33140-hack-boundaries.stderr new file mode 100644 index 000000000..62cfca545 --- /dev/null +++ b/src/test/ui/traits/issue-33140-hack-boundaries.stderr @@ -0,0 +1,68 @@ +error[E0119]: conflicting implementations of trait `Trait1` for type `(dyn std::marker::Send + 'static)` + --> $DIR/issue-33140-hack-boundaries.rs:18:1 + | +LL | impl Trait1 for dyn Send {} + | ------------------------ first implementation here +LL | impl Trait1 for dyn Send {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + 'static)` + +error[E0751]: found both positive and negative implementation of trait `Trait2` for type `(dyn std::marker::Send + 'static)`: + --> $DIR/issue-33140-hack-boundaries.rs:25:1 + | +LL | impl Trait2 for dyn Send {} + | ------------------------ positive implementation here +LL | impl !Trait2 for dyn Send {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ negative implementation here + +error[E0119]: conflicting implementations of trait `Trait3<(dyn std::marker::Sync + 'static)>` for type `(dyn std::marker::Send + 'static)` + --> $DIR/issue-33140-hack-boundaries.rs:32:1 + | +LL | impl Trait3 for dyn Send {} + | ---------------------------------- first implementation here +LL | impl Trait3 for dyn Send {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + 'static)` + +error[E0119]: conflicting implementations of trait `Trait4a` for type `(dyn std::marker::Send + 'static)` + --> $DIR/issue-33140-hack-boundaries.rs:39:1 + | +LL | impl Trait4a for T {} + | ----------------------------- first implementation here +LL | impl Trait4a for dyn Send {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + 'static)` + +error[E0119]: conflicting implementations of trait `Trait4b` for type `()` + --> $DIR/issue-33140-hack-boundaries.rs:46:1 + | +LL | impl Trait4b for () {} + | ------------------- first implementation here +LL | impl Trait4b for () {} + | ^^^^^^^^^^^^^^^^^^^ conflicting implementation for `()` + +error[E0119]: conflicting implementations of trait `Trait4c` for type `(dyn Trait1 + std::marker::Send + 'static)` + --> $DIR/issue-33140-hack-boundaries.rs:53:1 + | +LL | impl Trait4c for dyn Trait1 + Send {} + | ---------------------------------- first implementation here +LL | impl Trait4c for dyn Trait1 + Send {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn Trait1 + std::marker::Send + 'static)` + +error[E0119]: conflicting implementations of trait `Trait4d` for type `dyn std::marker::Send` + --> $DIR/issue-33140-hack-boundaries.rs:60:1 + | +LL | impl<'a> Trait4d for dyn Send + 'a {} + | ---------------------------------- first implementation here +LL | impl<'a> Trait4d for dyn Send + 'a {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `dyn std::marker::Send` + +error[E0119]: conflicting implementations of trait `Trait5` for type `(dyn std::marker::Send + 'static)` + --> $DIR/issue-33140-hack-boundaries.rs:67:1 + | +LL | impl Trait5 for dyn Send {} + | ------------------------ first implementation here +LL | impl Trait5 for dyn Send where u32: Copy {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + 'static)` + +error: aborting due to 8 previous errors + +Some errors have detailed explanations: E0119, E0751. +For more information about an error, try `rustc --explain E0119`. diff --git a/src/test/ui/traits/issue-33140.rs b/src/test/ui/traits/issue-33140.rs new file mode 100644 index 000000000..9bdac4b83 --- /dev/null +++ b/src/test/ui/traits/issue-33140.rs @@ -0,0 +1,47 @@ +trait Trait { + fn xyz() -> bool; +} + +impl Trait for dyn Send + Sync { + fn xyz() -> bool { false } +} + +impl Trait for dyn Sync + Send { +//~^ ERROR conflicting implementations + fn xyz() -> bool { true } +} + +trait Trait2 { + fn uvw() -> bool; +} + +impl Trait2 for dyn Send + Sync { + fn uvw() -> bool { false } +} + +impl Trait2 for dyn Sync + Send + Sync { +//~^ ERROR conflicting implementations + fn uvw() -> bool { true } +} + +struct Foo(T); +impl Foo { + fn abc() -> bool { //~ ERROR duplicate definitions with name `abc` + false + } +} + +impl Foo { + fn abc() -> bool { + true + } +} + +fn main() { + assert_eq!(::xyz(), false); + assert_eq!(::xyz(), true); + assert_eq!(::uvw(), false); + assert_eq!(::uvw(), true); + assert_eq!(>::abc(), false); + assert_eq!(>::abc(), true); +} diff --git a/src/test/ui/traits/issue-33140.stderr b/src/test/ui/traits/issue-33140.stderr new file mode 100644 index 000000000..392c56a28 --- /dev/null +++ b/src/test/ui/traits/issue-33140.stderr @@ -0,0 +1,31 @@ +error[E0119]: conflicting implementations of trait `Trait` for type `(dyn std::marker::Send + std::marker::Sync + 'static)` + --> $DIR/issue-33140.rs:9:1 + | +LL | impl Trait for dyn Send + Sync { + | ------------------------------ first implementation here +... +LL | impl Trait for dyn Sync + Send { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + std::marker::Sync + 'static)` + +error[E0119]: conflicting implementations of trait `Trait2` for type `(dyn std::marker::Send + std::marker::Sync + 'static)` + --> $DIR/issue-33140.rs:22:1 + | +LL | impl Trait2 for dyn Send + Sync { + | ------------------------------- first implementation here +... +LL | impl Trait2 for dyn Sync + Send + Sync { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + std::marker::Sync + 'static)` + +error[E0592]: duplicate definitions with name `abc` + --> $DIR/issue-33140.rs:29:5 + | +LL | fn abc() -> bool { + | ^^^^^^^^^^^^^^^^ duplicate definitions for `abc` +... +LL | fn abc() -> bool { + | ---------------- other definition for `abc` + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0119, E0592. +For more information about an error, try `rustc --explain E0119`. diff --git a/src/test/ui/traits/issue-35869.rs b/src/test/ui/traits/issue-35869.rs new file mode 100644 index 000000000..b9b4c972e --- /dev/null +++ b/src/test/ui/traits/issue-35869.rs @@ -0,0 +1,21 @@ +trait Foo { + fn foo(_: fn(u8) -> ()); + fn bar(_: Option); + fn baz(_: (u8, u16)); + fn qux() -> u8; +} + +struct Bar; + +impl Foo for Bar { + fn foo(_: fn(u16) -> ()) {} + //~^ ERROR method `foo` has an incompatible type for trait + fn bar(_: Option) {} + //~^ ERROR method `bar` has an incompatible type for trait + fn baz(_: (u16, u16)) {} + //~^ ERROR method `baz` has an incompatible type for trait + fn qux() -> u16 { 5u16 } + //~^ ERROR method `qux` has an incompatible type for trait +} + +fn main() {} diff --git a/src/test/ui/traits/issue-35869.stderr b/src/test/ui/traits/issue-35869.stderr new file mode 100644 index 000000000..0780109b8 --- /dev/null +++ b/src/test/ui/traits/issue-35869.stderr @@ -0,0 +1,71 @@ +error[E0053]: method `foo` has an incompatible type for trait + --> $DIR/issue-35869.rs:11:15 + | +LL | fn foo(_: fn(u16) -> ()) {} + | ^^^^^^^^^^^^^ + | | + | expected `u8`, found `u16` + | help: change the parameter type to match the trait: `fn(u8)` + | +note: type in trait + --> $DIR/issue-35869.rs:2:15 + | +LL | fn foo(_: fn(u8) -> ()); + | ^^^^^^^^^^^^ + = note: expected fn pointer `fn(fn(u8))` + found fn pointer `fn(fn(u16))` + +error[E0053]: method `bar` has an incompatible type for trait + --> $DIR/issue-35869.rs:13:15 + | +LL | fn bar(_: Option) {} + | ^^^^^^^^^^^ + | | + | expected `u8`, found `u16` + | help: change the parameter type to match the trait: `Option` + | +note: type in trait + --> $DIR/issue-35869.rs:3:15 + | +LL | fn bar(_: Option); + | ^^^^^^^^^^ + = note: expected fn pointer `fn(Option)` + found fn pointer `fn(Option)` + +error[E0053]: method `baz` has an incompatible type for trait + --> $DIR/issue-35869.rs:15:15 + | +LL | fn baz(_: (u16, u16)) {} + | ^^^^^^^^^^ + | | + | expected `u8`, found `u16` + | help: change the parameter type to match the trait: `(u8, u16)` + | +note: type in trait + --> $DIR/issue-35869.rs:4:15 + | +LL | fn baz(_: (u8, u16)); + | ^^^^^^^^^ + = note: expected fn pointer `fn((u8, _))` + found fn pointer `fn((u16, _))` + +error[E0053]: method `qux` has an incompatible type for trait + --> $DIR/issue-35869.rs:17:17 + | +LL | fn qux() -> u16 { 5u16 } + | ^^^ + | | + | expected `u8`, found `u16` + | help: change the output type to match the trait: `u8` + | +note: type in trait + --> $DIR/issue-35869.rs:5:17 + | +LL | fn qux() -> u8; + | ^^ + = note: expected fn pointer `fn() -> u8` + found fn pointer `fn() -> u16` + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0053`. diff --git a/src/test/ui/traits/issue-3683.rs b/src/test/ui/traits/issue-3683.rs new file mode 100644 index 000000000..b12c450c9 --- /dev/null +++ b/src/test/ui/traits/issue-3683.rs @@ -0,0 +1,18 @@ +// run-pass + +trait Foo { + fn a(&self) -> isize; + fn b(&self) -> isize { + self.a() + 2 + } +} + +impl Foo for isize { + fn a(&self) -> isize { + 3 + } +} + +pub fn main() { + assert_eq!(3.b(), 5); +} diff --git a/src/test/ui/traits/issue-38033.rs b/src/test/ui/traits/issue-38033.rs new file mode 100644 index 000000000..16b867ec8 --- /dev/null +++ b/src/test/ui/traits/issue-38033.rs @@ -0,0 +1,79 @@ +// run-pass +use std::marker; +use std::mem; + +fn main() { + let workers = (0..0).map(|_| result::()); + drop(join_all(workers).poll()); +} + +trait Future { + type Item; + type Error; + + fn poll(&mut self) -> Result; +} + +trait IntoFuture { + type Future: Future; + type Item; + type Error; + + fn into_future(self) -> Self::Future; +} + +impl IntoFuture for F { + type Future = F; + type Item = F::Item; + type Error = F::Error; + + fn into_future(self) -> F { + self + } +} + +struct FutureResult { + _inner: marker::PhantomData<(T, E)>, +} + +fn result() -> FutureResult { + loop {} +} + +impl Future for FutureResult { + type Item = T; + type Error = E; + + fn poll(&mut self) -> Result { + loop {} + } +} + +struct JoinAll + where I: IntoIterator, + I::Item: IntoFuture, +{ + elems: Vec<::Item>, +} + +fn join_all(_: I) -> JoinAll + where I: IntoIterator, + I::Item: IntoFuture, +{ + JoinAll { elems: vec![] } +} + +impl Future for JoinAll + where I: IntoIterator, + I::Item: IntoFuture, +{ + type Item = Vec<::Item>; + type Error = ::Error; + + fn poll(&mut self) -> Result { + let elems = mem::replace(&mut self.elems, Vec::new()); + Ok(elems.into_iter().map(|e| { + e + }).collect::>()) + } +} diff --git a/src/test/ui/traits/issue-38604.rs b/src/test/ui/traits/issue-38604.rs new file mode 100644 index 000000000..002a3c43f --- /dev/null +++ b/src/test/ui/traits/issue-38604.rs @@ -0,0 +1,16 @@ +trait Q {} +trait Foo where u32: Q { + fn foo(&self); +} + +impl Q<()> for u32 {} +impl Foo for () { + fn foo(&self) { + println!("foo!"); + } +} + +fn main() { + let _f: Box = //~ ERROR `Foo` cannot be made into an object + Box::new(()); //~ ERROR `Foo` cannot be made into an object +} diff --git a/src/test/ui/traits/issue-38604.stderr b/src/test/ui/traits/issue-38604.stderr new file mode 100644 index 000000000..d41488c15 --- /dev/null +++ b/src/test/ui/traits/issue-38604.stderr @@ -0,0 +1,33 @@ +error[E0038]: the trait `Foo` cannot be made into an object + --> $DIR/issue-38604.rs:14:13 + | +LL | let _f: Box = + | ^^^^^^^^^^^^ `Foo` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-38604.rs:2:22 + | +LL | trait Foo where u32: Q { + | --- ^^^^^^^ ...because it uses `Self` as a type parameter + | | + | this trait cannot be made into an object... + +error[E0038]: the trait `Foo` cannot be made into an object + --> $DIR/issue-38604.rs:15:9 + | +LL | Box::new(()); + | ^^^^^^^^^^^^ `Foo` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-38604.rs:2:22 + | +LL | trait Foo where u32: Q { + | --- ^^^^^^^ ...because it uses `Self` as a type parameter + | | + | this trait cannot be made into an object... + = note: required because of the requirements on the impl of `CoerceUnsized>` for `Box<()>` + = note: required by cast to type `Box` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/issue-3973.rs b/src/test/ui/traits/issue-3973.rs new file mode 100644 index 000000000..a5ed5b870 --- /dev/null +++ b/src/test/ui/traits/issue-3973.rs @@ -0,0 +1,25 @@ +struct Point { + x: f64, + y: f64, +} + +trait ToString_ { + fn to_string(&self) -> String; +} + +impl ToString_ for Point { + fn new(x: f64, y: f64) -> Point { + //~^ ERROR method `new` is not a member of trait `ToString_` + Point { x: x, y: y } + } + + fn to_string(&self) -> String { + format!("({}, {})", self.x, self.y) + } +} + +fn main() { + let p = Point::new(0.0, 0.0); + //~^ ERROR no function or associated item named `new` found for struct `Point` + println!("{}", p.to_string()); +} diff --git a/src/test/ui/traits/issue-3973.stderr b/src/test/ui/traits/issue-3973.stderr new file mode 100644 index 000000000..87ee08049 --- /dev/null +++ b/src/test/ui/traits/issue-3973.stderr @@ -0,0 +1,22 @@ +error[E0407]: method `new` is not a member of trait `ToString_` + --> $DIR/issue-3973.rs:11:5 + | +LL | / fn new(x: f64, y: f64) -> Point { +LL | | +LL | | Point { x: x, y: y } +LL | | } + | |_____^ not a member of trait `ToString_` + +error[E0599]: no function or associated item named `new` found for struct `Point` in the current scope + --> $DIR/issue-3973.rs:22:20 + | +LL | struct Point { + | ------------ function or associated item `new` not found for this struct +... +LL | let p = Point::new(0.0, 0.0); + | ^^^ function or associated item not found in `Point` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0407, E0599. +For more information about an error, try `rustc --explain E0407`. diff --git a/src/test/ui/traits/issue-4107.rs b/src/test/ui/traits/issue-4107.rs new file mode 100644 index 000000000..98433e806 --- /dev/null +++ b/src/test/ui/traits/issue-4107.rs @@ -0,0 +1,26 @@ +// run-pass +#![allow(dead_code)] + +pub fn main() { + let _id: &Mat2 = &Matrix::identity(1.0); +} + +pub trait Index { fn get(&self, _: Index) -> Result { panic!() } } +pub trait Dimensional: Index { } + +pub struct Mat2 { x: T } +pub struct Vec2 { x: T } + +impl Dimensional> for Mat2 { } +impl Index> for Mat2 { } + +impl Dimensional for Vec2 { } +impl Index for Vec2 { } + +pub trait Matrix: Dimensional { + fn identity(t:T) -> Self; +} + +impl Matrix> for Mat2 { + fn identity(t:T) -> Mat2 { Mat2{ x: t } } +} diff --git a/src/test/ui/traits/issue-43132.rs b/src/test/ui/traits/issue-43132.rs new file mode 100644 index 000000000..c886f4b0a --- /dev/null +++ b/src/test/ui/traits/issue-43132.rs @@ -0,0 +1,65 @@ +// run-pass +#![allow(unused)] + +fn main() { +} + +fn foo() { + let b = mk::< + Forward<(Box>,)>, + >(); + b.map_err(|_| ()).join(); +} + +fn mk() -> T { + loop {} +} + +impl, E> Future for (I,) { + type Error = E; +} + +struct Forward { + _a: T, +} + +impl Future for Forward +where + T::Error: From, +{ + type Error = T::Error; +} + +trait Future { + type Error; + + fn map_err(self, _: F) -> (Self, F) + where + F: FnOnce(Self::Error) -> E, + Self: Sized, + { + loop {} + } + + fn join(self) -> (MaybeDone, ()) + where + Self: Sized, + { + loop {} + } +} + +impl Future for Box { + type Error = S::Error; +} + +enum MaybeDone { + _Done(A::Error), +} + +impl Future for (A, F) +where + F: FnOnce(A::Error) -> U, +{ + type Error = U; +} diff --git a/src/test/ui/traits/issue-52893.rs b/src/test/ui/traits/issue-52893.rs new file mode 100644 index 000000000..d72598d5d --- /dev/null +++ b/src/test/ui/traits/issue-52893.rs @@ -0,0 +1,57 @@ +// check-fail +// +// regression test for issue 52893 +trait At { + type AtRes; + fn at(self) -> Self::AtRes; +} + +trait Push { + type PushRes; + fn push(self, other: T) -> Self::PushRes; +} + +trait AddClass { + type AddRes; + fn init(self, func: F); +} + +trait ToRef { + type RefRes; + fn to_ref(&self) -> Self::RefRes; +} + +struct Class

(P); + +impl

Class

{ + fn with(self) -> >::AddRes + where + Self: AddClass, + { + todo!() + } + + fn from(self) -> >::AddRes + where + Self: AddClass, + { + todo!() + } +} + +impl AddClass for Class

+where + Self: At, + >::AtRes: Push, + <>::AtRes as Push>::PushRes: ToRef + Push, +{ + type AddRes = (); + + fn init(self, func: F) { + let builder = self.at().push(func); + let output = builder.to_ref(); + builder.push(output); //~ ERROR mismatched types [E0308] + } +} + +fn main() {} diff --git a/src/test/ui/traits/issue-52893.stderr b/src/test/ui/traits/issue-52893.stderr new file mode 100644 index 000000000..0ee44921b --- /dev/null +++ b/src/test/ui/traits/issue-52893.stderr @@ -0,0 +1,22 @@ +error[E0308]: mismatched types + --> $DIR/issue-52893.rs:53:22 + | +LL | impl AddClass for Class

+ | - this type parameter +... +LL | builder.push(output); + | ---- ^^^^^^ expected type parameter `F`, found struct `Class` + | | + | arguments to this function are incorrect + | + = note: expected type parameter `F` + found struct `Class

` +note: associated function defined here + --> $DIR/issue-52893.rs:11:8 + | +LL | fn push(self, other: T) -> Self::PushRes; + | ^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/traits/issue-56202.rs b/src/test/ui/traits/issue-56202.rs new file mode 100644 index 000000000..0952843e6 --- /dev/null +++ b/src/test/ui/traits/issue-56202.rs @@ -0,0 +1,17 @@ +// build-pass + +trait FooTrait {} + +trait BarTrait { + fn foo(_: T) -> Self; +} + +struct FooStruct(u32); + +impl BarTrait for FooStruct { + fn foo(_: T) -> Self { + Self(u32::default()) + } +} + +fn main() {} diff --git a/src/test/ui/traits/issue-56488.rs b/src/test/ui/traits/issue-56488.rs new file mode 100644 index 000000000..e2f399692 --- /dev/null +++ b/src/test/ui/traits/issue-56488.rs @@ -0,0 +1,13 @@ +// run-pass + +#![feature(trait_alias)] + +mod alpha { + pub trait A {} + pub trait C = A; +} + +#[allow(unused_imports)] +use alpha::C; + +fn main() {} diff --git a/src/test/ui/traits/issue-59029-1.rs b/src/test/ui/traits/issue-59029-1.rs new file mode 100644 index 000000000..8ab47a4af --- /dev/null +++ b/src/test/ui/traits/issue-59029-1.rs @@ -0,0 +1,9 @@ +#![feature(trait_alias)] + +trait Svc { type Res; } + +trait MkSvc = Svc where Self::Res: Svc; +//~^ ERROR associated type `Res` not found for `Self` +//~| ERROR associated type `Res` not found for `Self` + +fn main() {} diff --git a/src/test/ui/traits/issue-59029-1.stderr b/src/test/ui/traits/issue-59029-1.stderr new file mode 100644 index 000000000..203a89285 --- /dev/null +++ b/src/test/ui/traits/issue-59029-1.stderr @@ -0,0 +1,15 @@ +error[E0220]: associated type `Res` not found for `Self` + --> $DIR/issue-59029-1.rs:5:52 + | +LL | trait MkSvc = Svc where Self::Res: Svc; + | ^^^ there is a similarly named associated type `Res` in the trait `Svc` + +error[E0220]: associated type `Res` not found for `Self` + --> $DIR/issue-59029-1.rs:5:52 + | +LL | trait MkSvc = Svc where Self::Res: Svc; + | ^^^ there is a similarly named associated type `Res` in the trait `Svc` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0220`. diff --git a/src/test/ui/traits/issue-59029-2.rs b/src/test/ui/traits/issue-59029-2.rs new file mode 100644 index 000000000..2bdb128d8 --- /dev/null +++ b/src/test/ui/traits/issue-59029-2.rs @@ -0,0 +1,8 @@ +// run-pass +#![feature(trait_alias)] + +trait Svc { type Res; } + +trait MkSvc = Svc where >::Res: Svc; + +fn main() {} diff --git a/src/test/ui/traits/issue-6128.rs b/src/test/ui/traits/issue-6128.rs new file mode 100644 index 000000000..07d92f8f8 --- /dev/null +++ b/src/test/ui/traits/issue-6128.rs @@ -0,0 +1,22 @@ +// run-pass + +use std::collections::HashMap; + +trait Graph { + fn f(&self, _: Edge); + fn g(&self, _: Node); +} + +impl Graph for HashMap { + fn f(&self, _e: E) { + panic!(); + } + fn g(&self, _e: isize) { + panic!(); + } +} + +pub fn main() { + let g : Box> = Box::new(HashMap::new()); + let _g2 : Box> = g as Box>; +} diff --git a/src/test/ui/traits/issue-6334.rs b/src/test/ui/traits/issue-6334.rs new file mode 100644 index 000000000..acf48da15 --- /dev/null +++ b/src/test/ui/traits/issue-6334.rs @@ -0,0 +1,46 @@ +// run-pass +// Tests that everything still compiles and runs fine even when +// we reorder the bounds. + + +trait A { + fn a(&self) -> usize; +} + +trait B { + fn b(&self) -> usize; +} + +trait C { + fn combine(&self, t: &T) -> usize; +} + +struct Foo; + +impl A for Foo { + fn a(&self) -> usize { 1 } +} + +impl B for Foo { + fn b(&self) -> usize { 2 } +} + +struct Bar; + +impl C for Bar { + // Note below: bounds in impl decl are in reverse order. + fn combine(&self, t: &T) -> usize { + (t.a() * 100) + t.b() + } +} + +fn use_c(s: &S, t: &T) -> usize { + s.combine(t) +} + +pub fn main() { + let foo = Foo; + let bar = Bar; + let r = use_c(&bar, &foo); + assert_eq!(r, 102); +} diff --git a/src/test/ui/traits/issue-65284-suggest-generic-trait-bound.rs b/src/test/ui/traits/issue-65284-suggest-generic-trait-bound.rs new file mode 100644 index 000000000..018ce0459 --- /dev/null +++ b/src/test/ui/traits/issue-65284-suggest-generic-trait-bound.rs @@ -0,0 +1,11 @@ +trait Foo { + fn foo(&self); +} + +trait Bar {} + +fn do_stuff(t : T) { + t.foo() //~ ERROR no method named `foo` found +} + +fn main() {} diff --git a/src/test/ui/traits/issue-65284-suggest-generic-trait-bound.stderr b/src/test/ui/traits/issue-65284-suggest-generic-trait-bound.stderr new file mode 100644 index 000000000..ae33e61d8 --- /dev/null +++ b/src/test/ui/traits/issue-65284-suggest-generic-trait-bound.stderr @@ -0,0 +1,17 @@ +error[E0599]: no method named `foo` found for type parameter `T` in the current scope + --> $DIR/issue-65284-suggest-generic-trait-bound.rs:8:7 + | +LL | fn do_stuff(t : T) { + | - method `foo` not found for this type parameter +LL | t.foo() + | ^^^ method not found in `T` + | + = help: items from traits can only be used if the type parameter is bounded by the trait +help: the following trait defines an item `foo`, perhaps you need to restrict type parameter `T` with it: + | +LL | fn do_stuff(t : T) { + | +++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/traits/issue-65673.rs b/src/test/ui/traits/issue-65673.rs new file mode 100644 index 000000000..e5c2fccb2 --- /dev/null +++ b/src/test/ui/traits/issue-65673.rs @@ -0,0 +1,12 @@ +#![feature(trait_alias)] // Enabled to reduce stderr output, but can be triggered even if disabled. +trait Trait {} +trait WithType { + type Ctx; +} +trait Alias = where T: Trait; + +impl WithType for T { + type Ctx = dyn Alias; + //~^ ERROR at least one trait is required for an object type [E0224] +} +fn main() {} diff --git a/src/test/ui/traits/issue-65673.stderr b/src/test/ui/traits/issue-65673.stderr new file mode 100644 index 000000000..8f01d7c53 --- /dev/null +++ b/src/test/ui/traits/issue-65673.stderr @@ -0,0 +1,12 @@ +error[E0224]: at least one trait is required for an object type + --> $DIR/issue-65673.rs:9:16 + | +LL | trait Alias = where T: Trait; + | -------------- this alias does not contain a trait +... +LL | type Ctx = dyn Alias; + | ^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/traits/issue-68295.rs b/src/test/ui/traits/issue-68295.rs new file mode 100644 index 000000000..7ff54539a --- /dev/null +++ b/src/test/ui/traits/issue-68295.rs @@ -0,0 +1,47 @@ +// check-fail +// +// regression test for #68295 + +struct Matrix(R, C, S); + +impl Matrix { + fn into_owned(self) -> Matrix> + where + (): Allocator, + { + unimplemented!() + } +} + +impl Matrix { + fn hermitian_part(&self) -> Matrix> + where + (): Allocator, + { + unimplemented!() + } +} + +trait Allocator { + type Buffer; +} + +trait Trait { + type Power; +} + +impl> Trait for () { + type Power = A::Buffer; +} + +type Owned = >::Power; + +fn crash(input: Matrix) -> Matrix +where + (): Allocator, +{ + input.into_owned() + //~^ ERROR mismatched types [E0308] +} + +fn main() {} diff --git a/src/test/ui/traits/issue-68295.stderr b/src/test/ui/traits/issue-68295.stderr new file mode 100644 index 000000000..cb6e6e076 --- /dev/null +++ b/src/test/ui/traits/issue-68295.stderr @@ -0,0 +1,17 @@ +error[E0308]: mismatched types + --> $DIR/issue-68295.rs:43:5 + | +LL | fn crash(input: Matrix) -> Matrix + | ----------------- expected `Matrix` because of return type +... +LL | input.into_owned() + | ^^^^^^^^^^^^^^^^^^ expected `u32`, found associated type + | + = note: expected struct `Matrix<_, _, u32>` + found struct `Matrix<_, _, <() as Allocator>::Buffer>` + = help: consider constraining the associated type `<() as Allocator>::Buffer` to `u32` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/traits/issue-70944.rs b/src/test/ui/traits/issue-70944.rs new file mode 100644 index 000000000..3286de9d5 --- /dev/null +++ b/src/test/ui/traits/issue-70944.rs @@ -0,0 +1,23 @@ +// check-pass +// Regression test of #70944, should compile fine. + +use std::ops::Index; + +pub struct KeyA; +pub struct KeyB; +pub struct KeyC; + +pub trait Foo: Index + Index + Index {} +pub trait FooBuilder { + type Inner: Foo; + fn inner(&self) -> &Self::Inner; +} + +pub fn do_stuff(foo: &impl FooBuilder) { + let inner = foo.inner(); + &inner[KeyA]; + &inner[KeyB]; + &inner[KeyC]; +} + +fn main() {} diff --git a/src/test/ui/traits/issue-71036.rs b/src/test/ui/traits/issue-71036.rs new file mode 100644 index 000000000..3d2df6fe9 --- /dev/null +++ b/src/test/ui/traits/issue-71036.rs @@ -0,0 +1,17 @@ +#![feature(unsize, dispatch_from_dyn)] + +use std::marker::Unsize; +use std::ops::DispatchFromDyn; + +#[allow(unused)] +struct Foo<'a, T: ?Sized> { + _inner: &'a &'a T, +} + +impl<'a, T: ?Sized + Unsize, U: ?Sized> DispatchFromDyn> for Foo<'a, T> {} +//~^ ERROR the trait bound `&'a T: Unsize<&'a U>` is not satisfied +//~| NOTE the trait `Unsize<&'a U>` is not implemented for `&'a T` +//~| NOTE all implementations of `Unsize` are provided automatically by the compiler +//~| NOTE required because of the requirements on the impl + +fn main() {} diff --git a/src/test/ui/traits/issue-71036.stderr b/src/test/ui/traits/issue-71036.stderr new file mode 100644 index 000000000..3ee6db40e --- /dev/null +++ b/src/test/ui/traits/issue-71036.stderr @@ -0,0 +1,12 @@ +error[E0277]: the trait bound `&'a T: Unsize<&'a U>` is not satisfied + --> $DIR/issue-71036.rs:11:1 + | +LL | impl<'a, T: ?Sized + Unsize, U: ?Sized> DispatchFromDyn> for Foo<'a, T> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Unsize<&'a U>` is not implemented for `&'a T` + | + = note: all implementations of `Unsize` are provided automatically by the compiler, see for more information + = note: required because of the requirements on the impl of `DispatchFromDyn<&'a &'a U>` for `&'a &'a T` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/issue-71136.rs b/src/test/ui/traits/issue-71136.rs new file mode 100644 index 000000000..b21756e2b --- /dev/null +++ b/src/test/ui/traits/issue-71136.rs @@ -0,0 +1,8 @@ +struct Foo(u8); + +#[derive(Clone)] +struct FooHolster { + the_foos: Vec, //~ERROR Clone +} + +fn main() {} diff --git a/src/test/ui/traits/issue-71136.stderr b/src/test/ui/traits/issue-71136.stderr new file mode 100644 index 000000000..62a2a64ed --- /dev/null +++ b/src/test/ui/traits/issue-71136.stderr @@ -0,0 +1,19 @@ +error[E0277]: the trait bound `Foo: Clone` is not satisfied + --> $DIR/issue-71136.rs:5:5 + | +LL | #[derive(Clone)] + | ----- in this derive macro expansion +LL | struct FooHolster { +LL | the_foos: Vec, + | ^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `Foo` + | + = note: required because of the requirements on the impl of `Clone` for `Vec` + = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info) +help: consider annotating `Foo` with `#[derive(Clone)]` + | +LL | #[derive(Clone)] + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/issue-72410.rs b/src/test/ui/traits/issue-72410.rs new file mode 100644 index 000000000..c95f1dfdc --- /dev/null +++ b/src/test/ui/traits/issue-72410.rs @@ -0,0 +1,18 @@ +// Regression test for #72410, this should be used with debug assertion enabled. + +// should be fine +pub trait Foo { + fn map() + where + Self: Sized, + for<'a> &'a mut [u8]: ; +} + +// should fail +pub trait Bar { + fn map() + where for<'a> &'a mut [dyn Bar]: ; + //~^ ERROR: the trait `Bar` cannot be made into an object +} + +fn main() {} diff --git a/src/test/ui/traits/issue-72410.stderr b/src/test/ui/traits/issue-72410.stderr new file mode 100644 index 000000000..c7beb834b --- /dev/null +++ b/src/test/ui/traits/issue-72410.stderr @@ -0,0 +1,25 @@ +error[E0038]: the trait `Bar` cannot be made into an object + --> $DIR/issue-72410.rs:14:19 + | +LL | where for<'a> &'a mut [dyn Bar]: ; + | ^^^^^^^^^^^^^^^^^ `Bar` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-72410.rs:13:8 + | +LL | pub trait Bar { + | --- this trait cannot be made into an object... +LL | fn map() + | ^^^ ...because associated function `map` has no `self` parameter +help: consider turning `map` into a method by giving it a `&self` argument + | +LL | fn map(&self) + | +++++ +help: alternatively, consider constraining `map` so it does not apply to trait objects + | +LL | where for<'a> &'a mut [dyn Bar]:, Self: Sized ; + | +++++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/issue-72455.rs b/src/test/ui/traits/issue-72455.rs new file mode 100644 index 000000000..b6c3bb222 --- /dev/null +++ b/src/test/ui/traits/issue-72455.rs @@ -0,0 +1,27 @@ +// check-pass + +pub trait ResultExt { + type Ok; + fn err_eprint_and_ignore(self) -> Option; +} + +impl ResultExt for std::result::Result +where + E: std::error::Error, +{ + type Ok = O; + fn err_eprint_and_ignore(self) -> Option + where + Self: , + { + match self { + Err(e) => { + eprintln!("{}", e); + None + } + Ok(o) => Some(o), + } + } +} + +fn main() {} diff --git a/src/test/ui/traits/issue-75627.rs b/src/test/ui/traits/issue-75627.rs new file mode 100644 index 000000000..93a2ec1cc --- /dev/null +++ b/src/test/ui/traits/issue-75627.rs @@ -0,0 +1,6 @@ +struct Foo(T, *const ()); + +unsafe impl Send for Foo {} +//~^ ERROR cannot find type + +fn main() {} diff --git a/src/test/ui/traits/issue-75627.stderr b/src/test/ui/traits/issue-75627.stderr new file mode 100644 index 000000000..432ddf2dc --- /dev/null +++ b/src/test/ui/traits/issue-75627.stderr @@ -0,0 +1,11 @@ +error[E0412]: cannot find type `T` in this scope + --> $DIR/issue-75627.rs:3:26 + | +LL | unsafe impl Send for Foo {} + | - ^ not found in this scope + | | + | help: you might be missing a type parameter: `` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0412`. diff --git a/src/test/ui/traits/issue-77982.rs b/src/test/ui/traits/issue-77982.rs new file mode 100644 index 000000000..f5be6cf21 --- /dev/null +++ b/src/test/ui/traits/issue-77982.rs @@ -0,0 +1,41 @@ +use std::collections::HashMap; + +fn what() { + let descr = String::new(); + let mut opts = HashMap::::new(); + let opt = String::new(); + + opts.get(opt.as_ref()); //~ ERROR type annotations needed + //~^ ERROR type annotations needed +} + +fn main() { + let ips: Vec<_> = (0..100_000).map(|_| u32::from(0u32.into())).collect(); + //~^ ERROR type annotations needed +} + +trait Foo<'a, T: ?Sized> { + fn foo(&self) -> Box { + todo!() + } +} + +trait Bar<'a, T: ?Sized> { + fn bar(&self) -> Box { + todo!() + } +} + +impl Foo<'static, u32> for () {} +impl<'a> Foo<'a, i16> for () {} + +impl<'a> Bar<'static, u32> for &'a () {} +impl<'a> Bar<'a, i16> for &'a () {} + +fn foo() { + let _ = ().foo(); //~ ERROR type annotations needed +} + +fn bar() { + let _ = (&()).bar(); //~ ERROR type annotations needed +} diff --git a/src/test/ui/traits/issue-77982.stderr b/src/test/ui/traits/issue-77982.stderr new file mode 100644 index 000000000..2b832e27c --- /dev/null +++ b/src/test/ui/traits/issue-77982.stderr @@ -0,0 +1,96 @@ +error[E0283]: type annotations needed + --> $DIR/issue-77982.rs:8:10 + | +LL | opts.get(opt.as_ref()); + | ^^^ cannot infer type of the type parameter `Q` declared on the associated function `get` + | + = note: multiple `impl`s satisfying `String: Borrow<_>` found in the following crates: `alloc`, `core`: + - impl Borrow for String; + - impl Borrow for T + where T: ?Sized; +note: required by a bound in `HashMap::::get` + --> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL + | +LL | K: Borrow, + | ^^^^^^^^^ required by this bound in `HashMap::::get` +help: consider specifying the type argument in the function call + | +LL | opts.get::(opt.as_ref()); + | +++++ + +error[E0283]: type annotations needed + --> $DIR/issue-77982.rs:8:10 + | +LL | opts.get(opt.as_ref()); + | ^^^ ------ type must be known at this point + | | + | cannot infer type of the type parameter `Q` declared on the associated function `get` + | + = note: multiple `impl`s satisfying `String: AsRef<_>` found in the following crates: `alloc`, `std`: + - impl AsRef for String; + - impl AsRef for String; + - impl AsRef<[u8]> for String; + - impl AsRef for String; +help: consider specifying the generic argument + | +LL | opts.get::(opt.as_ref()); + | +++++ + +error[E0283]: type annotations needed + --> $DIR/issue-77982.rs:13:59 + | +LL | let ips: Vec<_> = (0..100_000).map(|_| u32::from(0u32.into())).collect(); + | --------- ^^^^ + | | + | type must be known at this point + | + = note: multiple `impl`s satisfying `u32: From<_>` found in the following crates: `core`, `std`: + - impl From for u32; + - impl From for u32; + - impl From for u32; + - impl From for u32; + and 3 more +help: try using a fully qualified path to specify the expected types + | +LL | let ips: Vec<_> = (0..100_000).map(|_| u32::from(>::into(0u32))).collect(); + | +++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed for `Box` + --> $DIR/issue-77982.rs:36:9 + | +LL | let _ = ().foo(); + | ^ --- type must be known at this point + | +note: multiple `impl`s satisfying `(): Foo<'_, _>` found + --> $DIR/issue-77982.rs:29:1 + | +LL | impl Foo<'static, u32> for () {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | impl<'a> Foo<'a, i16> for () {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider giving this pattern a type, where the type for type parameter `T` is specified + | +LL | let _: Box = ().foo(); + | ++++++++ + +error[E0283]: type annotations needed for `Box` + --> $DIR/issue-77982.rs:40:9 + | +LL | let _ = (&()).bar(); + | ^ --- type must be known at this point + | +note: multiple `impl`s satisfying `&(): Bar<'_, _>` found + --> $DIR/issue-77982.rs:32:1 + | +LL | impl<'a> Bar<'static, u32> for &'a () {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | impl<'a> Bar<'a, i16> for &'a () {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider giving this pattern a type, where the type for type parameter `T` is specified + | +LL | let _: Box = (&()).bar(); + | ++++++++ + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0283`. diff --git a/src/test/ui/traits/issue-78372.rs b/src/test/ui/traits/issue-78372.rs new file mode 100644 index 000000000..92f9f4b46 --- /dev/null +++ b/src/test/ui/traits/issue-78372.rs @@ -0,0 +1,13 @@ +use std::ops::DispatchFromDyn; //~ ERROR use of unstable library feature 'dispatch_from_dyn' +struct Smaht(PhantomData); //~ ERROR cannot find type `PhantomData` in this scope +impl DispatchFromDyn> for T {} //~ ERROR cannot find type `U` in this scope +//~^ ERROR cannot find type `MISC` in this scope +//~| ERROR use of unstable library feature 'dispatch_from_dyn' +//~| ERROR the trait `DispatchFromDyn` may only be implemented for a coercion between structures +trait Foo: X {} +trait X { + fn foo(self: Smaht); +} +trait Marker {} +impl Marker for dyn Foo {} +fn main() {} diff --git a/src/test/ui/traits/issue-78372.stderr b/src/test/ui/traits/issue-78372.stderr new file mode 100644 index 000000000..7e781016e --- /dev/null +++ b/src/test/ui/traits/issue-78372.stderr @@ -0,0 +1,62 @@ +error[E0412]: cannot find type `PhantomData` in this scope + --> $DIR/issue-78372.rs:2:23 + | +LL | struct Smaht(PhantomData); + | ^^^^^^^^^^^ not found in this scope + | +help: consider importing this struct + | +LL | use std::marker::PhantomData; + | + +error[E0412]: cannot find type `U` in this scope + --> $DIR/issue-78372.rs:3:31 + | +LL | impl DispatchFromDyn> for T {} + | - ^ + | | + | similarly named type parameter `T` defined here + | +help: a type parameter with a similar name exists + | +LL | impl DispatchFromDyn> for T {} + | ~ +help: you might be missing a type parameter + | +LL | impl DispatchFromDyn> for T {} + | +++ + +error[E0412]: cannot find type `MISC` in this scope + --> $DIR/issue-78372.rs:3:34 + | +LL | impl DispatchFromDyn> for T {} + | - ^^^^ not found in this scope + | | + | help: you might be missing a type parameter: `, MISC` + +error[E0658]: use of unstable library feature 'dispatch_from_dyn' + --> $DIR/issue-78372.rs:1:5 + | +LL | use std::ops::DispatchFromDyn; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add `#![feature(dispatch_from_dyn)]` to the crate attributes to enable + +error[E0658]: use of unstable library feature 'dispatch_from_dyn' + --> $DIR/issue-78372.rs:3:9 + | +LL | impl DispatchFromDyn> for T {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add `#![feature(dispatch_from_dyn)]` to the crate attributes to enable + +error[E0378]: the trait `DispatchFromDyn` may only be implemented for a coercion between structures + --> $DIR/issue-78372.rs:3:1 + | +LL | impl DispatchFromDyn> for T {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0378, E0412, E0658. +For more information about an error, try `rustc --explain E0378`. diff --git a/src/test/ui/traits/issue-78632.rs b/src/test/ui/traits/issue-78632.rs new file mode 100644 index 000000000..c72a2aef4 --- /dev/null +++ b/src/test/ui/traits/issue-78632.rs @@ -0,0 +1,59 @@ +// check-pass +// +// Regression test for issue #78632 + +#![crate_type = "lib"] + +pub trait Corge { + type Fred; +} + +impl Corge for () { + type Fred = u32; +} + +pub trait Waldo { + type Quax; +} + +impl Waldo for u32 { + type Quax = u8; +} + +pub trait Grault +where + (): Corge, +{ + type Thud; + fn bar(_: <() as Corge>::Fred) {} +} + +impl Grault for T +where + T: Waldo, + (): Corge, + <() as Corge>::Fred: Waldo, +{ + type Thud = u8; +} + +pub trait Plugh { + fn baz(); +} + +#[derive(Copy, Clone, Debug)] +pub struct Qiz { + foo: T, +} + +impl Plugh<<() as Corge>::Fred> for Qiz +where + T: Grault, + (): Corge, +{ + fn baz() {} +} + +pub fn test() { + as Plugh>::baz(); +} diff --git a/src/test/ui/traits/issue-79458.rs b/src/test/ui/traits/issue-79458.rs new file mode 100644 index 000000000..a41add6a1 --- /dev/null +++ b/src/test/ui/traits/issue-79458.rs @@ -0,0 +1,10 @@ +// Negative implementations should not be shown in trait suggestions. +// This is a regression test of #79458. + +#[derive(Clone)] +struct Foo<'a, T> { + bar: &'a mut T + //~^ ERROR the trait bound `&mut T: Clone` is not satisfied +} + +fn main() {} diff --git a/src/test/ui/traits/issue-79458.stderr b/src/test/ui/traits/issue-79458.stderr new file mode 100644 index 000000000..cf2e4edf9 --- /dev/null +++ b/src/test/ui/traits/issue-79458.stderr @@ -0,0 +1,19 @@ +error[E0277]: the trait bound `&mut T: Clone` is not satisfied + --> $DIR/issue-79458.rs:6:5 + | +LL | #[derive(Clone)] + | ----- in this derive macro expansion +LL | struct Foo<'a, T> { +LL | bar: &'a mut T + | ^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `&mut T` + | + = help: the following other types implement trait `Clone`: + &T + *const T + *mut T + = note: `Clone` is implemented for `&T`, but not for `&mut T` + = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/issue-8153.rs b/src/test/ui/traits/issue-8153.rs new file mode 100644 index 000000000..81a8f5338 --- /dev/null +++ b/src/test/ui/traits/issue-8153.rs @@ -0,0 +1,16 @@ +// Test that duplicate methods in impls are not allowed + +struct Foo; + +trait Bar { + fn bar(&self) -> isize; +} + +impl Bar for Foo { + fn bar(&self) -> isize {1} + fn bar(&self) -> isize {2} //~ ERROR duplicate definitions +} + +fn main() { + println!("{}", Foo.bar()); +} diff --git a/src/test/ui/traits/issue-8153.stderr b/src/test/ui/traits/issue-8153.stderr new file mode 100644 index 000000000..b76bbc023 --- /dev/null +++ b/src/test/ui/traits/issue-8153.stderr @@ -0,0 +1,11 @@ +error[E0201]: duplicate definitions with name `bar`: + --> $DIR/issue-8153.rs:11:5 + | +LL | fn bar(&self) -> isize {1} + | ---------------------- previous definition of `bar` here +LL | fn bar(&self) -> isize {2} + | ^^^^^^^^^^^^^^^^^^^^^^ duplicate definition + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0201`. diff --git a/src/test/ui/traits/issue-82830.rs b/src/test/ui/traits/issue-82830.rs new file mode 100644 index 000000000..c8289b2e3 --- /dev/null +++ b/src/test/ui/traits/issue-82830.rs @@ -0,0 +1,16 @@ +trait A { + type B; +} + +type MaybeBox = >>::B; +struct P { + t: MaybeBox

, //~ ERROR: overflow evaluating the requirement `P: Sized` +} + +impl A for P { + type B = N; +} + +fn main() { + let t: MaybeBox

; +} diff --git a/src/test/ui/traits/issue-82830.stderr b/src/test/ui/traits/issue-82830.stderr new file mode 100644 index 000000000..f863143c7 --- /dev/null +++ b/src/test/ui/traits/issue-82830.stderr @@ -0,0 +1,15 @@ +error[E0275]: overflow evaluating the requirement `P: Sized` + --> $DIR/issue-82830.rs:7:8 + | +LL | t: MaybeBox

) { + obj.is_derived(); + Base::::is_base(obj); + Base::<()>::is_base(obj); +} + +fn main() { + let x: fn(_) = f::<()>; + let x: fn(_) = f::; +} diff --git a/src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.rs b/src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.rs new file mode 100644 index 000000000..538e74ee1 --- /dev/null +++ b/src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.rs @@ -0,0 +1,24 @@ +struct A(T); +struct B; + +trait I {} +impl I for B {} +impl I for B {} + +trait V { + fn method(self) -> U; +} + +impl V for A +where + T: I, +{ + fn method(self) -> U { unimplemented!() } +} + +fn main() { + let a = A(B); + a.method(); + //~^ ERROR type annotations needed + //~| ERROR type annotations needed +} diff --git a/src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.stderr b/src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.stderr new file mode 100644 index 000000000..75d45d905 --- /dev/null +++ b/src/test/ui/traits/not-suggest-non-existing-fully-qualified-path.stderr @@ -0,0 +1,38 @@ +error[E0282]: type annotations needed + --> $DIR/not-suggest-non-existing-fully-qualified-path.rs:21:7 + | +LL | a.method(); + | ^^^^^^ + | +help: try using a fully qualified path to specify the expected types + | +LL | as V>::method(a); + | +++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/not-suggest-non-existing-fully-qualified-path.rs:21:7 + | +LL | a.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `B: I<_>` found + --> $DIR/not-suggest-non-existing-fully-qualified-path.rs:5:1 + | +LL | impl I for B {} + | ^^^^^^^^^^^^^^^^^ +LL | impl I for B {} + | ^^^^^^^^^^^^^^^^^ +note: required because of the requirements on the impl of `V<_>` for `A` + --> $DIR/not-suggest-non-existing-fully-qualified-path.rs:12:12 + | +LL | impl V for A + | ^^^^ ^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | as V>::method(a); + | +++++++++++++++++++++++ ~ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0282, E0283. +For more information about an error, try `rustc --explain E0282`. diff --git a/src/test/ui/traits/object-does-not-impl-trait.rs b/src/test/ui/traits/object-does-not-impl-trait.rs new file mode 100644 index 000000000..b3b679813 --- /dev/null +++ b/src/test/ui/traits/object-does-not-impl-trait.rs @@ -0,0 +1,8 @@ +// Test that an object type `Box` is not considered to implement the +// trait `Foo`. Issue #5087. + +trait Foo {} +fn take_foo(f: F) {} +fn take_object(f: Box) { take_foo(f); } +//~^ ERROR `Box: Foo` is not satisfied +fn main() {} diff --git a/src/test/ui/traits/object-does-not-impl-trait.stderr b/src/test/ui/traits/object-does-not-impl-trait.stderr new file mode 100644 index 000000000..f1dd508a4 --- /dev/null +++ b/src/test/ui/traits/object-does-not-impl-trait.stderr @@ -0,0 +1,17 @@ +error[E0277]: the trait bound `Box: Foo` is not satisfied + --> $DIR/object-does-not-impl-trait.rs:6:44 + | +LL | fn take_object(f: Box) { take_foo(f); } + | -------- ^ the trait `Foo` is not implemented for `Box` + | | + | required by a bound introduced by this call + | +note: required by a bound in `take_foo` + --> $DIR/object-does-not-impl-trait.rs:5:15 + | +LL | fn take_foo(f: F) {} + | ^^^ required by this bound in `take_foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/object-one-type-two-traits.rs b/src/test/ui/traits/object-one-type-two-traits.rs new file mode 100644 index 000000000..86a2094ee --- /dev/null +++ b/src/test/ui/traits/object-one-type-two-traits.rs @@ -0,0 +1,31 @@ +// run-pass +#![allow(dead_code)] +#![allow(unused_variables)] +// Testing creating two vtables with the same self type, but different +// traits. + +use std::any::Any; + +trait Wrap { + fn get(&self) -> isize; + fn wrap(self: Box) -> Box; +} + +impl Wrap for isize { + fn get(&self) -> isize { + *self + } + fn wrap(self: Box) -> Box { + self as Box + } +} + +fn is(x: &dyn Any) -> bool { + x.is::() +} + +fn main() { + let x = Box::new(22isize) as Box; + println!("x={}", x.get()); + let y = x.wrap(); +} diff --git a/src/test/ui/traits/object/auto-dedup-in-impl.rs b/src/test/ui/traits/object/auto-dedup-in-impl.rs new file mode 100644 index 000000000..85698f194 --- /dev/null +++ b/src/test/ui/traits/object/auto-dedup-in-impl.rs @@ -0,0 +1,19 @@ +// Checks to make sure that `dyn Trait + Send` and `dyn Trait + Send + Send` are the same type. +// Issue: #47010 + +struct Struct; +impl Trait for Struct {} +trait Trait {} + +type Send1 = dyn Trait + Send; +type Send2 = dyn Trait + Send + Send; + +fn main () {} + +impl dyn Trait + Send { + fn test(&self) { println!("one"); } //~ ERROR duplicate definitions with name `test` +} + +impl dyn Trait + Send + Send { + fn test(&self) { println!("two"); } +} diff --git a/src/test/ui/traits/object/auto-dedup-in-impl.stderr b/src/test/ui/traits/object/auto-dedup-in-impl.stderr new file mode 100644 index 000000000..5f13c7813 --- /dev/null +++ b/src/test/ui/traits/object/auto-dedup-in-impl.stderr @@ -0,0 +1,12 @@ +error[E0592]: duplicate definitions with name `test` + --> $DIR/auto-dedup-in-impl.rs:14:5 + | +LL | fn test(&self) { println!("one"); } + | ^^^^^^^^^^^^^^ duplicate definitions for `test` +... +LL | fn test(&self) { println!("two"); } + | -------------- other definition for `test` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0592`. diff --git a/src/test/ui/traits/object/auto-dedup.rs b/src/test/ui/traits/object/auto-dedup.rs new file mode 100644 index 000000000..39d25eb7f --- /dev/null +++ b/src/test/ui/traits/object/auto-dedup.rs @@ -0,0 +1,46 @@ +// run-pass + +#![allow(unused_assignments)] + +// Test that duplicate auto trait bounds in trait objects don't create new types. +#[allow(unused_assignments)] +use std::marker::Send as SendAlias; + +// A dummy trait for the non-auto trait. +trait Trait {} + +// A dummy struct to implement `Trait` and `Send`. +struct Struct; + +impl Trait for Struct {} + +// These three functions should be equivalent. +fn takes_dyn_trait_send(_: Box) {} +fn takes_dyn_trait_send_send(_: Box) {} +fn takes_dyn_trait_send_sendalias(_: Box) {} + +impl dyn Trait + Send + Send { + fn do_nothing(&self) {} +} + +fn main() { + // 1. Moving into a variable with more `Send`s and back. + let mut dyn_trait_send = Box::new(Struct) as Box; + let dyn_trait_send_send: Box = dyn_trait_send; + dyn_trait_send = dyn_trait_send_send; + + // 2. Calling methods with different number of `Send`s. + let dyn_trait_send = Box::new(Struct) as Box; + takes_dyn_trait_send_send(dyn_trait_send); + + let dyn_trait_send_send = Box::new(Struct) as Box; + takes_dyn_trait_send(dyn_trait_send_send); + + // 3. Aliases to the trait are transparent. + let dyn_trait_send = Box::new(Struct) as Box; + takes_dyn_trait_send_sendalias(dyn_trait_send); + + // 4. Calling an impl that duplicates an auto trait. + let dyn_trait_send = Box::new(Struct) as Box; + dyn_trait_send.do_nothing(); +} diff --git a/src/test/ui/traits/object/bounds-cycle-1.rs b/src/test/ui/traits/object/bounds-cycle-1.rs new file mode 100644 index 000000000..314676492 --- /dev/null +++ b/src/test/ui/traits/object/bounds-cycle-1.rs @@ -0,0 +1,24 @@ +// Check that we don't have a cycle when we try to normalize `Self::U` in the +// bound below. + +// check-pass + +trait Is { + type T; +} + +impl Is for U { + type T = U; +} + +trait Obj { + type U: Is; +} + +fn is_obj(_: &T) {} + +fn f(x: &dyn Obj) { + is_obj(x) +} + +fn main() {} diff --git a/src/test/ui/traits/object/bounds-cycle-2.rs b/src/test/ui/traits/object/bounds-cycle-2.rs new file mode 100644 index 000000000..4c1df3805 --- /dev/null +++ b/src/test/ui/traits/object/bounds-cycle-2.rs @@ -0,0 +1,28 @@ +// Check that we don't have a cycle when we try to normalize `Self::V` in the +// bound below. + +// check-pass + +trait Is { + type T; +} + +impl Is for U { + type T = U; +} + +trait Super { + type V; +} + +trait Obj: Super { + type U: Is; +} + +fn is_obj(_: &T) {} + +fn f(x: &dyn Obj) { + is_obj(x) +} + +fn main() {} diff --git a/src/test/ui/traits/object/bounds-cycle-3.rs b/src/test/ui/traits/object/bounds-cycle-3.rs new file mode 100644 index 000000000..55726a5ae --- /dev/null +++ b/src/test/ui/traits/object/bounds-cycle-3.rs @@ -0,0 +1,25 @@ +// Check that we don't have a cycle when we try to normalize `Self::V` in the +// bound below. + +// check-pass + +trait Is { + type T; +} + +impl Is for U { + type T = U; +} + +trait Obj { + type U: Is; + type V; +} + +fn is_obj(_: &T) {} + +fn f(x: &dyn Obj) { + is_obj(x) +} + +fn main() {} diff --git a/src/test/ui/traits/object/bounds-cycle-4.rs b/src/test/ui/traits/object/bounds-cycle-4.rs new file mode 100644 index 000000000..f83cb75c7 --- /dev/null +++ b/src/test/ui/traits/object/bounds-cycle-4.rs @@ -0,0 +1,25 @@ +// Check that we don't have a cycle when we try to normalize `Self::U` in the +// bound below. Make sure that having a lifetime on the trait object doesn't break things + +// check-pass + +trait Is { + type T; +} + +impl Is for U { + type T = U; +} + +trait Obj<'a> { + type U: Is; + type V; +} + +fn is_obj<'a, T: ?Sized + Obj<'a>>(_: &T) {} + +fn f<'a>(x: &dyn Obj<'a, U = i32, V = i32>) { + is_obj(x) +} + +fn main() {} diff --git a/src/test/ui/traits/object/enforce-supertrait-projection.rs b/src/test/ui/traits/object/enforce-supertrait-projection.rs new file mode 100644 index 000000000..2c9b41eea --- /dev/null +++ b/src/test/ui/traits/object/enforce-supertrait-projection.rs @@ -0,0 +1,24 @@ +trait SuperTrait { + type A; + type B; +} + +trait Trait: SuperTrait::B> {} + +fn transmute(x: A) -> B { + foo::>(x) + //~^ ERROR type mismatch resolving ` as SuperTrait>::A == B` +} + +fn foo(x: T::A) -> B +where + T: Trait, +{ + x +} + +static X: u8 = 0; +fn main() { + let x = transmute::<&u8, &[u8; 1_000_000]>(&X); + println!("{:?}", x[100_000]); +} diff --git a/src/test/ui/traits/object/enforce-supertrait-projection.stderr b/src/test/ui/traits/object/enforce-supertrait-projection.stderr new file mode 100644 index 000000000..eab42ca56 --- /dev/null +++ b/src/test/ui/traits/object/enforce-supertrait-projection.stderr @@ -0,0 +1,26 @@ +error[E0271]: type mismatch resolving ` as SuperTrait>::A == B` + --> $DIR/enforce-supertrait-projection.rs:9:5 + | +LL | fn transmute(x: A) -> B { + | - - expected type parameter + | | + | found type parameter +LL | foo::>(x) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `B`, found type parameter `A` + | + = note: expected type parameter `B` + found type parameter `A` + = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound + = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters +note: required by a bound in `foo` + --> $DIR/enforce-supertrait-projection.rs:15:8 + | +LL | fn foo(x: T::A) -> B + | --- required by a bound in this +LL | where +LL | T: Trait, + | ^^^^^^^^^^^^ required by this bound in `foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0271`. diff --git a/src/test/ui/traits/object/exclusion.rs b/src/test/ui/traits/object/exclusion.rs new file mode 100644 index 000000000..766dceeaf --- /dev/null +++ b/src/test/ui/traits/object/exclusion.rs @@ -0,0 +1,19 @@ +// run-pass +trait Future: 'static { + // The requirement for Self: Sized must prevent instantiation of + // Future::forget in vtables, otherwise there's an infinite type + // recursion through as Future>::forget. + fn forget(self) where Self: Sized { + Box::new(Map(self)) as Box; + } +} + +struct Map(#[allow(unused_tuple_struct_fields)] A); +impl Future for Map {} + +pub struct Promise; +impl Future for Promise {} + +fn main() { + Promise.forget(); +} diff --git a/src/test/ui/traits/object/generics.rs b/src/test/ui/traits/object/generics.rs new file mode 100644 index 000000000..5a4a6aecc --- /dev/null +++ b/src/test/ui/traits/object/generics.rs @@ -0,0 +1,41 @@ +// run-pass +// test for #8664 + +use std::marker; + +pub trait Trait2 { + fn doit(&self) -> A; +} + +pub struct Impl { + m1: marker::PhantomData<(A1,A2,A3)>, + /* + * With A2 we get the ICE: + * task failed at 'index out of bounds: the len is 1 but the index is 1', + * src/librustc/middle/subst.rs:58 + */ + t: Box+'static> +} + +impl Impl { + pub fn step(&self) { + self.t.doit(); + } +} + +// test for #8601 + +enum Type { Constant(#[allow(unused_tuple_struct_fields)] T) } + +trait Trait { + fn method(&self, _: Type<(K,V)>) -> isize; +} + +impl Trait for () { + fn method(&self, _x: Type<(u8,V)>) -> isize { 0 } +} + +pub fn main() { + let a = Box::new(()) as Box>; + assert_eq!(a.method(Type::Constant((1, 2))), 0); +} diff --git a/src/test/ui/traits/object/issue-33140-traitobject-crate.rs b/src/test/ui/traits/object/issue-33140-traitobject-crate.rs new file mode 100644 index 000000000..8abd92da3 --- /dev/null +++ b/src/test/ui/traits/object/issue-33140-traitobject-crate.rs @@ -0,0 +1,108 @@ +// check-pass + +#![warn(order_dependent_trait_objects)] +#![allow(dyn_drop)] + +// Check that traitobject 0.1.0 compiles + +//! # traitobject +//! +//! Unsafe helpers for working with raw TraitObjects. + +/// A trait implemented for all trait objects. +/// +/// Implementations for all traits in std are provided. +pub unsafe trait Trait {} + +unsafe impl Trait for dyn (::std::any::Any) + Send { } +unsafe impl Trait for dyn (::std::any::Any) + Sync { } +unsafe impl Trait for dyn (::std::any::Any) + Send + Sync { } +unsafe impl Trait for dyn (::std::borrow::Borrow) + Send { } +unsafe impl Trait for dyn (::std::borrow::Borrow) + Sync { } +unsafe impl Trait for dyn (::std::borrow::Borrow) + Send + Sync { } +unsafe impl Trait for dyn (::std::borrow::BorrowMut) + Send { } +unsafe impl Trait for dyn (::std::borrow::BorrowMut) + Sync { } +unsafe impl Trait for dyn (::std::borrow::BorrowMut) + Send + Sync { } +unsafe impl Trait for dyn (::std::convert::AsMut) + Send { } +unsafe impl Trait for dyn (::std::convert::AsMut) + Sync { } +unsafe impl Trait for dyn (::std::convert::AsMut) + Send + Sync { } +unsafe impl Trait for dyn (::std::convert::AsRef) + Send { } +unsafe impl Trait for dyn (::std::convert::AsRef) + Sync { } +unsafe impl Trait for dyn (::std::convert::AsRef) + Send + Sync { } +unsafe impl Trait for dyn (::std::error::Error) + Send { } +unsafe impl Trait for dyn (::std::error::Error) + Sync { } +unsafe impl Trait for dyn (::std::error::Error) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::Binary) + Send { } +unsafe impl Trait for dyn (::std::fmt::Binary) + Sync { } +unsafe impl Trait for dyn (::std::fmt::Binary) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::Debug) + Send { } +unsafe impl Trait for dyn (::std::fmt::Debug) + Sync { } +unsafe impl Trait for dyn (::std::fmt::Debug) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::Display) + Send { } +unsafe impl Trait for dyn (::std::fmt::Display) + Sync { } +unsafe impl Trait for dyn (::std::fmt::Display) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::LowerExp) + Send { } +unsafe impl Trait for dyn (::std::fmt::LowerExp) + Sync { } +unsafe impl Trait for dyn (::std::fmt::LowerExp) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::LowerHex) + Send { } +unsafe impl Trait for dyn (::std::fmt::LowerHex) + Sync { } +unsafe impl Trait for dyn (::std::fmt::LowerHex) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::Octal) + Send { } +unsafe impl Trait for dyn (::std::fmt::Octal) + Sync { } +unsafe impl Trait for dyn (::std::fmt::Octal) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::Pointer) + Send { } +unsafe impl Trait for dyn (::std::fmt::Pointer) + Sync { } +unsafe impl Trait for dyn (::std::fmt::Pointer) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::UpperExp) + Send { } +unsafe impl Trait for dyn (::std::fmt::UpperExp) + Sync { } +unsafe impl Trait for dyn (::std::fmt::UpperExp) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::UpperHex) + Send { } +unsafe impl Trait for dyn (::std::fmt::UpperHex) + Sync { } +unsafe impl Trait for dyn (::std::fmt::UpperHex) + Send + Sync { } +unsafe impl Trait for dyn (::std::fmt::Write) + Send { } +unsafe impl Trait for dyn (::std::fmt::Write) + Sync { } +unsafe impl Trait for dyn (::std::fmt::Write) + Send + Sync { } +unsafe impl Trait for dyn (::std::hash::Hasher) + Send { } +unsafe impl Trait for dyn (::std::hash::Hasher) + Sync { } +unsafe impl Trait for dyn (::std::hash::Hasher) + Send + Sync { } +unsafe impl Trait for dyn (::std::io::BufRead) + Send { } +unsafe impl Trait for dyn (::std::io::BufRead) + Sync { } +unsafe impl Trait for dyn (::std::io::BufRead) + Send + Sync { } +unsafe impl Trait for dyn (::std::io::Read) + Send { } +unsafe impl Trait for dyn (::std::io::Read) + Sync { } +unsafe impl Trait for dyn (::std::io::Read) + Send + Sync { } +unsafe impl Trait for dyn (::std::io::Seek) + Send { } +unsafe impl Trait for dyn (::std::io::Seek) + Sync { } +unsafe impl Trait for dyn (::std::io::Seek) + Send + Sync { } +unsafe impl Trait for dyn (::std::io::Write) + Send { } +unsafe impl Trait for dyn (::std::io::Write) + Sync { } +unsafe impl Trait for dyn (::std::io::Write) + Send + Sync { } +unsafe impl Trait for dyn (::std::iter::IntoIterator) { } +unsafe impl Trait for dyn (::std::iter::Iterator) + Send { } +unsafe impl Trait for dyn (::std::iter::Iterator) + Sync { } +unsafe impl Trait for dyn (::std::iter::Iterator) + Send + Sync { } +unsafe impl Trait for dyn (::std::marker::Send) + Send { } +unsafe impl Trait for dyn (::std::marker::Send) + Sync { } +unsafe impl Trait for dyn (::std::marker::Send) + Send + Sync { } +//~^ WARNING conflicting implementations of trait `Trait` for type +//~| WARNING this was previously accepted by the compiler but is being phased out +unsafe impl Trait for dyn (::std::marker::Sync) + Send { } +//~^ WARNING conflicting implementations of trait `Trait` for type +//~| WARNING this was previously accepted by the compiler but is being phased out +unsafe impl Trait for dyn (::std::marker::Sync) + Sync { } +unsafe impl Trait for dyn (::std::marker::Sync) + Send + Sync { } +//~^ WARNING conflicting implementations of trait `Trait` for type +//~| WARNING this was previously accepted by the compiler but is being phased out +unsafe impl Trait for dyn (::std::ops::Drop) + Send { } +unsafe impl Trait for dyn (::std::ops::Drop) + Sync { } +unsafe impl Trait for dyn (::std::ops::Drop) + Send + Sync { } +unsafe impl Trait for dyn (::std::string::ToString) + Send { } +unsafe impl Trait for dyn (::std::string::ToString) + Sync { } +unsafe impl Trait for dyn (::std::string::ToString) + Send + Sync { } +fn assert_trait() {} + +fn main() { + assert_trait::(); + assert_trait::(); + assert_trait::(); +} diff --git a/src/test/ui/traits/object/issue-33140-traitobject-crate.stderr b/src/test/ui/traits/object/issue-33140-traitobject-crate.stderr new file mode 100644 index 000000000..77d71360b --- /dev/null +++ b/src/test/ui/traits/object/issue-33140-traitobject-crate.stderr @@ -0,0 +1,42 @@ +warning: conflicting implementations of trait `Trait` for type `(dyn std::marker::Send + std::marker::Sync + 'static)`: (E0119) + --> $DIR/issue-33140-traitobject-crate.rs:86:1 + | +LL | unsafe impl Trait for dyn (::std::marker::Send) + Sync { } + | ------------------------------------------------------ first implementation here +LL | unsafe impl Trait for dyn (::std::marker::Send) + Send + Sync { } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + std::marker::Sync + 'static)` + | +note: the lint level is defined here + --> $DIR/issue-33140-traitobject-crate.rs:3:9 + | +LL | #![warn(order_dependent_trait_objects)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #56484 + +warning: conflicting implementations of trait `Trait` for type `(dyn std::marker::Send + std::marker::Sync + 'static)`: (E0119) + --> $DIR/issue-33140-traitobject-crate.rs:89:1 + | +LL | unsafe impl Trait for dyn (::std::marker::Send) + Send + Sync { } + | ------------------------------------------------------------- first implementation here +... +LL | unsafe impl Trait for dyn (::std::marker::Sync) + Send { } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + std::marker::Sync + 'static)` + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #56484 + +warning: conflicting implementations of trait `Trait` for type `(dyn std::marker::Send + std::marker::Sync + 'static)`: (E0119) + --> $DIR/issue-33140-traitobject-crate.rs:93:1 + | +LL | unsafe impl Trait for dyn (::std::marker::Sync) + Send { } + | ------------------------------------------------------ first implementation here +... +LL | unsafe impl Trait for dyn (::std::marker::Sync) + Send + Sync { } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `(dyn std::marker::Send + std::marker::Sync + 'static)` + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #56484 + +warning: 3 warnings emitted + diff --git a/src/test/ui/traits/object/lifetime-first.rs b/src/test/ui/traits/object/lifetime-first.rs new file mode 100644 index 000000000..33757cb7c --- /dev/null +++ b/src/test/ui/traits/object/lifetime-first.rs @@ -0,0 +1,13 @@ +// run-pass +use std::fmt::Display; + +static BYTE: u8 = 33; + +fn main() { + let x: &(dyn 'static + Display) = &BYTE; + let y: Box = Box::new(BYTE); + let xstr = format!("{}", x); + let ystr = format!("{}", y); + assert_eq!(xstr, "33"); + assert_eq!(ystr, "33"); +} diff --git a/src/test/ui/traits/object/macro-matcher.rs b/src/test/ui/traits/object/macro-matcher.rs new file mode 100644 index 000000000..910978749 --- /dev/null +++ b/src/test/ui/traits/object/macro-matcher.rs @@ -0,0 +1,12 @@ +// `ty` matcher accepts trait object types + +macro_rules! m { + ($t: ty) => ( let _: $t; ) +} + +fn main() { + m!(dyn Copy + Send + 'static); + //~^ ERROR the trait `Copy` cannot be made into an object + m!(dyn 'static + Send); + m!(dyn 'static +); //~ ERROR at least one trait is required for an object type +} diff --git a/src/test/ui/traits/object/macro-matcher.stderr b/src/test/ui/traits/object/macro-matcher.stderr new file mode 100644 index 000000000..6d1e236c0 --- /dev/null +++ b/src/test/ui/traits/object/macro-matcher.stderr @@ -0,0 +1,19 @@ +error[E0224]: at least one trait is required for an object type + --> $DIR/macro-matcher.rs:11:8 + | +LL | m!(dyn 'static +); + | ^^^^^^^^^^^^^ + +error[E0038]: the trait `Copy` cannot be made into an object + --> $DIR/macro-matcher.rs:8:8 + | +LL | m!(dyn Copy + Send + 'static); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ `Copy` cannot be made into an object + | + = note: the trait cannot be made into an object because it requires `Self: Sized` + = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0038, E0224. +For more information about an error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/object/safety.rs b/src/test/ui/traits/object/safety.rs new file mode 100644 index 000000000..f43d332d6 --- /dev/null +++ b/src/test/ui/traits/object/safety.rs @@ -0,0 +1,17 @@ +// Check that static methods are not object-safe. + +trait Tr { + fn foo(); + fn bar(&self) { } +} + +struct St; + +impl Tr for St { + fn foo() {} +} + +fn main() { + let _: &dyn Tr = &St; //~ ERROR E0038 + //~^ ERROR E0038 +} diff --git a/src/test/ui/traits/object/safety.stderr b/src/test/ui/traits/object/safety.stderr new file mode 100644 index 000000000..cf534d984 --- /dev/null +++ b/src/test/ui/traits/object/safety.stderr @@ -0,0 +1,49 @@ +error[E0038]: the trait `Tr` cannot be made into an object + --> $DIR/safety.rs:15:22 + | +LL | let _: &dyn Tr = &St; + | ^^^ `Tr` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/safety.rs:4:8 + | +LL | trait Tr { + | -- this trait cannot be made into an object... +LL | fn foo(); + | ^^^ ...because associated function `foo` has no `self` parameter + = note: required because of the requirements on the impl of `CoerceUnsized<&dyn Tr>` for `&St` + = note: required by cast to type `&dyn Tr` +help: consider turning `foo` into a method by giving it a `&self` argument + | +LL | fn foo(&self); + | +++++ +help: alternatively, consider constraining `foo` so it does not apply to trait objects + | +LL | fn foo() where Self: Sized; + | +++++++++++++++++ + +error[E0038]: the trait `Tr` cannot be made into an object + --> $DIR/safety.rs:15:12 + | +LL | let _: &dyn Tr = &St; + | ^^^^^^^ `Tr` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/safety.rs:4:8 + | +LL | trait Tr { + | -- this trait cannot be made into an object... +LL | fn foo(); + | ^^^ ...because associated function `foo` has no `self` parameter +help: consider turning `foo` into a method by giving it a `&self` argument + | +LL | fn foo(&self); + | +++++ +help: alternatively, consider constraining `foo` so it does not apply to trait objects + | +LL | fn foo() where Self: Sized; + | +++++++++++++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/object/supertrait-lifetime-bound.rs b/src/test/ui/traits/object/supertrait-lifetime-bound.rs new file mode 100644 index 000000000..f929a9bb6 --- /dev/null +++ b/src/test/ui/traits/object/supertrait-lifetime-bound.rs @@ -0,0 +1,14 @@ +trait Foo: 'static { } + +trait Bar: Foo { } + +fn test1, S>() { } + +fn test2<'a>() { + // Here: the type `dyn Bar<&'a u32>` references `'a`, + // and so it does not outlive `'static`. + test1::, _>(); + //~^ ERROR lifetime may not live long enough +} + +fn main() { } diff --git a/src/test/ui/traits/object/supertrait-lifetime-bound.stderr b/src/test/ui/traits/object/supertrait-lifetime-bound.stderr new file mode 100644 index 000000000..ed2f86243 --- /dev/null +++ b/src/test/ui/traits/object/supertrait-lifetime-bound.stderr @@ -0,0 +1,11 @@ +error: lifetime may not live long enough + --> $DIR/supertrait-lifetime-bound.rs:10:5 + | +LL | fn test2<'a>() { + | -- lifetime `'a` defined here +... +LL | test1::, _>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static` + +error: aborting due to previous error + diff --git a/src/test/ui/traits/object/vs-lifetime-2.rs b/src/test/ui/traits/object/vs-lifetime-2.rs new file mode 100644 index 000000000..0b33dc7f6 --- /dev/null +++ b/src/test/ui/traits/object/vs-lifetime-2.rs @@ -0,0 +1,11 @@ +// A few contrived examples where lifetime should (or should not) be parsed as an object type. +// Lifetimes parsed as types are still rejected later by semantic checks. + +// `'static` is a lifetime, `'static +` is a type, `'a` is a type +fn g() where + 'static: 'static, + dyn 'static +: 'static + Copy, + //~^ ERROR at least one trait is required for an object type +{} + +fn main() {} diff --git a/src/test/ui/traits/object/vs-lifetime-2.stderr b/src/test/ui/traits/object/vs-lifetime-2.stderr new file mode 100644 index 000000000..9b8e793df --- /dev/null +++ b/src/test/ui/traits/object/vs-lifetime-2.stderr @@ -0,0 +1,9 @@ +error[E0224]: at least one trait is required for an object type + --> $DIR/vs-lifetime-2.rs:7:5 + | +LL | dyn 'static +: 'static + Copy, + | ^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/traits/object/vs-lifetime.rs b/src/test/ui/traits/object/vs-lifetime.rs new file mode 100644 index 000000000..14ae67cff --- /dev/null +++ b/src/test/ui/traits/object/vs-lifetime.rs @@ -0,0 +1,17 @@ +// A few contrived examples where lifetime should (or should not) be parsed as an object type. +// Lifetimes parsed as types are still rejected later by semantic checks. + +struct S<'a, T>(&'a u8, T); + +fn main() { + // `'static` is a lifetime argument, `'static +` is a type argument + let _: S<'static, u8>; + let _: S<'static, dyn 'static +>; + //~^ at least one trait is required for an object type + let _: S<'static, 'static>; + //~^ ERROR this struct takes 1 lifetime argument but 2 lifetime arguments were supplied + //~| ERROR this struct takes 1 generic argument but 0 generic arguments were supplied + let _: S; + //~^ ERROR type provided when a lifetime was expected + //~| ERROR at least one trait is required for an object type +} diff --git a/src/test/ui/traits/object/vs-lifetime.stderr b/src/test/ui/traits/object/vs-lifetime.stderr new file mode 100644 index 000000000..224465228 --- /dev/null +++ b/src/test/ui/traits/object/vs-lifetime.stderr @@ -0,0 +1,52 @@ +error[E0224]: at least one trait is required for an object type + --> $DIR/vs-lifetime.rs:9:23 + | +LL | let _: S<'static, dyn 'static +>; + | ^^^^^^^^^^^^^ + +error[E0107]: this struct takes 1 lifetime argument but 2 lifetime arguments were supplied + --> $DIR/vs-lifetime.rs:11:12 + | +LL | let _: S<'static, 'static>; + | ^ ------- help: remove this lifetime argument + | | + | expected 1 lifetime argument + | +note: struct defined here, with 1 lifetime parameter: `'a` + --> $DIR/vs-lifetime.rs:4:8 + | +LL | struct S<'a, T>(&'a u8, T); + | ^ -- + +error[E0107]: this struct takes 1 generic argument but 0 generic arguments were supplied + --> $DIR/vs-lifetime.rs:11:12 + | +LL | let _: S<'static, 'static>; + | ^ expected 1 generic argument + | +note: struct defined here, with 1 generic parameter: `T` + --> $DIR/vs-lifetime.rs:4:8 + | +LL | struct S<'a, T>(&'a u8, T); + | ^ - +help: add missing generic argument + | +LL | let _: S<'static, 'static, T>; + | +++ + +error[E0224]: at least one trait is required for an object type + --> $DIR/vs-lifetime.rs:14:14 + | +LL | let _: S; + | ^^^^^^^^^^^^^ + +error[E0747]: type provided when a lifetime was expected + --> $DIR/vs-lifetime.rs:14:14 + | +LL | let _: S; + | ^^^^^^^^^^^^^ + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0107, E0224, E0747. +For more information about an error, try `rustc --explain E0107`. diff --git a/src/test/ui/traits/object/with-lifetime-bound.rs b/src/test/ui/traits/object/with-lifetime-bound.rs new file mode 100644 index 000000000..05aab5e3b --- /dev/null +++ b/src/test/ui/traits/object/with-lifetime-bound.rs @@ -0,0 +1,34 @@ +// run-pass +// Uncovered during work on new scoping rules for safe destructors +// as an important use case to support properly. + + +pub struct E<'a> { + pub f: &'a u8, +} +impl<'b> E<'b> { + pub fn m(&self) -> &'b u8 { self.f } +} + +pub struct P<'c> { + pub g: &'c u8, +} +pub trait M { + fn n(&self) -> u8; +} +impl<'d> M for P<'d> { + fn n(&self) -> u8 { *self.g } +} + +fn extension<'e>(x: &'e E<'e>) -> Box { + loop { + let p = P { g: x.m() }; + return Box::new(p) as Box; + } +} + +fn main() { + let w = E { f: &10 }; + let o = extension(&w); + assert_eq!(o.n(), 10); +} diff --git a/src/test/ui/traits/object/with-self-in-projection-output-bad.rs b/src/test/ui/traits/object/with-self-in-projection-output-bad.rs new file mode 100644 index 000000000..f34fa80a0 --- /dev/null +++ b/src/test/ui/traits/object/with-self-in-projection-output-bad.rs @@ -0,0 +1,50 @@ +// Regression test for #56288. Checks that if a supertrait defines an associated type +// projection that references `Self`, then that associated type must still be explicitly +// specified in the `dyn Trait` variant, since we don't know what `Self` is anymore. + +trait Base { + type Output; +} + +trait Helper: Base::Target> { + type Target; +} + +impl Base for u32 +{ + type Output = i32; +} + +impl Helper for u32 +{ + type Target = i32; +} + +trait ConstI32 { + type Out; +} + +impl ConstI32 for T { + type Out = i32; +} + +// Test that you still need to manually give a projection type if the Output type +// is normalizable. +trait NormalizableHelper: + Base::Out> +{ + type Target; +} + +impl NormalizableHelper for u32 +{ + type Target = i32; +} + +fn main() { + let _x: Box> = Box::new(2u32); + //~^ ERROR the value of the associated type `Output` (from trait `Base`) must be specified + + let _y: Box> = Box::new(2u32); + //~^ ERROR the value of the associated type `Output` (from trait `Base`) must be specified +} diff --git a/src/test/ui/traits/object/with-self-in-projection-output-bad.stderr b/src/test/ui/traits/object/with-self-in-projection-output-bad.stderr new file mode 100644 index 000000000..641bfe236 --- /dev/null +++ b/src/test/ui/traits/object/with-self-in-projection-output-bad.stderr @@ -0,0 +1,21 @@ +error[E0191]: the value of the associated type `Output` (from trait `Base`) must be specified + --> $DIR/with-self-in-projection-output-bad.rs:45:21 + | +LL | type Output; + | ----------- `Output` defined here +... +LL | let _x: Box> = Box::new(2u32); + | ^^^^^^^^^^^^^^^^^^ help: specify the associated type: `Helper` + +error[E0191]: the value of the associated type `Output` (from trait `Base`) must be specified + --> $DIR/with-self-in-projection-output-bad.rs:48:21 + | +LL | type Output; + | ----------- `Output` defined here +... +LL | let _y: Box> = Box::new(2u32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: specify the associated type: `NormalizableHelper` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0191`. diff --git a/src/test/ui/traits/object/with-self-in-projection-output-good.rs b/src/test/ui/traits/object/with-self-in-projection-output-good.rs new file mode 100644 index 000000000..d1b7bf6c2 --- /dev/null +++ b/src/test/ui/traits/object/with-self-in-projection-output-good.rs @@ -0,0 +1,28 @@ +// build-pass (FIXME(62277): could be check-pass?) + +// Regression test related to #56288. Check that a supertrait projection (of +// `Output`) that references `Self` can be ok if it is referencing a projection (of +// `Self::Target`, in this case). Note that we still require the user to manually +// specify both `Target` and `Output` for now. + +trait Base { + type Output; +} + +trait Helper: Base::Target> { + type Target; +} + +impl Base for u32 +{ + type Output = i32; +} + +impl Helper for u32 +{ + type Target = i32; +} + +fn main() { + let _x: Box> = Box::new(2u32); +} diff --git a/src/test/ui/traits/object/with-self-in-projection-output-repeated-supertrait.rs b/src/test/ui/traits/object/with-self-in-projection-output-repeated-supertrait.rs new file mode 100644 index 000000000..39e817168 --- /dev/null +++ b/src/test/ui/traits/object/with-self-in-projection-output-repeated-supertrait.rs @@ -0,0 +1,51 @@ +// build-pass (FIXME(62277): could be check-pass?) + +// FIXME(eddyb) shorten the name so windows doesn't choke on it. +#![crate_name = "trait_test"] + +// Regression test related to #56288. Check that a supertrait projection (of +// `Output`) that references `Self` is ok if there is another occurrence of +// the same supertrait that specifies the projection explicitly, even if +// the projection's associated type is not explicitly specified in the object type. +// +// Note that in order for this to compile, we need the `Self`-referencing projection +// to normalize fairly directly to a concrete type, otherwise the trait resolver +// will hate us. +// +// There is a test in `trait-object-with-self-in-projection-output-bad.rs` that +// having a normalizing, but `Self`-containing projection does not *by itself* +// allow you to avoid writing the projected type (`Output`, in this example) +// explicitly. + +trait ConstI32 { + type Out; +} + +impl ConstI32 for T { + type Out = i32; +} + +trait Base { + type Output; +} + +trait NormalizingHelper: Base::Out> + Base { + type Target; +} + +impl Base for u32 +{ + type Output = i32; +} + +impl NormalizingHelper for u32 +{ + type Target = i32; +} + +fn main() { + // Make sure this works both with and without the associated type + // being specified. + let _x: Box> = Box::new(2u32); + let _y: Box> = Box::new(2u32); +} diff --git a/src/test/ui/traits/objects-owned-object-borrowed-method-headerless.rs b/src/test/ui/traits/objects-owned-object-borrowed-method-headerless.rs new file mode 100644 index 000000000..fce1341fc --- /dev/null +++ b/src/test/ui/traits/objects-owned-object-borrowed-method-headerless.rs @@ -0,0 +1,30 @@ +// run-pass +// Test invoked `&self` methods on owned objects where the values +// closed over do not contain managed values, and thus the boxes do +// not have headers. + +trait FooTrait { + fn foo(&self) -> usize; +} + +struct BarStruct { + x: usize +} + +impl FooTrait for BarStruct { + fn foo(&self) -> usize { + self.x + } +} + +pub fn main() { + let foos: Vec> = vec![ + Box::new(BarStruct{ x: 0 }) as Box, + Box::new(BarStruct{ x: 1 }) as Box, + Box::new(BarStruct{ x: 2 }) as Box, + ]; + + for i in 0..foos.len() { + assert_eq!(i, foos[i].foo()); + } +} diff --git a/src/test/ui/traits/operator-overloading-issue-52025.rs b/src/test/ui/traits/operator-overloading-issue-52025.rs new file mode 100644 index 000000000..7ce638832 --- /dev/null +++ b/src/test/ui/traits/operator-overloading-issue-52025.rs @@ -0,0 +1,57 @@ +// only-x86_64 +// build-pass + +use std::arch::x86_64::*; +use std::fmt::Debug; +use std::ops::*; + +pub trait Simd { + type Vf32: Copy + Debug + Add + Add; + + unsafe fn set1_ps(a: f32) -> Self::Vf32; + unsafe fn add_ps(a: Self::Vf32, b: Self::Vf32) -> Self::Vf32; +} + +#[derive(Copy, Debug, Clone)] +pub struct F32x4(pub __m128); + +impl Add for F32x4 { + type Output = F32x4; + + fn add(self, rhs: F32x4) -> F32x4 { + F32x4(unsafe { _mm_add_ps(self.0, rhs.0) }) + } +} + +impl Add for F32x4 { + type Output = F32x4; + fn add(self, rhs: f32) -> F32x4 { + F32x4(unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) }) + } +} + +pub struct Sse2; +impl Simd for Sse2 { + type Vf32 = F32x4; + + #[inline(always)] + unsafe fn set1_ps(a: f32) -> Self::Vf32 { + F32x4(_mm_set1_ps(a)) + } + + #[inline(always)] + unsafe fn add_ps(a: Self::Vf32, b: Self::Vf32) -> Self::Vf32 { + F32x4(_mm_add_ps(a.0, b.0)) + } +} + +unsafe fn test() -> S::Vf32 { + let a = S::set1_ps(3.0); + let b = S::set1_ps(2.0); + let result = a + b; + result +} + +fn main() { + println!("{:?}", unsafe { test::() }); +} diff --git a/src/test/ui/traits/overlap-not-permitted-for-builtin-trait.rs b/src/test/ui/traits/overlap-not-permitted-for-builtin-trait.rs new file mode 100644 index 000000000..4106f56d6 --- /dev/null +++ b/src/test/ui/traits/overlap-not-permitted-for-builtin-trait.rs @@ -0,0 +1,10 @@ +#![allow(dead_code)] +#![feature(negative_impls)] + +// Overlapping negative impls for `MyStruct` are not permitted: +struct MyStruct; +impl !Send for MyStruct {} +impl !Send for MyStruct {} +//~^ ERROR conflicting implementations of trait + +fn main() {} diff --git a/src/test/ui/traits/overlap-not-permitted-for-builtin-trait.stderr b/src/test/ui/traits/overlap-not-permitted-for-builtin-trait.stderr new file mode 100644 index 000000000..910c5e29d --- /dev/null +++ b/src/test/ui/traits/overlap-not-permitted-for-builtin-trait.stderr @@ -0,0 +1,11 @@ +error[E0119]: conflicting implementations of trait `std::marker::Send` for type `MyStruct` + --> $DIR/overlap-not-permitted-for-builtin-trait.rs:7:1 + | +LL | impl !Send for MyStruct {} + | ----------------------- first implementation here +LL | impl !Send for MyStruct {} + | ^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `MyStruct` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0119`. diff --git a/src/test/ui/traits/overlap-permitted-for-marker-traits.rs b/src/test/ui/traits/overlap-permitted-for-marker-traits.rs new file mode 100644 index 000000000..00823d13b --- /dev/null +++ b/src/test/ui/traits/overlap-permitted-for-marker-traits.rs @@ -0,0 +1,28 @@ +// run-pass +// Tests for RFC 1268: we allow overlapping impls of marker traits, +// that is, traits without items. In this case, a type `T` is +// `MyMarker` if it is either `Debug` or `Display`. + +#![feature(marker_trait_attr)] +#![feature(negative_impls)] + +use std::fmt::{Debug, Display}; + +#[marker] +trait MyMarker {} + +impl MyMarker for T {} +impl MyMarker for T {} + +fn foo(t: T) -> T { + t +} + +fn main() { + // Debug && Display: + assert_eq!(1, foo(1)); + assert_eq!(2.0, foo(2.0)); + + // Debug && !Display: + assert_eq!(vec![1], foo(vec![1])); +} diff --git a/src/test/ui/traits/param-without-lifetime-constraint.rs b/src/test/ui/traits/param-without-lifetime-constraint.rs new file mode 100644 index 000000000..a79b74dcd --- /dev/null +++ b/src/test/ui/traits/param-without-lifetime-constraint.rs @@ -0,0 +1,20 @@ +struct Article { + proof_reader: ProofReader, +} + +struct ProofReader { + name: String, +} + +pub trait HaveRelationship { + fn get_relation(&self) -> To; +} + +impl HaveRelationship<&ProofReader> for Article { + fn get_relation(&self) -> &ProofReader { + //~^ ERROR `impl` item signature doesn't match `trait` item signature + &self.proof_reader + } +} + +fn main() {} diff --git a/src/test/ui/traits/param-without-lifetime-constraint.stderr b/src/test/ui/traits/param-without-lifetime-constraint.stderr new file mode 100644 index 000000000..118b2cf3e --- /dev/null +++ b/src/test/ui/traits/param-without-lifetime-constraint.stderr @@ -0,0 +1,19 @@ +error: `impl` item signature doesn't match `trait` item signature + --> $DIR/param-without-lifetime-constraint.rs:14:5 + | +LL | fn get_relation(&self) -> To; + | ----------------------------- expected `fn(&'1 Article) -> &'2 ProofReader` +... +LL | fn get_relation(&self) -> &ProofReader { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 Article) -> &'1 ProofReader` + | + = note: expected `fn(&'1 Article) -> &'2 ProofReader` + found `fn(&'1 Article) -> &'1 ProofReader` +help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` + --> $DIR/param-without-lifetime-constraint.rs:10:31 + | +LL | fn get_relation(&self) -> To; + | ^^ consider borrowing this type parameter in the trait + +error: aborting due to previous error + diff --git a/src/test/ui/traits/parameterized-with-bounds.rs b/src/test/ui/traits/parameterized-with-bounds.rs new file mode 100644 index 000000000..832d4f6c8 --- /dev/null +++ b/src/test/ui/traits/parameterized-with-bounds.rs @@ -0,0 +1,21 @@ +// run-pass +// pretty-expanded FIXME #23616 + +#![allow(dead_code)] + + +trait A { fn get(self) -> T; } +trait B { fn get(self) -> (T,U); } +trait C<'a, U> { fn get(self) -> &'a U; } + +mod foo { + pub trait D<'a, T> { fn get(self) -> &'a T; } +} + +fn foo1(_: &(dyn A + Send)) {} +fn foo2(_: Box + Send + Sync>) {} +fn foo3(_: Box + 'static>) {} +fn foo4<'a, T>(_: Box + 'static + Send>) {} +fn foo5<'a, T>(_: Box + 'static + Send>) {} + +pub fn main() {} diff --git a/src/test/ui/traits/pointee-deduction.rs b/src/test/ui/traits/pointee-deduction.rs new file mode 100644 index 000000000..c333b0129 --- /dev/null +++ b/src/test/ui/traits/pointee-deduction.rs @@ -0,0 +1,22 @@ +// run-pass + +#![feature(ptr_metadata)] + +use std::alloc::Layout; +use std::ptr::Pointee; + +trait Foo { + type Bar; +} + +impl Foo for () { + type Bar = (); +} + +struct Wrapper1(#[allow(unused_tuple_struct_fields)] ::Bar); +struct Wrapper2(#[allow(unused_tuple_struct_fields)] as Pointee>::Metadata); + +fn main() { + let _: Wrapper2<()> = Wrapper2(()); + let _ = Layout::new::>(); +} diff --git a/src/test/ui/traits/pointee-tail-is-generic-errors.rs b/src/test/ui/traits/pointee-tail-is-generic-errors.rs new file mode 100644 index 000000000..28bc1da96 --- /dev/null +++ b/src/test/ui/traits/pointee-tail-is-generic-errors.rs @@ -0,0 +1,22 @@ +// edition:2018 + +#![feature(ptr_metadata)] +#![feature(type_alias_impl_trait)] + +type Opaque = impl std::fmt::Debug + ?Sized; + +fn opaque() -> &'static Opaque { + &[1] as &[i32] +} + +fn a() { + is_thin::(); + //~^ ERROR type mismatch resolving `::Metadata == ()` + + is_thin::(); + //~^ ERROR type mismatch resolving `::Metadata == ()` +} + +fn is_thin + ?Sized>() {} + +fn main() {} diff --git a/src/test/ui/traits/pointee-tail-is-generic-errors.stderr b/src/test/ui/traits/pointee-tail-is-generic-errors.stderr new file mode 100644 index 000000000..8456f8456 --- /dev/null +++ b/src/test/ui/traits/pointee-tail-is-generic-errors.stderr @@ -0,0 +1,40 @@ +error[E0271]: type mismatch resolving `::Metadata == ()` + --> $DIR/pointee-tail-is-generic-errors.rs:13:5 + | +LL | is_thin::(); + | ^^^^^^^^^^^^ expected `()`, found associated type + | + = note: expected unit type `()` + found associated type `::Metadata` + = help: consider constraining the associated type `::Metadata` to `()` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html +note: required by a bound in `is_thin` + --> $DIR/pointee-tail-is-generic-errors.rs:20:33 + | +LL | fn is_thin + ?Sized>() {} + | ^^^^^^^^^^^^^ required by this bound in `is_thin` + +error[E0271]: type mismatch resolving `::Metadata == ()` + --> $DIR/pointee-tail-is-generic-errors.rs:16:5 + | +LL | type Opaque = impl std::fmt::Debug + ?Sized; + | ----------------------------- the found opaque type +... +LL | is_thin::(); + | ^^^^^^^^^^^^^^^^^ expected `()`, found associated type + | + = note: expected unit type `()` + found associated type `::Metadata` +note: required by a bound in `is_thin` + --> $DIR/pointee-tail-is-generic-errors.rs:20:33 + | +LL | fn is_thin + ?Sized>() {} + | ^^^^^^^^^^^^^ required by this bound in `is_thin` +help: consider constraining the associated type `::Metadata` to `()` + | +LL | type Opaque = impl std::fmt::Debug + ?Sized; + | +++++++++++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0271`. diff --git a/src/test/ui/traits/pointee-tail-is-generic.rs b/src/test/ui/traits/pointee-tail-is-generic.rs new file mode 100644 index 000000000..e0da0fc38 --- /dev/null +++ b/src/test/ui/traits/pointee-tail-is-generic.rs @@ -0,0 +1,29 @@ +// check-pass +// edition:2018 + +#![feature(ptr_metadata)] +#![feature(type_alias_impl_trait)] + +type Opaque = impl std::future::Future; + +fn opaque() -> Opaque { + async {} +} + +fn a() { + // type parameter T is known to be sized + is_thin::(); + // tail of ADT (which is a type param) is known to be sized + is_thin::>(); + // opaque type is known to be sized + is_thin::(); +} + +fn a2() { + // associated type is known to be sized + is_thin::(); +} + +fn is_thin>() {} + +fn main() {} diff --git a/src/test/ui/traits/principal-less-objects.rs b/src/test/ui/traits/principal-less-objects.rs new file mode 100644 index 000000000..62bad0d7d --- /dev/null +++ b/src/test/ui/traits/principal-less-objects.rs @@ -0,0 +1,42 @@ +// run-pass +// Check that trait objects without a principal codegen properly. + +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::mem; + +// Array is to make sure the size is not exactly pointer-size, so +// we can be sure we are measuring the right size in the +// `size_of_val` test. +struct SetOnDrop<'a>(&'a AtomicUsize, #[allow(unused_tuple_struct_fields)] [u8; 64]); +impl<'a> Drop for SetOnDrop<'a> { + fn drop(&mut self) { + self.0.store(self.0.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + } +} + +trait TypeEq {} +impl TypeEq for T {} +fn assert_types_eq() where U: TypeEq {} + +fn main() { + // Check that different ways of writing the same type are equal. + assert_types_eq::(); + assert_types_eq::(); + assert_types_eq::(); + + // Check that codegen works. + // + // Using `AtomicUsize` here because `Cell` is not `Sync`, and + // so can't be made into a `Box`. + let c = AtomicUsize::new(0); + { + let d: Box = Box::new(SetOnDrop(&c, [0; 64])); + + assert_eq!(mem::size_of_val(&*d), + mem::size_of::()); + assert_eq!(mem::align_of_val(&*d), + mem::align_of::()); + assert_eq!(c.load(Ordering::Relaxed), 0); + } + assert_eq!(c.load(Ordering::Relaxed), 1); +} diff --git a/src/test/ui/traits/privacy.rs b/src/test/ui/traits/privacy.rs new file mode 100644 index 000000000..17a2e05e9 --- /dev/null +++ b/src/test/ui/traits/privacy.rs @@ -0,0 +1,24 @@ +// build-pass (FIXME(62277): could be check-pass?) +#![allow(dead_code)] +mod foo { + pub use self::bar::T; + mod bar { + pub trait T { + fn f(&self) {} + } + impl T for () {} + } +} + +fn g() { + use foo::T; + ().f(); // Check that this does not trigger a privacy error +} + +fn f() { + let error = ::std::thread::spawn(|| {}).join().unwrap_err(); + error.type_id(); // Regression test for #21670 +} + + +fn main() {} diff --git a/src/test/ui/traits/project-modulo-regions.rs b/src/test/ui/traits/project-modulo-regions.rs new file mode 100644 index 000000000..f0c0dd3ed --- /dev/null +++ b/src/test/ui/traits/project-modulo-regions.rs @@ -0,0 +1,55 @@ +// revisions: with_clause without_clause +// Tests that `EvaluatedToOkModuloRegions` from a projection sub-obligation +// is correctly propagated + +#![feature(rustc_attrs)] + +trait MyTrait { + type Assoc; +} + +struct MyStruct; + +impl MyTrait for MyStruct { + // Evaluating this projection will result in `EvaluatedToOkModuloRegions` + // (when `with_clause` is enabled) + type Assoc = ::Assoc; +} + +struct Bar; + +// The `where` clause on this impl will cause us to produce `EvaluatedToOkModuloRegions` +// when evaluating a projection involving this impl +#[cfg(with_clause)] +impl MyTrait for Bar where for<'b> &'b (): 'b { + type Assoc = bool; +} + +// This impl tests that the `EvaluatedToOkModuoRegions` result that we get +// is really due to the `where` clause on the `with_clause` impl +#[cfg(without_clause)] +impl MyTrait for Bar { + type Assoc = bool; +} + +// The implementation of `#[rustc_evaluate_where_clauses]` doesn't perform +// normalization, so we need to place the projection predicate behind a normal +// trait predicate +struct Helper {} +trait HelperTrait {} +impl HelperTrait for Helper where ::Assoc: Sized {} + +// Evaluating this 'where' clause will (recursively) end up evaluating +// `for<'b> &'b (): 'b`, which will produce `EvaluatedToOkModuloRegions` +#[rustc_evaluate_where_clauses] +fn test(val: MyStruct) where Helper: HelperTrait { + panic!() +} + +fn foo(val: MyStruct) { + test(val); + //[with_clause]~^ ERROR evaluate(Binder(TraitPredicate(, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + //[without_clause]~^^ ERROR evaluate(Binder(TraitPredicate(, polarity:Positive), [])) = Ok(EvaluatedToOk) +} + +fn main() {} diff --git a/src/test/ui/traits/project-modulo-regions.with_clause.stderr b/src/test/ui/traits/project-modulo-regions.with_clause.stderr new file mode 100644 index 000000000..2434c32c8 --- /dev/null +++ b/src/test/ui/traits/project-modulo-regions.with_clause.stderr @@ -0,0 +1,11 @@ +error: evaluate(Binder(TraitPredicate(, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + --> $DIR/project-modulo-regions.rs:50:5 + | +LL | fn test(val: MyStruct) where Helper: HelperTrait { + | ----------- predicate +... +LL | test(val); + | ^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/traits/project-modulo-regions.without_clause.stderr b/src/test/ui/traits/project-modulo-regions.without_clause.stderr new file mode 100644 index 000000000..9d35690d5 --- /dev/null +++ b/src/test/ui/traits/project-modulo-regions.without_clause.stderr @@ -0,0 +1,11 @@ +error: evaluate(Binder(TraitPredicate(, polarity:Positive), [])) = Ok(EvaluatedToOk) + --> $DIR/project-modulo-regions.rs:50:5 + | +LL | fn test(val: MyStruct) where Helper: HelperTrait { + | ----------- predicate +... +LL | test(val); + | ^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/traits/region-pointer-simple.rs b/src/test/ui/traits/region-pointer-simple.rs new file mode 100644 index 000000000..0456ca931 --- /dev/null +++ b/src/test/ui/traits/region-pointer-simple.rs @@ -0,0 +1,21 @@ +// run-pass +trait Foo { + fn f(&self) -> isize; +} + +struct A { + x: isize +} + +impl Foo for A { + fn f(&self) -> isize { + println!("Today's number is {}", self.x); + return self.x; + } +} + +pub fn main() { + let a = A { x: 3 }; + let b = (&a) as &dyn Foo; + assert_eq!(b.f(), 3); +} diff --git a/src/test/ui/traits/reservation-impl/coherence-conflict.rs b/src/test/ui/traits/reservation-impl/coherence-conflict.rs new file mode 100644 index 000000000..fa4a30931 --- /dev/null +++ b/src/test/ui/traits/reservation-impl/coherence-conflict.rs @@ -0,0 +1,14 @@ +// check that reservation impls are accounted for in negative reasoning. + +#![feature(rustc_attrs)] + +trait MyTrait {} +#[rustc_reservation_impl="this impl is reserved"] +impl MyTrait for () {} + +trait OtherTrait {} +impl OtherTrait for () {} +impl OtherTrait for T {} +//~^ ERROR conflicting implementations + +fn main() {} diff --git a/src/test/ui/traits/reservation-impl/coherence-conflict.stderr b/src/test/ui/traits/reservation-impl/coherence-conflict.stderr new file mode 100644 index 000000000..a811d7e32 --- /dev/null +++ b/src/test/ui/traits/reservation-impl/coherence-conflict.stderr @@ -0,0 +1,13 @@ +error[E0119]: conflicting implementations of trait `OtherTrait` for type `()` + --> $DIR/coherence-conflict.rs:11:1 + | +LL | impl OtherTrait for () {} + | ---------------------- first implementation here +LL | impl OtherTrait for T {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `()` + | + = note: this impl is reserved + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0119`. diff --git a/src/test/ui/traits/reservation-impl/no-use.rs b/src/test/ui/traits/reservation-impl/no-use.rs new file mode 100644 index 000000000..65a55d9e2 --- /dev/null +++ b/src/test/ui/traits/reservation-impl/no-use.rs @@ -0,0 +1,12 @@ +// check that reservation impls can't be used as normal impls in positive reasoning. + +#![feature(rustc_attrs)] + +trait MyTrait { fn foo(&self); } +#[rustc_reservation_impl = "foo"] +impl MyTrait for () { fn foo(&self) {} } + +fn main() { + <() as MyTrait>::foo(&()); + //~^ ERROR the trait bound `(): MyTrait` is not satisfied +} diff --git a/src/test/ui/traits/reservation-impl/no-use.stderr b/src/test/ui/traits/reservation-impl/no-use.stderr new file mode 100644 index 000000000..cefb2a879 --- /dev/null +++ b/src/test/ui/traits/reservation-impl/no-use.stderr @@ -0,0 +1,13 @@ +error[E0277]: the trait bound `(): MyTrait` is not satisfied + --> $DIR/no-use.rs:10:26 + | +LL | <() as MyTrait>::foo(&()); + | -------------------- ^^^ the trait `MyTrait` is not implemented for `()` + | | + | required by a bound introduced by this call + | + = help: the trait `MyTrait` is implemented for `()` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/reservation-impl/non-lattice-ok.rs b/src/test/ui/traits/reservation-impl/non-lattice-ok.rs new file mode 100644 index 000000000..a71051243 --- /dev/null +++ b/src/test/ui/traits/reservation-impl/non-lattice-ok.rs @@ -0,0 +1,59 @@ +// build-pass + +// Check that a reservation impl does not force other impls to follow +// a lattice discipline. + +// Why did we ever want to do this? +// +// We want to eventually add an `impl From for T` impl. That impl conflicts +// with existing impls - at least the `impl From for T` impl. There are +// 2 ways we thought of for dealing with that conflict: +// +// 1. Using specialization and doing some handling for the +// overlap. The current thought is to require ["intersection +// impls"][ii], specialization", which means providing an +// (higher-priority) impl for the intersection of every 2 conflicting +// impls that determines what happens in the intersection case. That's +// the first thing we thought about - see e.g. +// https://github.com/rust-lang/rust/issues/57012#issuecomment-452150775 +// +// 2. The other way is to notice that `impl From for T` is basically a +// marker trait since its only method is uninhabited, and allow for "marker +// trait overlap", where the conflict "doesn't matter" because it can't +// actually cause any ambiguity. +// +// Now it turned out lattice specialization doesn't work it, because an +// `impl From for Smaht` would require an `impl From for Smaht`, +// breaking backwards-compatibility in a fairly painful way. So if we want to +// go with a known approach, we should go with a "marker trait overlap"-style +// approach. +// +// [ii]: https://smallcultfollowing.com/babysteps/blog/2016/09/24/intersection-impls/ + +#![feature(rustc_attrs, never_type)] + +trait MyTrait {} + +impl MyTrait for ! {} + +trait MyFrom { + fn my_from(x: T) -> Self; +} + +// Given the "normal" impls for From +#[rustc_reservation_impl="this impl is reserved"] +impl MyFrom for T { + fn my_from(x: !) -> Self { match x {} } +} + +impl MyFrom for T { + fn my_from(x: T) -> Self { x } +} + +// ... we *do* want to allow this common pattern, of `From for MySmaht` +struct MySmaht(T); +impl MyFrom for MySmaht { + fn my_from(x: T) -> Self { MySmaht(x) } +} + +fn main() {} diff --git a/src/test/ui/traits/reservation-impl/ok.rs b/src/test/ui/traits/reservation-impl/ok.rs new file mode 100644 index 000000000..611c8d884 --- /dev/null +++ b/src/test/ui/traits/reservation-impl/ok.rs @@ -0,0 +1,28 @@ +// run-pass + +// rpass test for reservation impls. Not 100% required because `From` uses them, +// but still. + +#![feature(rustc_attrs)] + +use std::mem; + +trait MyTrait { + fn foo(&self, s: S) -> usize; +} + +#[rustc_reservation_impl = "foo"] +impl MyTrait for T { + fn foo(&self, _x: u64) -> usize { 0 } +} + +// reservation impls don't create coherence conflicts, even with +// non-chain overlap. +impl MyTrait for u32 { + fn foo(&self, _x: S) -> usize { mem::size_of::() } +} + +fn main() { + // ...and the non-reservation impl gets picked.XS + assert_eq!(0u32.foo(0u64), mem::size_of::()); +} diff --git a/src/test/ui/traits/resolution-in-overloaded-op.rs b/src/test/ui/traits/resolution-in-overloaded-op.rs new file mode 100644 index 000000000..a9bacc357 --- /dev/null +++ b/src/test/ui/traits/resolution-in-overloaded-op.rs @@ -0,0 +1,11 @@ +// #12402 Operator overloading only considers the method name, not which trait is implemented + +trait MyMul { + fn mul(&self, rhs: &Rhs) -> Res; +} + +fn foo>(a: &T, b: f64) -> f64 { + a * b //~ ERROR cannot multiply `&T` by `f64` +} + +fn main() {} diff --git a/src/test/ui/traits/resolution-in-overloaded-op.stderr b/src/test/ui/traits/resolution-in-overloaded-op.stderr new file mode 100644 index 000000000..34fae64e4 --- /dev/null +++ b/src/test/ui/traits/resolution-in-overloaded-op.stderr @@ -0,0 +1,16 @@ +error[E0369]: cannot multiply `&T` by `f64` + --> $DIR/resolution-in-overloaded-op.rs:8:7 + | +LL | a * b + | - ^ - f64 + | | + | &T + | +help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement + | +LL | fn foo>(a: &T, b: f64) -> f64 where &T: Mul { + | ++++++++++++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0369`. diff --git a/src/test/ui/traits/safety-fn-body.mir.stderr b/src/test/ui/traits/safety-fn-body.mir.stderr new file mode 100644 index 000000000..ea7b2048e --- /dev/null +++ b/src/test/ui/traits/safety-fn-body.mir.stderr @@ -0,0 +1,11 @@ +error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block + --> $DIR/safety-fn-body.rs:14:9 + | +LL | *self += 1; + | ^^^^^^^^^^ dereference of raw pointer + | + = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0133`. diff --git a/src/test/ui/traits/safety-fn-body.rs b/src/test/ui/traits/safety-fn-body.rs new file mode 100644 index 000000000..2cc4fe1b3 --- /dev/null +++ b/src/test/ui/traits/safety-fn-body.rs @@ -0,0 +1,19 @@ +// Check that an unsafe impl does not imply that unsafe actions are +// legal in the methods. + +// revisions: mir thir +// [thir]compile-flags: -Z thir-unsafeck + +unsafe trait UnsafeTrait : Sized { + fn foo(self) { } +} + +unsafe impl UnsafeTrait for *mut isize { + fn foo(self) { + // Unsafe actions are not made legal by taking place in an unsafe trait: + *self += 1; + //~^ ERROR E0133 + } +} + +fn main() { } diff --git a/src/test/ui/traits/safety-fn-body.thir.stderr b/src/test/ui/traits/safety-fn-body.thir.stderr new file mode 100644 index 000000000..23696c32b --- /dev/null +++ b/src/test/ui/traits/safety-fn-body.thir.stderr @@ -0,0 +1,11 @@ +error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block + --> $DIR/safety-fn-body.rs:14:9 + | +LL | *self += 1; + | ^^^^^ dereference of raw pointer + | + = note: raw pointers may be null, dangling or unaligned; they can violate aliasing rules and cause data races: all of these are undefined behavior + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0133`. diff --git a/src/test/ui/traits/safety-inherent-impl.rs b/src/test/ui/traits/safety-inherent-impl.rs new file mode 100644 index 000000000..50e15f0d2 --- /dev/null +++ b/src/test/ui/traits/safety-inherent-impl.rs @@ -0,0 +1,9 @@ +// Check that inherent impls cannot be unsafe. + +struct SomeStruct; + +unsafe impl SomeStruct { //~ ERROR inherent impls cannot be unsafe + fn foo(self) { } +} + +fn main() { } diff --git a/src/test/ui/traits/safety-inherent-impl.stderr b/src/test/ui/traits/safety-inherent-impl.stderr new file mode 100644 index 000000000..1c8f43fec --- /dev/null +++ b/src/test/ui/traits/safety-inherent-impl.stderr @@ -0,0 +1,11 @@ +error[E0197]: inherent impls cannot be unsafe + --> $DIR/safety-inherent-impl.rs:5:13 + | +LL | unsafe impl SomeStruct { + | ------ ^^^^^^^^^^ inherent impl for this type + | | + | unsafe because of this + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0197`. diff --git a/src/test/ui/traits/safety-ok-cc.rs b/src/test/ui/traits/safety-ok-cc.rs new file mode 100644 index 000000000..099ba80e5 --- /dev/null +++ b/src/test/ui/traits/safety-ok-cc.rs @@ -0,0 +1,24 @@ +// run-pass +// aux-build:trait_safety_lib.rs + +// Simple smoke test that unsafe traits can be compiled across crates. + + +extern crate trait_safety_lib as lib; + +use lib::Foo; + +struct Bar { x: isize } +unsafe impl Foo for Bar { + fn foo(&self) -> isize { self.x } +} + +fn take_foo(f: &F) -> isize { f.foo() } + +fn main() { + let x: isize = 22; + assert_eq!(22, take_foo(&x)); + + let x: Bar = Bar { x: 23 }; + assert_eq!(23, take_foo(&x)); +} diff --git a/src/test/ui/traits/safety-ok.rs b/src/test/ui/traits/safety-ok.rs new file mode 100644 index 000000000..d456a78b6 --- /dev/null +++ b/src/test/ui/traits/safety-ok.rs @@ -0,0 +1,18 @@ +// run-pass +// Simple smoke test that unsafe traits can be compiled etc. + + +unsafe trait Foo { + fn foo(&self) -> isize; +} + +unsafe impl Foo for isize { + fn foo(&self) -> isize { *self } +} + +fn take_foo(f: &F) -> isize { f.foo() } + +fn main() { + let x: isize = 22; + assert_eq!(22, take_foo(&x)); +} diff --git a/src/test/ui/traits/safety-trait-impl-cc.rs b/src/test/ui/traits/safety-trait-impl-cc.rs new file mode 100644 index 000000000..6f125e5f9 --- /dev/null +++ b/src/test/ui/traits/safety-trait-impl-cc.rs @@ -0,0 +1,15 @@ +// aux-build:trait_safety_lib.rs + +// Check that unsafe traits require unsafe impls and that inherent +// impls cannot be unsafe. + +extern crate trait_safety_lib as lib; + +struct Bar; +impl lib::Foo for Bar { //~ ERROR requires an `unsafe impl` declaration + fn foo(&self) -> isize { + panic!(); + } +} + +fn main() { } diff --git a/src/test/ui/traits/safety-trait-impl-cc.stderr b/src/test/ui/traits/safety-trait-impl-cc.stderr new file mode 100644 index 000000000..5a0f8d3b8 --- /dev/null +++ b/src/test/ui/traits/safety-trait-impl-cc.stderr @@ -0,0 +1,13 @@ +error[E0200]: the trait `Foo` requires an `unsafe impl` declaration + --> $DIR/safety-trait-impl-cc.rs:9:1 + | +LL | / impl lib::Foo for Bar { +LL | | fn foo(&self) -> isize { +LL | | panic!(); +LL | | } +LL | | } + | |_^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0200`. diff --git a/src/test/ui/traits/safety-trait-impl.rs b/src/test/ui/traits/safety-trait-impl.rs new file mode 100644 index 000000000..45258b78d --- /dev/null +++ b/src/test/ui/traits/safety-trait-impl.rs @@ -0,0 +1,18 @@ +// Check that unsafe traits require unsafe impls and that inherent +// impls cannot be unsafe. + +trait SafeTrait { + fn foo(&self) { } +} + +unsafe trait UnsafeTrait { + fn foo(&self) { } +} + +unsafe impl UnsafeTrait for u8 { } // OK + +impl UnsafeTrait for u16 { } //~ ERROR requires an `unsafe impl` declaration + +unsafe impl SafeTrait for u32 { } //~ ERROR the trait `SafeTrait` is not unsafe + +fn main() { } diff --git a/src/test/ui/traits/safety-trait-impl.stderr b/src/test/ui/traits/safety-trait-impl.stderr new file mode 100644 index 000000000..fc0f6c693 --- /dev/null +++ b/src/test/ui/traits/safety-trait-impl.stderr @@ -0,0 +1,16 @@ +error[E0200]: the trait `UnsafeTrait` requires an `unsafe impl` declaration + --> $DIR/safety-trait-impl.rs:14:1 + | +LL | impl UnsafeTrait for u16 { } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0199]: implementing the trait `SafeTrait` is not unsafe + --> $DIR/safety-trait-impl.rs:16:1 + | +LL | unsafe impl SafeTrait for u32 { } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0199, E0200. +For more information about an error, try `rustc --explain E0199`. diff --git a/src/test/ui/traits/self-without-lifetime-constraint.rs b/src/test/ui/traits/self-without-lifetime-constraint.rs new file mode 100644 index 000000000..99013d32a --- /dev/null +++ b/src/test/ui/traits/self-without-lifetime-constraint.rs @@ -0,0 +1,53 @@ +use std::error::Error; +use std::fmt; + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum ValueRef<'a> { + Null, + Integer(i64), + Real(f64), + Text(&'a [u8]), + Blob(&'a [u8]), +} + +impl<'a> ValueRef<'a> { + pub fn as_str(&self) -> FromSqlResult<&'a str, &'a &'a str> { + match *self { + ValueRef::Text(t) => { + std::str::from_utf8(t).map_err(|_| FromSqlError::InvalidType).map(|x| (x, &x)) + } + _ => Err(FromSqlError::InvalidType), + } + } +} + +#[derive(Debug)] +#[non_exhaustive] +pub enum FromSqlError { + InvalidType +} + +impl fmt::Display for FromSqlError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "InvalidType") + } +} + +impl Error for FromSqlError {} + +pub type FromSqlResult = Result<(T, K), FromSqlError>; + +pub trait FromSql: Sized { + fn column_result(value: ValueRef<'_>) -> FromSqlResult; +} + +impl FromSql for &str { + fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> { + //~^ ERROR `impl` item signature doesn't match `trait` item signature + value.as_str() + } +} + +pub fn main() { + println!("{}", "Hello World"); +} diff --git a/src/test/ui/traits/self-without-lifetime-constraint.stderr b/src/test/ui/traits/self-without-lifetime-constraint.stderr new file mode 100644 index 000000000..85fada3b8 --- /dev/null +++ b/src/test/ui/traits/self-without-lifetime-constraint.stderr @@ -0,0 +1,19 @@ +error: `impl` item signature doesn't match `trait` item signature + --> $DIR/self-without-lifetime-constraint.rs:45:5 + | +LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult; + | -------------------------------------------------------------------- expected `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), FromSqlError>` +... +LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult<&str, &&str> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), FromSqlError>` + | + = note: expected `fn(ValueRef<'1>) -> Result<(&'2 str, &'1 &'2 str), FromSqlError>` + found `fn(ValueRef<'1>) -> Result<(&'1 str, &'1 &'1 str), FromSqlError>` +help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` + --> $DIR/self-without-lifetime-constraint.rs:41:60 + | +LL | fn column_result(value: ValueRef<'_>) -> FromSqlResult; + | ^^^^ consider borrowing this type parameter in the trait + +error: aborting due to previous error + diff --git a/src/test/ui/traits/static-method-generic-inference.rs b/src/test/ui/traits/static-method-generic-inference.rs new file mode 100644 index 000000000..f4e94c4f7 --- /dev/null +++ b/src/test/ui/traits/static-method-generic-inference.rs @@ -0,0 +1,28 @@ +// Issue #3902. We are (at least currently) unable to infer `Self` +// based on `T`, even though there is only a single impl, because of +// the possibility of associated types and other things (basically: no +// constraints on `Self` here at all). + +mod base { + pub trait HasNew { + fn new() -> T; + fn dummy(&self) { } + } + + pub struct Foo { + dummy: (), + } + + impl HasNew for Foo { + fn new() -> Foo { + Foo { dummy: () } + } + } +} + +pub fn foo() { + let _f: base::Foo = base::HasNew::new(); + //~^ ERROR E0790 +} + +fn main() { } diff --git a/src/test/ui/traits/static-method-generic-inference.stderr b/src/test/ui/traits/static-method-generic-inference.stderr new file mode 100644 index 000000000..f1b8f23ec --- /dev/null +++ b/src/test/ui/traits/static-method-generic-inference.stderr @@ -0,0 +1,17 @@ +error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type + --> $DIR/static-method-generic-inference.rs:24:25 + | +LL | fn new() -> T; + | -------------- `HasNew::new` defined here +... +LL | let _f: base::Foo = base::HasNew::new(); + | ^^^^^^^^^^^^^^^^^ cannot call associated function of trait + | +help: use the fully-qualified path to the only available implementation + | +LL | let _f: base::Foo = base::<::base::Foo as HasNew>::new(); + | +++++++++++++++ + + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0790`. diff --git a/src/test/ui/traits/static-method-overwriting.rs b/src/test/ui/traits/static-method-overwriting.rs new file mode 100644 index 000000000..f669ffae6 --- /dev/null +++ b/src/test/ui/traits/static-method-overwriting.rs @@ -0,0 +1,34 @@ +// run-pass +#![allow(dead_code)] +mod base { + pub trait HasNew { + fn new() -> Self; + } + + pub struct Foo { + dummy: (), + } + + impl ::base::HasNew for Foo { + fn new() -> Foo { + println!("Foo"); + Foo { dummy: () } + } + } + + pub struct Bar { + dummy: (), + } + + impl ::base::HasNew for Bar { + fn new() -> Bar { + println!("Bar"); + Bar { dummy: () } + } + } +} + +pub fn main() { + let _f: base::Foo = base::HasNew::new(); + let _b: base::Bar = base::HasNew::new(); +} diff --git a/src/test/ui/traits/static-outlives-a-where-clause.rs b/src/test/ui/traits/static-outlives-a-where-clause.rs new file mode 100644 index 000000000..f0c2c1082 --- /dev/null +++ b/src/test/ui/traits/static-outlives-a-where-clause.rs @@ -0,0 +1,23 @@ +// run-pass + +trait Foo<'a> { + fn xyz(self); +} +impl<'a, T> Foo<'a> for T where 'static: 'a { + fn xyz(self) {} +} + +trait Bar { + fn uvw(self); +} +impl Bar for T where for<'a> T: Foo<'a> { + fn uvw(self) { self.xyz(); } +} + +fn foo(t: T) where T: Bar { + t.uvw(); +} + +fn main() { + foo(0); +} diff --git a/src/test/ui/traits/staticness-mismatch.rs b/src/test/ui/traits/staticness-mismatch.rs new file mode 100644 index 000000000..8710d0112 --- /dev/null +++ b/src/test/ui/traits/staticness-mismatch.rs @@ -0,0 +1,11 @@ +trait Foo { + fn dummy(&self) { } + fn bar(); +} + +impl Foo for isize { + fn bar(&self) {} + //~^ ERROR method `bar` has a `&self` declaration in the impl, but not in the trait +} + +fn main() {} diff --git a/src/test/ui/traits/staticness-mismatch.stderr b/src/test/ui/traits/staticness-mismatch.stderr new file mode 100644 index 000000000..b67ac5adb --- /dev/null +++ b/src/test/ui/traits/staticness-mismatch.stderr @@ -0,0 +1,12 @@ +error[E0185]: method `bar` has a `&self` declaration in the impl, but not in the trait + --> $DIR/staticness-mismatch.rs:7:5 + | +LL | fn bar(); + | --------- trait method declared without `&self` +... +LL | fn bar(&self) {} + | ^^^^^^^^^^^^^ `&self` used in impl + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0185`. diff --git a/src/test/ui/traits/suggest-deferences/issue-39029.fixed b/src/test/ui/traits/suggest-deferences/issue-39029.fixed new file mode 100644 index 000000000..a1abf668b --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/issue-39029.fixed @@ -0,0 +1,18 @@ +// run-rustfix +use std::net::TcpListener; + +struct NoToSocketAddrs(String); + +impl std::ops::Deref for NoToSocketAddrs { + type Target = String; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +fn main() { + let _works = TcpListener::bind("some string"); + let bad = NoToSocketAddrs("bad".to_owned()); + let _errors = TcpListener::bind(&*bad); + //~^ ERROR the trait bound `NoToSocketAddrs: ToSocketAddrs` is not satisfied +} diff --git a/src/test/ui/traits/suggest-deferences/issue-39029.rs b/src/test/ui/traits/suggest-deferences/issue-39029.rs new file mode 100644 index 000000000..90d097105 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/issue-39029.rs @@ -0,0 +1,18 @@ +// run-rustfix +use std::net::TcpListener; + +struct NoToSocketAddrs(String); + +impl std::ops::Deref for NoToSocketAddrs { + type Target = String; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +fn main() { + let _works = TcpListener::bind("some string"); + let bad = NoToSocketAddrs("bad".to_owned()); + let _errors = TcpListener::bind(&bad); + //~^ ERROR the trait bound `NoToSocketAddrs: ToSocketAddrs` is not satisfied +} diff --git a/src/test/ui/traits/suggest-deferences/issue-39029.stderr b/src/test/ui/traits/suggest-deferences/issue-39029.stderr new file mode 100644 index 000000000..5c324cd38 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/issue-39029.stderr @@ -0,0 +1,20 @@ +error[E0277]: the trait bound `NoToSocketAddrs: ToSocketAddrs` is not satisfied + --> $DIR/issue-39029.rs:16:37 + | +LL | let _errors = TcpListener::bind(&bad); + | ----------------- ^^^^ + | | | + | | the trait `ToSocketAddrs` is not implemented for `NoToSocketAddrs` + | | help: consider dereferencing here: `&*bad` + | required by a bound introduced by this call + | + = note: required because of the requirements on the impl of `ToSocketAddrs` for `&NoToSocketAddrs` +note: required by a bound in `TcpListener::bind` + --> $SRC_DIR/std/src/net/tcp.rs:LL:COL + | +LL | pub fn bind(addr: A) -> io::Result { + | ^^^^^^^^^^^^^ required by this bound in `TcpListener::bind` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/suggest-deferences/issue-62530.fixed b/src/test/ui/traits/suggest-deferences/issue-62530.fixed new file mode 100644 index 000000000..406caaa00 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/issue-62530.fixed @@ -0,0 +1,15 @@ +// run-rustfix +fn takes_str(_x: &str) {} + +fn takes_type_parameter(_x: T) where T: SomeTrait {} + +trait SomeTrait {} +impl SomeTrait for &'_ str {} +impl SomeTrait for char {} + +fn main() { + let string = String::new(); + takes_str(&string); // Ok + takes_type_parameter(&*string); // Error + //~^ ERROR the trait bound `&String: SomeTrait` is not satisfied +} diff --git a/src/test/ui/traits/suggest-deferences/issue-62530.rs b/src/test/ui/traits/suggest-deferences/issue-62530.rs new file mode 100644 index 000000000..53846be73 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/issue-62530.rs @@ -0,0 +1,15 @@ +// run-rustfix +fn takes_str(_x: &str) {} + +fn takes_type_parameter(_x: T) where T: SomeTrait {} + +trait SomeTrait {} +impl SomeTrait for &'_ str {} +impl SomeTrait for char {} + +fn main() { + let string = String::new(); + takes_str(&string); // Ok + takes_type_parameter(&string); // Error + //~^ ERROR the trait bound `&String: SomeTrait` is not satisfied +} diff --git a/src/test/ui/traits/suggest-deferences/issue-62530.stderr b/src/test/ui/traits/suggest-deferences/issue-62530.stderr new file mode 100644 index 000000000..d129328da --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/issue-62530.stderr @@ -0,0 +1,19 @@ +error[E0277]: the trait bound `&String: SomeTrait` is not satisfied + --> $DIR/issue-62530.rs:13:26 + | +LL | takes_type_parameter(&string); // Error + | -------------------- ^^^^^^^ + | | | + | | the trait `SomeTrait` is not implemented for `&String` + | | help: consider dereferencing here: `&*string` + | required by a bound introduced by this call + | +note: required by a bound in `takes_type_parameter` + --> $DIR/issue-62530.rs:4:44 + | +LL | fn takes_type_parameter(_x: T) where T: SomeTrait {} + | ^^^^^^^^^ required by this bound in `takes_type_parameter` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/suggest-deferences/multiple-0.fixed b/src/test/ui/traits/suggest-deferences/multiple-0.fixed new file mode 100644 index 000000000..b7160b75c --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/multiple-0.fixed @@ -0,0 +1,36 @@ +// run-rustfix +use std::ops::Deref; + +trait Happy {} +struct LDM; +impl Happy for &LDM {} + +struct Foo(LDM); +struct Bar(Foo); +struct Baz(Bar); +impl Deref for Foo { + type Target = LDM; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl Deref for Bar { + type Target = Foo; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl Deref for Baz { + type Target = Bar; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +fn foo(_: T) where T: Happy {} + +fn main() { + let baz = Baz(Bar(Foo(LDM))); + foo(&***baz); + //~^ ERROR the trait bound `&Baz: Happy` is not satisfied +} diff --git a/src/test/ui/traits/suggest-deferences/multiple-0.rs b/src/test/ui/traits/suggest-deferences/multiple-0.rs new file mode 100644 index 000000000..9ac55177f --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/multiple-0.rs @@ -0,0 +1,36 @@ +// run-rustfix +use std::ops::Deref; + +trait Happy {} +struct LDM; +impl Happy for &LDM {} + +struct Foo(LDM); +struct Bar(Foo); +struct Baz(Bar); +impl Deref for Foo { + type Target = LDM; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl Deref for Bar { + type Target = Foo; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl Deref for Baz { + type Target = Bar; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +fn foo(_: T) where T: Happy {} + +fn main() { + let baz = Baz(Bar(Foo(LDM))); + foo(&baz); + //~^ ERROR the trait bound `&Baz: Happy` is not satisfied +} diff --git a/src/test/ui/traits/suggest-deferences/multiple-0.stderr b/src/test/ui/traits/suggest-deferences/multiple-0.stderr new file mode 100644 index 000000000..efb3c7d12 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/multiple-0.stderr @@ -0,0 +1,19 @@ +error[E0277]: the trait bound `&Baz: Happy` is not satisfied + --> $DIR/multiple-0.rs:34:9 + | +LL | foo(&baz); + | --- ^^^^ + | | | + | | the trait `Happy` is not implemented for `&Baz` + | | help: consider dereferencing here: `&***baz` + | required by a bound introduced by this call + | +note: required by a bound in `foo` + --> $DIR/multiple-0.rs:30:26 + | +LL | fn foo(_: T) where T: Happy {} + | ^^^^^ required by this bound in `foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/suggest-deferences/multiple-1.rs b/src/test/ui/traits/suggest-deferences/multiple-1.rs new file mode 100644 index 000000000..91c6c7924 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/multiple-1.rs @@ -0,0 +1,54 @@ +use std::ops::{Deref, DerefMut}; + +trait Happy {} +struct LDM; +impl Happy for &mut LDM {} + +struct Foo(LDM); +struct Bar(Foo); +struct Baz(Bar); +impl Deref for Foo { + type Target = LDM; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl Deref for Bar { + type Target = Foo; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl Deref for Baz { + type Target = Bar; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl DerefMut for Foo { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} +impl DerefMut for Bar { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} +impl DerefMut for Baz { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + + +fn foo(_: T) where T: Happy {} + +fn main() { + // Currently the compiler doesn't try to suggest dereferences for situations + // where DerefMut involves. So this test is meant to ensure compiler doesn't + // generate incorrect help message. + let mut baz = Baz(Bar(Foo(LDM))); + foo(&mut baz); + //~^ ERROR the trait bound `&mut Baz: Happy` is not satisfied +} diff --git a/src/test/ui/traits/suggest-deferences/multiple-1.stderr b/src/test/ui/traits/suggest-deferences/multiple-1.stderr new file mode 100644 index 000000000..6e12321c2 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/multiple-1.stderr @@ -0,0 +1,18 @@ +error[E0277]: the trait bound `&mut Baz: Happy` is not satisfied + --> $DIR/multiple-1.rs:52:9 + | +LL | foo(&mut baz); + | --- ^^^^^^^^ the trait `Happy` is not implemented for `&mut Baz` + | | + | required by a bound introduced by this call + | + = help: the trait `Happy` is implemented for `&mut LDM` +note: required by a bound in `foo` + --> $DIR/multiple-1.rs:45:26 + | +LL | fn foo(_: T) where T: Happy {} + | ^^^^^ required by this bound in `foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/suggest-deferences/root-obligation.fixed b/src/test/ui/traits/suggest-deferences/root-obligation.fixed new file mode 100644 index 000000000..7a8433f90 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/root-obligation.fixed @@ -0,0 +1,13 @@ +// run-rustfix + +fn get_vowel_count(string: &str) -> usize { + string + .chars() + .filter(|c| "aeiou".contains(*c)) + //~^ ERROR expected a `Fn<(char,)>` closure, found `char` + .count() +} + +fn main() { + let _ = get_vowel_count("asdf"); +} diff --git a/src/test/ui/traits/suggest-deferences/root-obligation.rs b/src/test/ui/traits/suggest-deferences/root-obligation.rs new file mode 100644 index 000000000..51bac2107 --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/root-obligation.rs @@ -0,0 +1,13 @@ +// run-rustfix + +fn get_vowel_count(string: &str) -> usize { + string + .chars() + .filter(|c| "aeiou".contains(c)) + //~^ ERROR expected a `Fn<(char,)>` closure, found `char` + .count() +} + +fn main() { + let _ = get_vowel_count("asdf"); +} diff --git a/src/test/ui/traits/suggest-deferences/root-obligation.stderr b/src/test/ui/traits/suggest-deferences/root-obligation.stderr new file mode 100644 index 000000000..16e03e79c --- /dev/null +++ b/src/test/ui/traits/suggest-deferences/root-obligation.stderr @@ -0,0 +1,24 @@ +error[E0277]: expected a `Fn<(char,)>` closure, found `char` + --> $DIR/root-obligation.rs:6:38 + | +LL | .filter(|c| "aeiou".contains(c)) + | -------- ^ expected an `Fn<(char,)>` closure, found `char` + | | + | required by a bound introduced by this call + | + = help: the trait `Fn<(char,)>` is not implemented for `char` + = note: required because of the requirements on the impl of `FnOnce<(char,)>` for `&char` + = note: required because of the requirements on the impl of `Pattern<'_>` for `&char` +note: required by a bound in `core::str::::contains` + --> $SRC_DIR/core/src/str/mod.rs:LL:COL + | +LL | pub fn contains<'a, P: Pattern<'a>>(&'a self, pat: P) -> bool { + | ^^^^^^^^^^^ required by this bound in `core::str::::contains` +help: consider dereferencing here + | +LL | .filter(|c| "aeiou".contains(*c)) + | + + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.rs b/src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.rs new file mode 100644 index 000000000..9a2cf469d --- /dev/null +++ b/src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.rs @@ -0,0 +1,60 @@ +use std::ops::{Deref, DerefMut}; + +struct Thing; + +trait Method { + fn method(&self) -> T; + fn mut_method(&mut self) -> T; +} + +impl Method for Thing { + fn method(&self) -> i32 { 0 } + fn mut_method(&mut self) -> i32 { 0 } +} + +impl Method for Thing { + fn method(&self) -> u32 { 0 } + fn mut_method(&mut self) -> u32 { 0 } +} +trait MethodRef { + fn by_self(self); +} +impl MethodRef for &Thing { + fn by_self(self) {} +} +impl MethodRef for &Thing { + fn by_self(self) {} +} + + +struct DerefsTo(T); +impl Deref for DerefsTo { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl DerefMut for DerefsTo { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +fn main() { + let mut thing = Thing; + thing.method(); + //~^ ERROR type annotations needed + //~| ERROR type annotations needed + thing.mut_method(); //~ ERROR type annotations needed + thing.by_self(); //~ ERROR type annotations needed + + let mut deref_to = DerefsTo(Thing); + deref_to.method(); //~ ERROR type annotations needed + deref_to.mut_method(); //~ ERROR type annotations needed + deref_to.by_self(); //~ ERROR type annotations needed + + let mut deref_deref_to = DerefsTo(DerefsTo(Thing)); + deref_deref_to.method(); //~ ERROR type annotations needed + deref_deref_to.mut_method(); //~ ERROR type annotations needed + deref_deref_to.by_self(); //~ ERROR type annotations needed +} diff --git a/src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.stderr b/src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.stderr new file mode 100644 index 000000000..68b31a1ca --- /dev/null +++ b/src/test/ui/traits/suggest-fully-qualified-path-with-adjustment.stderr @@ -0,0 +1,186 @@ +error[E0282]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:45:11 + | +LL | thing.method(); + | ^^^^^^ + | +help: try using a fully qualified path to specify the expected types + | +LL | >::method(&thing); + | ++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:45:11 + | +LL | thing.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::method(&thing); + | ++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:48:11 + | +LL | thing.mut_method(); + | ^^^^^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::mut_method(&mut thing); + | +++++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:49:11 + | +LL | thing.by_self(); + | ^^^^^^^ + | +note: multiple `impl`s satisfying `&Thing: MethodRef<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:22:1 + | +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | <&Thing as MethodRef>::by_self(&thing); + | +++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:52:14 + | +LL | deref_to.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::method(&deref_to); + | ++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:53:14 + | +LL | deref_to.mut_method(); + | ^^^^^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::mut_method(&mut deref_to); + | +++++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:54:14 + | +LL | deref_to.by_self(); + | ^^^^^^^ + | +note: multiple `impl`s satisfying `&Thing: MethodRef<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:22:1 + | +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | <&Thing as MethodRef>::by_self(&deref_to); + | +++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:57:20 + | +LL | deref_deref_to.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::method(&deref_deref_to); + | ++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:58:20 + | +LL | deref_deref_to.mut_method(); + | ^^^^^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::mut_method(&mut deref_deref_to); + | +++++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:59:20 + | +LL | deref_deref_to.by_self(); + | ^^^^^^^ + | +note: multiple `impl`s satisfying `&Thing: MethodRef<_>` found + --> $DIR/suggest-fully-qualified-path-with-adjustment.rs:22:1 + | +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | <&Thing as MethodRef>::by_self(&deref_deref_to); + | +++++++++++++++++++++++++++++++++++ ~ + +error: aborting due to 10 previous errors + +Some errors have detailed explanations: E0282, E0283. +For more information about an error, try `rustc --explain E0282`. diff --git a/src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.rs b/src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.rs new file mode 100644 index 000000000..da640c8c8 --- /dev/null +++ b/src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.rs @@ -0,0 +1,64 @@ +use std::ops::{Deref, DerefMut}; + +struct Thing; + +trait Method { + fn method(&self) -> T; + fn mut_method(&mut self) -> T; +} + +impl Method for Thing { + fn method(&self) -> i32 { 0 } + fn mut_method(&mut self) -> i32 { 0 } +} + +impl Method for Thing { + fn method(&self) -> u32 { 0 } + fn mut_method(&mut self) -> u32 { 0 } +} + +trait MethodRef { + fn by_self(self); +} +impl MethodRef for &Thing { + fn by_self(self) {} +} +impl MethodRef for &Thing { + fn by_self(self) {} +} + +struct DerefsTo(T); +impl Deref for DerefsTo { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } +} +impl DerefMut for DerefsTo { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +fn main() { + let mut ref_thing = &Thing; + ref_thing.method(); + //~^ ERROR type annotations needed + //~| ERROR type annotations needed + ref_thing.by_self(); //~ ERROR type annotations needed + + let mut mut_thing = &mut Thing; + mut_thing.method(); //~ ERROR type annotations needed + mut_thing.mut_method(); //~ ERROR type annotations needed + mut_thing.by_self(); //~ ERROR type annotations needed + + let mut deref_to = &DerefsTo(Thing); + deref_to.method(); //~ ERROR type annotations needed + deref_to.mut_method(); //~ ERROR type annotations needed + deref_to.by_self(); //~ ERROR type annotations needed + + let mut deref_deref_to = &DerefsTo(DerefsTo(Thing)); + deref_deref_to.method(); //~ ERROR type annotations needed + deref_deref_to.mut_method(); //~ ERROR type annotations needed + deref_deref_to.by_self(); //~ ERROR type annotations needed +} diff --git a/src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.stderr b/src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.stderr new file mode 100644 index 000000000..27518a54e --- /dev/null +++ b/src/test/ui/traits/suggest-fully-qualified-path-without-adjustment.stderr @@ -0,0 +1,224 @@ +error[E0282]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:45:15 + | +LL | ref_thing.method(); + | ^^^^^^ + | +help: try using a fully qualified path to specify the expected types + | +LL | >::method(ref_thing); + | +++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:45:15 + | +LL | ref_thing.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::method(ref_thing); + | +++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:48:15 + | +LL | ref_thing.by_self(); + | ^^^^^^^ + | +note: multiple `impl`s satisfying `&Thing: MethodRef<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:23:1 + | +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | <&Thing as MethodRef>::by_self(ref_thing); + | ++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:51:15 + | +LL | mut_thing.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::method(mut_thing); + | +++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:52:15 + | +LL | mut_thing.mut_method(); + | ^^^^^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::mut_method(mut_thing); + | +++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:53:15 + | +LL | mut_thing.by_self(); + | ^^^^^^^ + | +note: multiple `impl`s satisfying `&Thing: MethodRef<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:23:1 + | +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | <&Thing as MethodRef>::by_self(mut_thing); + | ++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:56:14 + | +LL | deref_to.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::method(deref_to); + | +++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:57:14 + | +LL | deref_to.mut_method(); + | ^^^^^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::mut_method(deref_to); + | +++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:58:14 + | +LL | deref_to.by_self(); + | ^^^^^^^ + | +note: multiple `impl`s satisfying `&Thing: MethodRef<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:23:1 + | +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | <&Thing as MethodRef>::by_self(deref_to); + | ++++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:61:20 + | +LL | deref_deref_to.method(); + | ^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::method(deref_deref_to); + | +++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:62:20 + | +LL | deref_deref_to.mut_method(); + | ^^^^^^^^^^ + | +note: multiple `impl`s satisfying `Thing: Method<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:10:1 + | +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Method for Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | >::mut_method(deref_deref_to); + | +++++++++++++++++++++++++++++++++ ~ + +error[E0283]: type annotations needed + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:63:20 + | +LL | deref_deref_to.by_self(); + | ^^^^^^^ + | +note: multiple `impl`s satisfying `&Thing: MethodRef<_>` found + --> $DIR/suggest-fully-qualified-path-without-adjustment.rs:23:1 + | +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl MethodRef for &Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try using a fully qualified path to specify the expected types + | +LL | <&Thing as MethodRef>::by_self(deref_deref_to); + | ++++++++++++++++++++++++++++++++++ ~ + +error: aborting due to 12 previous errors + +Some errors have detailed explanations: E0282, E0283. +For more information about an error, try `rustc --explain E0282`. diff --git a/src/test/ui/traits/suggest-where-clause.rs b/src/test/ui/traits/suggest-where-clause.rs new file mode 100644 index 000000000..46d047a2d --- /dev/null +++ b/src/test/ui/traits/suggest-where-clause.rs @@ -0,0 +1,36 @@ +use std::mem; + +struct Misc(T); + +fn check() { + // suggest a where-clause, if needed + mem::size_of::(); + //~^ ERROR the size for values of type + + mem::size_of::>(); + //~^ ERROR the size for values of type + + // ... even if T occurs as a type parameter + + >::from; + //~^ ERROR `u64: From` is not satisfied + + ::Item>>::from; + //~^ ERROR `u64: From<::Item>` is not satisfied + + // ... but not if there are inference variables + + as From>::from; + //~^ ERROR `Misc<_>: From` is not satisfied + + // ... and also not if the error is not related to the type + + mem::size_of::<[T]>(); + //~^ ERROR the size for values of type + + mem::size_of::<[&U]>(); + //~^ ERROR the size for values of type +} + +fn main() { +} diff --git a/src/test/ui/traits/suggest-where-clause.stderr b/src/test/ui/traits/suggest-where-clause.stderr new file mode 100644 index 000000000..d4d9b4967 --- /dev/null +++ b/src/test/ui/traits/suggest-where-clause.stderr @@ -0,0 +1,102 @@ +error[E0277]: the size for values of type `U` cannot be known at compilation time + --> $DIR/suggest-where-clause.rs:7:20 + | +LL | fn check() { + | - this type parameter needs to be `std::marker::Sized` +LL | // suggest a where-clause, if needed +LL | mem::size_of::(); + | ^ doesn't have a size known at compile-time + | +note: required by a bound in `std::mem::size_of` + --> $SRC_DIR/core/src/mem/mod.rs:LL:COL + | +LL | pub const fn size_of() -> usize { + | ^ required by this bound in `std::mem::size_of` +help: consider removing the `?Sized` bound to make the type parameter `Sized` + | +LL - fn check() { +LL + fn check() { + | + +error[E0277]: the size for values of type `U` cannot be known at compilation time + --> $DIR/suggest-where-clause.rs:10:5 + | +LL | fn check() { + | - this type parameter needs to be `std::marker::Sized` +... +LL | mem::size_of::>(); + | ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | +note: required because it appears within the type `Misc` + --> $DIR/suggest-where-clause.rs:3:8 + | +LL | struct Misc(T); + | ^^^^ +note: required by a bound in `std::mem::size_of` + --> $SRC_DIR/core/src/mem/mod.rs:LL:COL + | +LL | pub const fn size_of() -> usize { + | ^ required by this bound in `std::mem::size_of` +help: consider removing the `?Sized` bound to make the type parameter `Sized` + | +LL - fn check() { +LL + fn check() { + | + +error[E0277]: the trait bound `u64: From` is not satisfied + --> $DIR/suggest-where-clause.rs:15:5 + | +LL | >::from; + | ^^^^^^^^^^^^^^^^^^^^^^ the trait `From` is not implemented for `u64` + | +help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement + | +LL | fn check() where u64: From { + | ++++++++++++++++++ + +error[E0277]: the trait bound `u64: From<::Item>` is not satisfied + --> $DIR/suggest-where-clause.rs:18:5 + | +LL | ::Item>>::from; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<::Item>` is not implemented for `u64` + | +help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement + | +LL | fn check() where u64: From<::Item> { + | ++++++++++++++++++++++++++++++++++++++ + +error[E0277]: the trait bound `Misc<_>: From` is not satisfied + --> $DIR/suggest-where-clause.rs:23:5 + | +LL | as From>::from; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From` is not implemented for `Misc<_>` + +error[E0277]: the size for values of type `[T]` cannot be known at compilation time + --> $DIR/suggest-where-clause.rs:28:20 + | +LL | mem::size_of::<[T]>(); + | ^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[T]` +note: required by a bound in `std::mem::size_of` + --> $SRC_DIR/core/src/mem/mod.rs:LL:COL + | +LL | pub const fn size_of() -> usize { + | ^ required by this bound in `std::mem::size_of` + +error[E0277]: the size for values of type `[&U]` cannot be known at compilation time + --> $DIR/suggest-where-clause.rs:31:20 + | +LL | mem::size_of::<[&U]>(); + | ^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `[&U]` +note: required by a bound in `std::mem::size_of` + --> $SRC_DIR/core/src/mem/mod.rs:LL:COL + | +LL | pub const fn size_of() -> usize { + | ^ required by this bound in `std::mem::size_of` + +error: aborting due to 7 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/superdefault-generics.rs b/src/test/ui/traits/superdefault-generics.rs new file mode 100644 index 000000000..e862c0e97 --- /dev/null +++ b/src/test/ui/traits/superdefault-generics.rs @@ -0,0 +1,39 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_snake_case)] + +// There is some other borrowck bug, so we make the stuff not mut. + + +use std::ops::Add; + +trait Positioned { + fn SetX(&mut self, _: S); + fn X(&self) -> S; +} + +trait Movable>: Positioned { + fn translate(&mut self, dx: S) { + let x = self.X() + dx; + self.SetX(x); + } +} + +struct Point { x: S, y: S } + +impl Positioned for Point { + fn SetX(&mut self, x: S) { + self.x = x; + } + fn X(&self) -> S { + self.x.clone() + } +} + +impl> Movable for Point {} + +pub fn main() { + let mut p = Point{ x: 1, y: 2}; + p.translate(3); + assert_eq!(p.X(), 4); +} diff --git a/src/test/ui/traits/syntax-polarity.rs b/src/test/ui/traits/syntax-polarity.rs new file mode 100644 index 000000000..c809f9e89 --- /dev/null +++ b/src/test/ui/traits/syntax-polarity.rs @@ -0,0 +1,21 @@ +// run-pass +#![allow(dead_code)] +// pretty-expanded FIXME #23616 + +#![feature(negative_impls)] + +struct TestType; + +impl TestType {} + +trait TestTrait {} + +impl !Send for TestType {} + +struct TestType2(T); + +impl TestType2 {} + +impl !Send for TestType2 {} + +fn main() {} diff --git a/src/test/ui/traits/syntax-trait-polarity.rs b/src/test/ui/traits/syntax-trait-polarity.rs new file mode 100644 index 000000000..ed2947493 --- /dev/null +++ b/src/test/ui/traits/syntax-trait-polarity.rs @@ -0,0 +1,25 @@ +#![feature(negative_impls)] + +use std::marker::Send; + +struct TestType; + +impl !TestType {} +//~^ ERROR inherent impls cannot be negative + +trait TestTrait {} + +unsafe impl !Send for TestType {} +//~^ ERROR negative impls cannot be unsafe +impl !TestTrait for TestType {} + +struct TestType2(T); + +impl !TestType2 {} +//~^ ERROR inherent impls cannot be negative + +unsafe impl !Send for TestType2 {} +//~^ ERROR negative impls cannot be unsafe +impl !TestTrait for TestType2 {} + +fn main() {} diff --git a/src/test/ui/traits/syntax-trait-polarity.stderr b/src/test/ui/traits/syntax-trait-polarity.stderr new file mode 100644 index 000000000..1fd40fb66 --- /dev/null +++ b/src/test/ui/traits/syntax-trait-polarity.stderr @@ -0,0 +1,37 @@ +error: inherent impls cannot be negative + --> $DIR/syntax-trait-polarity.rs:7:7 + | +LL | impl !TestType {} + | -^^^^^^^^ inherent impl for this type + | | + | negative because of this + +error[E0198]: negative impls cannot be unsafe + --> $DIR/syntax-trait-polarity.rs:12:13 + | +LL | unsafe impl !Send for TestType {} + | ------ -^^^^ + | | | + | | negative because of this + | unsafe because of this + +error: inherent impls cannot be negative + --> $DIR/syntax-trait-polarity.rs:18:10 + | +LL | impl !TestType2 {} + | -^^^^^^^^^^^^ inherent impl for this type + | | + | negative because of this + +error[E0198]: negative impls cannot be unsafe + --> $DIR/syntax-trait-polarity.rs:21:16 + | +LL | unsafe impl !Send for TestType2 {} + | ------ -^^^^ + | | | + | | negative because of this + | unsafe because of this + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0198`. diff --git a/src/test/ui/traits/test-2.rs b/src/test/ui/traits/test-2.rs new file mode 100644 index 000000000..342928e88 --- /dev/null +++ b/src/test/ui/traits/test-2.rs @@ -0,0 +1,17 @@ +#[allow(non_camel_case_types)] + + +trait bar { fn dup(&self) -> Self; fn blah(&self); } +impl bar for i32 { fn dup(&self) -> i32 { *self } fn blah(&self) {} } +impl bar for u32 { fn dup(&self) -> u32 { *self } fn blah(&self) {} } + +fn main() { + 10.dup::(); + //~^ ERROR this associated function takes 0 generic arguments but 1 + 10.blah::(); + //~^ ERROR this associated function takes 1 generic argument but 2 + (Box::new(10) as Box).dup(); + //~^ ERROR E0038 + //~| ERROR E0038 + //~| ERROR E0038 +} diff --git a/src/test/ui/traits/test-2.stderr b/src/test/ui/traits/test-2.stderr new file mode 100644 index 000000000..77ea4e4e9 --- /dev/null +++ b/src/test/ui/traits/test-2.stderr @@ -0,0 +1,85 @@ +error[E0107]: this associated function takes 0 generic arguments but 1 generic argument was supplied + --> $DIR/test-2.rs:9:8 + | +LL | 10.dup::(); + | ^^^------- help: remove these generics + | | + | expected 0 generic arguments + | +note: associated function defined here, with 0 generic parameters + --> $DIR/test-2.rs:4:16 + | +LL | trait bar { fn dup(&self) -> Self; fn blah(&self); } + | ^^^ + +error[E0107]: this associated function takes 1 generic argument but 2 generic arguments were supplied + --> $DIR/test-2.rs:11:8 + | +LL | 10.blah::(); + | ^^^^ --- help: remove this generic argument + | | + | expected 1 generic argument + | +note: associated function defined here, with 1 generic parameter: `X` + --> $DIR/test-2.rs:4:39 + | +LL | trait bar { fn dup(&self) -> Self; fn blah(&self); } + | ^^^^ - + +error[E0038]: the trait `bar` cannot be made into an object + --> $DIR/test-2.rs:13:22 + | +LL | (Box::new(10) as Box).dup(); + | ^^^^^^^^^^^^ `bar` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/test-2.rs:4:30 + | +LL | trait bar { fn dup(&self) -> Self; fn blah(&self); } + | --- ^^^^ ^^^^ ...because method `blah` has generic type parameters + | | | + | | ...because method `dup` references the `Self` type in its return type + | this trait cannot be made into an object... + = help: consider moving `dup` to another trait + = help: consider moving `blah` to another trait + +error[E0038]: the trait `bar` cannot be made into an object + --> $DIR/test-2.rs:13:5 + | +LL | (Box::new(10) as Box).dup(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `bar` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/test-2.rs:4:30 + | +LL | trait bar { fn dup(&self) -> Self; fn blah(&self); } + | --- ^^^^ ^^^^ ...because method `blah` has generic type parameters + | | | + | | ...because method `dup` references the `Self` type in its return type + | this trait cannot be made into an object... + = help: consider moving `dup` to another trait + = help: consider moving `blah` to another trait + +error[E0038]: the trait `bar` cannot be made into an object + --> $DIR/test-2.rs:13:6 + | +LL | (Box::new(10) as Box).dup(); + | ^^^^^^^^^^^^ `bar` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/test-2.rs:4:30 + | +LL | trait bar { fn dup(&self) -> Self; fn blah(&self); } + | --- ^^^^ ^^^^ ...because method `blah` has generic type parameters + | | | + | | ...because method `dup` references the `Self` type in its return type + | this trait cannot be made into an object... + = help: consider moving `dup` to another trait + = help: consider moving `blah` to another trait + = note: required because of the requirements on the impl of `CoerceUnsized>` for `Box<{integer}>` + = note: required by cast to type `Box` + +error: aborting due to 5 previous errors + +Some errors have detailed explanations: E0038, E0107. +For more information about an error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/test.rs b/src/test/ui/traits/test.rs new file mode 100644 index 000000000..72fc66f74 --- /dev/null +++ b/src/test/ui/traits/test.rs @@ -0,0 +1,6 @@ +#[allow(non_camel_case_types)] +trait foo { fn foo(&self); } + +impl isize for usize { fn foo(&self) {} } //~ ERROR trait + +fn main() {} diff --git a/src/test/ui/traits/test.stderr b/src/test/ui/traits/test.stderr new file mode 100644 index 000000000..668228abe --- /dev/null +++ b/src/test/ui/traits/test.stderr @@ -0,0 +1,9 @@ +error[E0404]: expected trait, found builtin type `isize` + --> $DIR/test.rs:4:6 + | +LL | impl isize for usize { fn foo(&self) {} } + | ^^^^^ not a trait + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0404`. diff --git a/src/test/ui/traits/to-str.rs b/src/test/ui/traits/to-str.rs new file mode 100644 index 000000000..9670edbfa --- /dev/null +++ b/src/test/ui/traits/to-str.rs @@ -0,0 +1,36 @@ +// run-pass +#![allow(non_camel_case_types)] + + +trait to_str { + fn to_string_(&self) -> String; +} + +impl to_str for isize { + fn to_string_(&self) -> String { self.to_string() } +} + +impl to_str for Vec { + fn to_string_(&self) -> String { + format!("[{}]", + self.iter() + .map(|e| e.to_string_()) + .collect::>() + .join(", ")) + } +} + +pub fn main() { + assert_eq!(1.to_string_(), "1".to_string()); + assert_eq!((vec![2, 3, 4]).to_string_(), "[2, 3, 4]".to_string()); + + fn indirect(x: T) -> String { + format!("{}!", x.to_string_()) + } + assert_eq!(indirect(vec![10, 20]), "[10, 20]!".to_string()); + + fn indirect2(x: T) -> String { + indirect(x) + } + assert_eq!(indirect2(vec![1]), "[1]!".to_string()); +} diff --git a/src/test/ui/traits/trait-or-new-type-instead.rs b/src/test/ui/traits/trait-or-new-type-instead.rs new file mode 100644 index 000000000..572b03efc --- /dev/null +++ b/src/test/ui/traits/trait-or-new-type-instead.rs @@ -0,0 +1,6 @@ +impl Option { +//~^ ERROR cannot define inherent `impl` for a type outside of the crate where the type is defined + pub fn foo(&self) { } +} + +fn main() { } diff --git a/src/test/ui/traits/trait-or-new-type-instead.stderr b/src/test/ui/traits/trait-or-new-type-instead.stderr new file mode 100644 index 000000000..4726b0668 --- /dev/null +++ b/src/test/ui/traits/trait-or-new-type-instead.stderr @@ -0,0 +1,14 @@ +error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined + --> $DIR/trait-or-new-type-instead.rs:1:1 + | +LL | / impl Option { +LL | | +LL | | pub fn foo(&self) { } +LL | | } + | |_^ impl for type defined outside of crate. + | + = note: define and implement a trait or new type instead + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0116`. diff --git a/src/test/ui/traits/trait-upcasting/basic.rs b/src/test/ui/traits/trait-upcasting/basic.rs new file mode 100644 index 000000000..484a222bc --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/basic.rs @@ -0,0 +1,87 @@ +// run-pass + +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: PartialEq + std::fmt::Debug + Send + Sync { + fn a(&self) -> i32 { + 10 + } + + fn z(&self) -> i32 { + 11 + } + + fn y(&self) -> i32 { + 12 + } +} + +trait Bar: Foo { + fn b(&self) -> i32 { + 20 + } + + fn w(&self) -> i32 { + 21 + } +} + +trait Baz: Bar { + fn c(&self) -> i32 { + 30 + } +} + +impl Foo for i32 { + fn a(&self) -> i32 { + 100 + } +} + +impl Bar for i32 { + fn b(&self) -> i32 { + 200 + } +} + +impl Baz for i32 { + fn c(&self) -> i32 { + 300 + } +} + +fn main() { + let baz: &dyn Baz = &1; + let _: &dyn std::fmt::Debug = baz; + assert_eq!(*baz, 1); + assert_eq!(baz.a(), 100); + assert_eq!(baz.b(), 200); + assert_eq!(baz.c(), 300); + assert_eq!(baz.z(), 11); + assert_eq!(baz.y(), 12); + assert_eq!(baz.w(), 21); + + let bar: &dyn Bar = baz; + let _: &dyn std::fmt::Debug = bar; + assert_eq!(*bar, 1); + assert_eq!(bar.a(), 100); + assert_eq!(bar.b(), 200); + assert_eq!(bar.z(), 11); + assert_eq!(bar.y(), 12); + assert_eq!(bar.w(), 21); + + let foo: &dyn Foo = baz; + let _: &dyn std::fmt::Debug = foo; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); + + let foo: &dyn Foo = bar; + let _: &dyn std::fmt::Debug = foo; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); +} diff --git a/src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs b/src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs new file mode 100644 index 000000000..8d0a9ef0a --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs @@ -0,0 +1,39 @@ +// run-pass +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: Bar + Bar {} +trait Bar { + fn bar(&self) -> String { + T::default().to_string() + } +} + +struct S1; + +impl Bar for S1 {} +impl Foo for S1 {} + +struct S2; +impl Bar for S2 {} +impl Bar for S2 {} +impl Foo for S2 {} + +fn test1(x: &dyn Foo) { + let s = x as &dyn Bar; + assert_eq!("0", &s.bar().to_string()); +} + +fn test2(x: &dyn Foo) { + let p = x as &dyn Bar; + assert_eq!("0", &p.bar().to_string()); + let q = x as &dyn Bar; + assert_eq!("false", &q.bar().to_string()); +} + +fn main() { + let s1 = S1; + test1(&s1); + let s2 = S2; + test2(&s2); +} diff --git a/src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.rs b/src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.rs new file mode 100644 index 000000000..511e41562 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.rs @@ -0,0 +1,13 @@ +trait A: B + A {} +//~^ ERROR cycle detected when computing the super predicates of `A` [E0391] + +trait B {} + +impl A for () {} + +impl B for () {} + +fn main() { + let a: Box = Box::new(()); + let _b: Box = a; +} diff --git a/src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr b/src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr new file mode 100644 index 000000000..15faab16a --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/cyclic-trait-resolution.stderr @@ -0,0 +1,21 @@ +error[E0391]: cycle detected when computing the super predicates of `A` + --> $DIR/cyclic-trait-resolution.rs:1:1 + | +LL | trait A: B + A {} + | ^^^^^^^^^^^^^^ + | +note: ...which requires computing the super traits of `A`... + --> $DIR/cyclic-trait-resolution.rs:1:14 + | +LL | trait A: B + A {} + | ^ + = note: ...which again requires computing the super predicates of `A`, completing the cycle +note: cycle used when collecting item types in top-level module + --> $DIR/cyclic-trait-resolution.rs:1:1 + | +LL | trait A: B + A {} + | ^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0391`. diff --git a/src/test/ui/traits/trait-upcasting/diamond.rs b/src/test/ui/traits/trait-upcasting/diamond.rs new file mode 100644 index 000000000..e4e23c1a2 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/diamond.rs @@ -0,0 +1,115 @@ +// run-pass + +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: PartialEq + std::fmt::Debug + Send + Sync { + fn a(&self) -> i32 { + 10 + } + + fn z(&self) -> i32 { + 11 + } + + fn y(&self) -> i32 { + 12 + } +} + +trait Bar1: Foo { + fn b(&self) -> i32 { + 20 + } + + fn w(&self) -> i32 { + 21 + } +} + +trait Bar2: Foo { + fn c(&self) -> i32 { + 30 + } + + fn v(&self) -> i32 { + 31 + } +} + +trait Baz: Bar1 + Bar2 { + fn d(&self) -> i32 { + 40 + } +} + +impl Foo for i32 { + fn a(&self) -> i32 { + 100 + } +} + +impl Bar1 for i32 { + fn b(&self) -> i32 { + 200 + } +} + +impl Bar2 for i32 { + fn c(&self) -> i32 { + 300 + } +} + +impl Baz for i32 { + fn d(&self) -> i32 { + 400 + } +} + +fn main() { + let baz: &dyn Baz = &1; + let _: &dyn std::fmt::Debug = baz; + assert_eq!(*baz, 1); + assert_eq!(baz.a(), 100); + assert_eq!(baz.b(), 200); + assert_eq!(baz.c(), 300); + assert_eq!(baz.d(), 400); + assert_eq!(baz.z(), 11); + assert_eq!(baz.y(), 12); + assert_eq!(baz.w(), 21); + assert_eq!(baz.v(), 31); + + let bar1: &dyn Bar1 = baz; + let _: &dyn std::fmt::Debug = bar1; + assert_eq!(*bar1, 1); + assert_eq!(bar1.a(), 100); + assert_eq!(bar1.b(), 200); + assert_eq!(bar1.z(), 11); + assert_eq!(bar1.y(), 12); + assert_eq!(bar1.w(), 21); + + let bar2: &dyn Bar2 = baz; + let _: &dyn std::fmt::Debug = bar2; + assert_eq!(*bar2, 1); + assert_eq!(bar2.a(), 100); + assert_eq!(bar2.c(), 300); + assert_eq!(bar2.z(), 11); + assert_eq!(bar2.y(), 12); + assert_eq!(bar2.v(), 31); + + let foo: &dyn Foo = baz; + let _: &dyn std::fmt::Debug = foo; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + + let foo: &dyn Foo = bar1; + let _: &dyn std::fmt::Debug = foo; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + + let foo: &dyn Foo = bar2; + let _: &dyn std::fmt::Debug = foo; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); +} diff --git a/src/test/ui/traits/trait-upcasting/invalid-upcast.rs b/src/test/ui/traits/trait-upcasting/invalid-upcast.rs new file mode 100644 index 000000000..240224504 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/invalid-upcast.rs @@ -0,0 +1,87 @@ +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo { + fn a(&self) -> i32 { + 10 + } + + fn z(&self) -> i32 { + 11 + } + + fn y(&self) -> i32 { + 12 + } +} + +trait Bar { + fn b(&self) -> i32 { + 20 + } + + fn w(&self) -> i32 { + 21 + } +} + +trait Baz { + fn c(&self) -> i32 { + 30 + } +} + +impl Foo for i32 { + fn a(&self) -> i32 { + 100 + } +} + +impl Bar for i32 { + fn b(&self) -> i32 { + 200 + } +} + +impl Baz for i32 { + fn c(&self) -> i32 { + 300 + } +} + +fn main() { + let baz: &dyn Baz = &1; + let _: &dyn std::fmt::Debug = baz; + //~^ ERROR mismatched types [E0308] + let _: &dyn Send = baz; + //~^ ERROR mismatched types [E0308] + let _: &dyn Sync = baz; + //~^ ERROR mismatched types [E0308] + + let bar: &dyn Bar = baz; + //~^ ERROR mismatched types [E0308] + let _: &dyn std::fmt::Debug = bar; + //~^ ERROR mismatched types [E0308] + let _: &dyn Send = bar; + //~^ ERROR mismatched types [E0308] + let _: &dyn Sync = bar; + //~^ ERROR mismatched types [E0308] + + let foo: &dyn Foo = baz; + //~^ ERROR mismatched types [E0308] + let _: &dyn std::fmt::Debug = foo; + //~^ ERROR mismatched types [E0308] + let _: &dyn Send = foo; + //~^ ERROR mismatched types [E0308] + let _: &dyn Sync = foo; + //~^ ERROR mismatched types [E0308] + + let foo: &dyn Foo = bar; + //~^ ERROR mismatched types [E0308] + let _: &dyn std::fmt::Debug = foo; + //~^ ERROR mismatched types [E0308] + let _: &dyn Send = foo; + //~^ ERROR mismatched types [E0308] + let _: &dyn Sync = foo; + //~^ ERROR mismatched types [E0308] +} diff --git a/src/test/ui/traits/trait-upcasting/invalid-upcast.stderr b/src/test/ui/traits/trait-upcasting/invalid-upcast.stderr new file mode 100644 index 000000000..b4530ed0c --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/invalid-upcast.stderr @@ -0,0 +1,168 @@ +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:54:35 + | +LL | let _: &dyn std::fmt::Debug = baz; + | -------------------- ^^^ expected trait `Debug`, found trait `Baz` + | | + | expected due to this + | + = note: expected reference `&dyn Debug` + found reference `&dyn Baz` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:56:24 + | +LL | let _: &dyn Send = baz; + | --------- ^^^ expected trait `Send`, found trait `Baz` + | | + | expected due to this + | + = note: expected reference `&dyn Send` + found reference `&dyn Baz` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:58:24 + | +LL | let _: &dyn Sync = baz; + | --------- ^^^ expected trait `Sync`, found trait `Baz` + | | + | expected due to this + | + = note: expected reference `&dyn Sync` + found reference `&dyn Baz` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:61:25 + | +LL | let bar: &dyn Bar = baz; + | -------- ^^^ expected trait `Bar`, found trait `Baz` + | | + | expected due to this + | + = note: expected reference `&dyn Bar` + found reference `&dyn Baz` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:63:35 + | +LL | let _: &dyn std::fmt::Debug = bar; + | -------------------- ^^^ expected trait `Debug`, found trait `Bar` + | | + | expected due to this + | + = note: expected reference `&dyn Debug` + found reference `&dyn Bar` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:65:24 + | +LL | let _: &dyn Send = bar; + | --------- ^^^ expected trait `Send`, found trait `Bar` + | | + | expected due to this + | + = note: expected reference `&dyn Send` + found reference `&dyn Bar` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:67:24 + | +LL | let _: &dyn Sync = bar; + | --------- ^^^ expected trait `Sync`, found trait `Bar` + | | + | expected due to this + | + = note: expected reference `&dyn Sync` + found reference `&dyn Bar` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:70:25 + | +LL | let foo: &dyn Foo = baz; + | -------- ^^^ expected trait `Foo`, found trait `Baz` + | | + | expected due to this + | + = note: expected reference `&dyn Foo` + found reference `&dyn Baz` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:72:35 + | +LL | let _: &dyn std::fmt::Debug = foo; + | -------------------- ^^^ expected trait `Debug`, found trait `Foo` + | | + | expected due to this + | + = note: expected reference `&dyn Debug` + found reference `&dyn Foo` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:74:24 + | +LL | let _: &dyn Send = foo; + | --------- ^^^ expected trait `Send`, found trait `Foo` + | | + | expected due to this + | + = note: expected reference `&dyn Send` + found reference `&dyn Foo` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:76:24 + | +LL | let _: &dyn Sync = foo; + | --------- ^^^ expected trait `Sync`, found trait `Foo` + | | + | expected due to this + | + = note: expected reference `&dyn Sync` + found reference `&dyn Foo` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:79:25 + | +LL | let foo: &dyn Foo = bar; + | -------- ^^^ expected trait `Foo`, found trait `Bar` + | | + | expected due to this + | + = note: expected reference `&dyn Foo` + found reference `&dyn Bar` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:81:35 + | +LL | let _: &dyn std::fmt::Debug = foo; + | -------------------- ^^^ expected trait `Debug`, found trait `Foo` + | | + | expected due to this + | + = note: expected reference `&dyn Debug` + found reference `&dyn Foo` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:83:24 + | +LL | let _: &dyn Send = foo; + | --------- ^^^ expected trait `Send`, found trait `Foo` + | | + | expected due to this + | + = note: expected reference `&dyn Send` + found reference `&dyn Foo` + +error[E0308]: mismatched types + --> $DIR/invalid-upcast.rs:85:24 + | +LL | let _: &dyn Sync = foo; + | --------- ^^^ expected trait `Sync`, found trait `Foo` + | | + | expected due to this + | + = note: expected reference `&dyn Sync` + found reference `&dyn Foo` + +error: aborting due to 15 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/traits/trait-upcasting/issue-11515-upcast-fn_mut-fn.rs b/src/test/ui/traits/trait-upcasting/issue-11515-upcast-fn_mut-fn.rs new file mode 100644 index 000000000..6d8800254 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/issue-11515-upcast-fn_mut-fn.rs @@ -0,0 +1,13 @@ +// run-pass +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +struct Test { + func: Box, +} + +fn main() { + let closure: Box = Box::new(|| ()); + let mut test = Box::new(Test { func: closure }); + (test.func)(); +} diff --git a/src/test/ui/traits/trait-upcasting/lifetime.rs b/src/test/ui/traits/trait-upcasting/lifetime.rs new file mode 100644 index 000000000..f029a6f08 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/lifetime.rs @@ -0,0 +1,95 @@ +// run-pass + +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: PartialEq + std::fmt::Debug + Send + Sync { + fn a(&self) -> i32 { + 10 + } + + fn z(&self) -> i32 { + 11 + } + + fn y(&self) -> i32 { + 12 + } +} + +trait Bar: Foo { + fn b(&self) -> i32 { + 20 + } + + fn w(&self) -> i32 { + 21 + } +} + +trait Baz: Bar { + fn c(&self) -> i32 { + 30 + } +} + +impl Foo for i32 { + fn a(&self) -> i32 { + 100 + } +} + +impl Bar for i32 { + fn b(&self) -> i32 { + 200 + } +} + +impl Baz for i32 { + fn c(&self) -> i32 { + 300 + } +} + +// Note: upcast lifetime means a shorter lifetime. +fn upcast_baz<'a: 'b, 'b, T>(v: Box, _l: &'b T) -> Box { + v +} +fn upcast_bar<'a: 'b, 'b, T>(v: Box, _l: &'b T) -> Box { + v +} +fn upcast_foo<'a: 'b, 'b, T>(v: Box, _l: &'b T) -> Box { + v +} + +fn main() { + let v = Box::new(1); + let l = &(); // dummy lifetime (shorter than `baz`) + + let baz: Box = v.clone(); + let u = upcast_baz(baz, &l); + assert_eq!(*u, 1); + assert_eq!(u.a(), 100); + assert_eq!(u.b(), 200); + assert_eq!(u.c(), 300); + + let baz: Box = v.clone(); + let bar: Box = baz; + let u = upcast_bar(bar, &l); + assert_eq!(*u, 1); + assert_eq!(u.a(), 100); + assert_eq!(u.b(), 200); + + let baz: Box = v.clone(); + let foo: Box = baz; + let u = upcast_foo(foo, &l); + assert_eq!(*u, 1); + assert_eq!(u.a(), 100); + + let baz: Box = v.clone(); + let bar: Box = baz; + let foo: Box = bar; + let u = upcast_foo(foo, &l); + assert_eq!(*u, 1); + assert_eq!(u.a(), 100); +} diff --git a/src/test/ui/traits/trait-upcasting/migrate-lint-deny.rs b/src/test/ui/traits/trait-upcasting/migrate-lint-deny.rs new file mode 100644 index 000000000..c67251018 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/migrate-lint-deny.rs @@ -0,0 +1,25 @@ +#![deny(deref_into_dyn_supertrait)] + +extern crate core; + +use core::ops::Deref; + +// issue 89190 +trait A {} +trait B: A {} +impl<'a> Deref for dyn 'a + B { + type Target = dyn A; + fn deref(&self) -> &Self::Target { + todo!() + } +} + +fn take_a(_: &dyn A) {} + +fn whoops(b: &dyn B) { + take_a(b) + //~^ ERROR `dyn B` implements `Deref` with supertrait `(dyn A + 'static)` as output + //~^^ WARN this was previously accepted by the compiler but is being phased out; +} + +fn main() {} diff --git a/src/test/ui/traits/trait-upcasting/migrate-lint-deny.stderr b/src/test/ui/traits/trait-upcasting/migrate-lint-deny.stderr new file mode 100644 index 000000000..35af9112a --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/migrate-lint-deny.stderr @@ -0,0 +1,16 @@ +error: `dyn B` implements `Deref` with supertrait `(dyn A + 'static)` as output + --> $DIR/migrate-lint-deny.rs:20:12 + | +LL | take_a(b) + | ^ + | +note: the lint level is defined here + --> $DIR/migrate-lint-deny.rs:1:9 + | +LL | #![deny(deref_into_dyn_supertrait)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #89460 + +error: aborting due to previous error + diff --git a/src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.rs b/src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.rs new file mode 100644 index 000000000..6986ad621 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.rs @@ -0,0 +1,22 @@ +// check-fail +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Bar { + fn bar(&self, _: T) {} +} + +trait Foo : Bar + Bar { + fn foo(&self, _: ()) {} +} + +struct S; + +impl Bar for S {} +impl Bar for S {} +impl Foo for S {} + +fn main() { + let s: &dyn Foo = &S; + let t: &dyn Bar<_> = s; //~ ERROR mismatched types +} diff --git a/src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.stderr b/src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.stderr new file mode 100644 index 000000000..e9670ad7d --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/multiple-occurence-ambiguousity.stderr @@ -0,0 +1,14 @@ +error[E0308]: mismatched types + --> $DIR/multiple-occurence-ambiguousity.rs:21:26 + | +LL | let t: &dyn Bar<_> = s; + | ----------- ^ expected trait `Bar`, found trait `Foo` + | | + | expected due to this + | + = note: expected reference `&dyn Bar<_>` + found reference `&dyn Foo` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/traits/trait-upcasting/replace-vptr.rs b/src/test/ui/traits/trait-upcasting/replace-vptr.rs new file mode 100644 index 000000000..1164e4361 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/replace-vptr.rs @@ -0,0 +1,49 @@ +// run-pass + +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait A { + fn foo_a(&self); +} + +trait B { + fn foo_b(&self); +} + +trait C: A + B { + fn foo_c(&self); +} + +struct S(i32); + +impl A for S { + fn foo_a(&self) { + unreachable!(); + } +} + +impl B for S { + fn foo_b(&self) { + assert_eq!(42, self.0); + } +} + +impl C for S { + fn foo_c(&self) { + unreachable!(); + } +} + +fn invoke_inner(b: &dyn B) { + b.foo_b(); +} + +fn invoke_outer(c: &dyn C) { + invoke_inner(c); +} + +fn main() { + let s = S(42); + invoke_outer(&s); +} diff --git a/src/test/ui/traits/trait-upcasting/struct.rs b/src/test/ui/traits/trait-upcasting/struct.rs new file mode 100644 index 000000000..0f3cb285b --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/struct.rs @@ -0,0 +1,174 @@ +// run-pass + +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +use std::rc::Rc; +use std::sync::Arc; + +trait Foo: PartialEq + std::fmt::Debug + Send + Sync { + fn a(&self) -> i32 { + 10 + } + + fn z(&self) -> i32 { + 11 + } + + fn y(&self) -> i32 { + 12 + } +} + +trait Bar: Foo { + fn b(&self) -> i32 { + 20 + } + + fn w(&self) -> i32 { + 21 + } +} + +trait Baz: Bar { + fn c(&self) -> i32 { + 30 + } +} + +impl Foo for i32 { + fn a(&self) -> i32 { + 100 + } +} + +impl Bar for i32 { + fn b(&self) -> i32 { + 200 + } +} + +impl Baz for i32 { + fn c(&self) -> i32 { + 300 + } +} + +fn test_box() { + let v = Box::new(1); + + let baz: Box = v.clone(); + assert_eq!(*baz, 1); + assert_eq!(baz.a(), 100); + assert_eq!(baz.b(), 200); + assert_eq!(baz.c(), 300); + assert_eq!(baz.z(), 11); + assert_eq!(baz.y(), 12); + assert_eq!(baz.w(), 21); + + let baz: Box = v.clone(); + let bar: Box = baz; + assert_eq!(*bar, 1); + assert_eq!(bar.a(), 100); + assert_eq!(bar.b(), 200); + assert_eq!(bar.z(), 11); + assert_eq!(bar.y(), 12); + assert_eq!(bar.w(), 21); + + let baz: Box = v.clone(); + let foo: Box = baz; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); + + let baz: Box = v.clone(); + let bar: Box = baz; + let foo: Box = bar; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); +} + +fn test_rc() { + let v = Rc::new(1); + + let baz: Rc = v.clone(); + assert_eq!(*baz, 1); + assert_eq!(baz.a(), 100); + assert_eq!(baz.b(), 200); + assert_eq!(baz.c(), 300); + assert_eq!(baz.z(), 11); + assert_eq!(baz.y(), 12); + assert_eq!(baz.w(), 21); + + let baz: Rc = v.clone(); + let bar: Rc = baz; + assert_eq!(*bar, 1); + assert_eq!(bar.a(), 100); + assert_eq!(bar.b(), 200); + assert_eq!(bar.z(), 11); + assert_eq!(bar.y(), 12); + assert_eq!(bar.w(), 21); + + let baz: Rc = v.clone(); + let foo: Rc = baz; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); + + let baz: Rc = v.clone(); + let bar: Rc = baz; + let foo: Rc = bar; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); +} + +fn test_arc() { + let v = Arc::new(1); + + let baz: Arc = v.clone(); + assert_eq!(*baz, 1); + assert_eq!(baz.a(), 100); + assert_eq!(baz.b(), 200); + assert_eq!(baz.c(), 300); + assert_eq!(baz.z(), 11); + assert_eq!(baz.y(), 12); + assert_eq!(baz.w(), 21); + + let baz: Arc = v.clone(); + let bar: Arc = baz; + assert_eq!(*bar, 1); + assert_eq!(bar.a(), 100); + assert_eq!(bar.b(), 200); + assert_eq!(bar.z(), 11); + assert_eq!(bar.y(), 12); + assert_eq!(bar.w(), 21); + + let baz: Arc = v.clone(); + let foo: Arc = baz; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); + + let baz: Arc = v.clone(); + let bar: Arc = baz; + let foo: Arc = bar; + assert_eq!(*foo, 1); + assert_eq!(foo.a(), 100); + assert_eq!(foo.z(), 11); + assert_eq!(foo.y(), 12); +} + +fn main() { + test_box(); + test_rc(); + test_arc(); +} diff --git a/src/test/ui/traits/trait-upcasting/subtrait-method.rs b/src/test/ui/traits/trait-upcasting/subtrait-method.rs new file mode 100644 index 000000000..3508e1528 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/subtrait-method.rs @@ -0,0 +1,70 @@ +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: PartialEq + std::fmt::Debug + Send + Sync { + fn a(&self) -> i32 { + 10 + } + + fn z(&self) -> i32 { + 11 + } + + fn y(&self) -> i32 { + 12 + } +} + +trait Bar: Foo { + fn b(&self) -> i32 { + 20 + } + + fn w(&self) -> i32 { + 21 + } +} + +trait Baz: Bar { + fn c(&self) -> i32 { + 30 + } +} + +impl Foo for i32 { + fn a(&self) -> i32 { + 100 + } +} + +impl Bar for i32 { + fn b(&self) -> i32 { + 200 + } +} + +impl Baz for i32 { + fn c(&self) -> i32 { + 300 + } +} + +fn main() { + let baz: &dyn Baz = &1; + + let bar: &dyn Bar = baz; + bar.c(); + //~^ ERROR no method named `c` found for reference `&dyn Bar` in the current scope [E0599] + + let foo: &dyn Foo = baz; + foo.b(); + //~^ ERROR no method named `b` found for reference `&dyn Foo` in the current scope [E0599] + foo.c(); + //~^ ERROR no method named `c` found for reference `&dyn Foo` in the current scope [E0599] + + let foo: &dyn Foo = bar; + foo.b(); + //~^ ERROR no method named `b` found for reference `&dyn Foo` in the current scope [E0599] + foo.c(); + //~^ ERROR no method named `c` found for reference `&dyn Foo` in the current scope [E0599] +} diff --git a/src/test/ui/traits/trait-upcasting/subtrait-method.stderr b/src/test/ui/traits/trait-upcasting/subtrait-method.stderr new file mode 100644 index 000000000..8c6901180 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/subtrait-method.stderr @@ -0,0 +1,68 @@ +error[E0599]: no method named `c` found for reference `&dyn Bar` in the current scope + --> $DIR/subtrait-method.rs:56:9 + | +LL | bar.c(); + | ^ help: there is an associated function with a similar name: `a` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `Baz` defines an item `c`, perhaps you need to implement it + --> $DIR/subtrait-method.rs:28:1 + | +LL | trait Baz: Bar { + | ^^^^^^^^^^^^^^ + +error[E0599]: no method named `b` found for reference `&dyn Foo` in the current scope + --> $DIR/subtrait-method.rs:60:9 + | +LL | foo.b(); + | ^ help: there is an associated function with a similar name: `a` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `Bar` defines an item `b`, perhaps you need to implement it + --> $DIR/subtrait-method.rs:18:1 + | +LL | trait Bar: Foo { + | ^^^^^^^^^^^^^^ + +error[E0599]: no method named `c` found for reference `&dyn Foo` in the current scope + --> $DIR/subtrait-method.rs:62:9 + | +LL | foo.c(); + | ^ help: there is an associated function with a similar name: `a` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `Baz` defines an item `c`, perhaps you need to implement it + --> $DIR/subtrait-method.rs:28:1 + | +LL | trait Baz: Bar { + | ^^^^^^^^^^^^^^ + +error[E0599]: no method named `b` found for reference `&dyn Foo` in the current scope + --> $DIR/subtrait-method.rs:66:9 + | +LL | foo.b(); + | ^ help: there is an associated function with a similar name: `a` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `Bar` defines an item `b`, perhaps you need to implement it + --> $DIR/subtrait-method.rs:18:1 + | +LL | trait Bar: Foo { + | ^^^^^^^^^^^^^^ + +error[E0599]: no method named `c` found for reference `&dyn Foo` in the current scope + --> $DIR/subtrait-method.rs:68:9 + | +LL | foo.c(); + | ^ help: there is an associated function with a similar name: `a` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `Baz` defines an item `c`, perhaps you need to implement it + --> $DIR/subtrait-method.rs:28:1 + | +LL | trait Baz: Bar { + | ^^^^^^^^^^^^^^ + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-1.rs b/src/test/ui/traits/trait-upcasting/type-checking-test-1.rs new file mode 100644 index 000000000..79ddedd41 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-1.rs @@ -0,0 +1,27 @@ +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: Bar + Bar {} +trait Bar { + fn bar(&self) -> Option { + None + } +} + +fn test_specific(x: &dyn Foo) { + let _ = x as &dyn Bar; // OK + let _ = x as &dyn Bar; // OK +} + +fn test_unknown_version(x: &dyn Foo) { + let _ = x as &dyn Bar<_>; // Ambiguous + //~^ ERROR non-primitive cast + //~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied +} + +fn test_infer_version(x: &dyn Foo) { + let a = x as &dyn Bar<_>; // OK + let _: Option = a.bar(); +} + +fn main() {} diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-1.stderr b/src/test/ui/traits/trait-upcasting/type-checking-test-1.stderr new file mode 100644 index 000000000..398537211 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-1.stderr @@ -0,0 +1,23 @@ +error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>` + --> $DIR/type-checking-test-1.rs:17:13 + | +LL | let _ = x as &dyn Bar<_>; // Ambiguous + | ^^^^^^^^^^^^^^^^ invalid cast + | +help: consider borrowing the value + | +LL | let _ = &x as &dyn Bar<_>; // Ambiguous + | + + +error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied + --> $DIR/type-checking-test-1.rs:17:13 + | +LL | let _ = x as &dyn Bar<_>; // Ambiguous + | ^ the trait `Bar<_>` is not implemented for `&dyn Foo` + | + = note: required for the cast from `&dyn Foo` to the object type `dyn Bar<_>` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0277, E0605. +For more information about an error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-2.rs b/src/test/ui/traits/trait-upcasting/type-checking-test-2.rs new file mode 100644 index 000000000..32754c538 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-2.rs @@ -0,0 +1,32 @@ +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: Bar + Bar {} +trait Bar { + fn bar(&self) -> Option { + None + } +} + +fn test_specific(x: &dyn Foo) { + let _ = x as &dyn Bar; // OK +} + +fn test_specific2(x: &dyn Foo) { + let _ = x as &dyn Bar; // OK +} + +fn test_specific3(x: &dyn Foo) { + let _ = x as &dyn Bar; // Error + //~^ ERROR non-primitive cast + //~^^ ERROR the trait bound `&dyn Foo: Bar` is not satisfied +} + +fn test_infer_arg(x: &dyn Foo) { + let a = x as &dyn Bar<_>; // Ambiguous + //~^ ERROR non-primitive cast + //~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied + let _ = a.bar(); +} + +fn main() {} diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-2.stderr b/src/test/ui/traits/trait-upcasting/type-checking-test-2.stderr new file mode 100644 index 000000000..93c71f54e --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-2.stderr @@ -0,0 +1,42 @@ +error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar` + --> $DIR/type-checking-test-2.rs:20:13 + | +LL | let _ = x as &dyn Bar; // Error + | ^^^^^^^^^^^^^^^^^^ invalid cast + | +help: consider borrowing the value + | +LL | let _ = &x as &dyn Bar; // Error + | + + +error[E0277]: the trait bound `&dyn Foo: Bar` is not satisfied + --> $DIR/type-checking-test-2.rs:20:13 + | +LL | let _ = x as &dyn Bar; // Error + | ^ the trait `Bar` is not implemented for `&dyn Foo` + | + = note: required for the cast from `&dyn Foo` to the object type `dyn Bar` + +error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>` + --> $DIR/type-checking-test-2.rs:26:13 + | +LL | let a = x as &dyn Bar<_>; // Ambiguous + | ^^^^^^^^^^^^^^^^ invalid cast + | +help: consider borrowing the value + | +LL | let a = &x as &dyn Bar<_>; // Ambiguous + | + + +error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied + --> $DIR/type-checking-test-2.rs:26:13 + | +LL | let a = x as &dyn Bar<_>; // Ambiguous + | ^ the trait `Bar<_>` is not implemented for `&dyn Foo` + | + = note: required for the cast from `&dyn Foo` to the object type `dyn Bar<_>` + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0277, E0605. +For more information about an error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-3.polonius.stderr b/src/test/ui/traits/trait-upcasting/type-checking-test-3.polonius.stderr new file mode 100644 index 000000000..e48ba709a --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-3.polonius.stderr @@ -0,0 +1,22 @@ +error: lifetime may not live long enough + --> $DIR/type-checking-test-3.rs:13:13 + | +LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'a>; // Error + | ^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + | + = help: consider replacing `'a` with `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-3.rs:18:13 + | +LL | fn test_wrong2<'a>(x: &dyn Foo<'a>) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'static>; // Error + | ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + | + = help: consider replacing `'a` with `'static` + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-3.rs b/src/test/ui/traits/trait-upcasting/type-checking-test-3.rs new file mode 100644 index 000000000..b3aa2279a --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-3.rs @@ -0,0 +1,21 @@ +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo<'a>: Bar<'a> {} +trait Bar<'a> {} + +fn test_correct(x: &dyn Foo<'static>) { + let _ = x as &dyn Bar<'static>; +} + +fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) { + let _ = x as &dyn Bar<'a>; // Error + //~^ ERROR lifetime may not live long enough +} + +fn test_wrong2<'a>(x: &dyn Foo<'a>) { + let _ = x as &dyn Bar<'static>; // Error + //~^ ERROR lifetime may not live long enough +} + +fn main() {} diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-3.stderr b/src/test/ui/traits/trait-upcasting/type-checking-test-3.stderr new file mode 100644 index 000000000..5ad151b50 --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-3.stderr @@ -0,0 +1,18 @@ +error: lifetime may not live long enough + --> $DIR/type-checking-test-3.rs:12:13 + | +LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'a>; // Error + | ^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-3.rs:17:13 + | +LL | fn test_wrong2<'a>(x: &dyn Foo<'a>) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'static>; // Error + | ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-4.polonius.stderr b/src/test/ui/traits/trait-upcasting/type-checking-test-4.polonius.stderr new file mode 100644 index 000000000..a3411f40a --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-4.polonius.stderr @@ -0,0 +1,33 @@ +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:17:13 + | +LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'static, 'a>; // Error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + | + = help: consider replacing `'a` with `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:22:13 + | +LL | fn test_wrong2<'a>(x: &dyn Foo<'static>, y: &'a u32) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'a, 'static>; // Error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + | + = help: consider replacing `'a` with `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:29:5 + | +LL | fn test_wrong3<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + | -- lifetime `'a` defined here +... +LL | y.get_b() // ERROR + | ^^^^^^^^^ returning this value requires that `'a` must outlive `'static` + | + = help: consider replacing `'a` with `'static` + +error: aborting due to 3 previous errors + diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-4.rs b/src/test/ui/traits/trait-upcasting/type-checking-test-4.rs new file mode 100644 index 000000000..70ccc87fc --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-4.rs @@ -0,0 +1,49 @@ +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo<'a>: Bar<'a, 'a> {} +trait Bar<'a, 'b> { + fn get_b(&self) -> Option<&'a u32> { + None + } +} + +fn test_correct(x: &dyn Foo<'static>) { + let _ = x as &dyn Bar<'static, 'static>; +} + +fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) { + let _ = x as &dyn Bar<'static, 'a>; // Error + //~^ ERROR lifetime may not live long enough +} + +fn test_wrong2<'a>(x: &dyn Foo<'static>, y: &'a u32) { + let _ = x as &dyn Bar<'a, 'static>; // Error + //~^ ERROR lifetime may not live long enough +} + +fn test_wrong3<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + let y = x as &dyn Bar<'_, '_>; + y.get_b() // ERROR + //~^ ERROR lifetime may not live long enough +} + +fn test_wrong4<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + <_ as Bar>::get_b(x) // ERROR + //~^ ERROR lifetime may not live long enough +} + +fn test_wrong5<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + <_ as Bar<'_, '_>>::get_b(x) // ERROR + //~^ ERROR lifetime may not live long enough +} + +fn test_wrong6<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + let y = x as &dyn Bar<'_, '_>; + y.get_b(); // ERROR + let z = y; + z.get_b() // ERROR + //~^ ERROR lifetime may not live long enough +} + +fn main() {} diff --git a/src/test/ui/traits/trait-upcasting/type-checking-test-4.stderr b/src/test/ui/traits/trait-upcasting/type-checking-test-4.stderr new file mode 100644 index 000000000..436129d0b --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/type-checking-test-4.stderr @@ -0,0 +1,52 @@ +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:16:13 + | +LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'static, 'a>; // Error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:21:13 + | +LL | fn test_wrong2<'a>(x: &dyn Foo<'static>, y: &'a u32) { + | -- lifetime `'a` defined here +LL | let _ = x as &dyn Bar<'a, 'static>; // Error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:27:5 + | +LL | fn test_wrong3<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + | -- lifetime `'a` defined here +LL | let y = x as &dyn Bar<'_, '_>; +LL | y.get_b() // ERROR + | ^^^^^^^^^ returning this value requires that `'a` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:32:5 + | +LL | fn test_wrong4<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + | -- lifetime `'a` defined here +LL | <_ as Bar>::get_b(x) // ERROR + | ^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:37:5 + | +LL | fn test_wrong5<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + | -- lifetime `'a` defined here +LL | <_ as Bar<'_, '_>>::get_b(x) // ERROR + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static` + +error: lifetime may not live long enough + --> $DIR/type-checking-test-4.rs:45:5 + | +LL | fn test_wrong6<'a>(x: &dyn Foo<'a>) -> Option<&'static u32> { + | -- lifetime `'a` defined here +... +LL | z.get_b() // ERROR + | ^^^^^^^^^ returning this value requires that `'a` must outlive `'static` + +error: aborting due to 6 previous errors + diff --git a/src/test/ui/traits/typeclasses-eq-example-static.rs b/src/test/ui/traits/typeclasses-eq-example-static.rs new file mode 100644 index 000000000..f982ad6a0 --- /dev/null +++ b/src/test/ui/traits/typeclasses-eq-example-static.rs @@ -0,0 +1,68 @@ +// run-pass + +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(dead_code)] + +// Example from lkuper's intern talk, August 2012 -- now with static +// methods! +use Color::{cyan, magenta, yellow, black}; +use ColorTree::{leaf, branch}; + +trait Equal { + fn isEq(a: &Self, b: &Self) -> bool; +} + +#[derive(Clone, Copy)] +enum Color { cyan, magenta, yellow, black } + +impl Equal for Color { + fn isEq(a: &Color, b: &Color) -> bool { + match (*a, *b) { + (cyan, cyan) => { true } + (magenta, magenta) => { true } + (yellow, yellow) => { true } + (black, black) => { true } + _ => { false } + } + } +} + +#[derive(Clone)] +enum ColorTree { + leaf(Color), + branch(Box, Box) +} + +impl Equal for ColorTree { + fn isEq(a: &ColorTree, b: &ColorTree) -> bool { + match (a, b) { + (&leaf(ref x), &leaf(ref y)) => { + Equal::isEq(&(*x).clone(), &(*y).clone()) + } + (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => { + Equal::isEq(&(**l1).clone(), &(**l2).clone()) && + Equal::isEq(&(**r1).clone(), &(**r2).clone()) + } + _ => { false } + } + } +} + +pub fn main() { + assert!(Equal::isEq(&cyan, &cyan)); + assert!(Equal::isEq(&magenta, &magenta)); + assert!(!Equal::isEq(&cyan, &yellow)); + assert!(!Equal::isEq(&magenta, &cyan)); + + assert!(Equal::isEq(&leaf(cyan), &leaf(cyan))); + assert!(!Equal::isEq(&leaf(cyan), &leaf(yellow))); + + assert!(Equal::isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))), + &branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))))); + + assert!(!Equal::isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))), + &branch(Box::new(leaf(magenta)), Box::new(leaf(magenta))))); + + println!("Assertions all succeeded!"); +} diff --git a/src/test/ui/traits/typeclasses-eq-example.rs b/src/test/ui/traits/typeclasses-eq-example.rs new file mode 100644 index 000000000..4400301e6 --- /dev/null +++ b/src/test/ui/traits/typeclasses-eq-example.rs @@ -0,0 +1,64 @@ +// run-pass + +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] +#![allow(dead_code)] + +// Example from lkuper's intern talk, August 2012. +use Color::{cyan, magenta, yellow, black}; +use ColorTree::{leaf, branch}; + +trait Equal { + fn isEq(&self, a: &Self) -> bool; +} + +#[derive(Clone, Copy)] +enum Color { cyan, magenta, yellow, black } + +impl Equal for Color { + fn isEq(&self, a: &Color) -> bool { + match (*self, *a) { + (cyan, cyan) => { true } + (magenta, magenta) => { true } + (yellow, yellow) => { true } + (black, black) => { true } + _ => { false } + } + } +} + +#[derive(Clone)] +enum ColorTree { + leaf(Color), + branch(Box, Box) +} + +impl Equal for ColorTree { + fn isEq(&self, a: &ColorTree) -> bool { + match (self, a) { + (&leaf(ref x), &leaf(ref y)) => { x.isEq(&(*y).clone()) } + (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => { + (*l1).isEq(&(**l2).clone()) && (*r1).isEq(&(**r2).clone()) + } + _ => { false } + } + } +} + +pub fn main() { + assert!(cyan.isEq(&cyan)); + assert!(magenta.isEq(&magenta)); + assert!(!cyan.isEq(&yellow)); + assert!(!magenta.isEq(&cyan)); + + assert!(leaf(cyan).isEq(&leaf(cyan))); + assert!(!leaf(cyan).isEq(&leaf(yellow))); + + assert!(branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))) + .isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))))); + + assert!(!branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))) + .isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(magenta))))); + + println!("Assertions all succeeded!"); +} diff --git a/src/test/ui/traits/ufcs-object.rs b/src/test/ui/traits/ufcs-object.rs new file mode 100644 index 000000000..700488c22 --- /dev/null +++ b/src/test/ui/traits/ufcs-object.rs @@ -0,0 +1,17 @@ +// run-pass +// Test that when you use ufcs form to invoke a trait method (on a +// trait object) everything works fine. + + +trait Foo { + fn test(&self) -> i32; +} + +impl Foo for i32 { + fn test(&self) -> i32 { *self } +} + +fn main() { + let a: &dyn Foo = &22; + assert_eq!(Foo::test(a), 22); +} diff --git a/src/test/ui/traits/use-before-def.rs b/src/test/ui/traits/use-before-def.rs new file mode 100644 index 000000000..1ee2b9419 --- /dev/null +++ b/src/test/ui/traits/use-before-def.rs @@ -0,0 +1,10 @@ +// run-pass +#![allow(non_camel_case_types)] + +// Issue #1761 + +// pretty-expanded FIXME #23616 + +impl foo for isize { fn foo(&self) -> isize { 10 } } +trait foo { fn foo(&self) -> isize; } +pub fn main() {} diff --git a/src/test/ui/traits/vtable-res-trait-param.rs b/src/test/ui/traits/vtable-res-trait-param.rs new file mode 100644 index 000000000..3d7c2eb08 --- /dev/null +++ b/src/test/ui/traits/vtable-res-trait-param.rs @@ -0,0 +1,23 @@ +trait TraitA { + fn method_a(&self) -> isize; +} + +trait TraitB { + fn gimme_an_a(&self, a: A) -> isize; +} + +impl TraitB for isize { + fn gimme_an_a(&self, a: A) -> isize { + a.method_a() + *self + } +} + +fn call_it(b: B) -> isize { + let y = 4; + b.gimme_an_a(y) //~ ERROR `{integer}: TraitA` is not satisfied +} + +fn main() { + let x = 3; + assert_eq!(call_it(x), 22); +} diff --git a/src/test/ui/traits/vtable-res-trait-param.stderr b/src/test/ui/traits/vtable-res-trait-param.stderr new file mode 100644 index 000000000..2b3e3de9b --- /dev/null +++ b/src/test/ui/traits/vtable-res-trait-param.stderr @@ -0,0 +1,17 @@ +error[E0277]: the trait bound `{integer}: TraitA` is not satisfied + --> $DIR/vtable-res-trait-param.rs:17:18 + | +LL | b.gimme_an_a(y) + | ---------- ^ the trait `TraitA` is not implemented for `{integer}` + | | + | required by a bound introduced by this call + | +note: required by a bound in `TraitB::gimme_an_a` + --> $DIR/vtable-res-trait-param.rs:6:21 + | +LL | fn gimme_an_a(&self, a: A) -> isize; + | ^^^^^^ required by this bound in `TraitB::gimme_an_a` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/vtable/issue-91807.rs b/src/test/ui/traits/vtable/issue-91807.rs new file mode 100644 index 000000000..f435ff09d --- /dev/null +++ b/src/test/ui/traits/vtable/issue-91807.rs @@ -0,0 +1,17 @@ +// check-pass +// incremental + +struct Struct(T); + +impl std::ops::Deref for Struct { + type Target = dyn Fn(T); + fn deref(&self) -> &Self::Target { + unimplemented!() + } +} + +fn main() { + let f = Struct(Default::default()); + f(0); + f(0); +} diff --git a/src/test/ui/traits/vtable/issue-97381.rs b/src/test/ui/traits/vtable/issue-97381.rs new file mode 100644 index 000000000..393cf91ef --- /dev/null +++ b/src/test/ui/traits/vtable/issue-97381.rs @@ -0,0 +1,30 @@ +use std::ops::Deref; +trait MyTrait: Deref {} +struct MyStruct(u32); +impl MyTrait for MyStruct {} +impl Deref for MyStruct { + type Target = u32; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} +fn get_concrete_value(i: u32) -> MyStruct { + MyStruct(i) +} +fn get_boxed_value(i: u32) -> Box { + Box::new(get_concrete_value(i)) +} +fn main() { + let v = [1, 2, 3] + .iter() + .map(|i| get_boxed_value(*i)) + .collect::>(); + + let el = &v[0]; + + for _ in v { + //~^ ERROR cannot move out of `v` because it is borrowed + println!("{}", ***el > 0); + } +} diff --git a/src/test/ui/traits/vtable/issue-97381.stderr b/src/test/ui/traits/vtable/issue-97381.stderr new file mode 100644 index 000000000..c4f8294e2 --- /dev/null +++ b/src/test/ui/traits/vtable/issue-97381.stderr @@ -0,0 +1,15 @@ +error[E0505]: cannot move out of `v` because it is borrowed + --> $DIR/issue-97381.rs:26:14 + | +LL | let el = &v[0]; + | - borrow of `v` occurs here +LL | +LL | for _ in v { + | ^ move out of `v` occurs here +LL | +LL | println!("{}", ***el > 0); + | ---- borrow later used here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0505`. diff --git a/src/test/ui/traits/vtable/vtable-diamond.rs b/src/test/ui/traits/vtable/vtable-diamond.rs new file mode 100644 index 000000000..dc3c17ac3 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-diamond.rs @@ -0,0 +1,44 @@ +// build-fail +#![feature(rustc_attrs)] + +#[rustc_dump_vtable] +trait A { + fn foo_a(&self) {} +} + +#[rustc_dump_vtable] +trait B: A { + fn foo_b(&self) {} +} + +#[rustc_dump_vtable] +trait C: A { + //~^ error vtable + fn foo_c(&self) {} +} + +#[rustc_dump_vtable] +trait D: B + C { + //~^ error vtable + fn foo_d(&self) {} +} + +struct S; + +impl A for S {} +impl B for S {} +impl C for S {} +impl D for S {} + +fn foo(d: &dyn D) { + d.foo_d(); +} + +fn bar(d: &dyn C) { + d.foo_c(); +} + +fn main() { + foo(&S); + bar(&S); +} diff --git a/src/test/ui/traits/vtable/vtable-diamond.stderr b/src/test/ui/traits/vtable/vtable-diamond.stderr new file mode 100644 index 000000000..f3718c5d8 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-diamond.stderr @@ -0,0 +1,29 @@ +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_a), + Method(::foo_b), + Method(::foo_c), + TraitVPtr(), + Method(::foo_d), + ] + --> $DIR/vtable-diamond.rs:21:1 + | +LL | trait D: B + C { + | ^^^^^^^^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_a), + Method(::foo_c), + ] + --> $DIR/vtable-diamond.rs:15:1 + | +LL | trait C: A { + | ^^^^^^^^^^ + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/traits/vtable/vtable-multi-level.rs b/src/test/ui/traits/vtable/vtable-multi-level.rs new file mode 100644 index 000000000..ebd55bcf3 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-multi-level.rs @@ -0,0 +1,143 @@ +// build-fail +#![feature(rustc_attrs)] + +// O --> G --> C --> A +// \ \ \-> B +// | |-> F --> D +// | \-> E +// |-> N --> J --> H +// \ \-> I +// |-> M --> K +// \-> L + +#[rustc_dump_vtable] +trait A { + //~^ error vtable + fn foo_a(&self) {} +} + +#[rustc_dump_vtable] +trait B { + //~^ error vtable + fn foo_b(&self) {} +} + +#[rustc_dump_vtable] +trait C: A + B { + //~^ error vtable + fn foo_c(&self) {} +} + +#[rustc_dump_vtable] +trait D { + //~^ error vtable + fn foo_d(&self) {} +} + +#[rustc_dump_vtable] +trait E { + //~^ error vtable + fn foo_e(&self) {} +} + +#[rustc_dump_vtable] +trait F: D + E { + //~^ error vtable + fn foo_f(&self) {} +} + +#[rustc_dump_vtable] +trait G: C + F { + fn foo_g(&self) {} +} + +#[rustc_dump_vtable] +trait H { + //~^ error vtable + fn foo_h(&self) {} +} + +#[rustc_dump_vtable] +trait I { + //~^ error vtable + fn foo_i(&self) {} +} + +#[rustc_dump_vtable] +trait J: H + I { + //~^ error vtable + fn foo_j(&self) {} +} + +#[rustc_dump_vtable] +trait K { + //~^ error vtable + fn foo_k(&self) {} +} + +#[rustc_dump_vtable] +trait L { + //~^ error vtable + fn foo_l(&self) {} +} + +#[rustc_dump_vtable] +trait M: K + L { + //~^ error vtable + fn foo_m(&self) {} +} + +#[rustc_dump_vtable] +trait N: J + M { + //~^ error vtable + fn foo_n(&self) {} +} + +#[rustc_dump_vtable] +trait O: G + N { + //~^ error vtable + fn foo_o(&self) {} +} + +struct S; + +impl A for S {} +impl B for S {} +impl C for S {} +impl D for S {} +impl E for S {} +impl F for S {} +impl G for S {} +impl H for S {} +impl I for S {} +impl J for S {} +impl K for S {} +impl L for S {} +impl M for S {} +impl N for S {} +impl O for S {} + +macro_rules! monomorphize_vtable { + ($trait:ident) => {{ + fn foo(_ : &dyn $trait) {} + foo(&S); + }} +} + +fn main() { + monomorphize_vtable!(O); + + monomorphize_vtable!(A); + monomorphize_vtable!(B); + monomorphize_vtable!(C); + monomorphize_vtable!(D); + monomorphize_vtable!(E); + monomorphize_vtable!(F); + monomorphize_vtable!(H); + monomorphize_vtable!(I); + monomorphize_vtable!(J); + monomorphize_vtable!(K); + monomorphize_vtable!(L); + monomorphize_vtable!(M); + monomorphize_vtable!(N); +} diff --git a/src/test/ui/traits/vtable/vtable-multi-level.stderr b/src/test/ui/traits/vtable/vtable-multi-level.stderr new file mode 100644 index 000000000..c4389e23f --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-multi-level.stderr @@ -0,0 +1,203 @@ +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_a), + Method(::foo_b), + TraitVPtr(), + Method(::foo_c), + Method(::foo_d), + TraitVPtr(), + Method(::foo_e), + TraitVPtr(), + Method(::foo_f), + TraitVPtr(), + Method(::foo_g), + Method(::foo_h), + TraitVPtr(), + Method(::foo_i), + TraitVPtr(), + Method(::foo_j), + TraitVPtr(), + Method(::foo_k), + TraitVPtr(), + Method(::foo_l), + TraitVPtr(), + Method(::foo_m), + TraitVPtr(), + Method(::foo_n), + TraitVPtr(), + Method(::foo_o), + ] + --> $DIR/vtable-multi-level.rs:97:1 + | +LL | trait O: G + N { + | ^^^^^^^^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_a), + ] + --> $DIR/vtable-multi-level.rs:14:1 + | +LL | trait A { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_b), + ] + --> $DIR/vtable-multi-level.rs:20:1 + | +LL | trait B { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_a), + Method(::foo_b), + TraitVPtr(), + Method(::foo_c), + ] + --> $DIR/vtable-multi-level.rs:26:1 + | +LL | trait C: A + B { + | ^^^^^^^^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_d), + ] + --> $DIR/vtable-multi-level.rs:32:1 + | +LL | trait D { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_e), + ] + --> $DIR/vtable-multi-level.rs:38:1 + | +LL | trait E { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_d), + Method(::foo_e), + TraitVPtr(), + Method(::foo_f), + ] + --> $DIR/vtable-multi-level.rs:44:1 + | +LL | trait F: D + E { + | ^^^^^^^^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_h), + ] + --> $DIR/vtable-multi-level.rs:55:1 + | +LL | trait H { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_i), + ] + --> $DIR/vtable-multi-level.rs:61:1 + | +LL | trait I { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_h), + Method(::foo_i), + TraitVPtr(), + Method(::foo_j), + ] + --> $DIR/vtable-multi-level.rs:67:1 + | +LL | trait J: H + I { + | ^^^^^^^^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_k), + ] + --> $DIR/vtable-multi-level.rs:73:1 + | +LL | trait K { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_l), + ] + --> $DIR/vtable-multi-level.rs:79:1 + | +LL | trait L { + | ^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_k), + Method(::foo_l), + TraitVPtr(), + Method(::foo_m), + ] + --> $DIR/vtable-multi-level.rs:85:1 + | +LL | trait M: K + L { + | ^^^^^^^^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_h), + Method(::foo_i), + TraitVPtr(), + Method(::foo_j), + Method(::foo_k), + TraitVPtr(), + Method(::foo_l), + TraitVPtr(), + Method(::foo_m), + TraitVPtr(), + Method(::foo_n), + ] + --> $DIR/vtable-multi-level.rs:91:1 + | +LL | trait N: J + M { + | ^^^^^^^^^^^^^^ + +error: aborting due to 14 previous errors + diff --git a/src/test/ui/traits/vtable/vtable-multiple.rs b/src/test/ui/traits/vtable/vtable-multiple.rs new file mode 100644 index 000000000..7a0111c5e --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-multiple.rs @@ -0,0 +1,33 @@ +// build-fail +#![feature(rustc_attrs)] + +#[rustc_dump_vtable] +trait A { + fn foo_a(&self) {} +} + +#[rustc_dump_vtable] +trait B { + //~^ error vtable + fn foo_b(&self) {} +} + +#[rustc_dump_vtable] +trait C: A + B { + //~^ error vtable + fn foo_c(&self) {} +} + +struct S; + +impl A for S {} +impl B for S {} +impl C for S {} + +fn foo(c: &dyn C) {} +fn bar(c: &dyn B) {} + +fn main() { + foo(&S); + bar(&S); +} diff --git a/src/test/ui/traits/vtable/vtable-multiple.stderr b/src/test/ui/traits/vtable/vtable-multiple.stderr new file mode 100644 index 000000000..0dcd84433 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-multiple.stderr @@ -0,0 +1,27 @@ +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_a), + Method(::foo_b), + TraitVPtr(), + Method(::foo_c), + ] + --> $DIR/vtable-multiple.rs:16:1 + | +LL | trait C: A + B { + | ^^^^^^^^^^^^^^ + +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_b), + ] + --> $DIR/vtable-multiple.rs:10:1 + | +LL | trait B { + | ^^^^^^^ + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/traits/vtable/vtable-non-object-safe.rs b/src/test/ui/traits/vtable/vtable-non-object-safe.rs new file mode 100644 index 000000000..7661bb574 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-non-object-safe.rs @@ -0,0 +1,18 @@ +// build-fail +#![feature(rustc_attrs)] + +// Ensure that non-object-safe methods in Iterator does not generate +// vtable entries. + +#[rustc_dump_vtable] +trait A: Iterator {} +//~^ error vtable + +impl A for T where T: Iterator {} + +fn foo(_a: &mut dyn A) { +} + +fn main() { + foo(&mut vec![0, 1, 2, 3].into_iter()); +} diff --git a/src/test/ui/traits/vtable/vtable-non-object-safe.stderr b/src/test/ui/traits/vtable/vtable-non-object-safe.stderr new file mode 100644 index 000000000..9345c2711 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-non-object-safe.stderr @@ -0,0 +1,16 @@ +error: vtable entries for ` as A>`: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method( as Iterator>::next), + Method( as Iterator>::size_hint), + Method( as Iterator>::advance_by), + Method( as Iterator>::nth), + ] + --> $DIR/vtable-non-object-safe.rs:8:1 + | +LL | trait A: Iterator {} + | ^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/traits/vtable/vtable-vacant.rs b/src/test/ui/traits/vtable/vtable-vacant.rs new file mode 100644 index 000000000..a64796358 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-vacant.rs @@ -0,0 +1,31 @@ +// build-fail +#![feature(rustc_attrs)] +#![feature(negative_impls)] +#![allow(where_clauses_object_safety)] + +// B --> A + +#[rustc_dump_vtable] +trait A { + fn foo_a1(&self) {} + fn foo_a2(&self) where Self: Send {} +} + +#[rustc_dump_vtable] +trait B: A { + //~^ error vtable + fn foo_b1(&self) {} + fn foo_b2(&self) where Self: Send {} +} + +struct S; +impl !Send for S {} + +impl A for S {} +impl B for S {} + +fn foo(_: &dyn B) {} + +fn main() { + foo(&S); +} diff --git a/src/test/ui/traits/vtable/vtable-vacant.stderr b/src/test/ui/traits/vtable/vtable-vacant.stderr new file mode 100644 index 000000000..5346a7027 --- /dev/null +++ b/src/test/ui/traits/vtable/vtable-vacant.stderr @@ -0,0 +1,16 @@ +error: vtable entries for ``: [ + MetadataDropInPlace, + MetadataSize, + MetadataAlign, + Method(::foo_a1), + Vacant, + Method(::foo_b1), + Vacant, + ] + --> $DIR/vtable-vacant.rs:15:1 + | +LL | trait B: A { + | ^^^^^^^^^^ + +error: aborting due to previous error + diff --git a/src/test/ui/traits/wf-object/maybe-bound.rs b/src/test/ui/traits/wf-object/maybe-bound.rs new file mode 100644 index 000000000..17771e976 --- /dev/null +++ b/src/test/ui/traits/wf-object/maybe-bound.rs @@ -0,0 +1,18 @@ +// Test that `dyn ... + ?Sized + ...` is okay (though `?Sized` has no effect in trait objects). + +trait Foo {} + +type _0 = dyn ?Sized + Foo; +//~^ ERROR `?Trait` is not permitted in trait object types + +type _1 = dyn Foo + ?Sized; +//~^ ERROR `?Trait` is not permitted in trait object types + +type _2 = dyn Foo + ?Sized + ?Sized; +//~^ ERROR `?Trait` is not permitted in trait object types +//~| ERROR `?Trait` is not permitted in trait object types + +type _3 = dyn ?Sized + Foo; +//~^ ERROR `?Trait` is not permitted in trait object types + +fn main() {} diff --git a/src/test/ui/traits/wf-object/maybe-bound.stderr b/src/test/ui/traits/wf-object/maybe-bound.stderr new file mode 100644 index 000000000..2fe3f0fc3 --- /dev/null +++ b/src/test/ui/traits/wf-object/maybe-bound.stderr @@ -0,0 +1,32 @@ +error: `?Trait` is not permitted in trait object types + --> $DIR/maybe-bound.rs:5:15 + | +LL | type _0 = dyn ?Sized + Foo; + | ^^^^^^ + +error: `?Trait` is not permitted in trait object types + --> $DIR/maybe-bound.rs:8:21 + | +LL | type _1 = dyn Foo + ?Sized; + | ^^^^^^ + +error: `?Trait` is not permitted in trait object types + --> $DIR/maybe-bound.rs:11:21 + | +LL | type _2 = dyn Foo + ?Sized + ?Sized; + | ^^^^^^ + +error: `?Trait` is not permitted in trait object types + --> $DIR/maybe-bound.rs:11:30 + | +LL | type _2 = dyn Foo + ?Sized + ?Sized; + | ^^^^^^ + +error: `?Trait` is not permitted in trait object types + --> $DIR/maybe-bound.rs:15:15 + | +LL | type _3 = dyn ?Sized + Foo; + | ^^^^^^ + +error: aborting due to 5 previous errors + diff --git a/src/test/ui/traits/wf-object/no-duplicates.rs b/src/test/ui/traits/wf-object/no-duplicates.rs new file mode 100644 index 000000000..678ede582 --- /dev/null +++ b/src/test/ui/traits/wf-object/no-duplicates.rs @@ -0,0 +1,33 @@ +// The purpose of this test is to demonstrate that duplicating object safe traits +// that are not auto-traits is rejected even though one could reasonably accept this. + +// Some arbitrary object-safe trait: +trait Obj {} + +// Demonstrate that recursive expansion of trait aliases doesn't affect stable behavior: +type _0 = dyn Obj + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +// Some variations: + +type _1 = dyn Send + Obj + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _2 = dyn Obj + Send + Obj; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +type _3 = dyn Obj + Send + Send; // But it is OK to duplicate auto traits. + +// Take higher ranked types into account. + +// Note that `'a` and `'b` are intentionally different to make sure we consider +// them semantically the same. +trait ObjL<'l> {} +type _4 = dyn for<'a> ObjL<'a> + for<'b> ObjL<'b>; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +trait ObjT {} +type _5 = dyn ObjT fn(&'a u8)> + ObjT fn(&'b u8)>; +//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225] + +fn main() {} diff --git a/src/test/ui/traits/wf-object/no-duplicates.stderr b/src/test/ui/traits/wf-object/no-duplicates.stderr new file mode 100644 index 000000000..50dfcf956 --- /dev/null +++ b/src/test/ui/traits/wf-object/no-duplicates.stderr @@ -0,0 +1,58 @@ +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:8:21 + | +LL | type _0 = dyn Obj + Obj; + | --- ^^^ additional non-auto trait + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:13:28 + | +LL | type _1 = dyn Send + Obj + Obj; + | --- ^^^ additional non-auto trait + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:16:28 + | +LL | type _2 = dyn Obj + Send + Obj; + | --- ^^^ additional non-auto trait + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Obj + Obj {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:26:34 + | +LL | type _4 = dyn for<'a> ObjL<'a> + for<'b> ObjL<'b>; + | ---------------- ^^^^^^^^^^^^^^^^ additional non-auto trait + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: for<'a> ObjL<'a> + for<'b> ObjL<'b> {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error[E0225]: only auto traits can be used as additional traits in a trait object + --> $DIR/no-duplicates.rs:30:42 + | +LL | type _5 = dyn ObjT fn(&'a u8)> + ObjT fn(&'b u8)>; + | ------------------------ ^^^^^^^^^^^^^^^^^^^^^^^^ additional non-auto trait + | | + | first non-auto trait + | + = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: ObjT fn(&'a u8)> + ObjT fn(&'b u8)> {}` + = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0225`. diff --git a/src/test/ui/traits/wf-object/only-maybe-bound.rs b/src/test/ui/traits/wf-object/only-maybe-bound.rs new file mode 100644 index 000000000..3e6db3e99 --- /dev/null +++ b/src/test/ui/traits/wf-object/only-maybe-bound.rs @@ -0,0 +1,7 @@ +// Test that `dyn ?Sized` (i.e., a trait object with only a maybe buond) is not allowed. + +type _0 = dyn ?Sized; +//~^ ERROR at least one trait is required for an object type [E0224] +//~| ERROR ?Trait` is not permitted in trait object types + +fn main() {} diff --git a/src/test/ui/traits/wf-object/only-maybe-bound.stderr b/src/test/ui/traits/wf-object/only-maybe-bound.stderr new file mode 100644 index 000000000..cbc41feec --- /dev/null +++ b/src/test/ui/traits/wf-object/only-maybe-bound.stderr @@ -0,0 +1,15 @@ +error: `?Trait` is not permitted in trait object types + --> $DIR/only-maybe-bound.rs:3:15 + | +LL | type _0 = dyn ?Sized; + | ^^^^^^ + +error[E0224]: at least one trait is required for an object type + --> $DIR/only-maybe-bound.rs:3:11 + | +LL | type _0 = dyn ?Sized; + | ^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0224`. diff --git a/src/test/ui/traits/wf-object/reverse-order.rs b/src/test/ui/traits/wf-object/reverse-order.rs new file mode 100644 index 000000000..4f676cbe3 --- /dev/null +++ b/src/test/ui/traits/wf-object/reverse-order.rs @@ -0,0 +1,15 @@ +// run-pass + +// Ensure that `dyn $($AutoTrait)+ ObjSafe` is well-formed. + +use std::marker::Unpin; + +// Some arbitrary object-safe trait: +trait Obj {} + +type _0 = dyn Unpin; +type _1 = dyn Send + Obj; +type _2 = dyn Send + Unpin + Obj; +type _3 = dyn Send + Unpin + Sync + Obj; + +fn main() {} diff --git a/src/test/ui/traits/where-clause-vs-impl.rs b/src/test/ui/traits/where-clause-vs-impl.rs new file mode 100644 index 000000000..7cfee27ef --- /dev/null +++ b/src/test/ui/traits/where-clause-vs-impl.rs @@ -0,0 +1,45 @@ +// run-pass +#![allow(dead_code)] +#![allow(unused_variables)] +// Test that when there is a conditional (but blanket) impl and a +// where clause, we don't get confused in trait resolution. +// +// Issue #18453. + +// pretty-expanded FIXME #23616 + +use std::rc::Rc; + +pub trait Foo { + fn foo(&mut self, msg: M); +} + +pub trait Bar { + fn dummy(&self) -> M; +} + +impl> Foo for F { + fn foo(&mut self, msg: M) { + } +} + +pub struct Both { + inner: Rc<(M, F)>, +} + +impl> Clone for Both { + fn clone(&self) -> Both { + Both { inner: self.inner.clone() } + } +} + +fn repro1>(_both: Both) { +} + +fn repro2>(msg: M, foo: F) { + let both = Both { inner: Rc::new((msg, foo)) }; + repro1(both.clone()); // <--- This clone causes problem +} + +pub fn main() { +} diff --git a/src/test/ui/traits/with-bounds-default.rs b/src/test/ui/traits/with-bounds-default.rs new file mode 100644 index 000000000..31f73d79c --- /dev/null +++ b/src/test/ui/traits/with-bounds-default.rs @@ -0,0 +1,32 @@ +// run-pass + +pub trait Clone2 { + /// Returns a copy of the value. The contents of boxes + /// are copied to maintain uniqueness, while the contents of + /// managed pointers are not copied. + fn clone(&self) -> Self; +} + +trait Getter { + fn do_get(&self) -> T; + + fn do_get2(&self) -> (T, T) { + let x = self.do_get(); + (x.clone(), x.clone()) + } + +} + +impl Getter for isize { + fn do_get(&self) -> isize { *self } +} + +impl Getter for Option { + fn do_get(&self) -> T { self.as_ref().unwrap().clone() } +} + + +pub fn main() { + assert_eq!(3.do_get2(), (3, 3)); + assert_eq!(Some("hi".to_string()).do_get2(), ("hi".to_string(), "hi".to_string())); +} diff --git a/src/test/ui/traits/with-dst.rs b/src/test/ui/traits/with-dst.rs new file mode 100644 index 000000000..a3e3b31df --- /dev/null +++ b/src/test/ui/traits/with-dst.rs @@ -0,0 +1,22 @@ +// build-pass (FIXME(62277): could be check-pass?) +// #55266 + +struct VTable { + _to_dst_ptr: fn(*mut ()) -> *mut DST, +} + +trait HasVTableFor { + const VTABLE: &'static VTable; +} + +impl HasVTableFor for T { + const VTABLE: &'static VTable = &VTable { + _to_dst_ptr: |_: *mut ()| unsafe { std::mem::zeroed() }, + }; +} + +pub fn push() { + >::VTABLE; +} + +fn main() {} -- cgit v1.2.3

, + | ^^^^^^^^^^^ + | +note: required because of the requirements on the impl of `A>` for `P` + --> $DIR/issue-82830.rs:10:12 + | +LL | impl A for P { + | ^^^^^^^ ^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/issue-83538-tainted-cache-after-cycle.rs b/src/test/ui/traits/issue-83538-tainted-cache-after-cycle.rs new file mode 100644 index 000000000..3cd68ff6f --- /dev/null +++ b/src/test/ui/traits/issue-83538-tainted-cache-after-cycle.rs @@ -0,0 +1,66 @@ +// Regression test for issue #83538. The problem here is that we have +// two cycles: +// +// * `Ty` embeds `Box` indirectly, which depends on `Global: 'static`, which is OkModuloRegions. +// * But `Ty` also references `First`, which has a cycle on itself. That should just be `Ok`. +// +// But our caching mechanism was blending both cycles and giving the incorrect result. + +#![feature(rustc_attrs)] +#![allow(bad_style)] + +struct First { + b: Vec, +} + +pub struct Second { + d: Vec, +} + +struct Third<'a, f> { + g: Vec<(f, &'a f)>, +} + +enum Ty { + j(Fourth, Fifth, Sixth), +} + +struct Fourth { + o: Vec, +} + +struct Fifth { + bounds: First, +} + +struct Sixth { + p: Box, +} + +#[rustc_evaluate_where_clauses] +fn forward<'a>() +where + Vec: Unpin, + Third<'a, Ty>: Unpin, +{ +} + +#[rustc_evaluate_where_clauses] +fn reverse<'a>() +where + Third<'a, Ty>: Unpin, + Vec: Unpin, +{ +} + +fn main() { + // Key is that Vec is "ok" and Third<'_, Ty> is "ok modulo regions": + + forward(); + //~^ ERROR evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOk) + //~| ERROR evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + + reverse(); + //~^ ERROR evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOk) + //~| ERROR evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) +} diff --git a/src/test/ui/traits/issue-83538-tainted-cache-after-cycle.stderr b/src/test/ui/traits/issue-83538-tainted-cache-after-cycle.stderr new file mode 100644 index 000000000..7c4041144 --- /dev/null +++ b/src/test/ui/traits/issue-83538-tainted-cache-after-cycle.stderr @@ -0,0 +1,38 @@ +error: evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOk) + --> $DIR/issue-83538-tainted-cache-after-cycle.rs:59:5 + | +LL | Vec: Unpin, + | ----- predicate +... +LL | forward(); + | ^^^^^^^ + +error: evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + --> $DIR/issue-83538-tainted-cache-after-cycle.rs:59:5 + | +LL | Third<'a, Ty>: Unpin, + | ----- predicate +... +LL | forward(); + | ^^^^^^^ + +error: evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + --> $DIR/issue-83538-tainted-cache-after-cycle.rs:63:5 + | +LL | Third<'a, Ty>: Unpin, + | ----- predicate +... +LL | reverse(); + | ^^^^^^^ + +error: evaluate(Binder(TraitPredicate( as std::marker::Unpin>, polarity:Positive), [])) = Ok(EvaluatedToOk) + --> $DIR/issue-83538-tainted-cache-after-cycle.rs:63:5 + | +LL | Vec: Unpin, + | ----- predicate +... +LL | reverse(); + | ^^^^^^^ + +error: aborting due to 4 previous errors + diff --git a/src/test/ui/traits/issue-84399-bad-fresh-caching.rs b/src/test/ui/traits/issue-84399-bad-fresh-caching.rs new file mode 100644 index 000000000..149400156 --- /dev/null +++ b/src/test/ui/traits/issue-84399-bad-fresh-caching.rs @@ -0,0 +1,55 @@ +// compile-flags: --crate-type lib +// check-pass +// +// Regression test for issue #84399 +// Tests that we keep the full `ParamEnv` when +// caching predicates with freshened types in the global cache + +use std::marker::PhantomData; +pub trait Allocator { + type Buffer; +} +pub struct DefaultAllocator; +impl Allocator for DefaultAllocator { + type Buffer = (); +} +pub type Owned = >::Buffer; +pub type MatrixMN = Matrix>; +pub type Matrix4 = Matrix; +pub struct Matrix { + pub data: S, + _phantoms: PhantomData, +} +pub fn set_object_transform(matrix: &Matrix4<()>) { + matrix.js_buffer_view(); +} +pub trait Storable { + type Cell; + fn slice_to_items(_buffer: &()) -> &[Self::Cell] { + unimplemented!() + } +} +pub type Cell = ::Cell; +impl Storable for MatrixMN +where + DefaultAllocator: Allocator, +{ + type Cell = (); +} +pub trait JsBufferView { + fn js_buffer_view(&self) -> usize { + unimplemented!() + } +} +impl JsBufferView for [MatrixMN] +where + DefaultAllocator: Allocator, + MatrixMN: Storable, + [Cell>]: JsBufferView, +{ + fn js_buffer_view(&self) -> usize { + as Storable>::slice_to_items(&()).js_buffer_view() + } +} +impl JsBufferView for [()] {} +impl JsBufferView for MatrixMN where DefaultAllocator: Allocator {} diff --git a/src/test/ui/traits/issue-85360-eval-obligation-ice.rs b/src/test/ui/traits/issue-85360-eval-obligation-ice.rs new file mode 100644 index 000000000..19131684a --- /dev/null +++ b/src/test/ui/traits/issue-85360-eval-obligation-ice.rs @@ -0,0 +1,143 @@ +// compile-flags: --edition=2021 + +#![feature(rustc_attrs)] + +use core::any::Any; +use core::marker::PhantomData; + +fn main() { + test::>>(make()); + //~^ ERROR evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOk) + //~| ERROR evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOk) + + test::>>(make()); + //~^ ERROR evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + //~| ERROR evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) +} + +#[rustc_evaluate_where_clauses] +fn test(_: T) {} + +fn make() -> T { + todo!() +} + +struct DerefWrap(T); + +impl core::ops::Deref for DerefWrap { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +struct Storage { + phantom: PhantomData<(T, D)>, +} + +type ReadStorage = Storage>>; + +pub trait Component { + type Storage; +} + +struct VecStorage; + +struct Pos; + +impl Component for Pos { + type Storage = VecStorage; +} + +struct GenericComp { + _t: T, +} + +impl Component for GenericComp { + type Storage = VecStorage; +} + +struct GenericComp2 { + _t: T, +} + +impl Component for GenericComp2 where for<'a> &'a bool: 'a { + type Storage = VecStorage; +} + +struct ReadData { + pos_interpdata: ReadStorage>, +} + +trait System { + type SystemData; + + fn run(data: Self::SystemData, any: Box); +} + +struct Sys; + +impl System for Sys { + type SystemData = (ReadData, ReadStorage); + + fn run((data, pos): Self::SystemData, any: Box) { + > as SystemData>::setup(any); + + ParJoin::par_join((&pos, &data.pos_interpdata)); + } +} + +trait ParJoin { + fn par_join(self) + where + Self: Sized, + { + } +} + +impl<'a, T, D> ParJoin for &'a Storage +where + T: Component, + D: core::ops::Deref>, + T::Storage: Sync, +{ +} + +impl ParJoin for (A, B) +where + A: ParJoin, + B: ParJoin, +{ +} + +pub trait SystemData { + fn setup(any: Box); +} + +impl SystemData for ReadStorage +where + T: Component, +{ + fn setup(any: Box) { + let storage: &MaskedStorage = any.downcast_ref().unwrap(); + + >>::cast(&storage); + } +} + +pub struct MaskedStorage { + _inner: T::Storage, +} + +pub unsafe trait CastFrom { + fn cast(t: &T) -> &Self; +} + +unsafe impl CastFrom for dyn Any +where + T: Any + 'static, +{ + fn cast(t: &T) -> &Self { + t + } +} diff --git a/src/test/ui/traits/issue-85360-eval-obligation-ice.stderr b/src/test/ui/traits/issue-85360-eval-obligation-ice.stderr new file mode 100644 index 000000000..ebf977dd6 --- /dev/null +++ b/src/test/ui/traits/issue-85360-eval-obligation-ice.stderr @@ -0,0 +1,38 @@ +error: evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOk) + --> $DIR/issue-85360-eval-obligation-ice.rs:9:5 + | +LL | test::>>(make()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | fn test(_: T) {} + | - predicate + +error: evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOk) + --> $DIR/issue-85360-eval-obligation-ice.rs:9:5 + | +LL | test::>>(make()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | fn test(_: T) {} + | ----- predicate + +error: evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + --> $DIR/issue-85360-eval-obligation-ice.rs:13:5 + | +LL | test::>>(make()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | fn test(_: T) {} + | - predicate + +error: evaluate(Binder(TraitPredicate(> as std::marker::Sized>, polarity:Positive), [])) = Ok(EvaluatedToOkModuloRegions) + --> $DIR/issue-85360-eval-obligation-ice.rs:13:5 + | +LL | test::>>(make()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | fn test(_: T) {} + | ----- predicate + +error: aborting due to 4 previous errors + diff --git a/src/test/ui/traits/issue-85735.rs b/src/test/ui/traits/issue-85735.rs new file mode 100644 index 000000000..fb387a9c9 --- /dev/null +++ b/src/test/ui/traits/issue-85735.rs @@ -0,0 +1,13 @@ +// Regression test for the invalid suggestion in #85735 (the +// underlying issue #21974 still exists here). + +trait Foo {} +impl<'a, 'b, T> Foo for T +where + T: FnMut(&'a ()), + //~^ ERROR: type annotations needed + T: FnMut(&'b ()), +{ +} + +fn main() {} diff --git a/src/test/ui/traits/issue-85735.stderr b/src/test/ui/traits/issue-85735.stderr new file mode 100644 index 000000000..fa280135b --- /dev/null +++ b/src/test/ui/traits/issue-85735.stderr @@ -0,0 +1,11 @@ +error[E0283]: type annotations needed: cannot satisfy `T: FnMut<(&'a (),)>` + --> $DIR/issue-85735.rs:7:8 + | +LL | T: FnMut(&'a ()), + | ^^^^^^^^^^^^^ + | + = note: cannot satisfy `T: FnMut<(&'a (),)>` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0283`. diff --git a/src/test/ui/traits/issue-87558.rs b/src/test/ui/traits/issue-87558.rs new file mode 100644 index 000000000..c5d86bd63 --- /dev/null +++ b/src/test/ui/traits/issue-87558.rs @@ -0,0 +1,9 @@ +struct ErrorKind; +struct Error(ErrorKind); +impl Fn(&isize) for Error { + //~^ ERROR manual implementations of `Fn` are experimental + //~| ERROR associated type bindings are not allowed here + fn from() {} //~ ERROR method `from` is not a member of trait `Fn` +} + +fn main() {} diff --git a/src/test/ui/traits/issue-87558.stderr b/src/test/ui/traits/issue-87558.stderr new file mode 100644 index 000000000..494274d8c --- /dev/null +++ b/src/test/ui/traits/issue-87558.stderr @@ -0,0 +1,24 @@ +error[E0407]: method `from` is not a member of trait `Fn` + --> $DIR/issue-87558.rs:6:5 + | +LL | fn from() {} + | ^^^^^^^^^^^^ not a member of trait `Fn` + +error[E0183]: manual implementations of `Fn` are experimental + --> $DIR/issue-87558.rs:3:6 + | +LL | impl Fn(&isize) for Error { + | ^^^^^^^^^^ manual implementations of `Fn` are experimental + | + = help: add `#![feature(unboxed_closures)]` to the crate attributes to enable + +error[E0229]: associated type bindings are not allowed here + --> $DIR/issue-87558.rs:3:6 + | +LL | impl Fn(&isize) for Error { + | ^^^^^^^^^^ associated type not allowed here + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0183, E0229, E0407. +For more information about an error, try `rustc --explain E0183`. diff --git a/src/test/ui/traits/issue-89119.rs b/src/test/ui/traits/issue-89119.rs new file mode 100644 index 000000000..170f69915 --- /dev/null +++ b/src/test/ui/traits/issue-89119.rs @@ -0,0 +1,11 @@ +// This is a regression test for issue #89119: an issue in intercrate mode caching. +// +// It requires multiple crates, of course, but the bug is triggered by the code in the dependency, +// not the main crate. This is why this file is empty. +// +// The auxiliary crate used in the test contains the code minimized from `zvariant-2.8.0`. + +// check-pass +// aux-build: issue_89119_intercrate_caching.rs + +fn main() {} diff --git a/src/test/ui/traits/issue-90195-2.rs b/src/test/ui/traits/issue-90195-2.rs new file mode 100644 index 000000000..b739dc46e --- /dev/null +++ b/src/test/ui/traits/issue-90195-2.rs @@ -0,0 +1,20 @@ +// check-pass +pub trait Archive { + type Archived; +} + +impl Archive for Option { + type Archived = (); +} +pub type Archived = ::Archived; + +pub trait Deserialize {} + +const ARRAY_SIZE: usize = 32; +impl<__D> Deserialize<__D> for () +where + Option<[u8; ARRAY_SIZE]>: Archive, + Archived>: Deserialize<__D>, +{ +} +fn main() {} diff --git a/src/test/ui/traits/issue-90195.rs b/src/test/ui/traits/issue-90195.rs new file mode 100644 index 000000000..543c9f197 --- /dev/null +++ b/src/test/ui/traits/issue-90195.rs @@ -0,0 +1,21 @@ +// check-pass +pub trait Archive { + type Archived; +} + +impl Archive for Option { + type Archived = (); +} +pub type Archived = ::Archived; + +pub trait Deserialize {} + +const ARRAY_SIZE: usize = 32; +impl<__D> Deserialize<__D> for () +where + Option<[u8; ARRAY_SIZE]>: Archive, + Option<[u8; ARRAY_SIZE]>: Archive, + Archived>: Deserialize<__D>, +{ +} +fn main() {} diff --git a/src/test/ui/traits/issue-90662-projection-caching.rs b/src/test/ui/traits/issue-90662-projection-caching.rs new file mode 100644 index 000000000..879f30071 --- /dev/null +++ b/src/test/ui/traits/issue-90662-projection-caching.rs @@ -0,0 +1,34 @@ +// check-pass + +// Regression test for issue #90662 +// Tests that projection caching does not cause a spurious error + +trait HasProvider {} +trait Provider { + type Interface: ?Sized; +} + +trait Repository {} +trait Service {} + +struct DbConnection; +impl Provider for DbConnection { + type Interface = DbConnection; +} + +struct RepositoryImpl; +impl> Provider for RepositoryImpl { + type Interface = dyn Repository; +} + +struct ServiceImpl; +impl> Provider for ServiceImpl { + type Interface = dyn Service; +} + +struct TestModule; +impl HasProvider<>::Interface> for TestModule {} +impl HasProvider<>::Interface> for TestModule {} +impl HasProvider<>::Interface> for TestModule {} + +fn main() {} diff --git a/src/test/ui/traits/issue-91594.rs b/src/test/ui/traits/issue-91594.rs new file mode 100644 index 000000000..930f7f0c6 --- /dev/null +++ b/src/test/ui/traits/issue-91594.rs @@ -0,0 +1,17 @@ +// #91594: This used to ICE. + +trait Component { + type Interface; +} +trait HasComponent {} + +struct Foo; + +impl HasComponent<>::Interface> for Foo {} +//~^ ERROR the trait bound `Foo: HasComponent<()>` is not satisfied + +impl> Component for Foo { + type Interface = u8; +} + +fn main() {} diff --git a/src/test/ui/traits/issue-91594.stderr b/src/test/ui/traits/issue-91594.stderr new file mode 100644 index 000000000..f2b3de13b --- /dev/null +++ b/src/test/ui/traits/issue-91594.stderr @@ -0,0 +1,16 @@ +error[E0277]: the trait bound `Foo: HasComponent<()>` is not satisfied + --> $DIR/issue-91594.rs:10:6 + | +LL | impl HasComponent<>::Interface> for Foo {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasComponent<()>` is not implemented for `Foo` + | + = help: the trait `HasComponent<>::Interface>` is implemented for `Foo` +note: required because of the requirements on the impl of `Component` for `Foo` + --> $DIR/issue-91594.rs:13:27 + | +LL | impl> Component for Foo { + | ^^^^^^^^^^^^ ^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/issue-91949-hangs-on-recursion.rs b/src/test/ui/traits/issue-91949-hangs-on-recursion.rs new file mode 100644 index 000000000..499a64f28 --- /dev/null +++ b/src/test/ui/traits/issue-91949-hangs-on-recursion.rs @@ -0,0 +1,31 @@ +// build-fail +// compile-flags: -Zinline-mir=no +// error-pattern: overflow evaluating the requirement `(): Sized` +// error-pattern: function cannot return without recursing + +// Regression test for #91949. +// This hanged *forever* on 1.56, fixed by #90423. + +#![recursion_limit = "256"] + +struct Wrapped(T); + +struct IteratorOfWrapped>(I); + +impl> Iterator for IteratorOfWrapped { + type Item = Wrapped; + fn next(&mut self) -> Option> { + self.0.next().map(Wrapped) + } +} + +fn recurse(elements: T) -> Vec +where + T: Iterator, +{ + recurse(IteratorOfWrapped(elements).map(|t| t.0)) +} + +fn main() { + recurse(std::iter::empty()); +} diff --git a/src/test/ui/traits/issue-91949-hangs-on-recursion.stderr b/src/test/ui/traits/issue-91949-hangs-on-recursion.stderr new file mode 100644 index 000000000..f5d6d72af --- /dev/null +++ b/src/test/ui/traits/issue-91949-hangs-on-recursion.stderr @@ -0,0 +1,24 @@ +warning: function cannot return without recursing + --> $DIR/issue-91949-hangs-on-recursion.rs:22:1 + | +LL | / fn recurse(elements: T) -> Vec +LL | | where +LL | | T: Iterator, + | |___________________________^ cannot return without recursing +LL | { +LL | recurse(IteratorOfWrapped(elements).map(|t| t.0)) + | ------------------------------------------------- recursive call site + | + = note: `#[warn(unconditional_recursion)]` on by default + = help: a `loop` may express intention better if this is on purpose + +error[E0275]: overflow evaluating the requirement `(): Sized` + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "512"]` attribute to your crate (`issue_91949_hangs_on_recursion`) + = note: required because of the requirements on the impl of `Iterator` for `std::iter::Empty<()>` + = note: 171 redundant requirements hidden + = note: required because of the requirements on the impl of `Iterator` for `IteratorOfWrapped<(), Map>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>, [closure@$DIR/issue-91949-hangs-on-recursion.rs:26:45: 26:48]>>` + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/issue-92292.rs b/src/test/ui/traits/issue-92292.rs new file mode 100644 index 000000000..bb3700a2b --- /dev/null +++ b/src/test/ui/traits/issue-92292.rs @@ -0,0 +1,32 @@ +// check-pass + +use std::marker::PhantomData; + +pub struct MyGenericType { + _marker: PhantomData<*const T>, +} + +pub struct MyNonGenericType; + +impl From> for MyNonGenericType { + fn from(_: MyGenericType) -> Self { + todo!() + } +} + +pub trait MyTrait { + const MY_CONSTANT: i32; +} + +impl MyTrait for MyGenericType +where + Self: Into, +{ + const MY_CONSTANT: i32 = 1; +} + +impl MyGenericType { + const MY_OTHER_CONSTANT: i32 = as MyTrait>::MY_CONSTANT; +} + +fn main() {} diff --git a/src/test/ui/traits/issue-9394-inherited-calls.rs b/src/test/ui/traits/issue-9394-inherited-calls.rs new file mode 100644 index 000000000..cc0dd4fc1 --- /dev/null +++ b/src/test/ui/traits/issue-9394-inherited-calls.rs @@ -0,0 +1,62 @@ +// run-pass + +trait Base: Base2 + Base3{ + fn foo(&self) -> String; + fn foo1(&self) -> String; + fn foo2(&self) -> String{ + "base foo2".to_string() + } +} + +trait Base2: Base3{ + fn baz(&self) -> String; +} + +trait Base3{ + fn root(&self) -> String; +} + +trait Super: Base{ + fn bar(&self) -> String; +} + +struct X; + +impl Base for X { + fn foo(&self) -> String{ + "base foo".to_string() + } + fn foo1(&self) -> String{ + "base foo1".to_string() + } + +} + +impl Base2 for X { + fn baz(&self) -> String{ + "base2 baz".to_string() + } +} + +impl Base3 for X { + fn root(&self) -> String{ + "base3 root".to_string() + } +} + +impl Super for X { + fn bar(&self) -> String{ + "super bar".to_string() + } +} + +pub fn main() { + let n = X; + let s = &n as &dyn Super; + assert_eq!(s.bar(),"super bar".to_string()); + assert_eq!(s.foo(),"base foo".to_string()); + assert_eq!(s.foo1(),"base foo1".to_string()); + assert_eq!(s.foo2(),"base foo2".to_string()); + assert_eq!(s.baz(),"base2 baz".to_string()); + assert_eq!(s.root(),"base3 root".to_string()); +} diff --git a/src/test/ui/traits/issue-95311.rs b/src/test/ui/traits/issue-95311.rs new file mode 100644 index 000000000..9d40d254a --- /dev/null +++ b/src/test/ui/traits/issue-95311.rs @@ -0,0 +1,19 @@ +// check-pass + +// Test to check that pointee trait doesn't let region variables escape into the cache + +#![feature(ptr_metadata)] + +trait Bar: Sized + 'static {} + +struct Foo { + marker: std::marker::PhantomData, +} + +impl Foo { + fn foo(value: &T) { + std::ptr::metadata(value); + } +} + +fn main() {} diff --git a/src/test/ui/traits/issue-95898.rs b/src/test/ui/traits/issue-95898.rs new file mode 100644 index 000000000..41a20b899 --- /dev/null +++ b/src/test/ui/traits/issue-95898.rs @@ -0,0 +1,9 @@ +// Test for #95898: The trait suggestion had an extra `:` after the trait. +// edition:2021 + +fn foo(t: T) { + t.clone(); + //~^ ERROR no method named `clone` found for type parameter `T` in the current scope +} + +fn main() {} diff --git a/src/test/ui/traits/issue-95898.stderr b/src/test/ui/traits/issue-95898.stderr new file mode 100644 index 000000000..ca7bacdbf --- /dev/null +++ b/src/test/ui/traits/issue-95898.stderr @@ -0,0 +1,17 @@ +error[E0599]: no method named `clone` found for type parameter `T` in the current scope + --> $DIR/issue-95898.rs:5:7 + | +LL | fn foo(t: T) { + | - method `clone` not found for this type parameter +LL | t.clone(); + | ^^^^^ method not found in `T` + | + = help: items from traits can only be used if the type parameter is bounded by the trait +help: the following trait defines an item `clone`, perhaps you need to restrict type parameter `T` with it: + | +LL | fn foo(t: T) { + | +++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/traits/issue-96664.rs b/src/test/ui/traits/issue-96664.rs new file mode 100644 index 000000000..3c5314af7 --- /dev/null +++ b/src/test/ui/traits/issue-96664.rs @@ -0,0 +1,16 @@ +// check-pass + +#![feature(trait_alias)] + +pub trait State = Clone + Send + Sync + PartialOrd + PartialEq + std::fmt::Display; +pub trait RandState = FnMut() -> S + Send; + +pub trait Evaluator { + type State; +} + +pub struct Evolver { + rand_state: Box>, +} + +fn main() {} diff --git a/src/test/ui/traits/issue-96665.rs b/src/test/ui/traits/issue-96665.rs new file mode 100644 index 000000000..a571d48d9 --- /dev/null +++ b/src/test/ui/traits/issue-96665.rs @@ -0,0 +1,16 @@ +// check-pass + +pub trait Sequence> {} + +pub trait NodeWalk>: + Sequence +{ +} + +pub trait GraphBase { + type NodeIndex; +} + +pub trait WalkableGraph: GraphBase {} + +fn main() {} diff --git a/src/test/ui/traits/issue-97576.rs b/src/test/ui/traits/issue-97576.rs new file mode 100644 index 000000000..fdc85e9fa --- /dev/null +++ b/src/test/ui/traits/issue-97576.rs @@ -0,0 +1,13 @@ +struct Foo { + bar: String, +} + +impl Foo { + pub fn new(bar: impl ToString) -> Self { + Self { + bar: bar.into(), //~ ERROR the trait bound `String: From` is not satisfied + } + } +} + +fn main() {} diff --git a/src/test/ui/traits/issue-97576.stderr b/src/test/ui/traits/issue-97576.stderr new file mode 100644 index 000000000..bdee073d6 --- /dev/null +++ b/src/test/ui/traits/issue-97576.stderr @@ -0,0 +1,11 @@ +error[E0277]: the trait bound `String: From` is not satisfied + --> $DIR/issue-97576.rs:8:22 + | +LL | bar: bar.into(), + | ^^^^ the trait `From` is not implemented for `String` + | + = note: required because of the requirements on the impl of `Into` for `impl ToString` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/issue-97695-double-trivial-bound.rs b/src/test/ui/traits/issue-97695-double-trivial-bound.rs new file mode 100644 index 000000000..213605b51 --- /dev/null +++ b/src/test/ui/traits/issue-97695-double-trivial-bound.rs @@ -0,0 +1,24 @@ +// compile-flags: -Zinline-mir --emit=mir +// build-pass + +pub trait Associate { + type Associated; +} + +pub struct Wrap<'a> { + pub field: &'a i32, +} + +pub trait Create { + fn create() -> Self; +} + +pub fn oh_no<'a, T>() +where + Wrap<'a>: Associate, + as Associate>::Associated: Create, +{ + as Associate>::Associated::create(); +} + +pub fn main() {} diff --git a/src/test/ui/traits/item-inside-macro.rs b/src/test/ui/traits/item-inside-macro.rs new file mode 100644 index 000000000..54bf872d0 --- /dev/null +++ b/src/test/ui/traits/item-inside-macro.rs @@ -0,0 +1,30 @@ +// run-pass +// Issue #34183 + +macro_rules! foo { + () => { + fn foo() { } + } +} + +macro_rules! bar { + () => { + fn bar(); + } +} + +trait Bleh { + foo!(); + bar!(); +} + +struct Test; + +impl Bleh for Test { + fn bar() {} +} + +fn main() { + Test::bar(); + Test::foo(); +} diff --git a/src/test/ui/traits/item-privacy.rs b/src/test/ui/traits/item-privacy.rs new file mode 100644 index 000000000..38d06b967 --- /dev/null +++ b/src/test/ui/traits/item-privacy.rs @@ -0,0 +1,134 @@ +#![feature(associated_type_defaults)] + +struct S; + +mod method { + trait A { + fn a(&self) { } + } + + pub trait B { + fn b(&self) { } + } + + pub trait C: A + B { + fn c(&self) { } + } + + impl A for ::S {} + impl B for ::S {} + impl C for ::S {} +} + +mod assoc_const { + trait A { + const A: u8 = 0; + } + + pub trait B { + const B: u8 = 0; + } + + pub trait C: A + B { + const C: u8 = 0; + } + + impl A for ::S {} + impl B for ::S {} + impl C for ::S {} +} + +mod assoc_ty { + trait A { + type A = u8; + } + + pub trait B { + type B = u8; + } + + pub trait C: A + B { + type C = u8; + } + + impl A for ::S {} + impl B for ::S {} + impl C for ::S {} +} + +fn check_method() { + // A is private + // B is pub, not in scope + // C : A + B is pub, in scope + use method::C; + + // Methods, method call + // a, b, c are resolved as trait items, their traits need to be in scope + S.a(); //~ ERROR no method named `a` found + S.b(); //~ ERROR no method named `b` found + S.c(); // OK + // a, b, c are resolved as inherent items, their traits don't need to be in scope + let c = &S as &dyn C; + c.a(); //~ ERROR associated function `a` is private + c.b(); // OK + c.c(); // OK + + // Methods, UFCS + // a, b, c are resolved as trait items, their traits need to be in scope + S::a(&S); + //~^ ERROR no function or associated item named `a` found + S::b(&S); + //~^ ERROR no function or associated item named `b` found + S::c(&S); // OK + // a, b, c are resolved as inherent items, their traits don't need to be in scope + ::a(&S); //~ ERROR associated function `a` is private + ::b(&S); // OK + C::c(&S); // OK +} + +fn check_assoc_const() { + // A is private + // B is pub, not in scope + // C : A + B is pub, in scope + use assoc_const::C; + + // Associated constants + // A, B, C are resolved as trait items, their traits need to be in scope + S::A; //~ ERROR no associated item named `A` found + S::B; //~ ERROR no associated item named `B` found + S::C; // OK + // A, B, C are resolved as inherent items, their traits don't need to be in scope + ::A; //~ ERROR associated constant `A` is private + //~^ ERROR the trait `assoc_const::C` cannot be made into an object + ::B; // ERROR the trait `assoc_const::C` cannot be made into an object + C::C; // OK +} + +fn check_assoc_ty() { + // A is private + // B is pub, not in scope + // C : A + B is pub, in scope + use assoc_ty::C; + + // Associated types + // A, B, C are resolved as trait items, their traits need to be in scope, not implemented yet + let _: S::A; //~ ERROR ambiguous associated type + let _: S::B; //~ ERROR ambiguous associated type + let _: S::C; //~ ERROR ambiguous associated type + // A, B, C are resolved as inherent items, their traits don't need to be in scope + let _: T::A; //~ ERROR associated type `A` is private + let _: T::B; // OK + let _: T::C; // OK + + // Associated types, bindings + let _: dyn assoc_ty::B< + B = u8, // OK + >; + let _: dyn C< + A = u8, //~ ERROR associated type `A` is private + B = u8, // OK + C = u8, // OK + >; +} + +fn main() {} diff --git a/src/test/ui/traits/item-privacy.stderr b/src/test/ui/traits/item-privacy.stderr new file mode 100644 index 000000000..7f78b37ba --- /dev/null +++ b/src/test/ui/traits/item-privacy.stderr @@ -0,0 +1,180 @@ +error[E0599]: no method named `a` found for struct `S` in the current scope + --> $DIR/item-privacy.rs:67:7 + | +LL | struct S; + | -------- method `a` not found for this struct +... +LL | S.a(); + | ^ method not found in `S` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `method::A` defines an item `a`, perhaps you need to implement it + --> $DIR/item-privacy.rs:6:5 + | +LL | trait A { + | ^^^^^^^ + +error[E0599]: no method named `b` found for struct `S` in the current scope + --> $DIR/item-privacy.rs:68:7 + | +LL | struct S; + | -------- method `b` not found for this struct +... +LL | fn b(&self) { } + | - the method is available for `S` here +... +LL | S.b(); + | ^ method not found in `S` + | + = help: items from traits can only be used if the trait is in scope +help: the following trait is implemented but not in scope; perhaps add a `use` for it: + | +LL | use method::B; + | + +error[E0624]: associated function `a` is private + --> $DIR/item-privacy.rs:72:7 + | +LL | fn a(&self) { } + | ----------- private associated function defined here +... +LL | c.a(); + | ^ private associated function + +error[E0599]: no function or associated item named `a` found for struct `S` in the current scope + --> $DIR/item-privacy.rs:78:8 + | +LL | struct S; + | -------- function or associated item `a` not found for this struct +... +LL | S::a(&S); + | ^ function or associated item not found in `S` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `method::A` defines an item `a`, perhaps you need to implement it + --> $DIR/item-privacy.rs:6:5 + | +LL | trait A { + | ^^^^^^^ + +error[E0599]: no function or associated item named `b` found for struct `S` in the current scope + --> $DIR/item-privacy.rs:80:8 + | +LL | struct S; + | -------- function or associated item `b` not found for this struct +... +LL | S::b(&S); + | ^ function or associated item not found in `S` + | + = help: items from traits can only be used if the trait is in scope +help: the following trait is implemented but not in scope; perhaps add a `use` for it: + | +LL | use method::B; + | + +error[E0624]: associated function `a` is private + --> $DIR/item-privacy.rs:84:14 + | +LL | fn a(&self) { } + | ----------- private associated function defined here +... +LL | ::a(&S); + | ^ private associated function + +error[E0599]: no associated item named `A` found for struct `S` in the current scope + --> $DIR/item-privacy.rs:97:8 + | +LL | struct S; + | -------- associated item `A` not found for this struct +... +LL | S::A; + | ^ associated item not found in `S` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `assoc_const::A` defines an item `A`, perhaps you need to implement it + --> $DIR/item-privacy.rs:24:5 + | +LL | trait A { + | ^^^^^^^ + +error[E0599]: no associated item named `B` found for struct `S` in the current scope + --> $DIR/item-privacy.rs:98:8 + | +LL | struct S; + | -------- associated item `B` not found for this struct +... +LL | S::B; + | ^ associated item not found in `S` + | + = help: items from traits can only be used if the trait is in scope +help: the following trait is implemented but not in scope; perhaps add a `use` for it: + | +LL | use assoc_const::B; + | + +error[E0624]: associated constant `A` is private + --> $DIR/item-privacy.rs:101:14 + | +LL | const A: u8 = 0; + | ----------- private associated constant defined here +... +LL | ::A; + | ^ private associated constant + +error[E0038]: the trait `assoc_const::C` cannot be made into an object + --> $DIR/item-privacy.rs:101:6 + | +LL | ::A; + | ^^^^^ `assoc_const::C` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/item-privacy.rs:25:15 + | +LL | const A: u8 = 0; + | ^ ...because it contains this associated `const` +... +LL | const B: u8 = 0; + | ^ ...because it contains this associated `const` +... +LL | pub trait C: A + B { + | - this trait cannot be made into an object... +LL | const C: u8 = 0; + | ^ ...because it contains this associated `const` + = help: consider moving `C` to another trait + = help: consider moving `A` to another trait + = help: consider moving `B` to another trait + +error[E0223]: ambiguous associated type + --> $DIR/item-privacy.rs:115:12 + | +LL | let _: S::A; + | ^^^^ help: use fully-qualified syntax: `::A` + +error[E0223]: ambiguous associated type + --> $DIR/item-privacy.rs:116:12 + | +LL | let _: S::B; + | ^^^^ help: use fully-qualified syntax: `::B` + +error[E0223]: ambiguous associated type + --> $DIR/item-privacy.rs:117:12 + | +LL | let _: S::C; + | ^^^^ help: use fully-qualified syntax: `::C` + +error: associated type `A` is private + --> $DIR/item-privacy.rs:119:12 + | +LL | let _: T::A; + | ^^^^ private associated type + +error: associated type `A` is private + --> $DIR/item-privacy.rs:128:9 + | +LL | A = u8, + | ^^^^^^ private associated type + +error: aborting due to 15 previous errors + +Some errors have detailed explanations: E0038, E0223, E0599, E0624. +For more information about an error, try `rustc --explain E0038`. diff --git a/src/test/ui/traits/kindck-owned-contains-1.rs b/src/test/ui/traits/kindck-owned-contains-1.rs new file mode 100644 index 000000000..8adb06ba3 --- /dev/null +++ b/src/test/ui/traits/kindck-owned-contains-1.rs @@ -0,0 +1,21 @@ +// run-pass +#![allow(non_snake_case)] +#![allow(non_camel_case_types)] + +trait repeat { fn get(&self) -> A; } + +impl repeat for Box { + fn get(&self) -> A { + (**self).clone() + } +} + +fn repeater(v: Box) -> Box+'static> { + Box::new(v) as Box+'static> // No +} + +pub fn main() { + let x = 3; + let y = repeater(Box::new(x)); + assert_eq!(x, y.get()); +} diff --git a/src/test/ui/traits/map-types.rs b/src/test/ui/traits/map-types.rs new file mode 100644 index 000000000..dc33b9618 --- /dev/null +++ b/src/test/ui/traits/map-types.rs @@ -0,0 +1,19 @@ +use std::collections::HashMap; + + + +trait Map +{ + fn get(&self, k: K) -> V { panic!() } +} + +impl Map for HashMap {} + +// Test that trait types printed in error msgs include the type arguments. + +fn main() { + let x: Box> = HashMap::new().into(); + let x: Box> = x; + let y: Box> = Box::new(x); + //~^ ERROR `Box>: Map` is not satisfied +} diff --git a/src/test/ui/traits/map-types.stderr b/src/test/ui/traits/map-types.stderr new file mode 100644 index 000000000..f685c50b0 --- /dev/null +++ b/src/test/ui/traits/map-types.stderr @@ -0,0 +1,12 @@ +error[E0277]: the trait bound `Box>: Map` is not satisfied + --> $DIR/map-types.rs:17:41 + | +LL | let y: Box> = Box::new(x); + | ^^^^^^^^^^^ the trait `Map` is not implemented for `Box>` + | + = help: the trait `Map` is implemented for `HashMap` + = note: required for the cast from `Box>` to the object type `dyn Map` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/matching-lifetimes.rs b/src/test/ui/traits/matching-lifetimes.rs new file mode 100644 index 000000000..1430dc655 --- /dev/null +++ b/src/test/ui/traits/matching-lifetimes.rs @@ -0,0 +1,20 @@ +// Tests that the trait matching code takes lifetime parameters into account. +// (Issue #15517.) + +struct Foo<'a,'b> { + x: &'a isize, + y: &'b isize, +} + +trait Tr : Sized { + fn foo(x: Self) {} +} + +impl<'a,'b> Tr for Foo<'a,'b> { + fn foo(x: Foo<'b,'a>) { + //~^ ERROR method not compatible with trait + //~^^ ERROR method not compatible with trait + } +} + +fn main(){} diff --git a/src/test/ui/traits/matching-lifetimes.stderr b/src/test/ui/traits/matching-lifetimes.stderr new file mode 100644 index 000000000..de1c878a5 --- /dev/null +++ b/src/test/ui/traits/matching-lifetimes.stderr @@ -0,0 +1,41 @@ +error[E0308]: method not compatible with trait + --> $DIR/matching-lifetimes.rs:14:5 + | +LL | fn foo(x: Foo<'b,'a>) { + | ^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch + | + = note: expected fn pointer `fn(Foo<'a, 'b>)` + found fn pointer `fn(Foo<'b, 'a>)` +note: the lifetime `'b` as defined here... + --> $DIR/matching-lifetimes.rs:13:9 + | +LL | impl<'a,'b> Tr for Foo<'a,'b> { + | ^^ +note: ...does not necessarily outlive the lifetime `'a` as defined here + --> $DIR/matching-lifetimes.rs:13:6 + | +LL | impl<'a,'b> Tr for Foo<'a,'b> { + | ^^ + +error[E0308]: method not compatible with trait + --> $DIR/matching-lifetimes.rs:14:5 + | +LL | fn foo(x: Foo<'b,'a>) { + | ^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch + | + = note: expected fn pointer `fn(Foo<'a, 'b>)` + found fn pointer `fn(Foo<'b, 'a>)` +note: the lifetime `'a` as defined here... + --> $DIR/matching-lifetimes.rs:13:6 + | +LL | impl<'a,'b> Tr for Foo<'a,'b> { + | ^^ +note: ...does not necessarily outlive the lifetime `'b` as defined here + --> $DIR/matching-lifetimes.rs:13:9 + | +LL | impl<'a,'b> Tr for Foo<'a,'b> { + | ^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/traits/method-private.rs b/src/test/ui/traits/method-private.rs new file mode 100644 index 000000000..4cd184854 --- /dev/null +++ b/src/test/ui/traits/method-private.rs @@ -0,0 +1,20 @@ +mod inner { + pub trait Bar { + fn method(&self); + } + + pub struct Foo; + + impl Foo { + fn method(&self) {} + } + + impl Bar for Foo { + fn method(&self) {} + } +} + +fn main() { + let foo = inner::Foo; + foo.method(); //~ ERROR is private +} diff --git a/src/test/ui/traits/method-private.stderr b/src/test/ui/traits/method-private.stderr new file mode 100644 index 000000000..8e991ec01 --- /dev/null +++ b/src/test/ui/traits/method-private.stderr @@ -0,0 +1,18 @@ +error[E0624]: associated function `method` is private + --> $DIR/method-private.rs:19:9 + | +LL | fn method(&self) {} + | ---------------- private associated function defined here +... +LL | foo.method(); + | ^^^^^^ private associated function + | + = help: items from traits can only be used if the trait is in scope +help: the following trait is implemented but not in scope; perhaps add a `use` for it: + | +LL | use inner::Bar; + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0624`. diff --git a/src/test/ui/traits/monad.rs b/src/test/ui/traits/monad.rs new file mode 100644 index 000000000..5d0612cf8 --- /dev/null +++ b/src/test/ui/traits/monad.rs @@ -0,0 +1,48 @@ +// run-pass + +#![allow(non_camel_case_types)] + + + +trait vec_monad { + fn bind(&self, f: F ) -> Vec where F: FnMut(&A) -> Vec ; +} + +impl vec_monad for Vec { + fn bind(&self, mut f: F) -> Vec where F: FnMut(&A) -> Vec { + let mut r = Vec::new(); + for elt in self { + r.extend(f(elt)); + } + r + } +} + +trait option_monad { + fn bind(&self, f: F) -> Option where F: FnOnce(&A) -> Option; +} + +impl option_monad for Option { + fn bind(&self, f: F) -> Option where F: FnOnce(&A) -> Option { + match *self { + Some(ref a) => { f(a) } + None => { None } + } + } +} + +fn transform(x: Option) -> Option { + x.bind(|n| Some(*n + 1) ).bind(|n| Some(n.to_string()) ) +} + +pub fn main() { + assert_eq!(transform(Some(10)), Some("11".to_string())); + assert_eq!(transform(None), None); + assert_eq!((vec!["hi".to_string()]) + .bind(|x| vec![x.clone(), format!("{}!", x)] ) + .bind(|x| vec![x.clone(), format!("{}?", x)] ), + ["hi".to_string(), + "hi?".to_string(), + "hi!".to_string(), + "hi!?".to_string()]); +} diff --git a/src/test/ui/traits/monomorphized-callees-with-ty-params-3314.rs b/src/test/ui/traits/monomorphized-callees-with-ty-params-3314.rs new file mode 100644 index 000000000..bc314a39d --- /dev/null +++ b/src/test/ui/traits/monomorphized-callees-with-ty-params-3314.rs @@ -0,0 +1,32 @@ +// run-pass +// pretty-expanded FIXME #23616 + +trait Serializer { +} + +trait Serializable { + fn serialize(&self, s: S); +} + +impl Serializable for isize { + fn serialize(&self, _s: S) { } +} + +struct F { a: A } + +impl Serializable for F { + fn serialize(&self, s: S) { + self.a.serialize(s); + } +} + +impl Serializer for isize { +} + +pub fn main() { + let foo = F { a: 1 }; + foo.serialize(1); + + let bar = F { a: F {a: 1 } }; + bar.serialize(2); +} diff --git a/src/test/ui/traits/multidispatch-bad.rs b/src/test/ui/traits/multidispatch-bad.rs new file mode 100644 index 000000000..b625b9615 --- /dev/null +++ b/src/test/ui/traits/multidispatch-bad.rs @@ -0,0 +1,22 @@ +// Test that we detect an illegal combination of types. + +trait Convert { + fn convert(&self) -> Target; +} + +impl Convert for i32 { + fn convert(&self) -> u32 { + *self as u32 + } +} + +fn test(_: T, _: U) +where T : Convert +{ +} + +fn a() { + test(22i32, 44i32); //~ ERROR mismatched types +} + +fn main() {} diff --git a/src/test/ui/traits/multidispatch-bad.stderr b/src/test/ui/traits/multidispatch-bad.stderr new file mode 100644 index 000000000..8b6e61006 --- /dev/null +++ b/src/test/ui/traits/multidispatch-bad.stderr @@ -0,0 +1,21 @@ +error[E0308]: mismatched types + --> $DIR/multidispatch-bad.rs:19:17 + | +LL | test(22i32, 44i32); + | ---- ^^^^^ expected `u32`, found `i32` + | | + | arguments to this function are incorrect + | +note: function defined here + --> $DIR/multidispatch-bad.rs:13:4 + | +LL | fn test(_: T, _: U) + | ^^^^ ---- ---- +help: change the type of the numeric literal from `i32` to `u32` + | +LL | test(22i32, 44u32); + | ~~~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/src/test/ui/traits/multidispatch-conditional-impl-not-considered.rs b/src/test/ui/traits/multidispatch-conditional-impl-not-considered.rs new file mode 100644 index 000000000..f845e198a --- /dev/null +++ b/src/test/ui/traits/multidispatch-conditional-impl-not-considered.rs @@ -0,0 +1,24 @@ +// run-pass +// Test that we correctly ignore the blanket impl +// because (in this case) `T` does not impl `Clone`. +// +// Issue #17594. + +use std::cell::RefCell; + +trait Foo { + fn foo(&self) {} +} + +impl Foo for T where T: Clone {} + +struct Bar; + +impl Bar { + fn foo(&self) {} +} + +fn main() { + let b = RefCell::new(Bar); + b.borrow().foo(); +} diff --git a/src/test/ui/traits/multidispatch-convert-ambig-dest.rs b/src/test/ui/traits/multidispatch-convert-ambig-dest.rs new file mode 100644 index 000000000..aa74e11c3 --- /dev/null +++ b/src/test/ui/traits/multidispatch-convert-ambig-dest.rs @@ -0,0 +1,31 @@ +// Check that we get an error in a multidisptach scenario where the +// set of impls is ambiguous. + +trait Convert { + fn convert(&self) -> Target; +} + +impl Convert for i32 { + fn convert(&self) -> i8 { + *self as i8 + } +} + +impl Convert for i32 { + fn convert(&self) -> i16 { + *self as i16 + } +} + +fn test(_: T, _: U) +where T : Convert +{ +} + +fn a() { + test(22, std::default::Default::default()); + //~^ ERROR type annotations needed + //~| ERROR type annotations needed +} + +fn main() {} diff --git a/src/test/ui/traits/multidispatch-convert-ambig-dest.stderr b/src/test/ui/traits/multidispatch-convert-ambig-dest.stderr new file mode 100644 index 000000000..cbec35934 --- /dev/null +++ b/src/test/ui/traits/multidispatch-convert-ambig-dest.stderr @@ -0,0 +1,41 @@ +error[E0282]: type annotations needed + --> $DIR/multidispatch-convert-ambig-dest.rs:26:5 + | +LL | test(22, std::default::Default::default()); + | ^^^^ cannot infer type of the type parameter `U` declared on the function `test` + | +help: consider specifying the generic arguments + | +LL | test::(22, std::default::Default::default()); + | ++++++++++ + +error[E0283]: type annotations needed + --> $DIR/multidispatch-convert-ambig-dest.rs:26:5 + | +LL | test(22, std::default::Default::default()); + | ^^^^ cannot infer type of the type parameter `U` declared on the function `test` + | +note: multiple `impl`s satisfying `i32: Convert<_>` found + --> $DIR/multidispatch-convert-ambig-dest.rs:8:1 + | +LL | impl Convert for i32 { + | ^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | impl Convert for i32 { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +note: required by a bound in `test` + --> $DIR/multidispatch-convert-ambig-dest.rs:21:11 + | +LL | fn test(_: T, _: U) + | ---- required by a bound in this +LL | where T : Convert + | ^^^^^^^^^^ required by this bound in `test` +help: consider specifying the type arguments in the function call + | +LL | test::(22, std::default::Default::default()); + | ++++++++ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0282, E0283. +For more information about an error, try `rustc --explain E0282`. diff --git a/src/test/ui/traits/multidispatch-infer-convert-target.rs b/src/test/ui/traits/multidispatch-infer-convert-target.rs new file mode 100644 index 000000000..626e1ae71 --- /dev/null +++ b/src/test/ui/traits/multidispatch-infer-convert-target.rs @@ -0,0 +1,36 @@ +// run-pass +// Test that we can infer the Target based on the Self or vice versa. + + +use std::mem; + +trait Convert { + fn convert(&self) -> Target; +} + +impl Convert for i16 { + fn convert(&self) -> u32 { + *self as u32 + } +} + +impl Convert for u32 { + fn convert(&self) -> i16 { + *self as i16 + } +} + +fn test(_: T, _: U, t_size: usize, u_size: usize) +where T : Convert +{ + assert_eq!(mem::size_of::(), t_size); + assert_eq!(mem::size_of::(), u_size); +} + +fn main() { + // T = i16, U = u32 + test(22_i16, Default::default(), 2, 4); + + // T = u32, U = i16 + test(22_u32, Default::default(), 4, 2); +} diff --git a/src/test/ui/traits/multidispatch1.rs b/src/test/ui/traits/multidispatch1.rs new file mode 100644 index 000000000..f2469e149 --- /dev/null +++ b/src/test/ui/traits/multidispatch1.rs @@ -0,0 +1,33 @@ +// run-pass + +use std::fmt::Debug; + +trait MyTrait { + fn get(&self) -> T; +} + +#[derive(Copy, Clone)] +struct MyType { + dummy: usize +} + +impl MyTrait for MyType { + fn get(&self) -> usize { self.dummy } +} + +impl MyTrait for MyType { + fn get(&self) -> u8 { self.dummy as u8 } +} + +fn test_eq(m: M, v: T) +where T : Eq + Debug, + M : MyTrait +{ + assert_eq!(m.get(), v); +} + +pub fn main() { + let value = MyType { dummy: 256 + 22 }; + test_eq::(value, value.dummy); + test_eq::(value, value.dummy as u8); +} diff --git a/src/test/ui/traits/multidispatch2.rs b/src/test/ui/traits/multidispatch2.rs new file mode 100644 index 000000000..20608aabb --- /dev/null +++ b/src/test/ui/traits/multidispatch2.rs @@ -0,0 +1,39 @@ +// run-pass + +use std::fmt::Debug; +use std::default::Default; + +trait MyTrait { + fn get(&self) -> T; +} + +impl MyTrait for T + where T : Default +{ + fn get(&self) -> T { + Default::default() + } +} + +#[derive(Copy, Clone)] +struct MyType { + dummy: usize +} + +impl MyTrait for MyType { + fn get(&self) -> usize { self.dummy } +} + +fn test_eq(m: M, v: T) +where T : Eq + Debug, + M : MyTrait +{ + assert_eq!(m.get(), v); +} + +pub fn main() { + test_eq(22_usize, 0_usize); + + let value = MyType { dummy: 256 + 22 }; + test_eq(value, value.dummy); +} diff --git a/src/test/ui/traits/mutual-recursion-issue-75860.rs b/src/test/ui/traits/mutual-recursion-issue-75860.rs new file mode 100644 index 000000000..d7d7307b4 --- /dev/null +++ b/src/test/ui/traits/mutual-recursion-issue-75860.rs @@ -0,0 +1,15 @@ +pub fn iso(a: F1, b: F2) -> (Box B>, Box A>) + where + F1: (Fn(A) -> B) + 'static, + F2: (Fn(B) -> A) + 'static, +{ + (Box::new(a), Box::new(b)) +} +pub fn iso_un_option() -> (Box B>, Box A>) { + let left = |o_a: Option<_>| o_a.unwrap(); + let right = |o_b: Option<_>| o_b.unwrap(); + iso(left, right) + //~^ ERROR overflow +} + +fn main() {} diff --git a/src/test/ui/traits/mutual-recursion-issue-75860.stderr b/src/test/ui/traits/mutual-recursion-issue-75860.stderr new file mode 100644 index 000000000..920f66121 --- /dev/null +++ b/src/test/ui/traits/mutual-recursion-issue-75860.stderr @@ -0,0 +1,16 @@ +error[E0275]: overflow evaluating the requirement `Option<_>: Sized` + --> $DIR/mutual-recursion-issue-75860.rs:11:5 + | +LL | iso(left, right) + | ^^^ + | + = help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`mutual_recursion_issue_75860`) +note: required by a bound in `Option` + --> $SRC_DIR/core/src/option.rs:LL:COL + | +LL | pub enum Option { + | ^ required by this bound in `Option` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0275`. diff --git a/src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs b/src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs new file mode 100644 index 000000000..0e17f0890 --- /dev/null +++ b/src/test/ui/traits/negative-impls/auxiliary/foreign_trait.rs @@ -0,0 +1,7 @@ +#![feature(negative_impls)] +#![feature(with_negative_coherence)] + +pub trait ForeignTrait {} + +impl ForeignTrait for u32 {} +impl !ForeignTrait for String {} diff --git a/src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.rs b/src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.rs new file mode 100644 index 000000000..1314f9cb0 --- /dev/null +++ b/src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.rs @@ -0,0 +1,53 @@ +// This tests issue #79683: note in the error message that the trait is +// explicitely unimplemented instead of suggesting to implement it. + +#![feature(negative_impls)] + +struct Qux; +//~^ NOTE method `clone` not found for this +//~^^ NOTE method `foo` not found for this + +impl !Clone for Qux {} + +trait Bar { + fn bar(&self); +} + +impl !Bar for u32 {} + +trait Foo { + fn foo(&self); +} +//~^^^ NOTE `Foo` defines an item `foo`, perhaps you need to implement it + +trait FooBar { + fn foo(&self); +} + +impl !Foo for Qux {} + +impl !FooBar for Qux {} + +impl !FooBar for u32 {} + +fn main() { + Qux.clone(); + //~^ ERROR no method named `clone` found for struct `Qux` + //~| NOTE method not found in `Qux` + //~| NOTE `Clone` defines an item `clone`, but is explicitly unimplemented + + 0_u32.bar(); + //~^ ERROR no method named `bar` found for type `u32` + //~| NOTE method not found in `u32` + //~| NOTE `Bar` defines an item `bar`, but is explicitly unimplemented + + Qux.foo(); + //~^ ERROR no method named `foo` found for struct `Qux` + //~| NOTE method not found in `Qux` + //~| NOTE the following traits define an item `foo`, but are explicitly unimplemented + + 0_u32.foo(); + //~^ ERROR no method named `foo` found for type `u32` + //~| NOTE method not found in `u32` + //~| NOTE `FooBar` defines an item `foo`, but is explicitly unimplemented +} diff --git a/src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.stderr b/src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.stderr new file mode 100644 index 000000000..b29442d7b --- /dev/null +++ b/src/test/ui/traits/negative-impls/explicitly-unimplemented-error-message.stderr @@ -0,0 +1,52 @@ +error[E0599]: no method named `clone` found for struct `Qux` in the current scope + --> $DIR/explicitly-unimplemented-error-message.rs:34:9 + | +LL | struct Qux; + | ---------- method `clone` not found for this struct +... +LL | Qux.clone(); + | ^^^^^ method not found in `Qux` + | + = help: items from traits can only be used if the trait is implemented and in scope + = note: the trait `Clone` defines an item `clone`, but is explicitly unimplemented + +error[E0599]: no method named `bar` found for type `u32` in the current scope + --> $DIR/explicitly-unimplemented-error-message.rs:39:11 + | +LL | 0_u32.bar(); + | ^^^ method not found in `u32` + | + = help: items from traits can only be used if the trait is implemented and in scope + = note: the trait `Bar` defines an item `bar`, but is explicitly unimplemented + +error[E0599]: no method named `foo` found for struct `Qux` in the current scope + --> $DIR/explicitly-unimplemented-error-message.rs:44:9 + | +LL | struct Qux; + | ---------- method `foo` not found for this struct +... +LL | Qux.foo(); + | ^^^ method not found in `Qux` + | + = help: items from traits can only be used if the trait is implemented and in scope + = note: the following traits define an item `foo`, but are explicitly unimplemented: + Foo + FooBar + +error[E0599]: no method named `foo` found for type `u32` in the current scope + --> $DIR/explicitly-unimplemented-error-message.rs:49:11 + | +LL | 0_u32.foo(); + | ^^^ method not found in `u32` + | + = help: items from traits can only be used if the trait is implemented and in scope +note: `Foo` defines an item `foo`, perhaps you need to implement it + --> $DIR/explicitly-unimplemented-error-message.rs:18:1 + | +LL | trait Foo { + | ^^^^^^^^^ + = note: the trait `FooBar` defines an item `foo`, but is explicitly unimplemented + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/traits/negative-impls/feature-gate-negative_impls.rs b/src/test/ui/traits/negative-impls/feature-gate-negative_impls.rs new file mode 100644 index 000000000..683fd6db6 --- /dev/null +++ b/src/test/ui/traits/negative-impls/feature-gate-negative_impls.rs @@ -0,0 +1,3 @@ +trait MyTrait {} +impl !MyTrait for u32 {} //~ ERROR negative trait bounds are not yet fully implemented +fn main() {} diff --git a/src/test/ui/traits/negative-impls/feature-gate-negative_impls.stderr b/src/test/ui/traits/negative-impls/feature-gate-negative_impls.stderr new file mode 100644 index 000000000..b253fbd0d --- /dev/null +++ b/src/test/ui/traits/negative-impls/feature-gate-negative_impls.stderr @@ -0,0 +1,12 @@ +error[E0658]: negative trait bounds are not yet fully implemented; use marker types for now + --> $DIR/feature-gate-negative_impls.rs:2:6 + | +LL | impl !MyTrait for u32 {} + | ^^^^^^^^ + | + = note: see issue #68318 for more information + = help: add `#![feature(negative_impls)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/traits/negative-impls/negated-auto-traits-error.rs b/src/test/ui/traits/negative-impls/negated-auto-traits-error.rs new file mode 100644 index 000000000..4bdad5dc5 --- /dev/null +++ b/src/test/ui/traits/negative-impls/negated-auto-traits-error.rs @@ -0,0 +1,68 @@ +// The dummy functions are used to avoid adding new cfail files. +// What happens is that the compiler attempts to squash duplicates and some +// errors are not reported. This way, we make sure that, for each function, different +// typeck phases are involved and all errors are reported. + +#![feature(negative_impls)] + +use std::marker::Send; + +struct Outer(T); + +struct Outer2(T); + +unsafe impl Sync for Outer2 {} + +fn is_send(_: T) {} +fn is_sync(_: T) {} + +fn dummy() { + struct TestType; + impl !Send for TestType {} + + Outer(TestType); + //~^ ERROR `dummy::TestType` cannot be sent between threads safely + //~| ERROR `dummy::TestType` cannot be sent between threads safely +} + +fn dummy1b() { + struct TestType; + impl !Send for TestType {} + + is_send(TestType); + //~^ ERROR `dummy1b::TestType` cannot be sent between threads safely +} + +fn dummy1c() { + struct TestType; + impl !Send for TestType {} + + is_send((8, TestType)); + //~^ ERROR `dummy1c::TestType` cannot be sent between threads safely +} + +fn dummy2() { + struct TestType; + impl !Send for TestType {} + + is_send(Box::new(TestType)); + //~^ ERROR `dummy2::TestType` cannot be sent between threads safely +} + +fn dummy3() { + struct TestType; + impl !Send for TestType {} + + is_send(Box::new(Outer2(TestType))); + //~^ ERROR `dummy3::TestType` cannot be sent between threads safely +} + +fn main() { + struct TestType; + impl !Send for TestType {} + + // This will complain about a missing Send impl because `Sync` is implement *just* + // for T that are `Send`. Look at #20366 and #19950 + is_sync(Outer2(TestType)); + //~^ ERROR `main::TestType` cannot be sent between threads safely +} diff --git a/src/test/ui/traits/negative-impls/negated-auto-traits-error.stderr b/src/test/ui/traits/negative-impls/negated-auto-traits-error.stderr new file mode 100644 index 000000000..c5d4ccc2f --- /dev/null +++ b/src/test/ui/traits/negative-impls/negated-auto-traits-error.stderr @@ -0,0 +1,125 @@ +error[E0277]: `dummy::TestType` cannot be sent between threads safely + --> $DIR/negated-auto-traits-error.rs:23:11 + | +LL | Outer(TestType); + | ----- ^^^^^^^^ `dummy::TestType` cannot be sent between threads safely + | | + | required by a bound introduced by this call + | + = help: the trait `Send` is not implemented for `dummy::TestType` +note: required by a bound in `Outer` + --> $DIR/negated-auto-traits-error.rs:10:17 + | +LL | struct Outer(T); + | ^^^^ required by this bound in `Outer` + +error[E0277]: `dummy::TestType` cannot be sent between threads safely + --> $DIR/negated-auto-traits-error.rs:23:5 + | +LL | Outer(TestType); + | ^^^^^^^^^^^^^^^ `dummy::TestType` cannot be sent between threads safely + | + = help: the trait `Send` is not implemented for `dummy::TestType` +note: required by a bound in `Outer` + --> $DIR/negated-auto-traits-error.rs:10:17 + | +LL | struct Outer(T); + | ^^^^ required by this bound in `Outer` + +error[E0277]: `dummy1b::TestType` cannot be sent between threads safely + --> $DIR/negated-auto-traits-error.rs:32:13 + | +LL | is_send(TestType); + | ------- ^^^^^^^^ `dummy1b::TestType` cannot be sent between threads safely + | | + | required by a bound introduced by this call + | + = help: the trait `Send` is not implemented for `dummy1b::TestType` +note: required by a bound in `is_send` + --> $DIR/negated-auto-traits-error.rs:16:15 + | +LL | fn is_send(_: T) {} + | ^^^^ required by this bound in `is_send` + +error[E0277]: `dummy1c::TestType` cannot be sent between threads safely + --> $DIR/negated-auto-traits-error.rs:40:13 + | +LL | is_send((8, TestType)); + | ------- ^^^^^^^^^^^^^ `dummy1c::TestType` cannot be sent between threads safely + | | + | required by a bound introduced by this call + | + = help: within `({integer}, dummy1c::TestType)`, the trait `Send` is not implemented for `dummy1c::TestType` + = note: required because it appears within the type `({integer}, dummy1c::TestType)` +note: required by a bound in `is_send` + --> $DIR/negated-auto-traits-error.rs:16:15 + | +LL | fn is_send(_: T) {} + | ^^^^ required by this bound in `is_send` + +error[E0277]: `dummy2::TestType` cannot be sent between threads safely + --> $DIR/negated-auto-traits-error.rs:48:13 + | +LL | is_send(Box::new(TestType)); + | ------- ^^^^^^^^^^^^^^^^^^ expected an implementor of trait `Send` + | | + | required by a bound introduced by this call + | + = note: the trait bound `Unique: Send` is not satisfied + = note: required because of the requirements on the impl of `Send` for `Unique` + = note: required because it appears within the type `Box` +note: required by a bound in `is_send` + --> $DIR/negated-auto-traits-error.rs:16:15 + | +LL | fn is_send(_: T) {} + | ^^^^ required by this bound in `is_send` +help: consider borrowing here + | +LL | is_send(&Box::new(TestType)); + | + + +error[E0277]: `dummy3::TestType` cannot be sent between threads safely + --> $DIR/negated-auto-traits-error.rs:56:13 + | +LL | is_send(Box::new(Outer2(TestType))); + | ------- ^^^^^^^^^^^^^^^^^^^^^^^^^^ `dummy3::TestType` cannot be sent between threads safely + | | + | required by a bound introduced by this call + | + = help: within `Outer2`, the trait `Send` is not implemented for `dummy3::TestType` +note: required because it appears within the type `Outer2` + --> $DIR/negated-auto-traits-error.rs:12:8 + | +LL | struct Outer2(T); + | ^^^^^^ + = note: required because of the requirements on the impl of `Send` for `Unique>` + = note: required because it appears within the type `Box>` +note: required by a bound in `is_send` + --> $DIR/negated-auto-traits-error.rs:16:15 + | +LL | fn is_send(_: T) {} + | ^^^^ required by this bound in `is_send` + +error[E0277]: `main::TestType` cannot be sent between threads safely + --> $DIR/negated-auto-traits-error.rs:66:13 + | +LL | is_sync(Outer2(TestType)); + | ------- ^^^^^^^^^^^^^^^^ `main::TestType` cannot be sent between threads safely + | | + | required by a bound introduced by this call + | + = help: the trait `Send` is not implemented for `main::TestType` +note: required because of the requirements on the impl of `Sync` for `Outer2` + --> $DIR/negated-auto-traits-error.rs:14:22 + | +LL | unsafe impl Sync for Outer2 {} + | ^^^^ ^^^^^^^^^ +note: required by a bound in `is_sync` + --> $DIR/negated-auto-traits-error.rs:17:15 + | +LL | fn is_sync(_: T) {} + | ^^^^ required by this bound in `is_sync` + +error: aborting due to 7 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/negative-impls/negated-auto-traits-rpass.rs b/src/test/ui/traits/negative-impls/negated-auto-traits-rpass.rs new file mode 100644 index 000000000..a1042f831 --- /dev/null +++ b/src/test/ui/traits/negative-impls/negated-auto-traits-rpass.rs @@ -0,0 +1,21 @@ +// run-pass +#![allow(unused_variables)] +#![feature(negative_impls)] + +use std::marker::Send; + +pub struct WaitToken; +impl !Send for WaitToken {} + +pub struct Test(#[allow(unused_tuple_struct_fields)] T); +unsafe impl Send for Test {} + +pub fn spawn(_: F) -> () where F: FnOnce(), F: Send + 'static {} + +fn main() { + let wt = Test(WaitToken); + spawn(move || { + let x = wt; + println!("Hello, World!"); + }); +} diff --git a/src/test/ui/traits/negative-impls/negative-default-impls.rs b/src/test/ui/traits/negative-impls/negative-default-impls.rs new file mode 100644 index 000000000..c68bca432 --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-default-impls.rs @@ -0,0 +1,11 @@ +#![feature(negative_impls)] +#![feature(specialization)] +//~^ WARN the feature `specialization` is incomplete + +trait MyTrait { + type Foo; +} + +default impl !MyTrait for u32 {} //~ ERROR negative impls cannot be default impls + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/negative-default-impls.stderr b/src/test/ui/traits/negative-impls/negative-default-impls.stderr new file mode 100644 index 000000000..ceb86559d --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-default-impls.stderr @@ -0,0 +1,19 @@ +warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/negative-default-impls.rs:2:12 + | +LL | #![feature(specialization)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #31844 for more information + = help: consider using `min_specialization` instead, which is more stable and complete + +error[E0750]: negative impls cannot be default impls + --> $DIR/negative-default-impls.rs:9:1 + | +LL | default impl !MyTrait for u32 {} + | ^^^^^^^ ^ + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0750`. diff --git a/src/test/ui/traits/negative-impls/negative-impls-basic.rs b/src/test/ui/traits/negative-impls/negative-impls-basic.rs new file mode 100644 index 000000000..474e03817 --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-impls-basic.rs @@ -0,0 +1,17 @@ +// A simple test that we are able to create negative impls, when the +// feature gate is given. +// +// run-pass + +#![feature(negative_impls)] +#![allow(dead_code)] + +struct TestType; + +trait TestTrait { + fn dummy(&self) {} +} + +impl !TestTrait for TestType {} + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/negative-specializes-negative.rs b/src/test/ui/traits/negative-impls/negative-specializes-negative.rs new file mode 100644 index 000000000..35297ab12 --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-specializes-negative.rs @@ -0,0 +1,13 @@ +#![feature(specialization)] //~ WARN the feature `specialization` is incomplete +#![feature(negative_impls)] + +// Test a negative impl that "specializes" another negative impl. +// +// run-pass + +trait MyTrait {} + +impl !MyTrait for T {} +impl !MyTrait for u32 {} + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/negative-specializes-negative.stderr b/src/test/ui/traits/negative-impls/negative-specializes-negative.stderr new file mode 100644 index 000000000..9a846143d --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-specializes-negative.stderr @@ -0,0 +1,12 @@ +warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/negative-specializes-negative.rs:1:12 + | +LL | #![feature(specialization)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #31844 for more information + = help: consider using `min_specialization` instead, which is more stable and complete + +warning: 1 warning emitted + diff --git a/src/test/ui/traits/negative-impls/negative-specializes-positive-item.rs b/src/test/ui/traits/negative-impls/negative-specializes-positive-item.rs new file mode 100644 index 000000000..4281eedaf --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-specializes-positive-item.rs @@ -0,0 +1,13 @@ +#![feature(specialization)] //~ WARN the feature `specialization` is incomplete +#![feature(negative_impls)] + +// Negative impl for u32 cannot "specialize" the base impl. +trait MyTrait { + fn foo(); +} +impl MyTrait for T { + default fn foo() {} +} +impl !MyTrait for u32 {} //~ ERROR E0751 + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/negative-specializes-positive-item.stderr b/src/test/ui/traits/negative-impls/negative-specializes-positive-item.stderr new file mode 100644 index 000000000..77b4373a2 --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-specializes-positive-item.stderr @@ -0,0 +1,22 @@ +warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/negative-specializes-positive-item.rs:1:12 + | +LL | #![feature(specialization)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #31844 for more information + = help: consider using `min_specialization` instead, which is more stable and complete + +error[E0751]: found both positive and negative implementation of trait `MyTrait` for type `u32`: + --> $DIR/negative-specializes-positive-item.rs:11:1 + | +LL | impl MyTrait for T { + | --------------------- positive implementation here +... +LL | impl !MyTrait for u32 {} + | ^^^^^^^^^^^^^^^^^^^^^ negative implementation here + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0751`. diff --git a/src/test/ui/traits/negative-impls/negative-specializes-positive.rs b/src/test/ui/traits/negative-impls/negative-specializes-positive.rs new file mode 100644 index 000000000..0e227691e --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-specializes-positive.rs @@ -0,0 +1,14 @@ +#![feature(specialization)] //~ WARN the feature `specialization` is incomplete +#![feature(negative_impls)] + +// Negative impl for u32 cannot "specialize" the base impl. +trait MyTrait {} +impl MyTrait for T {} +impl !MyTrait for u32 {} //~ ERROR E0751 + +// The second impl specializes the first, no error. +trait MyTrait2 {} +impl MyTrait2 for T {} +impl MyTrait2 for u32 {} + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/negative-specializes-positive.stderr b/src/test/ui/traits/negative-impls/negative-specializes-positive.stderr new file mode 100644 index 000000000..e5dc81b3e --- /dev/null +++ b/src/test/ui/traits/negative-impls/negative-specializes-positive.stderr @@ -0,0 +1,21 @@ +warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/negative-specializes-positive.rs:1:12 + | +LL | #![feature(specialization)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #31844 for more information + = help: consider using `min_specialization` instead, which is more stable and complete + +error[E0751]: found both positive and negative implementation of trait `MyTrait` for type `u32`: + --> $DIR/negative-specializes-positive.rs:7:1 + | +LL | impl MyTrait for T {} + | --------------------- positive implementation here +LL | impl !MyTrait for u32 {} + | ^^^^^^^^^^^^^^^^^^^^^ negative implementation here + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0751`. diff --git a/src/test/ui/traits/negative-impls/no-items.rs b/src/test/ui/traits/negative-impls/no-items.rs new file mode 100644 index 000000000..5fc6be9b3 --- /dev/null +++ b/src/test/ui/traits/negative-impls/no-items.rs @@ -0,0 +1,11 @@ +#![feature(negative_impls)] + +trait MyTrait { + type Foo; +} + +impl !MyTrait for u32 { + type Foo = i32; //~ ERROR negative impls cannot have any items +} + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/no-items.stderr b/src/test/ui/traits/negative-impls/no-items.stderr new file mode 100644 index 000000000..67b94bba1 --- /dev/null +++ b/src/test/ui/traits/negative-impls/no-items.stderr @@ -0,0 +1,9 @@ +error[E0749]: negative impls cannot have any items + --> $DIR/no-items.rs:8:5 + | +LL | type Foo = i32; + | ^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0749`. diff --git a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs new file mode 100644 index 000000000..a5b856465 --- /dev/null +++ b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.rs @@ -0,0 +1,26 @@ +use std::cell::Cell; +use std::marker::PhantomPinned; +use std::pin::Pin; + +struct MyType<'a>(Cell>>, PhantomPinned); + +impl<'a> Clone for &'a mut MyType<'a> { + //~^ ERROR E0751 + fn clone(&self) -> &'a mut MyType<'a> { + self.0.take().unwrap() + } +} + +fn main() { + let mut unpinned = MyType(Cell::new(None), PhantomPinned); + let bad_addr = &unpinned as *const MyType<'_> as usize; + let mut p = Box::pin(MyType(Cell::new(Some(&mut unpinned)), PhantomPinned)); + + // p_mut1 is okay: it does not point to the bad_addr + let p_mut1: Pin<&mut MyType<'_>> = p.as_mut(); + assert_ne!(bad_addr, &*p_mut1 as *const _ as usize); + + // but p_mut2 does point to bad_addr! this is unsound + let p_mut2: Pin<&mut MyType<'_>> = p_mut1.clone(); + assert_eq!(bad_addr, &*p_mut2 as *const _ as usize); +} diff --git a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.stderr b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.stderr new file mode 100644 index 000000000..d7039e3db --- /dev/null +++ b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-clone.stderr @@ -0,0 +1,11 @@ +error[E0751]: found both positive and negative implementation of trait `std::clone::Clone` for type `&mut MyType<'_>`: + --> $DIR/pin-unsound-issue-66544-clone.rs:7:1 + | +LL | impl<'a> Clone for &'a mut MyType<'a> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ positive implementation here + | + = note: negative implementation in crate `core` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0751`. diff --git a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs new file mode 100644 index 000000000..606cc65a8 --- /dev/null +++ b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.rs @@ -0,0 +1,33 @@ +// Demonstrate that "rogue" `DerefMut` impls for `&T` are not allowed. +// +// https://github.com/rust-lang/rust/issues/66544 + +use std::cell::Cell; +use std::marker::PhantomPinned; +use std::ops::DerefMut; +use std::pin::Pin; + +struct MyType<'a>(Cell>>, PhantomPinned); + +impl<'a> DerefMut for &'a MyType<'a> { + //~^ ERROR E0751 + fn deref_mut(&mut self) -> &mut MyType<'a> { + self.0.take().unwrap() + } +} + +fn main() { + let mut unpinned = MyType(Cell::new(None), PhantomPinned); + let bad_addr = &unpinned as *const MyType<'_> as usize; + let p = Box::pin(MyType(Cell::new(Some(&mut unpinned)), PhantomPinned)); + + // p_ref is okay: it does not point to the bad_addr + let mut p_ref: Pin<&MyType<'_>> = p.as_ref(); + assert_ne!(bad_addr, &*p_ref as *const _ as usize); + + // but p_mut does point to bad_addr! this is unsound + let p_mut: Pin<&mut MyType<'_>> = p_ref.as_mut(); + assert_eq!(bad_addr, &*p_mut as *const _ as usize); + + println!("oh no!"); +} diff --git a/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.stderr b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.stderr new file mode 100644 index 000000000..a0b62a8ba --- /dev/null +++ b/src/test/ui/traits/negative-impls/pin-unsound-issue-66544-derefmut.stderr @@ -0,0 +1,11 @@ +error[E0751]: found both positive and negative implementation of trait `std::ops::DerefMut` for type `&MyType<'_>`: + --> $DIR/pin-unsound-issue-66544-derefmut.rs:12:1 + | +LL | impl<'a> DerefMut for &'a MyType<'a> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ positive implementation here + | + = note: negative implementation in crate `core` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0751`. diff --git a/src/test/ui/traits/negative-impls/positive-specializes-negative.rs b/src/test/ui/traits/negative-impls/positive-specializes-negative.rs new file mode 100644 index 000000000..a06b35765 --- /dev/null +++ b/src/test/ui/traits/negative-impls/positive-specializes-negative.rs @@ -0,0 +1,9 @@ +#![feature(specialization)] //~ WARN the feature `specialization` is incomplete +#![feature(negative_impls)] + +trait MyTrait {} + +impl !MyTrait for T {} +impl MyTrait for u32 {} //~ ERROR E0751 + +fn main() {} diff --git a/src/test/ui/traits/negative-impls/positive-specializes-negative.stderr b/src/test/ui/traits/negative-impls/positive-specializes-negative.stderr new file mode 100644 index 000000000..c091bc81d --- /dev/null +++ b/src/test/ui/traits/negative-impls/positive-specializes-negative.stderr @@ -0,0 +1,21 @@ +warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/positive-specializes-negative.rs:1:12 + | +LL | #![feature(specialization)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + = note: see issue #31844 for more information + = help: consider using `min_specialization` instead, which is more stable and complete + +error[E0751]: found both positive and negative implementation of trait `MyTrait` for type `u32`: + --> $DIR/positive-specializes-negative.rs:7:1 + | +LL | impl !MyTrait for T {} + | ---------------------- negative implementation here +LL | impl MyTrait for u32 {} + | ^^^^^^^^^^^^^^^^^^^^ positive implementation here + +error: aborting due to previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0751`. diff --git a/src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs b/src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs new file mode 100644 index 000000000..c1f96ab8c --- /dev/null +++ b/src/test/ui/traits/negative-impls/rely-on-negative-impl-in-coherence.rs @@ -0,0 +1,24 @@ +// check-pass + +#![feature(negative_impls)] +#![feature(with_negative_coherence)] + +// aux-build: foreign_trait.rs + +// Test that we cannot implement `LocalTrait` for `String`, +// even though there is a `String: !ForeignTrait` impl. +// +// This may not be the behavior we want long term, but it's the +// current semantics that we implemented so as to land `!Foo` impls +// quickly. See internals thread: +// +// https://internals.rust-lang.org/t/foo/11587/ + +extern crate foreign_trait; +use foreign_trait::ForeignTrait; + +trait LocalTrait {} +impl LocalTrait for T {} +impl LocalTrait for String {} + +fn main() {} diff --git a/src/test/ui/traits/no-fallback-multiple-impls.rs b/src/test/ui/traits/no-fallback-multiple-impls.rs new file mode 100644 index 000000000..7ed3796f0 --- /dev/null +++ b/src/test/ui/traits/no-fallback-multiple-impls.rs @@ -0,0 +1,16 @@ +trait Fallback { + fn foo(&self) {} +} + +impl Fallback for i32 {} + +impl Fallback for u64 {} + +impl Fallback for usize {} + +fn main() { + missing(); + //~^ ERROR cannot find function `missing` in this scope + 0.foo(); + // But then we shouldn't report an inference ambiguity here... +} diff --git a/src/test/ui/traits/no-fallback-multiple-impls.stderr b/src/test/ui/traits/no-fallback-multiple-impls.stderr new file mode 100644 index 000000000..61c9e5aaa --- /dev/null +++ b/src/test/ui/traits/no-fallback-multiple-impls.stderr @@ -0,0 +1,9 @@ +error[E0425]: cannot find function `missing` in this scope + --> $DIR/no-fallback-multiple-impls.rs:12:5 + | +LL | missing(); + | ^^^^^^^ not found in this scope + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0425`. diff --git a/src/test/ui/traits/no_send-struct.rs b/src/test/ui/traits/no_send-struct.rs new file mode 100644 index 000000000..75a363f9f --- /dev/null +++ b/src/test/ui/traits/no_send-struct.rs @@ -0,0 +1,17 @@ +#![feature(negative_impls)] + +use std::marker::Send; + +struct Foo { + a: isize, +} + +impl !Send for Foo {} + +fn bar(_: T) {} + +fn main() { + let x = Foo { a: 5 }; + bar(x); + //~^ ERROR `Foo` cannot be sent between threads safely +} diff --git a/src/test/ui/traits/no_send-struct.stderr b/src/test/ui/traits/no_send-struct.stderr new file mode 100644 index 000000000..ee7bdf282 --- /dev/null +++ b/src/test/ui/traits/no_send-struct.stderr @@ -0,0 +1,18 @@ +error[E0277]: `Foo` cannot be sent between threads safely + --> $DIR/no_send-struct.rs:15:9 + | +LL | bar(x); + | --- ^ `Foo` cannot be sent between threads safely + | | + | required by a bound introduced by this call + | + = help: the trait `Send` is not implemented for `Foo` +note: required by a bound in `bar` + --> $DIR/no_send-struct.rs:11:11 + | +LL | fn bar(_: T) {} + | ^^^^ required by this bound in `bar` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/traits/normalize-supertrait.rs b/src/test/ui/traits/normalize-supertrait.rs new file mode 100644 index 000000000..021a93eac --- /dev/null +++ b/src/test/ui/traits/normalize-supertrait.rs @@ -0,0 +1,37 @@ +// Regression test for #77653 +// When monomorphizing `f` we need to prove `dyn Derived<()>: Base<()>`. This +// requires us to normalize the `Base<<() as Proj>::S>` to `Base<()>` when +// comparing the supertrait `Derived<()>` to the expected trait. + +// build-pass + +trait Proj { + type S; +} + +impl Proj for () { + type S = (); +} + +impl Proj for i32 { + type S = i32; +} + +trait Base { + fn is_base(&self); +} + +trait Derived: Base + Base<()> { + fn is_derived(&self); +} + +fn f(obj: &dyn Derived