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

Forward reference error in recursive template #19241

Open
dlangBugzillaToGithub opened this issue Mar 19, 2017 · 2 comments
Open

Forward reference error in recursive template #19241

dlangBugzillaToGithub opened this issue Mar 19, 2017 · 2 comments

Comments

@dlangBugzillaToGithub
Copy link

Jack Applegame (@japplegame) reported this on 2017-03-19T09:56:25Z

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

CC List

Description

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;
}
@dlangBugzillaToGithub
Copy link
Author

slavo5150 commented on 2017-11-25T06:50:30Z

*** Issue 17230 has been marked as a duplicate of this issue. ***

@dlangBugzillaToGithub
Copy link
Author

simen.kjaras commented on 2017-12-27T10:00:52Z

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;
}

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