Skip to content

Commit

Permalink
Improve the message for empty_struct
Browse files Browse the repository at this point in the history
Change-Id: I17b1fb652234016bbdb94f7c813e24ea8890cd85
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/216120
Reviewed-by: Konstantin Shcheglov <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
  • Loading branch information
bwilkerson authored and [email protected] committed Oct 11, 2021
1 parent 50fb0c6 commit 12f7507
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
8 changes: 5 additions & 3 deletions pkg/analyzer/lib/src/dart/error/ffi_code.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ class FfiCode extends AnalyzerErrorCode {

/**
* Parameters:
* 0: the name of the struct class
* 0: the name of the subclass
* 1: the name of the superclass
*/
static const FfiCode EMPTY_STRUCT = FfiCode(
'EMPTY_STRUCT',
"Struct '{0}' is empty. Empty structs are undefined behavior.",
correctionMessage: "Try adding a field to '{0}' or use a different Struct.",
"The class '{0}' can’t be empty because it's a subclass of '{1}'.",
correctionMessage:
"Try adding a field to '{0}' or use a different superclass.",
);

/**
Expand Down
10 changes: 5 additions & 5 deletions pkg/analyzer/lib/src/generated/ffi_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
compound = node;
if (node.declaredElement!.isEmptyStruct) {
_errorReporter.reportErrorForNode(
FfiCode.EMPTY_STRUCT, node.name, [node.name.name]);
FfiCode.EMPTY_STRUCT, node.name, [node.name.name, className]);
}
if (className == _structClassName) {
_validatePackedAnnotation(node.metadata);
Expand Down Expand Up @@ -722,10 +722,10 @@ class FfiVerifier extends RecursiveAstVisitor<void> {
} else if (declaredType.isCompoundSubtype) {
final clazz = (declaredType as InterfaceType).element;
if (clazz.isEmptyStruct) {
// TODO(brianwilkerson) There are no tests for this branch. Ensure
// that the diagnostic is correct and add tests.
_errorReporter
.reportErrorForNode(FfiCode.EMPTY_STRUCT, node, [clazz.name]);
_errorReporter.reportErrorForNode(FfiCode.EMPTY_STRUCT, node, [
clazz.name,
clazz.supertype!.getDisplayString(withNullability: false)
]);
}
_validatePackingNesting(compound!.declaredElement!, clazz,
errorNode: fieldType);
Expand Down
7 changes: 4 additions & 3 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13746,11 +13746,12 @@ FfiCode:
correctionMessage: "Try allocating it via allocation, or load from a 'Pointer'."
comment: No parameters.
EMPTY_STRUCT:
problemMessage: "Struct '{0}' is empty. Empty structs are undefined behavior."
correctionMessage: "Try adding a field to '{0}' or use a different Struct."
problemMessage: "The class '{0}' can’t be empty because it's a subclass of '{1}'."
correctionMessage: "Try adding a field to '{0}' or use a different superclass."
comment: |-
Parameters:
0: the name of the struct class
0: the name of the subclass
1: the name of the superclass
EXTRA_ANNOTATION_ON_STRUCT_FIELD:
problemMessage: Fields in a struct class must have exactly one annotation indicating the native type.
correctionMessage: Try removing the extra annotation.
Expand Down

0 comments on commit 12f7507

Please sign in to comment.