www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5373] New: Regression (2.051) CTFE and std.string.replace() causes "Bad binary function q{a == b}..

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373

           Summary: Regression (2.051) CTFE and std.string.replace()
                    causes "Bad binary function q{a == b}..
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: soul8o8 gmail.com


--- Comment #0 from Heywood Floyd <soul8o8 gmail.com> 2010-12-24 21:31:04 PST
---
Using replace() in a mixin doesn't seem to work if the mixin is placed inside a
class. (I have no idea what's going on here.)

Isolated example:

// - - - - 8< - - - - - - - - - - - -

import std.stdio, std.string;
version(bug)
{
    class Foo
    {
        mixin(anint("a"));
        this()
        {
            a = 9;
        }
    }
}


void main()
{
    version(bug)
    {
        auto a = new Foo;
        writeln("a:", a.a);        
    }
    else //works
    {
        mixin(anint("a"));
        a = 9;
        writeln("a:",a);        
    }
}

string anint(string name)
{
    return replace("int $name;", "$name", name);
}

// - - - - 8< - - - - - - - - - - - -

# dmd -run testmixin.d
a:9
# dmd -version=bug -run testmixin.d
/Library/Compilers/dmd2/osx/bin/../../src/phobos/std/functional.d(177): Error:
static assert  "Bad binary function q{a == b}. You need to use a valid D
expression using symbols a of type dchar and b of type const(char)[]."
/Library/Compilers/dmd2/osx/bin/../../src/phobos/std/functional.d(180):       
instantiated from here: Body!(dchar,const(char)[])
/Library/Compilers/dmd2/osx/bin/../../src/phobos/std/algorithm.d(2149):       
instantiated from here: result!(dchar,const(char)[])
# _

// - - - - 8< - - - - - - - - - - - -

I'm pretty sure this worked in 2.050, or at least 2.049. (Some program I'm
working on started emitting these errors today. Didn't before. Not entirely
sure when I last built it. Sorry.)

BR
/HF

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 24 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373


Trass3r <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
                 CC|                            |mrmocool gmx.de
           Platform|Other                       |All
         OS/Version|Mac OS X                    |All


--- Comment #1 from Trass3r <mrmocool gmx.de> 2011-01-19 11:26:36 PST ---
Very interesting that you got code like that as well.
I also use replace in a mixin inside a class in cl4d.

This error was introduced in 2.051 I think.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373



--- Comment #2 from Trass3r <mrmocool gmx.de> 2011-01-19 12:14:40 PST ---
Ok I tested different revisions of dmd with phobos etc. of 2.050
Seems like r809 introduced it. Can anybody confirm?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373



--- Comment #3 from Trass3r <mrmocool gmx.de> 2011-01-19 12:42:42 PST ---
Yeah it was introduced with 2.051.
The release date in the changelog is wrong.
2.051 probably contains dmd r810 and the bug was introduced in r809.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373


Rob Jacques <sandford jhu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sandford jhu.edu


--- Comment #4 from Rob Jacques <sandford jhu.edu> 2011-02-03 14:58:36 PST ---
*** Issue 5406 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373



--- Comment #5 from Rob Jacques <sandford jhu.edu> 2011-02-03 15:00:16 PST ---
This is the patch from Issue 5406. It appear to solve both issue's test cases.

