-
Notifications
You must be signed in to change notification settings - Fork 214
Continue to label that is not for a loop #2586
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
Comments
😱 I still think we should just get rid of labels entirely. :) |
Please don't. |
Shouldn't this be reported as an SDK bug? |
Right, as specified it should be flagged as a compile-time error, because So we could report the error (and deal with the breakage), or we could generalize the spec to allow it. It is a tiny generalization, and if it is actually working already then it seems reasonable to generalize the spec. @munificent, @kallentu, @natebosch, @stereotype441, @lrhn, @leafpetersen, @jakemac53, WDYT? Presumably the first step would be to create a language test and verify that it is actually working. |
Does using for(int i = 0; i == 0; i++) //Useless loop, but the continue is illegal outside of it.
{
what: {
print("hello");
continue what;
print("dead code");
}
print("goodbye"); //Oddly enough this gets flagged as dead code, even though it runs fine.
} I have found label- |
In that case it's probably better to start reporting the compile-time error for |
I'd be vary about allowing Also, that's not what it currently does. As @Jetz72 says, it looks like a continue to any label between the loop header and the void main() {
// label:
{
for (var i = 0; i < 3; i++) {
print("a:$i");
label:
{
for (var j = 0; j < 2; j++) {
print("b:$i:$j");
// label:
continue label;
print("c:$i:$j");
}
}
print("d:$i");
}
print("e");
}
print("done");
} Uncommenting different Not going to try to guess why (who am I kidding, I'm guessing we desugar Also, the analyzer thinks the And yes, it's an implementation bug, so it should really be in the SDK repo. |
Why not? Do you have use cases for it? |
I use labels often enough that I don't want to lose them. If we remove Until that, if we accept that you can break out of a loop, it would be a design flaw if you couldn't break a loop anyway, just because the break is inside another breakable construct (say a switch, it doesn't even have to be nested loops). We could just remove labels from constructs other than loops and switches then. That should simplify things. Right? A cases for breaks, which is not just breaking from an outer loop (from #171): var particularElement;
found: {
for (var e in someList) {
if (predicate(e)) {
particularElement = e;
break found;
}
}
return;
}
useElement(particularElement); Sure, I could just inline everything after the Let's keep labels, they are there for a good reason. I'd be fine with disallowing labels on non-composite statements (maybe only on loops/switch/block-statements, not even try/catch), allowing |
(Still catching up on email after a week vacation) I'm in favor of removing support for As far as the question of whether we should remove label support entirely, maybe we should discuss that in a separate issue. But as long as we're discussing it here, here's my opinion. Labels are definitely an obscure feature, and I hardly ever use them (digging through my own code I could only find one example). If the language didn't already support them, I would definitely argue against adding them; our time and energy is better spent on other features. However, I don't really see a lot of benefit to removing them either. It would not simplify our implementations very much, and given how little they're used in pracice I don't think it would really ease cognitive burden for our customers very much either. But what it would do is frustrate a lot of existing customers by forcing them to go to one or two spots in their program that use labels, possibly in areas of their code they haven't touched in a while, and rewrite them in a non-trivial way, for no obvious benefit to themselves. I think it's important not to create that kind of customer frustration without a clear benefit. |
I don't know why I didn't see the usage and explanation of the break tag in Dart's tutorial. Is this a bug in the documentation? |
|
Typical Chicken and Egg scenario :p It's in part used rarely because folks don't know labels exist. |
Okay, I've looked at the official documentation a few times and didn't find any support for the break tag. I thought it was not supported, but seeing other people's code using it, I felt a contradiction. Although it's not a complicated syntax, I think the official website should be comprehensive. |
Is this valid code?
Here,
outer
labels the block, notfor (int i
. So, according to18.15 Continue
in the specification,continue outer;
should be a compile-time error? But this code runs :-)And the analyzer also does not report it as an error.
And I suspect that it might do something unexpected for flow analysis as well, possibly because of how the analyzer drives it.
https://github.com/dart-lang/sdk/blob/578b602deca0790f4e2bf25acaaa9571ed17192c/pkg/vm/lib/transformations/type_flow/utils.dart#L322-L330
The text was updated successfully, but these errors were encountered: