Skip to content

@noreturn compiles because of noreturn #5375

Open
@dlangBugzillaToGithub

Description

@dlangBugzillaToGithub

Jonathan M Davis reported this on 2023-09-27T15:38:13Z

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

Description

This code compiles

string foo() @noreturn
{
    return null;
}

It's not noreturn, and it wasn't correctly marked as noreturn, since the spec says that noreturn is a return type rather than an attribute, but the definition of noreturn in object.d:

alias noreturn = typeof(*null);

makes it possible. I just ran into some code today which incorrectly used @noreturn, and it didn't catch what noreturn is supposed to catch (even though the person writing the code thought that that's what it was doing).

Strictly speaking, I'm not sure that it's a bug that the alias for noreturn makes it possible to use @noreturn, but it seems error-prone for folks who misremember (or mislearned) how noreturn is supposed to work - and as I understand it, there was discussion of implementing @noreturn before we ended up with noreturn, so it's not entirely surprising that someone would make this mistake.

So, I would argue that ideally, we would disallow @noreturn, though given how noreturn is defined, I don't know what a good way to do that would be. Perhaps typeof(*null) should just be disallowed for attributes, though I don't know if special-casing that is any different from special-casing @noreturn to block it. But maybe actual compiler people will have a better idea.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions