Issue 24869 - Qualifier dropped from return type when AliasSeq is indexed with template argument
Summary: Qualifier dropped from return type when AliasSeq is indexed with template arg...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-11-20 16:34 UTC by Paul Backus
Modified: 2024-11-21 18:16 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Paul Backus 2024-11-20 16:34:07 UTC
As of DMD 2.109.1, the following program fails to compile:

---
alias AliasSeq(Args...) = Args;

struct S
{
    int n;
    ref const(AliasSeq!(int)[i]) get(size_t i)() const => n;
}

void main()
{
    const S s;
    s.get!0;
}
---

The error message is:

---
bug.d(6): Error: expression `this.n` of type `const(int)` is not implicitly convertible to return type `ref int`
bug.d(12): Error: template instance `bug.S.get!0LU` error instantiating
---

As show in the error message, the compiler has erroneously determined the return type of `get!0` to be `int`, when it should instead be `const(int)`.

The same error also occurs with the immutable, inout, and shared qualifiers.
Comment 1 Nick Treleaven 2024-11-21 18:07:12 UTC
Reduced:

struct S
{
    int n;
    ref int get() const => n;
}

void main()
{
    const S s;
    s.get;
}
Comment 2 Nick Treleaven 2024-11-21 18:12:36 UTC
>     ref int get() const => n;

Sorry, you're right, ignore comment 1. That should be `ref const(int)`.

It's weird as the function prototype type is OK:

    ref const(AliasSeq!(int)[0]) g() const;
    pragma(msg, typeof(g)); // const ref const(int)()
Comment 3 Nick Treleaven 2024-11-21 18:16:09 UTC
OK, making the prototype a template again shows the wrong return type:

    ref const(AliasSeq!(int)[i]) get(size_t i)() const;
    pragma(msg, typeof(get!0)); // const ref int()