locked
Lambda expression causes ccrewrite error RRS feed

  • Question

  • I have a scenario that gives the following error message when I attempt to build with runtime contract checking turned on:

    Object reference not set to an instance of an object.

    The error list pane in Visual Studio reports ccrewrite as the source of the error.  Here is the code to reproduce:

    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    
    namespace ConsoleApplication1
    {
      [ContractClass(typeof(IFooContract))]
      public interface IFoo
      {
        void Bar(IEnumerable<string> items);
      }
    
      [ContractClassFor(typeof(IFoo))]
      internal abstract class IFooContract : IFoo
      {
        public void Bar(IEnumerable<string> items)
        {
          Contract.Requires(items != null);
          Contract.Requires(Contract.ForAll(items, x => x != null));
        }
      }
    
      public class Foo : IFoo
      {
        public void Bar(IEnumerable<string> items)
        {
          items.Select(x => x.Length);
        }
      }
    
      class Program
      {
        static void Main(string[] args) { }
      }
    }

    The error only appears if the following line is present in the IFooContract class:

    Contract.Requires(Contract.ForAll(items, x => x != null));

    However, the error seems the be related to the lambda expression x => x.Length in the Foo class.  I can get the build to succeed without errors if I use a static method instead of a lambda expression, as follows:

      public class Foo : IFoo
      {
        public void Bar(IEnumerable<string> items)
        {
          items.Select(GetLength);
        }
    
        private static int GetLength(string item)
        {
          return item.Length;
        }
      }

    Is this a bug?

    I am using version 1.4.30707.2

     

    Tuesday, August 31, 2010 3:32 AM

Answers

  • Yes, most definitely a bug... I'm working on it. Sorry about the problem!
    Wednesday, September 1, 2010 2:53 AM

All replies

  • Yes, most definitely a bug... I'm working on it. Sorry about the problem!
    Wednesday, September 1, 2010 2:53 AM
  • Has this bug been fixed? I'm encountering similar issues using the latest build from devlabs (version 1.4.30903.0).
    Jimit Ndiaye
    Monday, September 27, 2010 8:40 AM
  • Not yet! Sorry about that.
    Monday, September 27, 2010 6:06 PM