From 65d3e9019f1f06a436ff3f8d922d63df7bc7479b Mon Sep 17 00:00:00 2001 From: k-hara Date: Fri, 13 Dec 2013 10:24:54 +0900 Subject: [PATCH] fix Issue 11727 - Repeated error message with using forward referenced enum as variable --- src/expression.c | 11 +++++---- src/expression.h | 6 ++--- src/statement.c | 2 ++ test/fail_compilation/diag11727.d | 39 +++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 test/fail_compilation/diag11727.d diff --git a/src/expression.c b/src/expression.c index 9074557fbbce..ff3f31b7fb9d 100644 --- a/src/expression.c +++ b/src/expression.c @@ -2030,10 +2030,11 @@ void Expression::deprecation(const char *format, ...) } } -int Expression::rvalue() +int Expression::rvalue(bool allowVoid) { - if (type && type->toBasetype()->ty == Tvoid) - { error("expression %s is void and has no value", toChars()); + if (!allowVoid && type && type->toBasetype()->ty == Tvoid) + { + error("expression %s is void and has no value", toChars()); #if 0 dump(0); halt(); @@ -4931,7 +4932,7 @@ Expression *TypeExp::semantic(Scope *sc) return e; } -int TypeExp::rvalue() +int TypeExp::rvalue(bool allowVoid) { error("type %s has no value", toChars()); return 0; @@ -5095,7 +5096,7 @@ void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) buf->writestring(td->toChars()); } -int TemplateExp::rvalue() +int TemplateExp::rvalue(bool allowVoid) { error("template %s has no value", toChars()); return 0; diff --git a/src/expression.h b/src/expression.h index 6f421864a0f8..7c1794b07b44 100644 --- a/src/expression.h +++ b/src/expression.h @@ -136,7 +136,7 @@ class Expression : public RootObject void error(const char *format, ...); void warning(const char *format, ...); void deprecation(const char *format, ...); - virtual int rvalue(); + virtual int rvalue(bool allowVoid = false); static Expression *combine(Expression *e1, Expression *e2); static Expressions *arraySyntaxCopy(Expressions *exps); @@ -589,7 +589,7 @@ class TypeExp : public Expression TypeExp(Loc loc, Type *type); Expression *syntaxCopy(); Expression *semantic(Scope *sc); - int rvalue(); + int rvalue(bool allowVoid = false); void toCBuffer(OutBuffer *buf, HdrGenState *hgs); Expression *optimize(int result, bool keepLvalue = false); elem *toElem(IRState *irs); @@ -614,7 +614,7 @@ class TemplateExp : public Expression FuncDeclaration *fd; TemplateExp(Loc loc, TemplateDeclaration *td, FuncDeclaration *fd = NULL); - int rvalue(); + int rvalue(bool allowVoid = false); void toCBuffer(OutBuffer *buf, HdrGenState *hgs); int isLvalue(); Expression *toLvalue(Scope *sc, Expression *e); diff --git a/src/statement.c b/src/statement.c index 16207a294986..6a3848ed81f3 100644 --- a/src/statement.c +++ b/src/statement.c @@ -3726,6 +3726,8 @@ Statement *ReturnStatement::semantic(Scope *sc) exp = exp->inferType(fld->treq->nextOf()->nextOf()); exp = exp->semantic(sc); exp = resolveProperties(sc, exp); + if (!exp->rvalue(true)) // don't make error for void expression + exp = new ErrorExp(); if (exp->op == TOKcall) exp = valueNoDtor(exp); diff --git a/test/fail_compilation/diag11727.d b/test/fail_compilation/diag11727.d new file mode 100644 index 000000000000..341ecad043db --- /dev/null +++ b/test/fail_compilation/diag11727.d @@ -0,0 +1,39 @@ +/* +TEST_OUTPUT: +--- +fail_compilation/diag11727.d(10): Error: type n has no value +--- +*/ +auto returnEnum() +{ + enum n; + return n; +} +void main() +{ + assert(returnEnum() == 0); +} + +/* +TEST_OUTPUT: +--- +fail_compilation/diag11727.d(26): Error: type void has no value +--- +*/ +auto returnVoid() +{ + alias v = void; + return v; +} + +/* +TEST_OUTPUT: +--- +fail_compilation/diag11727.d(38): Error: template t() has no value +--- +*/ +auto returnTemplate() +{ + template t() {} + return t; +}