| 1 |
// EXTRA_SOURCES: imports/ufcs5a.d imports/ufcs5b.d imports/ufcs5c.d imports/ufcs5d.d imports/ufcs5e.d |
| 2 |
// UNICODE_NAMES: |
| 3 |
|
| 4 |
module ufcs; |
| 5 |
|
| 6 |
extern (C) int printf(const char*, ...); |
| 7 |
|
| 8 |
/*******************************************/ |
| 9 |
|
| 10 |
struct S {} |
| 11 |
|
| 12 |
int foo(int n) { return 1; } |
| 13 |
int foo(int n, int m) { return 2; } |
| 14 |
int goo(int[] a) { return 1; } |
| 15 |
int goo(int[] a, int m) { return 2; } |
| 16 |
int bar(S s) { return 1; } |
| 17 |
int bar(S s, int n) { return 2; } |
| 18 |
|
| 19 |
int baz(X)(X x) { return 1; } |
| 20 |
int baz(X)(X x, int n) { return 2; } |
| 21 |
|
| 22 |
int temp; |
| 23 |
ref int boo(int n) { return temp; } |
| 24 |
ref int coo(int[] a) { return temp; } |
| 25 |
ref int mar(S s) { return temp; } |
| 26 |
|
| 27 |
ref int maz(X)(X x) { return temp; } |
| 28 |
|
| 29 |
void test1() |
| 30 |
{ |
| 31 |
int n; |
| 32 |
int[] a; |
| 33 |
S s; |
| 34 |
|
| 35 |
assert( foo(4) == 1); assert( baz(4) == 1); |
| 36 |
assert( 4.foo() == 1); assert( 4.baz() == 1); |
| 37 |
assert( 4.foo == 1); assert( 4.baz == 1); |
| 38 |
assert( foo(4, 2) == 2); assert( baz(4, 2) == 2); |
| 39 |
assert( 4.foo(2) == 2); assert( 4.baz(2) == 2); |
| 40 |
assert((4.foo = 2) == 2); assert((4.baz = 2) == 2); |
| 41 |
|
| 42 |
assert( goo(a) == 1); assert( baz(a) == 1); |
| 43 |
assert( a.goo() == 1); assert( a.baz() == 1); |
| 44 |
assert( a.goo == 1); assert( a.baz == 1); |
| 45 |
assert( goo(a, 2) == 2); assert( baz(a, 2) == 2); |
| 46 |
assert( a.goo(2) == 2); assert( a.baz(2) == 2); |
| 47 |
assert((a.goo = 2) == 2); assert((a.baz = 2) == 2); |
| 48 |
|
| 49 |
assert( bar(s) == 1); assert( baz(s) == 1); |
| 50 |
assert( s.bar() == 1); assert( s.baz() == 1); |
| 51 |
assert( s.bar == 1); assert( s.baz == 1); |
| 52 |
assert( bar(s, 2) == 2); assert( baz(s, 2) == 2); |
| 53 |
assert( s.bar(2) == 2); assert( s.baz(2) == 2); |
| 54 |
assert((s.bar = 2) == 2); assert((s.baz = 2) == 2); |
| 55 |
|
| 56 |
assert(( boo(4) = 2) == 2); assert(( maz(4) = 2) == 2); |
| 57 |
assert((4.boo = 2) == 2); assert((4.maz = 2) == 2); |
| 58 |
assert(( coo(a) = 2) == 2); assert(( maz(a) = 2) == 2); |
| 59 |
assert((a.coo = 2) == 2); assert((a.maz = 2) == 2); |
| 60 |
assert(( mar(s) = 2) == 2); assert(( maz(s) = 2) == 2); |
| 61 |
assert((s.mar = 2) == 2); assert((s.maz = 2) == 2); |
| 62 |
} |
| 63 |
|
| 64 |
int hoo(T)(int n) { return 1; } |
| 65 |
int hoo(T)(int n, int m) { return 2; } |
| 66 |
int koo(T)(int[] a) { return 1; } |
| 67 |
int koo(T)(int[] a, int m) { return 2; } |
| 68 |
int var(T)(S s) { return 1; } |
| 69 |
int var(T)(S s, int n) { return 2; } |
| 70 |
|
| 71 |
int vaz(T, X)(X x) { return 1; } |
| 72 |
int vaz(T, X)(X x, int n) { return 2; } |
| 73 |
|
| 74 |
//int temp; |
| 75 |
ref int voo(T)(int n) { return temp; } |
| 76 |
ref int woo(T)(int[] a) { return temp; } |
| 77 |
ref int nar(T)(S s) { return temp; } |
| 78 |
|
| 79 |
ref int naz(T, X)(X x) { return temp; } |
| 80 |
|
| 81 |
void test2() |
| 82 |
{ |
| 83 |
int n; |
| 84 |
int[] a; |
| 85 |
S s; |
| 86 |
|
| 87 |
assert( hoo!int(4) == 1); assert( vaz!int(4) == 1); |
| 88 |
assert( 4.hoo!int() == 1); assert( 4.vaz!int() == 1); |
| 89 |
assert( 4.hoo!int == 1); assert( 4.vaz!int == 1); |
| 90 |
assert( hoo!int(4, 2) == 2); assert( vaz!int(4, 2) == 2); |
| 91 |
assert( 4.hoo!int(2) == 2); assert( 4.vaz!int(2) == 2); |
| 92 |
assert((4.hoo!int = 2) == 2); assert((4.vaz!int = 2) == 2); |
| 93 |
|
| 94 |
assert( koo!int(a) == 1); assert( vaz!int(a) == 1); |
| 95 |
assert( a.koo!int() == 1); assert( a.vaz!int() == 1); |
| 96 |
assert( a.koo!int == 1); assert( a.vaz!int == 1); |
| 97 |
assert( koo!int(a, 2) == 2); assert( vaz!int(a, 2) == 2); |
| 98 |
assert( a.koo!int(2) == 2); assert( a.vaz!int(2) == 2); |
| 99 |
assert((a.koo!int = 2) == 2); assert((a.vaz!int = 2) == 2); |
| 100 |
|
| 101 |
assert( var!int(s) == 1); assert( vaz!int(s) == 1); |
| 102 |
assert( s.var!int() == 1); assert( s.vaz!int() == 1); |
| 103 |
assert( s.var!int == 1); assert( s.vaz!int == 1); |
| 104 |
assert( var!int(s, 2) == 2); assert( vaz!int(s, 2) == 2); |
| 105 |
assert( s.var!int(2) == 2); assert( s.vaz!int(2) == 2); |
| 106 |
assert((s.var!int = 2) == 2); assert((s.vaz!int = 2) == 2); |
| 107 |
|
| 108 |
assert(( voo!int(4) = 2) == 2); assert(( naz!int(4) = 2) == 2); |
| 109 |
assert((4.voo!int = 2) == 2); assert((4.naz!int = 2) == 2); |
| 110 |
assert(( woo!int(a) = 2) == 2); assert(( naz!int(a) = 2) == 2); |
| 111 |
assert((a.woo!int = 2) == 2); assert((a.naz!int = 2) == 2); |
| 112 |
assert(( nar!int(s) = 2) == 2); assert(( naz!int(s) = 2) == 2); |
| 113 |
assert((s.nar!int = 2) == 2); assert((s.naz!int = 2) == 2); |
| 114 |
} |
| 115 |
|
| 116 |
/*******************************************/ |
| 117 |
|
| 118 |
auto init(T)(T val) { return 1; } |
| 119 |
|
| 120 |
auto sort(alias fun, T)(T val) { return 1; } |
| 121 |
|
| 122 |
@property auto max(alias fun, T)(T val) { return 1; } |
| 123 |
|
| 124 |
@property auto infinity(alias opt, T)(T val) { return 1; } |
| 125 |
|
| 126 |
void test3() |
| 127 |
{ |
| 128 |
// See built-in 'init' property |
| 129 |
assert(1 .init == 0); |
| 130 |
assert([1] .init == null); |
| 131 |
assert([1:1].init == null); |
| 132 |
assert(1.0 .init is double.nan); |
| 133 |
assert('c' .init == 0xFF); |
| 134 |
assert("s" .init == null); |
| 135 |
|
| 136 |
// x.init() has parens, so it runs UFCS call |
| 137 |
assert( 1 .init() == 1); |
| 138 |
assert([1] .init() == 1); |
| 139 |
assert([1:1].init() == 1); |
| 140 |
assert(1.0 .init() == 1); |
| 141 |
assert('c' .init() == 1); |
| 142 |
assert("s" .init() == 1); |
| 143 |
|
| 144 |
// x.init!YYY matches templatized UFCS call. |
| 145 |
assert( 1 .init!int() == 1); |
| 146 |
assert([1] .init!(int[])() == 1); |
| 147 |
assert([1:1].init!(int[int])() == 1); |
| 148 |
assert(1.0 .init!double() == 1); |
| 149 |
assert('c' .init!char() == 1); |
| 150 |
assert("s" .init!string() == 1); |
| 151 |
|
| 152 |
assert([1].sort!"a<b"() == 1); |
| 153 |
|
| 154 |
// templatized properties runs UFCS call. |
| 155 |
assert(1024.max!"a<b" == 1); |
| 156 |
assert(1024.max == int.max); |
| 157 |
|
| 158 |
assert(3.14.infinity!"+" == 1); |
| 159 |
assert(3.14.infinity == (double).infinity); |
| 160 |
} |
| 161 |
|
| 162 |
/*******************************************/ |
| 163 |
|
| 164 |
template Signal4() |
| 165 |
{ |
| 166 |
void connect(){} |
| 167 |
} |
| 168 |
struct S4 |
| 169 |
{ |
| 170 |
mixin Signal4!() s; |
| 171 |
} |
| 172 |
void test4() |
| 173 |
{ |
| 174 |
S4 s; |
| 175 |
s.s.connect(); // s.s is TOKdotexp, so never match UFCS |
| 176 |
} |
| 177 |
|
| 178 |
/*******************************************/ |
| 179 |
|
| 180 |
auto f5_1(int) { return 1; } |
| 181 |
auto f5_2(string) { return 2; } |
| 182 |
auto f5_3(double) { return 3; } |
| 183 |
alias f5_4 = f5_1, f5_4 = f5_2; |
| 184 |
alias f5_5 = f5_3, f5_5 = f5_4; |
| 185 |
|
| 186 |
@property p5_1(int) { return 1; } @property p5_1(int, int) { return 1; } |
| 187 |
@property p5_2(string) { return 2; } @property p5_2(string, int) { return 2; } |
| 188 |
@property p5_3(double) { return 3; } @property p5_3(double, int) { return 3; } |
| 189 |
alias p5_4 = p5_1, p5_4 = p5_2; alias p5_4 = p5_1, p5_4 = p5_2; |
| 190 |
alias p5_5 = p5_3, p5_5 = p5_4; alias p5_5 = p5_3, p5_5 = p5_4; |
| 191 |
|
| 192 |
// import overload set 'f5ov' and 'p5ov' |
| 193 |
import imports.ufcs5b, imports.ufcs5c; |
| 194 |
|
| 195 |
void test5() |
| 196 |
{ |
| 197 |
{ |
| 198 |
// f5_1 .. f5_5 are symbols which declared in module scope |
| 199 |
assert(100.f5_1() == 1); |
| 200 |
assert(001.f5_1() == 1); // https://issues.dlang.org/show_bug.cgi?id=8346 |
| 201 |
assert("s".f5_2() == 2); |
| 202 |
assert(1.4.f5_3() == 3); |
| 203 |
assert(100.f5_4() == 1); |
| 204 |
assert("s".f5_4() == 2); |
| 205 |
assert(100.f5_5() == 1); |
| 206 |
assert("s".f5_5() == 2); |
| 207 |
assert(1.4.f5_5() == 3); |
| 208 |
// overload set |
| 209 |
assert(100.f5ov() == 1); |
| 210 |
assert("s".f5ov() == 2); |
| 211 |
// UFCS does not see function local alias |
| 212 |
alias func5 = f5_1; |
| 213 |
static assert(!__traits(compiles, { 1.func5(); })); |
| 214 |
|
| 215 |
// property getter/setter |
| 216 |
assert(100.p5_1 == 1); assert((100.p5_1 = 1) == 1); |
| 217 |
assert("s".p5_2 == 2); assert(("s".p5_2 = 1) == 2); |
| 218 |
assert(1.4.p5_3 == 3); assert((1.4.p5_3 = 1) == 3); |
| 219 |
assert(100.p5_4 == 1); assert((100.p5_4 = 1) == 1); |
| 220 |
assert("s".p5_4 == 2); assert(("s".p5_4 = 1) == 2); |
| 221 |
assert(100.p5_5 == 1); assert((100.p5_5 = 1) == 1); |
| 222 |
assert("s".p5_5 == 2); assert(("s".p5_5 = 1) == 2); |
| 223 |
assert(1.4.p5_5 == 3); assert((1.4.p5_5 = 1) == 3); |
| 224 |
// overload set ); assert( |
| 225 |
assert(100.p5ov == 1); assert((100.p5ov = 1) == 1); |
| 226 |
assert("s".p5ov == 2); assert(("s".p5ov = 1) == 2); |
| 227 |
// local alias |
| 228 |
alias prop5 = p5_1; |
| 229 |
static assert(!__traits(compiles, { 1.prop5; })); |
| 230 |
static assert(!__traits(compiles, { 1.prop5 = 1; })); |
| 231 |
} |
| 232 |
|
| 233 |
{ |
| 234 |
// f5a1 .. f5a5 are symbols which declared in module scope |
| 235 |
import imports.ufcs5a; |
| 236 |
// overload set 'f5ov' and 'p5ov' |
| 237 |
import imports.ufcs5b, imports.ufcs5c; |
| 238 |
|
| 239 |
assert(100.f5a1() == 1); |
| 240 |
assert("s".f5a2() == 2); |
| 241 |
assert(1.4.f5a3() == 3); |
| 242 |
assert(100.f5a4() == 1); |
| 243 |
assert("s".f5a4() == 2); |
| 244 |
assert(100.f5a5() == 1); |
| 245 |
assert("s".f5a5() == 2); |
| 246 |
assert(1.4.f5a5() == 3); |
| 247 |
assert(100.f5ov() == 1); |
| 248 |
assert("s".f5ov() == 2); |
| 249 |
|
| 250 |
assert(100.p5a1 == 1); assert((100.p5a1 = 1) == 1); |
| 251 |
assert("s".p5a2 == 2); assert(("s".p5a2 = 1) == 2); |
| 252 |
assert(1.4.p5a3 == 3); assert((1.4.p5a3 = 1) == 3); |
| 253 |
assert(100.p5a4 == 1); assert((100.p5a4 = 1) == 1); |
| 254 |
assert("s".p5a4 == 2); assert(("s".p5a4 = 1) == 2); |
| 255 |
assert(100.p5a5 == 1); assert((100.p5a5 = 1) == 1); |
| 256 |
assert("s".p5a5 == 2); assert(("s".p5a5 = 1) == 2); |
| 257 |
assert(1.4.p5a5 == 3); assert((1.4.p5a5 = 1) == 3); |
| 258 |
assert(100.p5ov == 1); assert((100.p5ov = 1) == 1); |
| 259 |
assert("s".p5ov == 2); assert(("s".p5ov = 1) == 2); |
| 260 |
} |
| 261 |
|
| 262 |
{ |
| 263 |
// selective imports also work as expected |
| 264 |
import imports.ufcs5a : f5a1, f5a2; |
| 265 |
import imports.ufcs5a : p5a1, p5a2; |
| 266 |
|
| 267 |
assert(100.f5a1() == 1); |
| 268 |
assert("s".f5a2() == 2); |
| 269 |
static assert(!__traits(compiles, { 1.4.f5a3(); })); |
| 270 |
static assert(!__traits(compiles, { 100.f5a4(); })); |
| 271 |
static assert(!__traits(compiles, { "s".f5a4(); })); |
| 272 |
static assert(!__traits(compiles, { 100.f5a5(); })); |
| 273 |
static assert(!__traits(compiles, { "s".f5a5(); })); |
| 274 |
static assert(!__traits(compiles, { 1.4.f5a5(); })); |
| 275 |
|
| 276 |
assert(100.p5a1 == 1); assert((100.p5a1 = 1) == 1); |
| 277 |
assert("s".p5a2 == 2); assert(("s".p5a2 = 1) == 2); |
| 278 |
static assert(!__traits(compiles, { 1.4.p5a3; }) && !__traits(compiles, { 1.4.p5a3 = 1; })); |
| 279 |
static assert(!__traits(compiles, { 100.p5a4; }) && !__traits(compiles, { 100.p5a4 = 1; })); |
| 280 |
static assert(!__traits(compiles, { "s".p5a4; }) && !__traits(compiles, { "s".p5a4 = 1; })); |
| 281 |
static assert(!__traits(compiles, { 100.p5a5; }) && !__traits(compiles, { 100.p5a5 = 1; })); |
| 282 |
static assert(!__traits(compiles, { "s".p5a5; }) && !__traits(compiles, { "s".p5a5 = 1; })); |
| 283 |
static assert(!__traits(compiles, { 1.4.p5a5; }) && !__traits(compiles, { 1.4.p5a5 = 1; })); |
| 284 |
} |
| 285 |
|
| 286 |
{ |
| 287 |
// renamed imports also work as expected |
| 288 |
import imports.ufcs5a : f5x1 = f5a1, f5x2 = f5a2; |
| 289 |
import imports.ufcs5a : p5x1 = p5a1, p5x2 = p5a2; |
| 290 |
|
| 291 |
assert(100.f5x1() == 1); |
| 292 |
assert("s".f5x2() == 2); |
| 293 |
static assert(!__traits(compiles, { 100.f5a1(); })); |
| 294 |
static assert(!__traits(compiles, { "s".f5a2(); })); |
| 295 |
static assert(!__traits(compiles, { 1.4.f5a3(); })); |
| 296 |
static assert(!__traits(compiles, { 100.f5a4(); })); |
| 297 |
static assert(!__traits(compiles, { "s".f5a4(); })); |
| 298 |
static assert(!__traits(compiles, { 100.f5a5(); })); |
| 299 |
static assert(!__traits(compiles, { "s".f5a5(); })); |
| 300 |
static assert(!__traits(compiles, { 1.4.f5a5(); })); |
| 301 |
|
| 302 |
assert(100.p5x1 == 1); assert((100.p5x1 = 1) == 1); |
| 303 |
assert("s".p5x2 == 2); assert(("s".p5x2 = 1) == 2); |
| 304 |
static assert(!__traits(compiles, { 100.p5a1; }) && !__traits(compiles, { 100.p5a1 = 1; })); |
| 305 |
static assert(!__traits(compiles, { "s".p5a2; }) && !__traits(compiles, { "s".p5a2 = 1; })); |
| 306 |
static assert(!__traits(compiles, { 1.4.p5a3; }) && !__traits(compiles, { 1.4.p5a3 = 1; })); |
| 307 |
static assert(!__traits(compiles, { 100.p5a4; }) && !__traits(compiles, { 100.p5a4 = 1; })); |
| 308 |
static assert(!__traits(compiles, { "s".p5a4; }) && !__traits(compiles, { "s".p5a4 = 1; })); |
| 309 |
static assert(!__traits(compiles, { 100.p5a5; }) && !__traits(compiles, { 100.p5a5 = 1; })); |
| 310 |
static assert(!__traits(compiles, { "s".p5a5; }) && !__traits(compiles, { "s".p5a5 = 1; })); |
| 311 |
static assert(!__traits(compiles, { 1.4.p5a5; }) && !__traits(compiles, { 1.4.p5a5 = 1; })); |
| 312 |
} |
| 313 |
|
| 314 |
{ |
| 315 |
auto c5 = new C5(); |
| 316 |
foreach (name; __traits(allMembers, C5)) |
| 317 |
{ |
| 318 |
static if (name.length >= 4 && name[0..4] == "test") |
| 319 |
{ |
| 320 |
mixin("c5."~name~"();"); // call test function |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
class B5 |
| 327 |
{ |
| 328 |
int g5bm(int) { return 0; } |
| 329 |
static int g5bs(int) { return 0; } |
| 330 |
|
| 331 |
} |
| 332 |
class C5 : B5 |
| 333 |
{ |
| 334 |
// normal import works. |
| 335 |
import imports.ufcs5a; |
| 336 |
void test1() |
| 337 |
{ |
| 338 |
assert(100.f5a1() == 1); |
| 339 |
assert("s".f5a2() == 2); |
| 340 |
assert(1.4.f5a3() == 3); |
| 341 |
assert(100.f5a4() == 1); |
| 342 |
assert("s".f5a4() == 2); |
| 343 |
assert(100.f5a5() == 1); |
| 344 |
assert("s".f5a5() == 2); |
| 345 |
assert(1.4.f5a5() == 3); |
| 346 |
|
| 347 |
assert(100.p5a1 == 1); assert((100.p5a1 = 1) == 1); |
| 348 |
assert("s".p5a2 == 2); assert(("s".p5a2 = 1) == 2); |
| 349 |
assert(1.4.p5a3 == 3); assert((1.4.p5a3 = 1) == 3); |
| 350 |
assert(100.p5a4 == 1); assert((100.p5a4 = 1) == 1); |
| 351 |
assert("s".p5a4 == 2); assert(("s".p5a4 = 1) == 2); |
| 352 |
assert(100.p5a5 == 1); assert((100.p5a5 = 1) == 1); |
| 353 |
assert("s".p5a5 == 2); assert(("s".p5a5 = 1) == 2); |
| 354 |
assert(1.4.p5a5 == 3); assert((1.4.p5a5 = 1) == 3); |
| 355 |
} |
| 356 |
|
| 357 |
// selective imports also work as expected |
| 358 |
import imports.ufcs5d : f5d1, f5d2; |
| 359 |
import imports.ufcs5d : p5d1, p5d2; |
| 360 |
void test2() |
| 361 |
{ |
| 362 |
assert(100.f5d1() == 1); |
| 363 |
assert("s".f5d2() == 2); |
| 364 |
static assert(!__traits(compiles, { 1.4.f5d3(); })); |
| 365 |
static assert(!__traits(compiles, { 100.f5d4(); })); |
| 366 |
static assert(!__traits(compiles, { "s".f5d4(); })); |
| 367 |
static assert(!__traits(compiles, { 100.f5d5(); })); |
| 368 |
static assert(!__traits(compiles, { "s".f5d5(); })); |
| 369 |
static assert(!__traits(compiles, { 1.4.f5d5(); })); |
| 370 |
|
| 371 |
assert(100.p5d1 == 1); assert((100.p5d1 = 1) == 1); |
| 372 |
assert("s".p5d2 == 2); assert(("s".p5d2 = 1) == 2); |
| 373 |
static assert(!__traits(compiles, { 1.4.p5d3; }) && !__traits(compiles, { 1.4.p5d3 = 1; })); |
| 374 |
static assert(!__traits(compiles, { 100.p5d4; }) && !__traits(compiles, { 100.p5d4 = 1; })); |
| 375 |
static assert(!__traits(compiles, { "s".p5d4; }) && !__traits(compiles, { "s".p5d4 = 1; })); |
| 376 |
static assert(!__traits(compiles, { 100.p5d5; }) && !__traits(compiles, { 100.p5d5 = 1; })); |
| 377 |
static assert(!__traits(compiles, { "s".p5d5; }) && !__traits(compiles, { "s".p5d5 = 1; })); |
| 378 |
static assert(!__traits(compiles, { 1.4.p5d5; }) && !__traits(compiles, { 1.4.p5d5 = 1; })); |
| 379 |
} |
| 380 |
|
| 381 |
// renamed imports also work as expected |
| 382 |
import imports.ufcs5e : f5y1 = f5e1, f5y2 = f5e2; |
| 383 |
import imports.ufcs5e : p5y1 = p5e1, p5y2 = p5e2; |
| 384 |
void test3() |
| 385 |
{ |
| 386 |
assert(100.f5y1() == 1); |
| 387 |
assert("s".f5y2() == 2); |
| 388 |
static assert(!__traits(compiles, { 100.f5e1(); })); |
| 389 |
static assert(!__traits(compiles, { "s".f5e2(); })); |
| 390 |
static assert(!__traits(compiles, { 1.4.f5e3(); })); |
| 391 |
static assert(!__traits(compiles, { 100.f5e4(); })); |
| 392 |
static assert(!__traits(compiles, { "s".f5e4(); })); |
| 393 |
static assert(!__traits(compiles, { 100.f5e5(); })); |
| 394 |
static assert(!__traits(compiles, { "s".f5e5(); })); |
| 395 |
static assert(!__traits(compiles, { 1.4.f5e5(); })); |
| 396 |
|
| 397 |
assert(100.p5y1 == 1); assert((100.p5y1 = 1) == 1); |
| 398 |
assert("s".p5y2 == 2); assert(("s".p5y2 = 1) == 2); |
| 399 |
static assert(!__traits(compiles, { 100.p5e1; }) && !__traits(compiles, { (100.p5e1 = 1); })); |
| 400 |
static assert(!__traits(compiles, { "s".p5e2; }) && !__traits(compiles, { ("s".p5e2 = 1); })); |
| 401 |
static assert(!__traits(compiles, { 1.4.p5e3; }) && !__traits(compiles, { (1.4.p5e3 = 1); })); |
| 402 |
static assert(!__traits(compiles, { 100.p5e4; }) && !__traits(compiles, { (100.p5e4 = 1); })); |
| 403 |
static assert(!__traits(compiles, { "s".p5e4; }) && !__traits(compiles, { ("s".p5e4 = 1); })); |
| 404 |
static assert(!__traits(compiles, { 100.p5e5; }) && !__traits(compiles, { (100.p5e5 = 1); })); |
| 405 |
static assert(!__traits(compiles, { "s".p5e5; }) && !__traits(compiles, { ("s".p5e5 = 1); })); |
| 406 |
static assert(!__traits(compiles, { 1.4.p5e5; }) && !__traits(compiles, { (1.4.p5e5 = 1); })); |
| 407 |
} |
| 408 |
|
| 409 |
int g5cm(int) { return 0; } |
| 410 |
static int g5cs(int) { return 0; } |
| 411 |
void test4() |
| 412 |
{ |
| 413 |
// UFCS does not see aggregate members |
| 414 |
static assert(!__traits(compiles, { 1.g5cm(); })); |
| 415 |
static assert(!__traits(compiles, { 1.g5cs(); })); |
| 416 |
|
| 417 |
// Even if it is in base class |
| 418 |
static assert(!__traits(compiles, { 1.g5bm(); })); |
| 419 |
static assert(!__traits(compiles, { 1.g5bs(); })); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
/*******************************************/ |
| 424 |
// https://issues.dlang.org/show_bug.cgi?id=662 |
| 425 |
|
| 426 |
enum Etest |
| 427 |
{ |
| 428 |
a,b,c,d |
| 429 |
} |
| 430 |
|
| 431 |
//typedef int testi = 10; |
| 432 |
//typedef Test Test2; |
| 433 |
|
| 434 |
int test() { return 33; } |
| 435 |
|
| 436 |
class Test |
| 437 |
{ |
| 438 |
static int test(int i) { return i; } |
| 439 |
} |
| 440 |
|
| 441 |
int test(Etest test) |
| 442 |
{ |
| 443 |
return cast(int)test; |
| 444 |
} |
| 445 |
|
| 446 |
//int test(testi i) |
| 447 |
//{ |
| 448 |
// return cast(int)i; |
| 449 |
//} |
| 450 |
string to(T)(int i) { |
| 451 |
assert(i == 22); |
| 452 |
return "22"; |
| 453 |
} |
| 454 |
|
| 455 |
void test682() |
| 456 |
{ |
| 457 |
assert(22.to!string() == "22"); |
| 458 |
assert((new Test).test(11) == 11); |
| 459 |
assert(Test.test(11) == 11); |
| 460 |
//assert(Test2.test(11) == 11); |
| 461 |
assert(test() == 33); |
| 462 |
assert(ufcs.test() == 33); |
| 463 |
assert(Etest.d.test() == Etest.d); |
| 464 |
//testi i; |
| 465 |
//assert(i.test() == i.init); |
| 466 |
} |
| 467 |
|
| 468 |
/*******************************************/ |
| 469 |
// https://issues.dlang.org/show_bug.cgi?id=3382 |
| 470 |
|
| 471 |
@property T twice(T)(T x){ return x * x; } |
| 472 |
char toupper(char c){ return ('a'<=c && c<='z') ? cast(char)(c - 'a' + 'A') : c; } |
| 473 |
|
| 474 |
@property ref T setter(T)(ref T x, T v){ x = v; return x; } |
| 475 |
|
| 476 |
auto iota(T)(T min, T max) |
| 477 |
{ |
| 478 |
static struct Result |
| 479 |
{ |
| 480 |
T cur, end; |
| 481 |
|
| 482 |
T front() { return cur; } |
| 483 |
bool empty() { return front == end; } |
| 484 |
void popFront() { cur++; } |
| 485 |
} |
| 486 |
return Result(min, max); |
| 487 |
} |
| 488 |
|
| 489 |
auto map(string s, R)(R range) |
| 490 |
{ |
| 491 |
static struct Result |
| 492 |
{ |
| 493 |
R source; |
| 494 |
auto front() { auto a = source.front; return mixin(s); } |
| 495 |
alias source this; |
| 496 |
} |
| 497 |
return Result(range); |
| 498 |
} |
| 499 |
|
| 500 |
auto filter(string s, R)(R range) |
| 501 |
{ |
| 502 |
static struct Result |
| 503 |
{ |
| 504 |
R source; |
| 505 |
alias source this; |
| 506 |
void popFront() |
| 507 |
{ |
| 508 |
while (true) |
| 509 |
{ |
| 510 |
auto a = source.front; |
| 511 |
if (mixin(s)) break; |
| 512 |
source.popFront(); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
return Result(range); |
| 517 |
} |
| 518 |
|
| 519 |
void test3382() |
| 520 |
{ |
| 521 |
auto r = iota(0, 10).map!"a*3"().filter!"a%2 != 0"(); |
| 522 |
foreach (e; r) |
| 523 |
printf("e = %d\n", e); |
| 524 |
|
| 525 |
assert(10.twice == 100); |
| 526 |
assert(0.5.twice == 0.25); |
| 527 |
assert('c'.toupper() == 'C'); |
| 528 |
} |
| 529 |
|
| 530 |
/*******************************************/ |
| 531 |
// https://issues.dlang.org/show_bug.cgi?id=6185 |
| 532 |
|
| 533 |
ref T front(T)(T[] array) { return array[0]; } |
| 534 |
bool empty(T)(T[] array) { return array.length == 0; } |
| 535 |
void popFront(T)(ref T[] array) { array = array[1..$]; } |
| 536 |
|
| 537 |
bool equal(T, U)(T t, U u) |
| 538 |
{ |
| 539 |
while (true) |
| 540 |
{ |
| 541 |
if (t.empty) return u.empty; |
| 542 |
if (u.empty || t.front != u.front) return false; |
| 543 |
t.popFront(); |
| 544 |
u.popFront(); |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
void test6185() |
| 549 |
{ |
| 550 |
auto r1 = [1,2,3].map!"a*2"; |
| 551 |
assert(equal(r1, [2,4,6])); |
| 552 |
|
| 553 |
auto r2 = r1.map!"a+2"(); |
| 554 |
assert(equal(r2, [4,6,8])); |
| 555 |
} |
| 556 |
|
| 557 |
/*******************************************/ |
| 558 |
// https://issues.dlang.org/show_bug.cgi?id=6070 |
| 559 |
|
| 560 |
enum test6070a = ["test"].foo6070(); |
| 561 |
enum test6070b = foo6070(["test"]); |
| 562 |
|
| 563 |
string foo6070(string[] s) { return ""; } |
| 564 |
|
| 565 |
/*******************************************/ |
| 566 |
// https://issues.dlang.org/show_bug.cgi?id=7670 |
| 567 |
|
| 568 |
struct A7670 |
| 569 |
{ |
| 570 |
double x; |
| 571 |
} |
| 572 |
@property ref double y7670(return ref A7670 a) |
| 573 |
{ |
| 574 |
return a.x; |
| 575 |
} |
| 576 |
void test7670() |
| 577 |
{ |
| 578 |
A7670 a1; |
| 579 |
a1.y7670() = 2.0; // OK |
| 580 |
a1.y7670 = 2.0; // Error |
| 581 |
} |
| 582 |
|
| 583 |
/*******************************************/ |
| 584 |
// https://issues.dlang.org/show_bug.cgi?id=7703 |
| 585 |
void f7703(T)(T a) { } |
| 586 |
|
| 587 |
void test7703() |
| 588 |
{ |
| 589 |
int x; |
| 590 |
x.f7703; // accepted |
| 591 |
x.f7703(); // accepted |
| 592 |
x.f7703!int; // rejected -- "f(x) isn't a template" |
| 593 |
x.f7703!int(); // accepted |
| 594 |
} |
| 595 |
|
| 596 |
/*******************************************/ |
| 597 |
// https://issues.dlang.org/show_bug.cgi?id=7773 |
| 598 |
|
| 599 |
//import core.stdc.stdio; |
| 600 |
void writeln7773(int n){} |
| 601 |
void test7773() |
| 602 |
{ |
| 603 |
(int.max).writeln7773(); // OK |
| 604 |
int.max.writeln7773(); // error |
| 605 |
} |
| 606 |
|
| 607 |
/*******************************************/ |
| 608 |
// https://issues.dlang.org/show_bug.cgi?id=7943 |
| 609 |
|
| 610 |
struct Foo7943 |
| 611 |
{ |
| 612 |
int _member; |
| 613 |
alias _member this; |
| 614 |
} |
| 615 |
|
| 616 |
int foo7943(Foo7943 f) { return 1; } |
| 617 |
int foo7943(int i) { return 2; } |
| 618 |
|
| 619 |
void test7943() |
| 620 |
{ |
| 621 |
Foo7943 f; |
| 622 |
assert(f.foo7943() == 1); |
| 623 |
} |
| 624 |
|
| 625 |
/*******************************************/ |
| 626 |
// https://issues.dlang.org/show_bug.cgi?id=8180 |
| 627 |
|
| 628 |
int writeln8180(T...)(T args) { return 1; } |
| 629 |
|
| 630 |
struct Tuple8180(T...) |
| 631 |
{ |
| 632 |
T field; |
| 633 |
alias field this; |
| 634 |
} |
| 635 |
|
| 636 |
void test8180() |
| 637 |
{ |
| 638 |
auto t = Tuple8180!(int)(10); |
| 639 |
assert(t.writeln8180() == 1); |
| 640 |
} |
| 641 |
|
| 642 |
/*******************************************/ |
| 643 |
// https://issues.dlang.org/show_bug.cgi?id=8245 |
| 644 |
|
| 645 |
string toStr8245(immutable(char)* p) { return null; } |
| 646 |
@property string asStr8245(immutable(char)* p) { return null; } |
| 647 |
|
| 648 |
void test8245() |
| 649 |
{ |
| 650 |
immutable(char)* p = "foobar".ptr; |
| 651 |
p.toStr8245(); |
| 652 |
p.asStr8245; // Error: no property 'asStr' for type 'immutable(char)' |
| 653 |
} |
| 654 |
|
| 655 |
/*******************************************/ |
| 656 |
// https://issues.dlang.org/show_bug.cgi?id=8252 |
| 657 |
|
| 658 |
bool f(int x) { return !x; } |
| 659 |
|
| 660 |
void test8252() |
| 661 |
{ |
| 662 |
static assert(!1.f); // ok |
| 663 |
static assert( 0.f); // fail |
| 664 |
} |
| 665 |
|
| 666 |
/*******************************************/ |
| 667 |
// https://issues.dlang.org/show_bug.cgi?id=8453 |
| 668 |
|
| 669 |
T[] sort8453(T)(T[] a) { return a; } |
| 670 |
|
| 671 |
void test8453() |
| 672 |
{ |
| 673 |
int[int] foo; |
| 674 |
auto bar1 = foo.keys().sort8453(); // OK |
| 675 |
auto bar2 = foo.keys.sort8453(); // Error |
| 676 |
} |
| 677 |
|
| 678 |
/*******************************************/ |
| 679 |
// https://issues.dlang.org/show_bug.cgi?id=8503 |
| 680 |
|
| 681 |
//UTF-8 chars |
| 682 |
void α8503(int i) {} |
| 683 |
|
| 684 |
void test8503() |
| 685 |
{ |
| 686 |
0.α8503(); // Error |
| 687 |
1.α8503(); // Error |
| 688 |
} |
| 689 |
|
| 690 |
/*******************************************/ |
| 691 |
// https://issues.dlang.org/show_bug.cgi?id=9014 |
| 692 |
|
| 693 |
@property ref int foo9014(int[] a) |
| 694 |
{ |
| 695 |
return a[0]; |
| 696 |
} |
| 697 |
void test9014() |
| 698 |
{ |
| 699 |
int[] bar; |
| 700 |
static assert(!__traits(compiles, { |
| 701 |
bar.foo9014 = missing.foo9014; |
| 702 |
})); |
| 703 |
} |
| 704 |
|
| 705 |
/*******************************************/ |
| 706 |
// https://issues.dlang.org/show_bug.cgi?id=9590 |
| 707 |
|
| 708 |
auto func9590(E)(lazy E expr) { } |
| 709 |
|
| 710 |
int f9590a() { assert(0); } |
| 711 |
void f9590b() { assert(0); } |
| 712 |
|
| 713 |
void test9590() |
| 714 |
{ |
| 715 |
func9590(f9590a()); // ok, no exceptions (lazy) |
| 716 |
f9590a().func9590; // ok, no exceptions (lazy) |
| 717 |
|
| 718 |
func9590(f9590b()); // ok, no exceptions (lazy) |
| 719 |
f9590b().func9590; // L12: NG |
| 720 |
} |
| 721 |
|
| 722 |
/*******************************************/ |
| 723 |
// https://issues.dlang.org/show_bug.cgi?id=9946 |
| 724 |
|
| 725 |
size_t count9946(alias x)(int[] haystack) |
| 726 |
{ |
| 727 |
return 0; |
| 728 |
} |
| 729 |
void test9946() |
| 730 |
{ |
| 731 |
int[] data; |
| 732 |
auto n1 = count9946!5(data); // OK |
| 733 |
auto n2 = data.count9946!5; // OK |
| 734 |
auto a1 = new int[count9946!5(data)]; // OK |
| 735 |
auto a2 = new int[data.count9946!5]; // Error |
| 736 |
} |
| 737 |
|
| 738 |
/*******************************************/ |
| 739 |
// https://issues.dlang.org/show_bug.cgi?id=10618 |
| 740 |
|
| 741 |
template Temp10618(T) |
| 742 |
{ |
| 743 |
size_t len = 1; |
| 744 |
} |
| 745 |
void test10618() |
| 746 |
{ |
| 747 |
auto arr = new int[Temp10618!int.len]; |
| 748 |
assert(arr.length == 1); |
| 749 |
} |
| 750 |
|
| 751 |
/*******************************************/ |
| 752 |
// https://issues.dlang.org/show_bug.cgi?id=10003 |
| 753 |
|
| 754 |
void foo10003(void *p) {} |
| 755 |
void test10003() |
| 756 |
{ |
| 757 |
void* p; |
| 758 |
p.foo10003(); |
| 759 |
} |
| 760 |
|
| 761 |
/*******************************************/ |
| 762 |
// https://issues.dlang.org/show_bug.cgi?id=10041 |
| 763 |
|
| 764 |
auto writeln10041(T...)(T args) { return typeof(args[0]).stringof; } |
| 765 |
|
| 766 |
void test10041() |
| 767 |
{ |
| 768 |
auto aa = [1: 2]; |
| 769 |
assert(aa.writeln10041 == "int[int]"); |
| 770 |
assert(writeln10041(aa) == "int[int]"); |
| 771 |
} |
| 772 |
|
| 773 |
/*******************************************/ |
| 774 |
// https://issues.dlang.org/show_bug.cgi?id=10047 |
| 775 |
|
| 776 |
struct Typedef10047(T) |
| 777 |
{ |
| 778 |
template opDispatch(string name) |
| 779 |
{ |
| 780 |
static assert(0); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
struct A10047 {} |
| 785 |
int foo10047(Typedef10047!A10047 a) { return 10; } |
| 786 |
|
| 787 |
void test10047() |
| 788 |
{ |
| 789 |
Typedef10047!A10047 a; |
| 790 |
assert(a.foo10047() == 10); |
| 791 |
} |
| 792 |
|
| 793 |
/*******************************************/ |
| 794 |
// https://issues.dlang.org/show_bug.cgi?id=10166 |
| 795 |
|
| 796 |
auto foo10166() |
| 797 |
{ |
| 798 |
0.bar10166!({})(0); |
| 799 |
} |
| 800 |
|
| 801 |
void bar10166(alias handler, T)(T t, int i) |
| 802 |
{ |
| 803 |
t.bar10166!buzz10166(i); |
| 804 |
} |
| 805 |
|
| 806 |
void buzz10166() {} |
| 807 |
|
| 808 |
/*******************************************/ |
| 809 |
// https://issues.dlang.org/show_bug.cgi?id=10526 |
| 810 |
|
| 811 |
struct S10526 |
| 812 |
{ |
| 813 |
int opDispatch(string s, A...)(A args) |
| 814 |
if (s[0..3] == "foo") |
| 815 |
{ |
| 816 |
return 1; |
| 817 |
} |
| 818 |
} |
| 819 |
int bar10526(X)(X) { return 2; } |
| 820 |
int baz10526(T, X)(X) { return 3; } |
| 821 |
|
| 822 |
void test10526() |
| 823 |
{ |
| 824 |
S10526 s; |
| 825 |
|
| 826 |
// with parenthesis |
| 827 |
assert(s.foo10526() == 1); |
| 828 |
assert(s.bar10526() == 2); |
| 829 |
assert(s.baz10526!string() == 3); |
| 830 |
|
| 831 |
// without parenthesis |
| 832 |
assert(s.foo10526 == 1); |
| 833 |
assert(s.bar10526 == 2); |
| 834 |
assert(s.baz10526!string == 3); |
| 835 |
} |
| 836 |
|
| 837 |
/********************************************************/ |
| 838 |
// https://issues.dlang.org/show_bug.cgi?id=10609 |
| 839 |
|
| 840 |
int foo10609(int x) { return x; } |
| 841 |
|
| 842 |
void test10609() |
| 843 |
{ |
| 844 |
int x = 1; |
| 845 |
static assert(__traits(compiles, foo10609(x))); |
| 846 |
static assert(__traits(compiles, 1.foo10609 )); |
| 847 |
static assert(__traits(compiles, x.foo10609 )); |
| 848 |
} |
| 849 |
|
| 850 |
/*******************************************/ |
| 851 |
// https://issues.dlang.org/show_bug.cgi?id=11312 |
| 852 |
|
| 853 |
struct S11312; |
| 854 |
|
| 855 |
S11312* getS11312() { return null; } |
| 856 |
int getValue(S11312*) { return 10; } |
| 857 |
|
| 858 |
void test11312() |
| 859 |
{ |
| 860 |
S11312* op = getS11312(); |
| 861 |
int x = op.getValue(); |
| 862 |
assert(x == 10); |
| 863 |
} |
| 864 |
|
| 865 |
/*******************************************/ |
| 866 |
// https://issues.dlang.org/show_bug.cgi?id=15123 |
| 867 |
|
| 868 |
auto keys15123(K, V)(V[K] aa) { return [1]; } |
| 869 |
auto values15123(K, V)(V[K] aa) { return [2]; } |
| 870 |
|
| 871 |
alias id15123(alias arg) = arg; |
| 872 |
|
| 873 |
enum int[int] aa15123 = [1:2]; |
| 874 |
static assert(id15123!(aa15123.keys15123) == [1]); // TypeIdentifier + UFCS |
| 875 |
|
| 876 |
T[T] f15123(T)() { return [1:2]; } |
| 877 |
static assert(id15123!(f15123!int.values15123) == [2]); // TypeInstance + UFCS |
| 878 |
|
| 879 |
/*******************************************/ |
| 880 |
|
| 881 |
int main() |
| 882 |
{ |
| 883 |
test1(); |
| 884 |
test2(); |
| 885 |
test3(); |
| 886 |
test4(); |
| 887 |
test5(); |
| 888 |
test682(); |
| 889 |
test3382(); |
| 890 |
test6185(); |
| 891 |
test7670(); |
| 892 |
test7703(); |
| 893 |
test7773(); |
| 894 |
test7943(); |
| 895 |
test8180(); |
| 896 |
test8245(); |
| 897 |
test8252(); |
| 898 |
test8453(); |
| 899 |
test8503(); |
| 900 |
test9014(); |
| 901 |
test9590(); |
| 902 |
test9946(); |
| 903 |
test10618(); |
| 904 |
test10003(); |
| 905 |
test10041(); |
| 906 |
test10047(); |
| 907 |
test10526(); |
| 908 |
test11312(); |
| 909 |
|
| 910 |
printf("Success\n"); |
| 911 |
return 0; |
| 912 |
} |