Featured post
c# - Mono + named/optional parameters = compiler bug? -
i have encountered unforeseen consequences of porting mono 2.8.1. problem can boiled down sample program (i have been unable reduce further, after cutting several classes , ~1000 lines of code file quoted below)
public class person { public person(int age, string name = null){} public person(double income, string name = null){} public person(double income, int age, string name = null){} } class program { static void main() { person p = new person(1.0, name: "john doe"); } }
compilation of above code mcs gives output:
test.cs(22,24): error cs0584: internal compiler error: internal error test.cs(22,20): error cs0266: cannot implicitly convert type `object' `namedparams.person'. explicit conversion exists (are missing cast?) compilation failed: 2 error(s), 0 warnings
removing use of optional/named parameter (i.e. calling new person(1.0, null, "john doe") or new person(1.0, null, name:"john doe"), or new person(1.0, "john doe") ) leads flawless compilation. also, under vs2010 file (and whole solution started with) compiles fine. casting removes error cs0266, not cs0584 -- no surprise there.
my question: me doing wrong, or mcs (i.e. bug in mcs obvious me -- else ,,internal error'' mean, perhaps it's ok such program won't compile), or maybe microsoft compiler in vs2010 should not let such code compile?
i bet it's mcs who's wrong (unable guess right constructor), perhaps it's otherwise , shouldn't know better?
ps. tried searching known bug in both google , novell's bugzilla, unable find relevant. again, may blind ;)
okay, here goes. crash indeed due third overload, person(double income, int age, string name = null)
. compiler sees trying pass less arguments listed in signature goes looking optional arguments. happily notices name
optional , assumes not passing argument. appending placeholder @ end of supplied arguments. next, goes reorder named arguments in list end in right position. means john doe
correctly @ last position name
, placeholder gets age
position. compiler tries fill in default values, shocked find placeholder @ location has no default value. thinks can not happen, since placeholder added optional argument , not optional anymore. not knowing do, throws exception.
the following patch seems fix issue (however may break else, no warranty):
--- mono-2.6.7.orig/mcs/mcs/ecore.cs 2009-10-02 12:51:12.000000000 +0200 +++ mono-2.6.7/mcs/mcs/ecore.cs 2010-12-21 02:26:44.000000000 +0100 @@ -3803,6 +3803,15 @@ int args_gap = math.abs (arg_count - param_count); if (optional_count != 0) { + // readjust optional arguments passed named arguments + (int = 0; < arguments.count; i++) { + namedargument na = arguments[i] namedargument; + if (na == null) + continue; + int index = pd.getparameterindexbyname (na.name.value); + if (pd.fixedparameters[index].hasdefaultvalue) + optional_count--; + } if (args_gap > optional_count) return int.maxvalue - 10000 + args_gap - optional_count;
- Get link
- X
- Other Apps
Comments
Post a Comment