Jump to page: 1 2
Thread overview
[Issue 3231] New: Function declared to return a type with its same name doesn't compile
Aug 06, 2009
Tim M
Aug 06, 2009
Tim M
Aug 07, 2009
BCS
Aug 07, 2009
Cristi Vlasceanu
Aug 07, 2009
BCS
Aug 08, 2009
Cristi Vlasceanu
Aug 08, 2009
Stewart Gordon
Aug 08, 2009
Cristi Vlasceanu
Aug 08, 2009
Stewart Gordon
Aug 08, 2009
Tim M
Aug 09, 2009
Stewart Gordon
Aug 09, 2009
BCS
Aug 09, 2009
BCS
August 06, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231

           Summary: Function declared to return a type with its same name
                    doesn't compile
           Product: D
           Version: 2.032
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: rejects-valid
          Severity: blocker
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: tim.matthews7@gmail.com


class Bar
{
}

class Foo
{
    Bar Bar(); //declare a func without the implementation
}

void main()
{
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231





--- Comment #1 from Tim M <tim.matthews7@gmail.com>  2009-08-06 16:37:53 PDT ---
The error reported:

test.d(11): Error: function test.Foo.Bar is used as a type

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 06, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231





--- Comment #2 from Tim M <tim.matthews7@gmail.com>  2009-08-06 16:52:53 PDT ---
Sorry I shouldn't have rushed this but it if I provide the implementation it makes no difference. Are we meant to be allowed a function with the same name as its return type or is this a bug?

class Bar
{
}

class Foo
{
    Bar Bar()
    {
        return null;
    }
}

void main()
{
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231


Jarrett Billingsley <jarrett.billingsley@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |jarrett.billingsley@gmail.c
                   |                            |om
         Resolution|                            |INVALID




--- Comment #3 from Jarrett Billingsley <jarrett.billingsley@gmail.com>  2009-08-06 17:22:42 PDT ---
Name lookup in D is simple: it starts with the scope in which the name is used, and moves outward until it finds a symbol of that name, regardless of how the symbol is used.  The function 'Bar' is inserted into Foo's namespace before its return type is resolved.  When the return type is resolved, it finds the function Bar() itself instead of the class Bar at module scope, and you get an error.

Marking this as invalid unless it can be proven otherwise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231


BCS <shro8822@vandals.uidaho.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |shro8822@vandals.uidaho.edu




--- Comment #4 from BCS <shro8822@vandals.uidaho.edu>  2009-08-06 17:53:44 PDT ---
I also think this is invalid.

As a work around:

class Bar
{
}

class Foo
{
    .Bar Bar()   // note '.' to start from global scope
    {
        return null;
    }
}

void main()
{
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231


Cristi Vlasceanu <cristian@zerobugs.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |cristian@zerobugs.org




--- Comment #5 from Cristi Vlasceanu <cristian@zerobugs.org>  2009-08-06 21:43:34 PDT ---
The bug was filed as a blocker for the .net implementation because there are cases like this System.Text.RegularExpressions that has a static Match Match(System.String, System.String) method.

I think that marking the bug as invalid because of a limitation in the implementation is ridiculous.

The fix (which I am applying to the my source tree at http://dnet.codeplex.com/) is very simple: continue the look up in enclosing scope.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231





--- Comment #6 from Jarrett Billingsley <jarrett.billingsley@gmail.com>  2009-08-06 22:22:05 PDT ---
(In reply to comment #5)
> The bug was filed as a blocker for the .net implementation because there are cases like this System.Text.RegularExpressions that has a static Match Match(System.String, System.String) method.
> 
> I think that marking the bug as invalid because of a limitation in the implementation is ridiculous.

It's not a limitation in the implementation, it's how the language is defined. Java - and by proxy, C#, since MS doesn't seem to have strayed too far from their "inspiration" - allows for multiple symbols of the same name to be distinguished based on how they're used.  D does not.  If you have a disagreement, it's with the spec, not the implementation.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 07, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231





--- Comment #7 from BCS <shro8822@vandals.uidaho.edu>  2009-08-07 09:58:05 PDT ---
(In reply to comment #5)
> The bug was filed as a blocker for the .net implementation because there are cases like this System.Text.RegularExpressions that has a static Match Match(System.String, System.String) method.
> 
> I think that marking the bug as invalid because of a limitation in the implementation is ridiculous.
> 
> The fix (which I am applying to the my source tree at http://dnet.codeplex.com/) is very simple: continue the look up in enclosing scope.

Short of cases like this (illegal in both C# and D):

    class Foo
    {
        class Bar { }
        Bar Bar() { return new Bar(); }
    }

There is no need to alter the compiler, just require fully qualified names. Heck, for imports of other CLR code via meta-data, that isn't even needed as IIRC assemblies alway use fully qualified names internally.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231





--- Comment #8 from Cristi Vlasceanu <cristian@zerobugs.org>  2009-08-07 21:28:56 PDT ---
@BCS
That case is indeed illegal, AND handled correctly by my fix.

You can try it out either by downloading the dnet compiler, or by applying the change to DMD, here's the full code:

Type *TypeIdentifier::semantic(Loc loc, Scope *sc)
{
    Type *t;
    Expression *e;
    Dsymbol *s;

    //printf("TypeIdentifier::semantic(%s)\n", toChars());
    resolve(loc, sc, &e, &t, &s);
    if (t)
    {
    //printf("\tit's a type %d, %s, %s\n", t->ty, t->toChars(), t->deco);

    if (t->ty == Ttypedef)
    {   TypeTypedef *tt = (TypeTypedef *)t;

        if (tt->sym->sem == 1)
        error(loc, "circular reference of typedef %s", tt->toChars());
    }
    t = t->addMod(mod);
    }
    else
    {
#ifdef DEBUG
    if (!global.gag)
        printf("1: ");
#endif
    if (s)
    {
#if TARGET_NET
            //http://d.puremagic.com/issues/show_bug.cgi?id=3231
        if (sc->enclosing)
             return semantic(loc, sc->enclosing);
#endif
        s->error(loc, "is used as a type");
        //halt();
    }
    else
        error(loc, "%s is used as a type", toChars());
    t = tvoid;
    }
    //t->print();
    return t;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3231


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com




--- Comment #9 from Stewart Gordon <smjg@iname.com>  2009-08-08 12:37:01 PDT ---
(In reply to comment #5)
> I think that marking the bug as invalid because of a limitation in the implementation is ridiculous.

AIUI this isn't a limitation in the implementation, but a characteristic of how D is designed.

> The fix (which I am applying to the my source tree at http://dnet.codeplex.com/) is very simple: continue the look up in enclosing scope.

Better beware of hijacking vulnerabilities.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2