Skip to content

Commit

Permalink
Improve diagnostics with anonymous jump targets
Browse files Browse the repository at this point in the history
Change-Id: Ia904bc8d865c7a3302d4428d3dbf3660a7570f9e
Reviewed-on: https://dart-review.googlesource.com/76605
Reviewed-by: Jens Johansen <jensj@google.com>
  • Loading branch information
peter-ahe-google authored and commit-bot@chromium.org committed Sep 26, 2018
1 parent c38eab4 commit 48d1b5e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 12 deletions.
20 changes: 20 additions & 0 deletions pkg/front_end/lib/src/fasta/fasta_codes_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ Message _withArgumentsAmbiguousSupertypes(
arguments: {'name': name, 'type': _type, 'type2': _type2});
}

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeAnonymousBreakTargetOutsideFunction =
messageAnonymousBreakTargetOutsideFunction;

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const MessageCode messageAnonymousBreakTargetOutsideFunction =
const MessageCode("AnonymousBreakTargetOutsideFunction",
analyzerCode: "LABEL_IN_OUTER_SCOPE",
message: r"""Can't break to a target in a different function.""");

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Code<Null> codeAnonymousContinueTargetOutsideFunction =
messageAnonymousContinueTargetOutsideFunction;

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const MessageCode messageAnonymousContinueTargetOutsideFunction =
const MessageCode("AnonymousContinueTargetOutsideFunction",
analyzerCode: "LABEL_IN_OUTER_SCOPE",
message: r"""Can't continue at a target in a different function.""");

// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
const Template<
Message Function(
Expand Down
33 changes: 23 additions & 10 deletions pkg/front_end/lib/src/fasta/kernel/body_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3892,11 +3892,7 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
labelToken.charOffset,
length: labelToken.length));
} else if (target.functionNestingLevel != functionNestingLevel) {
Token labelToken = breakKeyword.next;
push(problemInLoopOrSwitch = buildProblemStatement(
fasta.templateBreakTargetOutsideFunction.withArguments(name),
labelToken.charOffset,
length: labelToken.length));
push(buildProblemTargetOutsideLocalFunction(name, breakKeyword));
} else {
Statement statement =
forest.breakStatement(breakKeyword, identifier, endToken);
Expand All @@ -3905,6 +3901,27 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
}
}

Statement buildProblemTargetOutsideLocalFunction(String name, Token keyword) {
Statement problem;
bool isBreak = optional("break", keyword);
if (name != null) {
Template<Message Function(String)> template = isBreak
? fasta.templateBreakTargetOutsideFunction
: fasta.templateContinueTargetOutsideFunction;
problem = buildProblemStatement(
template.withArguments(name), offsetForToken(keyword),
length: lengthOfSpan(keyword, keyword.next));
} else {
Message message = isBreak
? fasta.messageAnonymousBreakTargetOutsideFunction
: fasta.messageAnonymousContinueTargetOutsideFunction;
problem = buildProblemStatement(message, offsetForToken(keyword),
length: lengthForToken(keyword));
}
problemInLoopOrSwitch ??= problem;
return problem;
}

@override
void handleContinueStatement(
bool hasTarget, Token continueKeyword, Token endToken) {
Expand Down Expand Up @@ -3954,11 +3971,7 @@ abstract class BodyBuilder extends ScopeListener<JumpTarget>
labelToken.charOffset,
length: labelToken.length));
} else if (target.functionNestingLevel != functionNestingLevel) {
Token labelToken = continueKeyword.next;
push(problemInLoopOrSwitch = buildProblemStatement(
fasta.templateContinueTargetOutsideFunction.withArguments(name),
labelToken.charOffset,
length: labelToken.length));
push(buildProblemTargetOutsideLocalFunction(name, continueKeyword));
} else {
Statement statement =
forest.continueStatement(continueKeyword, identifier, endToken);
Expand Down
4 changes: 2 additions & 2 deletions pkg/front_end/messages.status
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ AccessError/analyzerCode: Fail
AccessError/example: Fail
AmbiguousSupertypes/example: Fail
AnnotationOnEnumConstant/example: Fail
AnonymousBreakTargetOutsideFunction/statement: Fail # Duplicated error as parser also complains.
AnonymousContinueTargetOutsideFunction/statement: Fail # Duplicated error as parser also complains.
ArgumentTypeNotAssignable/example: Fail
AssertAsExpression/analyzerCode: Fail
AssertAsExpression/example: Fail
Expand All @@ -19,7 +21,6 @@ AssertExtraneousArgument/example: Fail
AsyncAsIdentifier/example: Fail
AwaitAsIdentifier/example: Fail
AwaitNotAsync/example: Fail
BreakTargetOutsideFunction/example: Fail
BuiltInIdentifierAsType/example: Fail
BuiltInIdentifierInDeclaration/example: Fail
CannotAssignToParenthesizedExpression/example: Fail
Expand Down Expand Up @@ -78,7 +79,6 @@ ConstructorNotFound/example: Fail
ConstructorNotSync/example: Fail
ContinueLabelNotTarget/example: Fail
ContinueOutsideOfLoop/script1: Fail
ContinueTargetOutsideFunction/example: Fail
ContinueWithoutLabelInCase/script1: Fail
CouldNotParseUri/analyzerCode: Fail
CouldNotParseUri/example: Fail
Expand Down
36 changes: 36 additions & 0 deletions pkg/front_end/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,24 @@ InvalidBreakTarget:
BreakTargetOutsideFunction:
template: "Can't break to '#name' in a different function."
analyzerCode: LABEL_IN_OUTER_SCOPE
statement: |
label: while (true) {
void f() {
while (true) {
break label;
}
}
}
AnonymousBreakTargetOutsideFunction:
template: "Can't break to a target in a different function."
analyzerCode: LABEL_IN_OUTER_SCOPE
statement: |
while (true) {
void f() {
break;
}
}
ContinueOutsideOfLoop:
index: 2
Expand All @@ -773,6 +791,24 @@ InvalidContinueTarget:
ContinueTargetOutsideFunction:
template: "Can't continue at '#name' in a different function."
analyzerCode: LABEL_IN_OUTER_SCOPE
statement: |
label: while (true) {
void f() {
while (true) {
continue label;
}
}
}
AnonymousContinueTargetOutsideFunction:
template: "Can't continue at a target in a different function."
analyzerCode: LABEL_IN_OUTER_SCOPE
statement: |
while (true) {
void f() {
continue;
}
}
ContinueLabelNotTarget:
template: "Target of continue must be a label."
Expand Down

0 comments on commit 48d1b5e

Please sign in to comment.