www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21563] New: Make shadowing mixin template names an error

https://issues.dlang.org/show_bug.cgi?id=21563

          Issue ID: 21563
           Summary: Make shadowing mixin template names an error
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

If a mixin template only mixes in one name and that name is shadowed, this
should be an error. It is almost always unintended.

For mixin templates that declares multiple names, partial shadowing could be
the intended behavior, and as a corner case, full shadowing, too.
Meta-programming code would be complicated unnecessarily if it had to check
that not all members of a mixin template are shadowed. However, if the mixin
template contains one member (one name) only, shadowing is very likely to be
unintended.
Unless the mixin template name is a template parameter, the author of the mixin
statement has control over it.

Example code:

mixin template M()
{
    void f() { }
}

struct S
{
    int f(int x) { return x; }
    mixin M; // does nothing
}

void main()
{
    S s;
    s.f(); // error
}

The improvement would be:


mixin template M()
{
    void f() { }
}

struct S
{
    int f(int x) { return x; }
    mixin M; // error: only member `M().f` of `M()` shadowed.
}

The corrective action would be to give the mixin a name and alias the eponymous
member(s):

mixin template M()
{
    void f() { }
}

struct S
{
    int f(int x) { return x; }
    mixin M m;
    alias f = m.f;
}

--
Jan 20