Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Commit

Permalink
Bug 131: Handle condition expressions of type void
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuclaw committed Jun 14, 2014
1 parent 6090d4b commit 1186784
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions gcc/d/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
2014-06-14 Iain Buclaw <ibuclaw@gdcproject.org>

* d-elem.cc(CondExp::toElem): Handle void type condition expressions.
* d-codegen.h(d_types_compatible): First check equality of types, then
implicit compatibility.
* d-convert.cc(d_default_conversion): Remove function, fold
Expand Down
17 changes: 12 additions & 5 deletions gcc/d/d-elem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@ Expression::toElem (IRState *)
elem *
CondExp::toElem (IRState *irs)
{
tree cn = convert_for_condition (econd->toElem (irs), econd->type);
tree t1 = convert_expr (e1->toElemDtor (irs), e1->type, type);
tree t2 = convert_expr (e2->toElemDtor (irs), e2->type, type);
return build3 (COND_EXPR, type->toCtype(), cn, t1, t2);
tree cond = convert_for_condition (econd->toElem (irs), econd->type);
tree t1 = e1->toElemDtor (irs);
tree t2 = e2->toElemDtor (irs);

if (type->ty != Tvoid)
{
t1 = convert_expr (t1, e1->type, type);
t2 = convert_expr (t2, e2->type, type);
}

return build3 (COND_EXPR, type->toCtype(), cond, t1, t2);
}

elem *
Expand Down Expand Up @@ -1387,7 +1394,7 @@ CastExp::toElem (IRState *irs)

// Just evaluate e1 if it has any side effects
if (tbtype->ty == Tvoid)
return build1 (NOP_EXPR, tbtype->toCtype(), t);
return build_nop (tbtype->toCtype(), t);

return convert_expr (t, ebtype, tbtype);
}
Expand Down
18 changes: 18 additions & 0 deletions gcc/testsuite/gdc.test/runnable/gdc.d
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,22 @@ int[0][1] test127c; // ICE

/******************************************/

// Bug 131

struct S131
{
this(string ) { }
string opAssign(string v) { return v; }
}

void test131()
{
S131[string] s;
s["foo"] = "bar";
}

/******************************************/

// Bug 133

void delegate()[] D133;
Expand Down Expand Up @@ -662,6 +678,8 @@ void main()
test77();
test108();
test115();
test131();
test133();

printf("Success!\n");
}
62 changes: 62 additions & 0 deletions libphobos/src/std/json.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ struct JSONValue
return type_tag;
}

/// Sets the _type of this $(D JSONValue). Previous content is cleared.
/// $(RED Scheduled for deprecation in September 2014. Instead, please
/// assign the value with the adequate type to $(D JSONValue) directly.)
@property JSON_TYPE type(JSON_TYPE newType)
{
if (type_tag != newType
&& ((type_tag != JSON_TYPE.INTEGER && type_tag != JSON_TYPE.UINTEGER)
|| (newType != JSON_TYPE.INTEGER && newType != JSON_TYPE.UINTEGER)))
{
final switch (newType)
{
case JSON_TYPE.STRING:
store.str = store.str.init;
break;
case JSON_TYPE.INTEGER:
store.integer = store.integer.init;
break;
case JSON_TYPE.UINTEGER:
store.uinteger = store.uinteger.init;
break;
case JSON_TYPE.FLOAT:
store.floating = store.floating.init;
break;
case JSON_TYPE.OBJECT:
store.object = store.object.init;
break;
case JSON_TYPE.ARRAY:
store.array = store.array.init;
break;
case JSON_TYPE.TRUE:
case JSON_TYPE.FALSE:
case JSON_TYPE.NULL:
break;
}
}
return type_tag = newType;
}

/// Value getter/setter for $(D JSON_TYPE.STRING).
/// Throws $(D JSONException) for read access if $(D type) is not $(D JSON_TYPE.STRING).
@property inout(string) str() inout
Expand Down Expand Up @@ -1007,3 +1045,27 @@ unittest
]
}`);
}

unittest
{
// Bugzilla 12332

JSONValue jv;
jv.type = JSON_TYPE.INTEGER;
jv = 1;
assert(jv.type == JSON_TYPE.INTEGER);
assert(jv.integer == 1);
jv.type = JSON_TYPE.UINTEGER;
assert(jv.uinteger == 1);

jv.type = JSON_TYPE.STRING;
assertThrown!JSONException(jv.integer == 1);
assert(jv.str is null);
jv.str = "123";
assert(jv.str == "123");
jv.type = JSON_TYPE.STRING;
assert(jv.str == "123");

jv.type = JSON_TYPE.TRUE;
assert(jv.type == JSON_TYPE.TRUE);
}

0 comments on commit 1186784

Please sign in to comment.