Bug 24607 - Compiling enum with mcs 3.6.1 throws System.ArgumentException "Constant does not match the defined type."
Summary: Compiling enum with mcs 3.6.1 throws System.ArgumentException "Constant does ...
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.6.0
Hardware: PC Windows
: --- minor
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-11-18 08:06 UTC by Igor
Modified: 2016-02-09 18:39 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description Igor 2014-11-18 08:06:58 UTC
Compiling the code below throws System.ArgumentException "Constant does not match the defined type."
Compiling with Visual studio 2012, Mono.Csharp.dll retrieved via nuget:
https://www.nuget.org/packages/Mono.CSharp/3.6.1

  [TestMethod]
  public void Test()
  {
    var evaluator = new Mono.CSharp.Evaluator(new CompilerContext(new CompilerSettings(), new CompilingResult()));
    string input = @"
        using System;
        
        public class Foo {
          public string Bar(string s) { return s.ToUpper(); }
        }
        
        enum Gender {
          Male = 1,
          Female = 2
        }
    ";
    evaluator.Compile(input);
  }

If I remove Male and Female enum values, then it compiles ok.

Stack trace:
at System.Reflection.Emit.TypeBuilder.SetConstantValue(ModuleBuilder module, Int32 tk, Type destType, Object value)
at System.Reflection.Emit.FieldBuilder.SetConstant(Object defaultValue)
at Mono.CSharp.Const.Emit()
at Mono.CSharp.TypeDefinition.Emit()
at Mono.CSharp.TypeDefinition.EmitContainer()
at Mono.CSharp.TypeContainer.EmitContainer()
at Mono.CSharp.NamespaceContainer.EmitContainer()
at Mono.CSharp.TypeContainer.EmitContainer()
at Mono.CSharp.ModuleContainer.EmitContainer()
at Mono.CSharp.Evaluator.CompileBlock(Class host, Undo undo, Report Report)
at Mono.CSharp.Evaluator.Compile(String input, CompiledMethod& compiled)
at Mono.CSharp.Evaluator.Compile(String input)
Comment 1 Marek Safar 2014-11-18 11:08:28 UTC
Sadly, this is .net limitation/bug.

C# compiler uses TypeBuilder to define enums, it cannot use EnumBuilder because it does not support nesting.

TypeBuilder does not support SetConstant with unclosed type on .net though.
Comment 2 g.polzoni 2016-02-09 14:03:33 UTC
So it's not possible to define enum?
Comment 3 Marek Safar 2016-02-09 14:38:41 UTC
Not before .net limitations are fixed
Comment 4 Marek Safar 2016-02-09 14:40:00 UTC
Well, we could special case not nested enums but I am not sure it's worth it
Comment 5 g.polzoni 2016-02-09 18:39:20 UTC
Ok. Understood.