Bug 32135 - C# 6 string interpolation and lambdas miscompilation
Summary: C# 6 string interpolation and lambdas miscompilation
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 4.0.0
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2015-07-20 06:08 UTC by nsf
Modified: 2015-07-20 13:33 UTC (History)
1 user (show)

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:

Description nsf 2015-07-20 06:08:32 UTC
Mono versions:
[nsf @ ~]$ mcs --version
Mono C# compiler version
[nsf @ ~]$ mono --version
Mono JIT compiler version 4.0.1 (tarball Wed Jun 10 09:11:17 UTC 2015)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen

Given this code:
using System;

class App {
        static Action a;

        static void Do(Action cb) {

        static void SetupOK() {
                int number = 0;
                Do(() => {
                        a = () => Console.WriteLine(string.Format("Number: {0}", ++number));

        static void SetupBAD() {
                int number = 0;
                Do(() => {
                        a = () => Console.WriteLine($"Number: {++number}");

        static void Main() {

SetupOK works fine, SetupBAD produces wrong code. I guess it has something to do with the way compiler captures variables in this nested lambda case with C# 6's string interpolation.

Miscompiled code dies with the following exception:

Unhandled Exception:
System.InvalidProgramException: Invalid IL code in App:<SetupBAD>m__1 (): IL_0005: ldloc.0
Comment 1 Marek Safar 2015-07-20 13:33:43 UTC
Fixed in master and Mono 4.2