Skip to content

Commit

Permalink
move incompatibleTypes to expressionsem.d (#16857)
Browse files Browse the repository at this point in the history
...where the vast majority of its uses are. Also it is a semantic routine and should not be in an AST module
  • Loading branch information
thewilsonator authored Sep 20, 2024
1 parent 1cd5fed commit 993a4be
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
56 changes: 0 additions & 56 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -3031,28 +3031,6 @@ extern (C++) abstract class UnaExp : Expression
return e;
}

/********************************
* The type for a unary expression is incompatible.
* Print error message.
* Returns:
* ErrorExp
*/
extern (D) final Expression incompatibleTypes()
{
if (e1.type.toBasetype() == Type.terror)
return e1;

if (e1.op == EXP.type)
{
error(loc, "incompatible type for `%s(%s)`: cannot use `%s` with types", EXPtoString(op).ptr, e1.toChars(), EXPtoString(op).ptr);
}
else
{
error(loc, "incompatible type for `%s(%s)`: `%s`", EXPtoString(op).ptr, e1.toChars(), e1.type.toChars());
}
return ErrorExp.get();
}

/*********************
* Mark the operand as will never be dereferenced,
* which is useful info for @safe checks.
Expand Down Expand Up @@ -3097,40 +3075,6 @@ extern (C++) abstract class BinExp : Expression
return e;
}

/********************************
* The types for a binary expression are incompatible.
* Print error message.
* Returns:
* ErrorExp
*/
extern (D) final Expression incompatibleTypes()
{
if (e1.type.toBasetype() == Type.terror)
return e1;
if (e2.type.toBasetype() == Type.terror)
return e2;

// CondExp uses 'a ? b : c' but we're comparing 'b : c'
const(char)* thisOp = (op == EXP.question) ? ":" : EXPtoString(op).ptr;
if (e1.op == EXP.type || e2.op == EXP.type)
{
error(loc, "incompatible types for `(%s) %s (%s)`: cannot use `%s` with types",
e1.toChars(), thisOp, e2.toChars(), EXPtoString(op).ptr);
}
else if (e1.type.equals(e2.type))
{
error(loc, "incompatible types for `(%s) %s (%s)`: both operands are of type `%s`",
e1.toChars(), thisOp, e2.toChars(), e1.type.toChars());
}
else
{
auto ts = toAutoQualChars(e1.type, e2.type);
error(loc, "incompatible types for `(%s) %s (%s)`: `%s` and `%s`",
e1.toChars(), thisOp, e2.toChars(), ts[0], ts[1]);
}
return ErrorExp.get();
}

extern (D) final bool checkIntegralBin()
{
bool r1 = e1.checkIntegral();
Expand Down
55 changes: 55 additions & 0 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,61 @@ StringExp toUTF8(StringExp se, Scope* sc)
}
return se;
}
/********************************
* The type for a unary expression is incompatible.
* Print error message.
* Returns:
* ErrorExp
*/
extern (D) Expression incompatibleTypes(UnaExp e)
{
if (e.e1.type.toBasetype() == Type.terror)
return e.e1;

if (e.e1.op == EXP.type)
{
error(e.loc, "incompatible type for `%s(%s)`: cannot use `%s` with types", EXPtoString(e.op).ptr, e.e1.toChars(), EXPtoString(e.op).ptr);
}
else
{
error(e.loc, "incompatible type for `%s(%s)`: `%s`", EXPtoString(e.op).ptr, e.e1.toChars(), e.e1.type.toChars());
}
return ErrorExp.get();
}

/********************************
* The types for a binary expression are incompatible.
* Print error message.
* Returns:
* ErrorExp
*/
extern (D) Expression incompatibleTypes(BinExp e)
{
if (e.e1.type.toBasetype() == Type.terror)
return e.e1;
if (e.e2.type.toBasetype() == Type.terror)
return e.e2;

// CondExp uses 'a ? b : c' but we're comparing 'b : c'
const(char)* thisOp = (e.op == EXP.question) ? ":" : EXPtoString(e.op).ptr;
if (e.e1.op == EXP.type || e.e2.op == EXP.type)
{
error(e.loc, "incompatible types for `(%s) %s (%s)`: cannot use `%s` with types",
e.e1.toChars(), thisOp, e.e2.toChars(), EXPtoString(e.op).ptr);
}
else if (e.e1.type.equals(e.e2.type))
{
error(e.loc, "incompatible types for `(%s) %s (%s)`: both operands are of type `%s`",
e.e1.toChars(), thisOp, e.e2.toChars(), e.e1.type.toChars());
}
else
{
auto ts = toAutoQualChars(e.e1.type, e.e2.type);
error(e.loc, "incompatible types for `(%s) %s (%s)`: `%s` and `%s`",
e.e1.toChars(), thisOp, e.e2.toChars(), ts[0], ts[1]);
}
return ErrorExp.get();
}

private Expression reorderSettingAAElem(BinExp exp, Scope* sc)
{
Expand Down

0 comments on commit 993a4be

Please sign in to comment.