[Issue 12406] New: Broken delegate closure

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Mar 18 17:56:07 PDT 2014


https://d.puremagic.com/issues/show_bug.cgi?id=12406

           Summary: Broken delegate closure
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: lt.infiltrator at gmail.com


--- Comment #0 from Infiltrator <lt.infiltrator at gmail.com> 2014-03-18 17:56:05 PDT ---
In some ((apparently) random and confusing) cases, a delegate's closure is
broken, and seems to be influenced by completely unrelated pieces of code.

http://dpaste.dzfl.pl/5f9ebc61274a




------------------------------------------------------------------------
import std.functional;
import std.stdio;

class UserInterface {
   void title(string title) { _messages = title; } /* Changing this to
writeln(title) and removing display(), below, magically fixes it. */
   string display() {
      writeln(_messages);
      stdout.flush;
      return readln;
   }
   string _messages;
}

class Screen {
   void addMessageSource(string delegate()) { }
   void addOption(Screen delegate() action) {
      _options ~= Option(action);
   }
   Screen execute(UserInterface ui) {
      ui.title(title);
      auto action = _options[0].action;
      ui.display; /* Removing this line magically fixes it. */
      return action();
   }
   this(string title) { this.title = title; }
   struct Option { Screen delegate() action; }
   string title;
   Option[] _options;
}

string printStatus(int) { return "Printing status"; }

void configureScreen(Screen screen) {
   auto systemStatus = new Screen("System status");
   int x;
   systemStatus.addMessageSource(&curry!(printStatus, x));  /* Removing this
line magically fixes it.  */
   Screen renameName() { return new Screen("Rename system"); }
   Screen renameSelect() {
      auto ret = new Screen("Select system");
      void addItem() { ret.addOption(&renameName); }  /* Removing this line
magically fixes it. */
      ret.addOption(() => screen);  /* The segfault occurs here. */
      return ret;
   }
   screen.addOption(&renameSelect);
}
int main() {
   auto screen = new Screen("Systems screen");
   configureScreen(screen);
   while(screen) screen = screen.execute(new UserInterface);
   return 0;
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list