﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
2426	Improve error message for when-statement in function	Henrik Tidefelt	somebody	"A more informative error message than STATEMENT_GENERIC_FAILURE is needed in the case of a when-statement appearing in a function.

The following diff (from a MathCore perspective) describes a possible solution:
{{{
diff --git a/OMCLibrary/Code/Compiler/FrontEnd/InstSection.mo b/OMCLibrary/Code/Compiler/FrontEnd/InstSection.mo
index dfba61c..d0034f4 100644
--- a/OMCLibrary/Code/Compiler/FrontEnd/InstSection.mo
+++ b/OMCLibrary/Code/Compiler/FrontEnd/InstSection.mo
@@ -2550,6 +2550,14 @@ algorithm
       then
         (cache,{stmt});

+    // illegal when clause in function algorithm^M
+    case (cache,env,ih,pre,_,alg as SCode.ALG_WHEN_A(_, _, _),_,_,impl,_,_)^M
+      equation^M
+        true = ClassInf.isFunction(ci_state);^M
+        Error.addSourceMessage(Error.WHEN_IN_FUNCTION,{},SCode.getStatementInfo(alg));^M
+      then^M
+        fail();^M
+^M
     // assert(cond,msg)
     case (cache,env,ih,pre,_,SCode.ALG_NORETCALL(exp=Absyn.CALL(function_ = Absyn.CREF_IDENT(name = ""assert""),
           functionArgs = Absyn.FUNCTIONARGS(args = {cond,msg},argNames = {})), info = info),source,_,impl,_,_)
diff --git a/OMCLibrary/Code/Compiler/Util/Error.mo b/OMCLibrary/Code/Compiler/Util/Error.mo
index 02c3701..c00d9fa 100644
--- a/OMCLibrary/Code/Compiler/Util/Error.mo
+++ b/OMCLibrary/Code/Compiler/Util/Error.mo
@@ -616,6 +616,8 @@ public constant Message SEPARATE_COMPILATION_PACKAGE_FAILED = MESSAGE(536, SCRIP
   Util.gettext(""Failed to compile all functions in package %s.""));
 public constant Message INVALID_ARRAY_DIM_IN_SCALAR_OP = MESSAGE(537, TRANSLATION(), ERROR(),
   Util.gettext(""The operator scalar requires all dimension size to be 1, but the input has type %s.""));
+public constant Message WHEN_IN_FUNCTION = MESSAGE(538, TRANSLATION(), ERROR(),^M
+  Util.gettext(""Functions may not contain when-statements.""));^M


 public constant Message MATCH_SHADOWING = MESSAGE(5001, TRANSLATION(), ERROR(),
}}}
"	defect	closed	high	1.14.0	Frontend	trunk	fixed		
