1// Copyright 2013 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/values.h" 9 10#include <stdint.h> 11 12namespace base { 13 14// Trying to construct a Value from a pointer should not work or implicitly 15// convert to bool. 16void DisallowValueConstructionFromPointers() { 17 int* ptr = nullptr; 18 19 { 20 Value v(ptr); // expected-error {{call to deleted constructor of 'Value'}} 21 } 22 23 { 24 Value::Dict dict; 25 dict.Set("moo", ptr); // expected-error {{call to deleted member function 'Set'}} 26 dict.SetByDottedPath("moo.moo", ptr); // expected-error {{call to deleted member function 'SetByDottedPath'}} 27 28 Value::Dict().Set("moo", ptr); // expected-error {{call to deleted member function 'Set'}} 29 Value::Dict().SetByDottedPath("moo", ptr); // expected-error {{call to deleted member function 'SetByDottedPath'}} 30 } 31 32 { 33 Value::List list; 34 list.Append(ptr); // expected-error {{call to deleted member function 'Append'}} 35 36 Value::List().Append(ptr); // expected-error {{call to deleted member function 'Append'}} 37 } 38} 39 40// Value (largely) follows the semantics of JSON, which does not support 64-bit 41// integers. Constructing a Value from a 64-bit integer should not work. 42 43void DisallowValueConstructionFromInt64() { 44 int64_t big_int = 0; 45 46 { 47 Value v(big_int); // expected-error {{call to constructor of 'Value' is ambiguous}} 48 } 49 50 { 51 Value::Dict dict; 52 dict.Set("あいうえお", big_int); // expected-error {{call to member function 'Set' is ambiguous}} 53 dict.SetByDottedPath("あいうえお", big_int); // expected-error {{call to member function 'SetByDottedPath' is ambiguous}} 54 55 Value::Dict().Set("あいうえお", big_int); // expected-error {{call to member function 'Set' is ambiguous}} 56 Value::Dict().SetByDottedPath("あいうえお", big_int); // expected-error {{call to member function 'SetByDottedPath' is ambiguous}} 57 } 58 59 { 60 Value::List list; 61 list.Append(big_int); // expected-error {{call to member function 'Append' is ambiguous}} 62 63 Value::List().Append(big_int); // expected-error {{call to member function 'Append' is ambiguous}} 64 } 65} 66 67void TakesValueView(ValueView v) {} 68 69// Trying to construct a ValueView from a pointer should not work or implicitly 70// convert to bool. 71void DisallowValueViewConstructionFromPointers() { 72 int* ptr = nullptr; 73 74 ValueView v(ptr); // expected-error {{call to deleted constructor of 'ValueView'}} 75 TakesValueView(ptr); // expected-error {{conversion function from 'int *' to 'ValueView' invokes a deleted function}} 76} 77 78// Verify that obvious ways of unsafely constructing a ValueView from a 79// temporary are disallowed. 80void DisallowValueViewConstructionFromTemporaryString() { 81 [[maybe_unused]] ValueView v = std::string(); // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 82 // Not an error here since the lifetime of the temporary lasts until the end 83 // of the full expression, i.e. until TakesValueView() returns. 84 TakesValueView(std::string()); 85} 86 87void DisallowValueViewConstructionFromTemporaryBlob() { 88 [[maybe_unused]] ValueView v = Value::BlobStorage(); // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 89 // Not an error here since the lifetime of the temporary lasts until the end 90 // of the full expression, i.e. until TakesValueView() returns. 91 TakesValueView(Value::BlobStorage()); 92} 93 94void DisallowValueViewConstructionFromTemporaryDict() { 95 [[maybe_unused]] ValueView v = Value::Dict(); // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 96 // Not an error here since the lifetime of the temporary lasts until the end 97 // of the full expression, i.e. until TakesValueView() returns. 98 TakesValueView(Value::Dict()); 99} 100 101void DisallowValueViewConstructionFromTemporaryList() { 102 [[maybe_unused]] ValueView v = Value::List(); // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 103 // Not an error here since the lifetime of the temporary lasts until the end 104 // of the full expression, i.e. until TakesValueView() returns. 105 TakesValueView(Value::List()); 106} 107 108void DisallowValueViewConstructionFromTemporaryValue() { 109 [[maybe_unused]] ValueView v = Value(); // expected-error {{object backing the pointer will be destroyed at the end of the full-expression}} 110 // Not an error here since the lifetime of the temporary lasts until the end 111 // of the full expression, i.e. until TakesValueView() returns. 112 TakesValueView(Value()); 113} 114 115} // namespace base 116