6

I've been troubleshooting with this error for hours and I can't seem to understand why this happens. Consider the following code:

using System;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;

namespace Contracts
{
    class Data
    {
        public object TestData1 { get; set; }
        public object TestData2 { get; set; }
    }

    class Program
    {
        static void Main()
        {
            Data d = new Data();
            Method(d);
        }

        static void Method(Data d)
        {
            Contract.Requires(Methods.TestMethod1("test"));
            Contract.Requires(Methods.TestMethod2("test1", "test2"));
            Contract.Requires(Methods.TestMethod3(d, x => x.TestData1));
            Contract.Requires(Methods.TestMethod4(d, x => x.TestData1, x => x.TestData2));
        }
    }

    static class Methods
    {
        [Pure]
        public static bool TestMethod1(string str) { return true; }

        [Pure]
        public static bool TestMethod2(params string[] strs) { return true; }

        [Pure]
        public static bool TestMethod3<T>(T obj, Expression<Func<T, object>> exp) { return true; }

        [Pure]
        public static bool TestMethod4<T>(T obj, params Expression<Func<T, object>>[] exps) { return true; }
    }
}

When I compile the project, the line "Contract.Requires(Methods.TestMethod4(d, x => x.TestData1, x => x.TestData2));" causes the following compilation error:

Malformed contract. Found Requires after assignment in method 'Contracts.Program.Method(Contracts.Data)'.

How come "Contract.Requires(Methods.TestMethod2("test1", "test2"));" doesn't cause an error but "Contract.Requires(Methods.TestMethod4(d, x => x.TestData1, x => x.TestData2));" does?

Please help! :(

Marco
  • 5,555
  • 2
  • 17
  • 23
  • I would consider this a bug. From the looks of it, it does not handle the code generated for `Expression` very well. Perhaps report on MS connect. – leppie Jun 30 '11 at 16:32
  • But make sure you have the right (error) line. What happens when you remove that last Requires? – H H Jul 01 '11 at 18:22
  • The program compiles without any problems. – Marco Jul 01 '11 at 21:46
  • 1
    I posted the problem on the MSDN forums and [they think it's a bug too](http://social.msdn.microsoft.com/Forums/en/codecontracts/thread/24a4730f-c497-40b3-a5d7-8776711320d2?prof=required). – Marco Jul 01 '11 at 21:48
  • @Marco - I would add your comment as an answer and mark it answered. – Charles Lambert Aug 03 '11 at 19:44

1 Answers1

2

I posted the problem on the MSDN forums and they think it's a bug too.

Marco
  • 5,555
  • 2
  • 17
  • 23