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

fix Issue 23586 - DMD forgets a variable was just declared #14747

Merged
merged 1 commit into from
Dec 28, 2022

Conversation

ibuclaw
Copy link
Member

@ibuclaw ibuclaw commented Dec 28, 2022

The grammar/relationship of labelled statements to case statements are:

LabeledStatement:
    Identifier :
    Identifier : Statement

Statement:
    EmptyStatement
    NonEmptyStatement
    ScopeBlockStatement

NonEmptyStatement:
    NonEmptyStatementNoCaseNoDefault
    CaseStatement
    CaseRangeStatement
    DefaultStatement

CaseStatement:
    case ArgumentList : ScopeStatementList(opt)

ScopeStatementList:
    StatementListNoCaseNoDefault

StatementListNoCaseNoDefault:
    StatementNoCaseNoDefault
    StatementNoCaseNoDefault StatementListNoCaseNoDefault

StatementNoCaseNoDefault:
    EmptyStatement
    NonEmptyStatementNoCaseNoDefault
    ScopeBlockStatement

So the code:

Bar:
case 0:
    auto y = 7;
    return y;

Should result in a tree that looks like (as per the grammar rules):

LabeledStatement (
    Identifier(Bar) : Statement (
        CaseStatement (
            ArgumentList(0) : ScopeStatementList(
                DeclarationStatement(auto y = 7),
                ReturnStatement(y),
            )
        )
    )
)

However, instead it's currently being parsed as:

LabeledStatement (
    Identifier(Bar) : Statement (
        CaseStatement (
            ArgumentList(0) : ScopeStatementList(
                DeclarationStatement(auto y = 7)
            )
        )
    )
)
ReturnStatement(y)

Because both case and default case statements check whether ParseStatementFlags.curlyScope is in effect to determine whether to only parse one statement, or a list of statements. This flag is removed when a labelled statement is being parsed - forward the flag on so that it will be correctly picked up.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @ibuclaw!

Bugzilla references

Auto-close Bugzilla Severity Description
23586 major DMD forgets a variable was just declared.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#14747"

@ibuclaw ibuclaw marked this pull request as draft December 28, 2022 00:57
@ibuclaw
Copy link
Member Author

ibuclaw commented Dec 28, 2022

Initial failures are due to Statement.hasCode returning true when it sees a LabelStatement.

However...

extern (C++) final class HasCode : StoppableVisitor
{
alias visit = typeof(super).visit;
public:
override void visit(Statement s)
{
stop = true;
}
override void visit(ExpStatement s)
{
if (s.exp !is null)
{
stop = s.exp.hasCode();
}
}
override void visit(CompoundStatement s)
{
}
override void visit(ScopeStatement s)
{
}
override void visit(ImportStatement s)
{
}
override void visit(CaseStatement s)
{
}
override void visit(DefaultStatement s)
{
}
}

If default and case statements are assumed to have no executable code, it seems odd that labelled statements aren't treated the same way - in both case and label statements, it's the enclosed statement that has code, not the case/label itself.

@ibuclaw ibuclaw marked this pull request as ready for review December 28, 2022 01:44
@thewilsonator
Copy link
Contributor

Should this go to stable?

@ibuclaw
Copy link
Member Author

ibuclaw commented Dec 28, 2022

Should this go to stable?

On the one hand, I'd prefer if this had a little more battle testing done, and it's not actually fixing any regression. On the other, it does seem to be a small localized fix.

@thewilsonator
Copy link
Contributor

Well your choice. Merge when you'r made your decision.

@dlang-bot dlang-bot merged commit 6b2c13a into dlang:master Dec 28, 2022
@ibuclaw ibuclaw deleted the issue23586 branch December 28, 2022 11:02
@ibuclaw
Copy link
Member Author

ibuclaw commented Dec 28, 2022

The next beta is scheduled to be released on the 1st anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants