{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/CodeContracts/issues/38","labels_url":"https://api.github.com/repos/Microsoft/CodeContracts/issues/38/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/CodeContracts/issues/38/comments","events_url":"https://api.github.com/repos/Microsoft/CodeContracts/issues/38/events","html_url":"https://github.com/Microsoft/CodeContracts/issues/38","id":73394373,"number":38,"title":"CCRewriter produces invalid IL on async method or iterator blocks","user":{"login":"SergeyTeplyakov","id":726448,"avatar_url":"https://avatars.githubusercontent.com/u/726448?v=3","gravatar_id":"","url":"https://api.github.com/users/SergeyTeplyakov","html_url":"https://github.com/SergeyTeplyakov","followers_url":"https://api.github.com/users/SergeyTeplyakov/followers","following_url":"https://api.github.com/users/SergeyTeplyakov/following{/other_user}","gists_url":"https://api.github.com/users/SergeyTeplyakov/gists{/gist_id}","starred_url":"https://api.github.com/users/SergeyTeplyakov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SergeyTeplyakov/subscriptions","organizations_url":"https://api.github.com/users/SergeyTeplyakov/orgs","repos_url":"https://api.github.com/users/SergeyTeplyakov/repos","events_url":"https://api.github.com/users/SergeyTeplyakov/events{/privacy}","received_events_url":"https://api.github.com/users/SergeyTeplyakov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-05-05T18:36:17Z","updated_at":"2015-05-20T17:21:24Z","closed_at":null,"body":"Code Contracts version: 1.7.11202.10 (reproduces on earlier versions as well).\r\nPerform Runtime Contract Checking != Full\r\nDebug/Release: both\r\n\r\nUsing CCRewrite with Perform Runtime Contract Checking not equals to Full with async method (or methods with iterator blocks) with complex `Contract.Requires` (that has `II` or `&&` in it) provides invalid assembly. Using such an assembly leads to `System.InvalidProgramExecution` exception and peverify shows following error:\r\n\r\n```\r\n[IL]: Error: [PATHToExecutable\\CcRewriterBug\\bin\\Release\\CCRewriterBug.exe:\r\nCcRewriterBug.Program+<FooEnumerable>d__3::MoveNext][offset 0x00000021] Stack underflow\r\n```\r\n\r\nSteps to reproduce:\r\n\r\n```c#\r\nstatic async Task FooAsync(string str)\r\n{\r\n   // Works fine with Contract.Requires(str != null);\r\n    Contract.Requires(str != null && str.Length > 1);\r\n    await Task.Delay(42);\r\n}\r\n```\r\nor\r\n\r\n```c#\r\nstatic IEnumerable<string> FooEnumerable(string str)\r\n{\r\n    Contract.Requires(str != null && str.Length > 1);\r\n    yield return str;\r\n}\r\n```\r\nIn latter case if Perform Runtime Contract Checking is not Full CCRewrite produces following IL in `MoveMethod`:\r\n\r\n```\r\n.method private final hidebysig newslot virtual \r\n\tinstance bool MoveNext () cil managed \r\n{\r\n\t.override method instance bool [mscorlib]System.Collections.IEnumerator::MoveNext()\r\n\t// Method begins at RVA 0x21ac\r\n\t// Code size 64 (0x40)\r\n\t.maxstack 2\r\n\t.locals init (\r\n\t\t[0] int32 CS$0$0000\r\n\t)\r\n\r\n\tIL_0000: ldarg.0\r\n\tIL_0001: ldfld int32 CcRewriterBug.Program/'<FooEnumerable>d__0'::'<>1__state'\r\n\tIL_0006: stloc.0\r\n\tIL_0007: ldloc.0\r\n\tIL_0008: switch (IL_001a, IL_0037)\r\n\r\n\tIL_0015: br IL_003e\r\n\r\n\tIL_001a: ldarg.0\r\n\tIL_001b: ldc.i4.m1\r\n\tIL_001c: stfld int32 CcRewriterBug.Program/'<FooEnumerable>d__0'::'<>1__state'\r\n\tIL_0021: pop // this instruction is invalid!\r\n\tIL_0022: ldarg.0\r\n\tIL_0023: ldarg.0\r\n\tIL_0024: ldfld string CcRewriterBug.Program/'<FooEnumerable>d__0'::str\r\n\tIL_0029: stfld string CcRewriterBug.Program/'<FooEnumerable>d__0'::'<>2__current'\r\n\tIL_002e: ldarg.0\r\n\tIL_002f: ldc.i4.1\r\n\tIL_0030: stfld int32 CcRewriterBug.Program/'<FooEnumerable>d__0'::'<>1__state'\r\n\tIL_0035: ldc.i4.1\r\n\tIL_0036: ret\r\n\r\n\tIL_0037: ldarg.0\r\n\tIL_0038: ldc.i4.m1\r\n\tIL_0039: stfld int32 CcRewriterBug.Program/'<FooEnumerable>d__0'::'<>1__state'\r\n\r\n\tIL_003e: ldc.i4.0\r\n\tIL_003f: ret\r\n} // end of method '<FooEnumerable>d__0'::MoveNext```\r\n"},"comment":{"url":"https://api.github.com/repos/Microsoft/CodeContracts/issues/comments/103966782","html_url":"https://github.com/Microsoft/CodeContracts/issues/38#issuecomment-103966782","issue_url":"https://api.github.com/repos/Microsoft/CodeContracts/issues/38","id":103966782,"user":{"login":"danielcweber","id":1899410,"avatar_url":"https://avatars.githubusercontent.com/u/1899410?v=3","gravatar_id":"","url":"https://api.github.com/users/danielcweber","html_url":"https://github.com/danielcweber","followers_url":"https://api.github.com/users/danielcweber/followers","following_url":"https://api.github.com/users/danielcweber/following{/other_user}","gists_url":"https://api.github.com/users/danielcweber/gists{/gist_id}","starred_url":"https://api.github.com/users/danielcweber/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danielcweber/subscriptions","organizations_url":"https://api.github.com/users/danielcweber/orgs","repos_url":"https://api.github.com/users/danielcweber/repos","events_url":"https://api.github.com/users/danielcweber/events{/privacy}","received_events_url":"https://api.github.com/users/danielcweber/received_events","type":"User","site_admin":false},"created_at":"2015-05-20T17:21:24Z","updated_at":"2015-05-20T17:21:24Z","body":"Can you clarify:You posted two examples, one for async, one for iterators. Does the pull request fix the async example for you ?"}}