Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7106 from JinShil/static_alias_this_3
Fix Issue 17684 - [REG 2.062] static alias this (Part 3)
- Loading branch information
Showing
3 changed files
with
105 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,132 @@ | ||
struct IntFieldTest | ||
struct StructField(T) | ||
{ | ||
static int Field; | ||
static T Field; | ||
static alias Field this; | ||
} | ||
|
||
struct IntPropertyTest | ||
struct StructProperty(T) | ||
{ | ||
static int Field; | ||
static T Field; | ||
|
||
static @property int property() | ||
static @property T property() | ||
{ | ||
return Field; | ||
} | ||
|
||
static @property void property(int value) | ||
static @property void property(T value) | ||
{ | ||
Field = value; | ||
} | ||
|
||
static alias property this; | ||
} | ||
|
||
struct BoolFieldTest | ||
class ClassField(T) | ||
{ | ||
static bool Field; | ||
static T Field; | ||
static alias Field this; | ||
} | ||
|
||
struct BoolPropertyTest | ||
class ClassProperty(T) | ||
{ | ||
static bool Field; | ||
static T Field; | ||
|
||
static @property bool property() | ||
static @property T property() | ||
{ | ||
return Field; | ||
} | ||
|
||
static @property void property(bool value) | ||
static @property void property(T value) | ||
{ | ||
Field = value; | ||
} | ||
|
||
static alias property this; | ||
} | ||
|
||
void main() | ||
bool boolTest(T)() | ||
{ | ||
// Test `static alias this` to a field of boolean type | ||
BoolFieldTest = false; | ||
assert(BoolFieldTest == false); | ||
alias t = T; | ||
|
||
t = false; // tests AssignExp | ||
assert(t == false); | ||
|
||
bool boolValue = BoolFieldTest; | ||
bool boolValue = t; // tests AssignExp | ||
assert(boolValue == false); | ||
|
||
BoolFieldTest = !BoolFieldTest; | ||
assert(BoolFieldTest == true); | ||
t = !t; // tests NotExp | ||
assert(t == true); | ||
|
||
boolValue = BoolFieldTest; | ||
boolValue = t; | ||
assert(boolValue == true); | ||
|
||
// Test `static alias this` to a property of boolean type | ||
BoolPropertyTest = false; | ||
assert(BoolPropertyTest == false); | ||
assert(boolValue && t); // tests AndAndExp | ||
assert(t && boolValue); | ||
|
||
boolValue = BoolPropertyTest; | ||
assert(boolValue == false); | ||
boolValue = false; | ||
assert(boolValue || t); // tests OrOrExp | ||
assert(t || boolValue); | ||
|
||
BoolPropertyTest = !BoolPropertyTest; | ||
assert(BoolPropertyTest == true); | ||
assert(t != boolValue); // tests CmpExp | ||
assert(boolValue != t); | ||
|
||
boolValue = BoolPropertyTest; | ||
assert(boolValue == true); | ||
|
||
// Test `static alias this` to a field of int type | ||
IntFieldTest = 42; // test assignment | ||
assert(IntFieldTest == 42); | ||
|
||
int intValue = IntFieldTest; | ||
assert(intValue == 42); | ||
boolValue = true; | ||
assert(t == boolValue); | ||
assert(boolValue == t); | ||
|
||
IntFieldTest++; // test a few unary and binary operators | ||
assert(IntFieldTest == 43); | ||
t = true; | ||
return t; // tests ReturnStatement | ||
} | ||
|
||
IntFieldTest += 1; | ||
assert(IntFieldTest == 44); | ||
int intTest(T)() | ||
{ | ||
alias t = T; | ||
|
||
IntFieldTest--; | ||
assert(IntFieldTest == 43); | ||
t = 42; // tests AssignExp | ||
assert(t == 42); | ||
|
||
IntFieldTest -= 1; | ||
assert(IntFieldTest == 42); | ||
int intValue = t; | ||
assert(intValue == 42); | ||
|
||
assert(~IntFieldTest == ~42); | ||
assert(t == 42); // tests CmpExp | ||
assert(42 == t); | ||
assert(t != 43); | ||
assert(43 != t); | ||
assert(t < 43); | ||
assert(43 > t); | ||
assert(t <= 42); | ||
assert(42 >= t); | ||
|
||
// Test `static alias this` to a property of int type | ||
IntPropertyTest = 42; // test assignment | ||
assert(IntPropertyTest == 42); | ||
// These currently don't work for properties due to https://issues.dlang.org/show_bug.cgi?id=8006 | ||
static if (!(typeid(T) is typeid(StructProperty!int)) && !(typeid(T) is typeid(ClassProperty!int))) | ||
{ | ||
t++; // test a few unary and binary operators | ||
assert(t == 43); | ||
|
||
intValue = IntPropertyTest; | ||
assert(intValue == 42); | ||
t += 1; | ||
assert(t == 44); | ||
|
||
// These currently don't work due to https://issues.dlang.org/show_bug.cgi?id=8006 | ||
// IntPropertyTest++; | ||
// assert(IntPropertyTest == 43); | ||
t--; | ||
assert(t == 43); | ||
|
||
// IntPropertyTest += 1; | ||
// assert(IntPropertyTest == 44); | ||
t -= 1; | ||
assert(t == 42); | ||
} | ||
|
||
// IntPropertyTest--; | ||
// assert(IntPropertyTest == 43); | ||
assert(~t == ~42); // tests ComExp | ||
|
||
// IntPropertyTest -= 1; | ||
// assert(IntPropertyTest == 42); | ||
return t; // tests ReturnStatement | ||
} | ||
|
||
assert(~IntPropertyTest == ~42); | ||
void main() | ||
{ | ||
assert(boolTest!(StructField!(bool))()); | ||
assert(boolTest!(StructProperty!(bool))()); | ||
assert(boolTest!(ClassField!(bool))()); | ||
assert(boolTest!(ClassProperty!(bool))()); | ||
|
||
assert(intTest!(StructField!(int))() == 42); | ||
assert(intTest!(StructProperty!(int))() == 42); | ||
assert(intTest!(ClassField!(int))() == 42); | ||
assert(intTest!(ClassProperty!(int))() == 42); | ||
} |