Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong choice of generic mutable/const/immutable methods #19304

Open
dlangBugzillaToGithub opened this issue Aug 20, 2017 · 1 comment
Open

Wrong choice of generic mutable/const/immutable methods #19304

dlangBugzillaToGithub opened this issue Aug 20, 2017 · 1 comment

Comments

@dlangBugzillaToGithub
Copy link

Илья Ярошенко reported this on 2017-08-20T05:19:27Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=17766

CC List

  • ag0aep6g
  • ZombineDev

Description

static struct Foo()
    {
        char opIndex(Indexes...)(Indexes indexes) immutable
        {
            return 'i';
        }

        char opIndex()() const
        {
            return 'c';
        }

        char opIndex(Indexes...)(Indexes indexes)
        {
            return 'm';
        }
    }
    pragma(msg, Foo!()()[]);
    pragma(msg, (cast(const) Foo!()())[]);
    pragma(msg, (cast(immutable) Foo!()())[]);

Prints
'c'
'c'
'c'

But shuold be

'm'
'c'
'i'

Type qualifier should have priority.
@dlangBugzillaToGithub
Copy link
Author

alexandru.ermicioi commented on 2018-10-22T19:11:27Z

This is not the only case, here is another case of wrong selection:

----------------------
class Mquartz(T, Z) {
    Z pass(T component) const {
        "Mutable pass".writeln;
        return component.z;
    }
    const(Z) pass(const T component) const {
        "Const pass".writeln;
        return component.z;
    }
    const(Z) pass(immutable T component) const {
        "Immutable pass".writeln;
        return component.z;
    }
    const(Z) pass(inout T component) const {
        "Inout pass".writeln;
        return component.z;
    }
}

struct V {
    int z;
}

void main() {

    auto m = new Mquartz!(V, typeof(V.z));

    // V v = V(21);
    // writeln(m.pass(v));
    writeln(m.pass(V(20)));
    writeln(m.pass(const(V)(20)));
    writeln(m.pass(immutable(V)(20)));
    writeln(m.pass(inout(V)(20)));
}
-------------------------
Current logic will select only const version with const argument instead of selecting right ones:
-------------------------
Const pass
20
Const pass
20
Const pass
20
Const pass
20
-------------------------
The overload selection logic works as expected only if m is const, then response will as expected:
-------------------------
Mutable pass
20
Const pass
20
Immutable pass
20
Inout pass
20
-------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant