Skip to content

Commit

Permalink
CQ. ErrorReporter, use atOffset() instead of reportErrorForOffset()
Browse files Browse the repository at this point in the history
Change-Id: I506f71d1e7aa95f1c62fca1746e4a86c78720336
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353782
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Keerti Parthasarathy <keertip@google.com>
  • Loading branch information
scheglov authored and pull[bot] committed Feb 22, 2024
1 parent d9ecb2c commit ef68d43
Show file tree
Hide file tree
Showing 34 changed files with 1,114 additions and 552 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ class CodeFragmentParser {
}
accessors.add(accessor);
} else {
errorReporter.reportErrorForOffset(TransformSetErrorCode.wrongToken,
token.offset + delta, token.length, ['.', token.kind.displayName]);
errorReporter.atOffset(
offset: token.offset + delta,
length: token.length,
errorCode: TransformSetErrorCode.wrongToken,
arguments: ['.', token.kind.displayName],
contextMessages: null,
data: null,
);
return null;
}
}
Expand All @@ -106,8 +112,14 @@ class CodeFragmentParser {
var expression = _parseLogicalAndExpression();
if (currentIndex < _tokens.length) {
var token = _tokens[currentIndex];
errorReporter.reportErrorForOffset(TransformSetErrorCode.unexpectedToken,
token.offset + delta, token.length, [token.kind.displayName]);
errorReporter.atOffset(
offset: token.offset + delta,
length: token.length,
errorCode: TransformSetErrorCode.unexpectedToken,
arguments: [token.kind.displayName],
contextMessages: null,
data: null,
);
return null;
}
return expression;
Expand Down Expand Up @@ -140,16 +152,25 @@ class CodeFragmentParser {
offset = last.offset;
length = last.length;
}
errorReporter.reportErrorForOffset(TransformSetErrorCode.missingToken,
offset + delta, length, [validKindsDisplayString()]);
errorReporter.atOffset(
offset: offset + delta,
length: length,
errorCode: TransformSetErrorCode.missingToken,
arguments: [validKindsDisplayString()],
contextMessages: null,
data: null,
);
return null;
}
if (!validKinds.contains(token.kind)) {
errorReporter.reportErrorForOffset(
TransformSetErrorCode.wrongToken,
token.offset + delta,
token.length,
[validKindsDisplayString(), token.kind.displayName]);
errorReporter.atOffset(
offset: token.offset + delta,
length: token.length,
errorCode: TransformSetErrorCode.wrongToken,
arguments: [validKindsDisplayString(), token.kind.displayName],
contextMessages: null,
data: null,
);
return null;
}
return token;
Expand Down Expand Up @@ -217,8 +238,14 @@ class CodeFragmentParser {
advance();
return TypeArgumentAccessor(argumentIndex);
} else {
errorReporter.reportErrorForOffset(TransformSetErrorCode.unknownAccessor,
token.offset + delta, token.length, [identifier]);
errorReporter.atOffset(
offset: token.offset + delta,
length: token.length,
errorCode: TransformSetErrorCode.unknownAccessor,
arguments: [identifier],
contextMessages: null,
data: null,
);
return null;
}
}
Expand Down Expand Up @@ -293,11 +320,14 @@ class CodeFragmentParser {
var variableName = token.lexeme;
var generator = variableScope.lookup(variableName);
if (generator == null) {
errorReporter.reportErrorForOffset(
TransformSetErrorCode.undefinedVariable,
token.offset + delta,
token.length,
[variableName]);
errorReporter.atOffset(
offset: token.offset + delta,
length: token.length,
errorCode: TransformSetErrorCode.undefinedVariable,
arguments: [variableName],
contextMessages: null,
data: null,
);
return null;
}
return VariableReference(generator);
Expand All @@ -323,8 +353,14 @@ class CodeFragmentParser {
offset = token.offset + delta;
length = token.length;
}
errorReporter.reportErrorForOffset(
TransformSetErrorCode.expectedPrimary, offset, length);
errorReporter.atOffset(
offset: offset,
length: length,
errorCode: TransformSetErrorCode.expectedPrimary,
arguments: null,
contextMessages: null,
data: null,
);
return null;
}
}
Expand Down Expand Up @@ -454,8 +490,14 @@ class _CodeFragmentScanner {

/// Report the presence of an invalid character at the given [offset].
Null _reportInvalidCharacter(int offset) {
errorReporter.reportErrorForOffset(TransformSetErrorCode.invalidCharacter,
offset + delta, 1, [content.substring(offset, offset + 1)]);
errorReporter.atOffset(
offset: offset + delta,
length: 1,
errorCode: TransformSetErrorCode.invalidCharacter,
arguments: [content.substring(offset, offset + 1)],
contextMessages: null,
data: null,
);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,29 @@ class TransformSetParser {
}
var endIndex = template.indexOf(_closeComponent, variableStart + 2);
if (endIndex < 0) {
errorReporter.reportErrorForOffset(
TransformSetErrorCode.missingTemplateEnd,
templateOffset + variableStart,
2);
errorReporter.atOffset(
offset: templateOffset + variableStart,
length: 2,
errorCode: TransformSetErrorCode.missingTemplateEnd,
arguments: null,
contextMessages: null,
data: null,
);
// Ignore the invalid component, treating it as if it extended to the
// end of the template.
return components;
} else {
var name = template.substring(variableStart + 2, endIndex).trim();
var generator = variableScope.lookup(name);
if (generator == null) {
errorReporter.reportErrorForOffset(
TransformSetErrorCode.undefinedVariable,
templateOffset + template.indexOf(name, variableStart),
name.length,
[name]);
errorReporter.atOffset(
offset: templateOffset + template.indexOf(name, variableStart),
length: name.length,
errorCode: TransformSetErrorCode.undefinedVariable,
arguments: [name],
contextMessages: null,
data: null,
);
// Ignore the invalid component.
} else {
components.add(TemplateVariable(generator));
Expand Down Expand Up @@ -254,8 +261,14 @@ class TransformSetParser {
var span = e.span;
var offset = span?.start.offset ?? 0;
var length = span?.length ?? 0;
errorReporter.reportErrorForOffset(
TransformSetErrorCode.yamlSyntaxError, offset, length, [e.message]);
errorReporter.atOffset(
offset: offset,
length: length,
errorCode: TransformSetErrorCode.yamlSyntaxError,
arguments: [e.message],
contextMessages: null,
data: null,
);
}
return null;
}
Expand All @@ -266,8 +279,14 @@ class TransformSetParser {
void _reportError(TransformSetErrorCode code, YamlNode node,
[List<String> arguments = const []]) {
var span = node.span;
errorReporter.reportErrorForOffset(
code, span.start.offset, span.length, arguments);
errorReporter.atOffset(
offset: span.start.offset,
length: span.length,
errorCode: code,
arguments: arguments,
contextMessages: null,
data: null,
);
}

/// Report that the value represented by the [node] does not have the
Expand Down
29 changes: 25 additions & 4 deletions pkg/analyzer/lib/error/listener.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,14 @@ class ErrorReporter {
// declaration. This might make it easier to be consistent.
if (constructor.name != null) {
var offset = constructor.returnType.offset;
reportErrorForOffset(
code, offset, constructor.name!.end - offset, arguments);
atOffset(
offset: offset,
length: constructor.name!.end - offset,
errorCode: code,
arguments: arguments,
contextMessages: null,
data: null,
);
} else {
atNode(
constructor.returnType,
Expand Down Expand Up @@ -212,6 +218,7 @@ class ErrorReporter {

/// Report an error with the given [errorCode] and [arguments]. The location
/// of the error is specified by the given [offset] and [length].
@Deprecated('Use atOffset() instead')
void reportErrorForOffset(
ErrorCode errorCode,
int offset,
Expand All @@ -234,7 +241,14 @@ class ErrorReporter {
/// of the error is specified by the given [span].
void reportErrorForSpan(ErrorCode errorCode, SourceSpan span,
[List<Object>? arguments]) {
reportErrorForOffset(errorCode, span.start.offset, span.length, arguments);
atOffset(
offset: span.start.offset,
length: span.length,
errorCode: errorCode,
arguments: arguments,
contextMessages: null,
data: null,
);
}

/// Report an error with the given [errorCode] and [arguments]. The [token] is
Expand Down Expand Up @@ -268,7 +282,14 @@ class ErrorReporter {
@Deprecated('Use reportErrorForNode(), it will convert types as well')
void reportTypeErrorForNode(
ErrorCode errorCode, AstNode node, List<Object> arguments) {
reportErrorForOffset(errorCode, node.offset, node.length, arguments);
atOffset(
offset: node.offset,
length: node.length,
errorCode: errorCode,
arguments: arguments,
contextMessages: null,
data: null,
);
}

/// Convert all [Element]s in the [arguments] into their display strings.
Expand Down
13 changes: 7 additions & 6 deletions pkg/analyzer/lib/src/dart/constant/constant_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,13 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
switch (result) {
case InvalidConstant():
if (!result.avoidReporting) {
_errorReporter.reportErrorForOffset(
result.errorCode,
result.offset,
result.length,
result.arguments,
result.contextMessages,
_errorReporter.atOffset(
offset: result.offset,
length: result.length,
errorCode: result.errorCode,
arguments: result.arguments,
contextMessages: result.contextMessages,
data: null,
);
}
case DartObjectImpl():
Expand Down
26 changes: 14 additions & 12 deletions pkg/analyzer/lib/src/dart/constant/evaluation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,13 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
Constant evaluateAndReportInvalidConstant(AstNode node) {
var result = evaluateConstant(node);
if (result case InvalidConstant(avoidReporting: false)) {
_errorReporter.reportErrorForOffset(
result.errorCode,
result.offset,
result.length,
result.arguments,
result.contextMessages,
_errorReporter.atOffset(
offset: result.offset,
length: result.length,
errorCode: result.errorCode,
arguments: result.arguments,
contextMessages: result.contextMessages,
data: null,
);
}
return result;
Expand Down Expand Up @@ -1946,12 +1947,13 @@ class ConstantVisitor extends UnifyingAstVisitor<Constant> {
// interaction with g3 more elegantly.
case InvalidConstant(isUnresolved: true):
if (!expressionValue.avoidReporting) {
_errorReporter.reportErrorForOffset(
expressionValue.errorCode,
expressionValue.offset,
expressionValue.length,
expressionValue.arguments,
expressionValue.contextMessages,
_errorReporter.atOffset(
offset: expressionValue.offset,
length: expressionValue.length,
errorCode: expressionValue.errorCode,
arguments: expressionValue.arguments,
contextMessages: expressionValue.contextMessages,
data: null,
);
}
return ConstantEvaluationEngine._unresolvedObject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ class BinaryExpressionResolver {
? WarningCode.UNNECESSARY_NULL_COMPARISON_FALSE
: WarningCode.UNNECESSARY_NULL_COMPARISON_TRUE;
var offset = start.offset;
_errorReporter.reportErrorForOffset(errorCode, offset, end.end - offset);
_errorReporter.atOffset(
offset: offset,
length: end.end - offset,
errorCode: errorCode,
arguments: null,
contextMessages: null,
data: null,
);
}

if (left is SimpleIdentifierImpl && right is NullLiteralImpl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ class ExtensionMemberResolver {
return extension.asResolutionResult;
},
(noneMoreSpecific) {
_errorReporter.reportErrorForOffset(
CompileTimeErrorCode.AMBIGUOUS_EXTENSION_MEMBER_ACCESS,
nameEntity.offset,
nameEntity.length,
[
_errorReporter.atOffset(
offset: nameEntity.offset,
length: nameEntity.length,
errorCode: CompileTimeErrorCode.AMBIGUOUS_EXTENSION_MEMBER_ACCESS,
arguments: [
name,
noneMoreSpecific.map((e) {
var name = e.extension.name;
Expand All @@ -121,6 +121,8 @@ class ExtensionMemberResolver {
return "unnamed extension on '$type'";
}).commaSeparatedWithAnd,
],
contextMessages: null,
data: null,
);
return ResolutionResult.ambiguous;
},
Expand Down
Loading

0 comments on commit ef68d43

Please sign in to comment.