1// Copyright 2011 The Chromium Authors 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// This is a "No Compile Test" suite. 6// http://dev.chromium.org/developers/testing/no-compile-tests 7 8#include "base/functional/callback.h" 9 10namespace base { 11 12class Parent { 13}; 14 15class Child : Parent { 16}; 17 18#if defined(NCTEST_EQUALS_REQUIRES_SAMETYPE) // [r"invalid operands to binary expression \('RepeatingCallback<void \(\)>' and 'RepeatingCallback<int \(\)>'\)"] 19 20// Attempting to call comparison function on two callbacks of different type. 21// 22// This should be a compile time failure because each callback type should be 23// considered distinct. 24void WontCompile() { 25 RepeatingCallback<void()> c1; 26 RepeatingCallback<int()> c2; 27 c1 == c2; 28} 29 30#elif defined(NCTEST_CONSTRUCTION_FROM_SUBTYPE) // [r"no viable conversion from 'RepeatingCallback<Parent \(\)>' to 'RepeatingCallback<Child \(\)>'"] 31 32// Construction of RepeatingCallback<A> from RepeatingCallback<B> if A is 33// supertype of B. 34// 35// While this is technically safe, most people aren't used to it when coding 36// C++ so if this is happening, it is almost certainly an error. 37void WontCompile() { 38 RepeatingCallback<Parent()> cb_a; 39 RepeatingCallback<Child()> cb_b = cb_a; 40} 41 42#elif defined(NCTEST_ASSIGNMENT_FROM_SUBTYPE) // [r"fatal error: no viable overloaded '='"] 43 44// Assignment of RepeatingCallback<A> from RepeatingCallback<B> if A is 45// supertype of B. See explanation for NCTEST_CONSTRUCTION_FROM_SUBTYPE. 46void WontCompile() { 47 RepeatingCallback<Parent()> cb_a; 48 RepeatingCallback<Child()> cb_b; 49 cb_a = cb_b; 50} 51 52#elif defined(NCTEST_ONCE_THEN_MISMATCH) // [r"\|then\| callback's parameter must be constructible from return type of \|this\|\."] 53 54// Calling Then() with a callback that can't receive the original 55// callback's return type. Here we would pass `int*` to `float*`. 56void WontCompile() { 57 OnceCallback<int*()> original; 58 OnceCallback<void(float*)> then; 59 std::move(original).Then(std::move(then)); 60} 61 62#elif defined(NCTEST_ONCE_THEN_MISMATCH_VOID_RESULT) // [r"\|then\| callback cannot accept parameters if \|this\| has a void return type\."] 63 64// Calling Then() with a callback that can't receive the original 65// callback's return type. Here we would pass `void` to `float`. 66void WontCompile() { 67 OnceCallback<void()> original; 68 OnceCallback<void(float)> then; 69 std::move(original).Then(std::move(then)); 70} 71 72#elif defined(NCTEST_ONCE_THEN_MISMATCH_VOID_PARAM) // [r"\|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."] 73 74// Calling Then() with a callback that can't receive the original 75// callback's return type. Here we would pass `int` to `void`. 76void WontCompile() { 77 OnceCallback<int()> original; 78 OnceCallback<void()> then; 79 std::move(original).Then(std::move(then)); 80} 81 82#elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH) // [r"\|then\| callback's parameter must be constructible from return type of \|this\|\."] 83 84// Calling Then() with a callback that can't receive the original 85// callback's return type. Here we would pass `int*` to `float*`. 86void WontCompile() { 87 RepeatingCallback<int*()> original; 88 RepeatingCallback<void(float*)> then; 89 std::move(original).Then(std::move(then)); 90} 91 92#elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH_VOID_RESULT) // [r"\|then\| callback cannot accept parameters if \|this\| has a void return type\."] 93 94// Calling Then() with a callback that can't receive the original 95// callback's return type. Here we would pass `void` to `float`. 96void WontCompile() { 97 RepeatingCallback<void()> original; 98 RepeatingCallback<void(float)> then; 99 std::move(original).Then(std::move(then)); 100} 101 102#elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH_VOID_PARAM) // [r"\|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."] 103 104// Calling Then() with a callback that can't receive the original 105// callback's return type. Here we would pass `int` to `void`. 106void WontCompile() { 107 RepeatingCallback<int()> original; 108 RepeatingCallback<void()> then; 109 std::move(original).Then(std::move(then)); 110} 111 112#elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH) // [r"\|then\| callback's parameter must be constructible from return type of \|this\|\."] 113 114// Calling Then() with a callback that can't receive the original 115// callback's return type. Here we would pass `int*` to `float*`. 116void WontCompile() { 117 RepeatingCallback<int*()> original; 118 RepeatingCallback<void(float*)> then; 119 original.Then(then); 120} 121 122#elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH_VOID_RESULT) // [r"\|then\| callback cannot accept parameters if \|this\| has a void return type\."] 123 124// Calling Then() with a callback that can't receive the original 125// callback's return type. Here we would pass `void` to `float`. 126void WontCompile() { 127 RepeatingCallback<void()> original; 128 RepeatingCallback<void(float)> then; 129 original.Then(then); 130} 131 132#elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH_VOID_PARAM) // [r"\|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."] 133 134// Calling Then() with a callback that can't receive the original 135// callback's return type. Here we would pass `int` to `void`. 136void WontCompile() { 137 RepeatingCallback<int()> original; 138 RepeatingCallback<void()> then; 139 original.Then(then); 140} 141 142#endif 143 144} // namespace base 145