Skip to content

Issue 3144 - Invalid break accepted #660

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

Closed
wants to merge 1 commit into from

Conversation

yebblies
Copy link
Member

Force curly braces on switch statement's body.

It also disables the technically allowed but absolutely useless version:

switch(exp)
    default:
       statement;

I don't see any reason to allow this.

http://d.puremagic.com/issues/show_bug.cgi?id=3144

Force curly braces on switch statement's body.
@alexrp
Copy link
Member

alexrp commented Jul 8, 2012

For what it's worth, looks good to me.

@andralex
Copy link
Member

OK with me. I had mixed feelings about the language change but then I remembered each label introduces its own scope. So having a scope with the whole switch statement seems like a reasonable special case.

@yebblies
Copy link
Member Author

@andralex This change is syntax only, switch statement semantics are unaffected.

@nazriel
Copy link
Contributor

nazriel commented Oct 6, 2012

LGTM 👍

@9rnsr @dawgfoto what do you think folks?

@JakobOvrum
Copy link
Member

Will this break useful idioms like the following?

enum Foo { a, b }

Foo foo;
final switch(foo) with(Foo)
{
    case a:
        ...
        break;
    case b:
        ...
        break;
}

I know we could switch the statements so that with() is first here, but, a) LOTS of code already does it this way, and b) I think this way reads better.

@yebblies
Copy link
Member Author

yebblies commented Oct 6, 2012

@JakobOvrum Yes it will break that, and I agree it shouldn't.

@braddr
Copy link
Member

braddr commented Oct 6, 2012

Also, any spec updates to go with this pull request?

@yebblies
Copy link
Member Author

yebblies commented Oct 6, 2012

@braddr No, I'm going to take that part out.

@9rnsr
Copy link
Contributor

9rnsr commented Oct 13, 2012

I also sometimes use switch(foo) with(Foo) idiom, and it is one of valid use I think. Therefore, the parser change is not correct to fix issue 3144.

The right way to fix it is: If a switch statement has zero or one cases (CaseStatement or DefaultStatement, excluding CaseRangeStatement), it will be an error in semantic analysis.

@yebblies
Copy link
Member Author

I also sometimes use switch(foo) with(Foo) idiom, and it is one of valid use I think. Therefore, the parser change is not correct to fix issue 3144.

Yes, I've come to agree.

The right way to fix it is: If a switch statement has zero or one cases (CaseStatement or DefaultStatement, excluding CaseRangeStatement), it will be an error in semantic analysis.

The bug I found 9 months ago was that dmd keeps searching for a closing } even if the switch block didn't start with a {. It will be fairly easy to fix this while preserving with switch(foo) with(Foo) idiom.

I'm fairly sure 'zero cases' is already an error, but 'one case' is perfectly valid.

@9rnsr
Copy link
Contributor

9rnsr commented Oct 13, 2012

The bug I found 9 months ago was that dmd keeps searching for a closing } even if the switch block didn't start with a {.

OK, just now I have understand the root cause of the bug.
Will you update this pull request based on the discussion?

I'm fairly sure 'zero cases' is already an error, but 'one case' is perfectly valid.

That's correct. I'd like to withdraw the point in my argument.

@9rnsr
Copy link
Contributor

9rnsr commented Oct 14, 2012

I've opened another pull request #1182, which doesn't rejectswitch(cond) with(exp) { ... } idiom.

@yebblies yebblies closed this Oct 21, 2012
braddr pushed a commit to braddr/dmd that referenced this pull request Oct 22, 2012
@yebblies yebblies deleted the issue3144 branch November 22, 2013 08:39
@yebblies
Copy link
Member Author

Heh, turns out switch(cond) with(exp) { ... } is actually a bad idea, because when used with a variable, the 'with' initialization is skipped, resulting in crashes.

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

Successfully merging this pull request may close these issues.

7 participants