template binaryFunImpl(alias fun, string parm1Name, string parm2Name) {
    static if (is(typeof(fun) : string)) {
        enum testAsExpression = "{ ElementType1 "
            ~parm1Name~"; ElementType2 "
            ~parm2Name~"; return ("~fun~");}()";

        typeof(mixin(testAsExpression))
        result(ElementType1, ElementType2)(ElementType1 __a, ElementType2 __b)
            if(__traits(compiles, mixin(testAsExpression)))
        {
            mixin("alias __a "~parm1Name~";");
            mixin("alias __b "~parm2Name~";");
            mixin("return (" ~ fun ~ ");");
        }
    } else {
        alias fun result;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373



--- Comment #6 from Trass3r <mrmocool gmx.de> 2011-02-03 15:16:34 PST ---
Well the other bug report was purely about phobos.
The question is if this is just a workaround for an issue that actually needs
to be addressed in dmd.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373


Rob Jacques <sandford jhu.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|rejects-valid               |patch
           Priority|P2                          |P3
          Component|DMD                         |Phobos
           Severity|normal                      |regression


--- Comment #7 from Rob Jacques <sandford jhu.edu> 2011-02-03 16:38:01 PST ---
(In reply to comment #6)
 Well the other bug report was purely about phobos.
 The question is if this is just a workaround for an issue that actually needs
 to be addressed in dmd.
_Not_ a Phobos bug? *sigh* Here's the bug in all it's detail: //Reduction 1 import std.string; pragma(msg, replace(int.stringof,"int","real")); //Reduction 2 pragma(msg, indexOf("int","real") ); //Reduction 3 pragma(msg, is(typeof(startsWith!"a == b"("int","real"))) ); //Reduction 4 bool startsWith2(alias pred = "a == b", R, E) (R doesThisStart, E withThis) if (is(typeof(binaryFun!pred(doesThisStart.front, withThis)))) { return true; } pragma(msg, is(typeof(startsWith2!"a == b"("int","real"))) ); //Final Reduction pragma(msg, is(typeof(binaryFun!"a == b"("int".front, "real"))) ); So what's happening here is that DMD is trying to execute replace at compile-time. Replace calls indexOf, which calls startsWith. Now startsWith has three overload sets which differ by their template constraints. So DMD evaluates each constraint in turn. Which then causes in invalid binaryFun call, which triggers a static assert. And static assert stops compilation then and there. As this is per spec (AssignExpression is evaluated at compile time, and converted to a boolean value. If the value is true, the static assert is ignored. If the value is false, an error diagnostic is issued and the compile fails. Unlike AssertExpressions, StaticAsserts are always checked and evaluted by the compiler unless they appear in an unsatisfied conditional.) This isn't a regression in DMD; it's a regression in Phobos due to better conformance of DMD to the spec. Now, there does appear to be an inconsistency between static assert's behavior during CTFE vs Non-CTFE, but that's more of an issue with the Non-CTFE behavior vs the CTFE behavior. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 03 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373



--- Comment #8 from Trass3r <mrmocool gmx.de> 2011-02-21 08:16:16 PST ---
Well, as of 2.052 replace() has moved to std.array and can't be called at
compile-time anymore at all cause of Appender using pointers.

I've opened a new report: http://d.puremagic.com/issues/show_bug.cgi?id=5632

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 21 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373


Trass3r <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #9 from Trass3r <mrmocool gmx.de> 2011-07-12 05:21:13 PDT ---
I think we can close this one now.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 12 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373


Trass3r <mrmocool gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |


--- Comment #10 from Trass3r <mrmocool gmx.de> 2011-08-08 13:30:08 PDT ---
replace is broken again in 2.054.
I get the "Bad binary function q{a == b}" with my code but this example doesn't
even get so far:
Error: argument to mixin must be a string, not (['i','n','t',' ','a',';'])

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 08 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #11 from Don <clugdbug yahoo.com.au> 2011-09-01 04:51:58 PDT ---
(In reply to comment #10)
 replace is broken again in 2.054.
 I get the "Bad binary function q{a == b}" with my code but this example doesn't
 even get so far:
 Error: argument to mixin must be a string, not (['i','n','t',' ','a',';'])
That's bug 2156, which is completely different to this one. It has nothing to do with CTFE. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 01 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5373


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #12 from Walter Bright <bugzilla digitalmars.com> 2011-09-02
14:02:34 PDT ---
https://github.com/D-Programming-Language/dmd/commit/842db08516f0606ea9b278d179f8f7c51776f805

https://github.com/D-Programming-Language/dmd/commit/ff1965347d6ff2b0c3ebb71ff7c06254aba38c66

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