D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17267 - Forward reference error in recursive template
Summary: Forward reference error in recursive template
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords:
: 17230 (view as issue list)
Depends on:
Blocks:
 
Reported: 2017-03-19 09:56 UTC by Jack Applegame
Modified: 2024-12-13 18:51 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jack Applegame 2017-03-19 09:56:25 UTC
This code compiles:
struct A(T) {
    T* ptr;
    void m() {
        pragma(msg, T.sizeof);
    }
}

struct B {
    int i;
    A!B a;
}

Following should compile, but doesn't (struct foo.B no size because of forward reference):

// example 1 (struct definition inside function)
struct A(T) {
    T* ptr;
    void m() {
        pragma(msg, T.sizeof);
    }
}

void foo() {
    static struct B {
        int i;
        A!B a;
    }
}

// example 2 (sizeof outside member function)
struct A(T) {
    T* ptr;
    pragma(msg, T.sizeof);
}

struct B {
    int i;
    A!B a;
}
Comment 1 Mike Franklin 2017-11-25 06:50:30 UTC
*** Issue 17230 has been marked as a duplicate of this issue. ***
Comment 2 Simen Kjaeraas 2017-12-27 10:00:52 UTC
Another, possibly simplified example:

// Mixed templates
struct S1 {
    S2!()* arr;
}
struct S2()
{
    // struct S1 no size because of forward reference
    private byte[S1.sizeof] payload;
}

It seems to be caused by some ordering issues in DMD, where it tries to calculate the size of S1, notices that it depends on a templated type, and just up and dies.

Note also that these variants compile:

// No templates
struct S3 {
    S4* arr;
}
struct S4 {
    private byte[S3.sizeof] payload;
}

// All templates
struct S5() {
    S6!()* arr;
}
struct S6() {
    private byte[S5!().sizeof] payload;
}
S6!() dummy; // Force instantiation

// Mixed templates, revisited
struct S7() {
    S8* arr;
}
struct S8
{
    private byte[S7!().sizeof] payload;
}
Comment 3 dlangBugzillaToGithub 2024-12-13 18:51:52 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19241

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB