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

Constructor flow analysis doesn't understand noreturn #20811

Open
pbackus opened this issue Feb 2, 2025 · 3 comments
Open

Constructor flow analysis doesn't understand noreturn #20811

pbackus opened this issue Feb 2, 2025 · 3 comments

Comments

@pbackus
Copy link
Contributor

pbackus commented Feb 2, 2025

As of DMD 2.109.1, the following program fails to compile:

struct MustInit
{
    int n;
    @disable this();
    this(int n) { this.n = n; }
}

struct S
{
    MustInit member;
    this(bool b, int n, int m)
    {
        if (b == true) {
            member = MustInit(n);
            return;
        }
        if (b == false) {
            member = MustInit(m);
            return;
        }
        assert(0); // unreachable
    }
}

The error message is:

bug.d(11): Error: constructor bug.S.this field member must be initialized but skipped

Since the code path that skips the initialization of member contains assert(0), and will never return, this code should be allowed to compile.

@pbackus
Copy link
Contributor Author

pbackus commented Feb 2, 2025

See also #20054

@Herringway
Copy link
Contributor

It doesn't seem to understand noreturn in general here. A throw new Exception("Whoopsie daisy"); or a call to noreturn foo(); ought to succeed as well?

@pbackus
Copy link
Contributor Author

pbackus commented Feb 2, 2025

Yes, ideally any noreturn expression would work here, not just assert(0).

@pbackus pbackus changed the title Constructor flow analysis doesn't understand assert(0) Constructor flow analysis doesn't understand noreturn Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants