Skip to content

Commit

Permalink
Augment. Report AUGMENTATION_TYPE_PARAMETER_COUNT.
Browse files Browse the repository at this point in the history
Change-Id: Iafd5613c7ed87f9f9aff244f0852a663bb45d642
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/372562
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Jun 24, 2024
1 parent e159bfe commit 6d62515
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ CompileTimeErrorCode.AUGMENTATION_MODIFIER_MISSING:
status: needsFix
CompileTimeErrorCode.AUGMENTATION_OF_DIFFERENT_DECLARATION_KIND:
status: noFix
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT:
status: noFix
CompileTimeErrorCode.AUGMENTATION_WITHOUT_DECLARATION:
status: noFix
CompileTimeErrorCode.AUGMENTATION_WITHOUT_IMPORT:
Expand Down
45 changes: 45 additions & 0 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ class ClassElementImpl extends ClassOrMixinElementImpl
return augmentedInternal;
}

AugmentedClassElementImpl? get augmentedIfReally {
if (augmentationTarget != null) {
if (augmented case AugmentedClassElementImpl augmented) {
return augmented;
}
}
return null;
}

@override
set constructors(List<ConstructorElementImpl> constructors) {
assert(!isMixinApplication);
Expand Down Expand Up @@ -2734,6 +2743,15 @@ class EnumElementImpl extends InterfaceElementImpl
return augmentedInternal;
}

AugmentedEnumElementImpl? get augmentedIfReally {
if (augmentationTarget != null) {
if (augmented case AugmentedEnumElementImpl augmented) {
return augmented;
}
}
return null;
}

List<FieldElementImpl> get constants {
return fields.where((field) => field.isEnumConstant).toList();
}
Expand Down Expand Up @@ -2972,6 +2990,15 @@ class ExtensionElementImpl extends InstanceElementImpl
return augmentedInternal;
}

AugmentedExtensionElementImpl? get augmentedIfReally {
if (augmentationTarget != null) {
if (augmented case AugmentedExtensionElementImpl augmented) {
return augmented;
}
}
return null;
}

@override
List<Element> get children => [
...super.children,
Expand Down Expand Up @@ -3090,6 +3117,15 @@ class ExtensionTypeElementImpl extends InterfaceElementImpl
return augmentedInternal;
}

AugmentedExtensionTypeElementImpl? get augmentedIfReally {
if (augmentationTarget != null) {
if (augmented case AugmentedExtensionTypeElementImpl augmented) {
return augmented;
}
}
return null;
}

@override
ElementKind get kind {
return ElementKind.EXTENSION_TYPE;
Expand Down Expand Up @@ -5358,6 +5394,15 @@ class MixinElementImpl extends ClassOrMixinElementImpl
return augmentedInternal;
}

AugmentedMixinElementImpl? get augmentedIfReally {
if (augmentationTarget != null) {
if (augmented case AugmentedMixinElementImpl augmented) {
return augmented;
}
}
return null;
}

@override
bool get isBase {
return hasModifier(Modifier.BASE);
Expand Down
10 changes: 10 additions & 0 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
"Try changing the augmentation to match the declaration kind.",
);

static const CompileTimeErrorCode AUGMENTATION_TYPE_PARAMETER_COUNT =
CompileTimeErrorCode(
'AUGMENTATION_TYPE_PARAMETER_COUNT',
"The augmentation must have the same number of type parameters as the "
"declaration.",
correctionMessage:
"Try changing the augmentation to match the declaration type "
"parameters.",
);

static const CompileTimeErrorCode AUGMENTATION_WITHOUT_DECLARATION =
CompileTimeErrorCode(
'AUGMENTATION_WITHOUT_DECLARATION',
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.AUGMENTATION_MODIFIER_EXTRA,
CompileTimeErrorCode.AUGMENTATION_MODIFIER_MISSING,
CompileTimeErrorCode.AUGMENTATION_OF_DIFFERENT_DECLARATION_KIND,
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT,
CompileTimeErrorCode.AUGMENTATION_WITHOUT_DECLARATION,
CompileTimeErrorCode.AUGMENTATION_WITHOUT_IMPORT,
CompileTimeErrorCode.AUGMENTATION_WITHOUT_LIBRARY,
Expand Down
81 changes: 81 additions & 0 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,14 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
augmentationElement: element,
);

if (element.augmentedIfReally case var augmented?) {
_checkAugmentationTypeParameters(
nameToken: node.name,
typeParameterList: node.typeParameters,
declarationTypeParameters: augmented.declaration.typeParameters,
);
}

_checkClassAugmentationTargetAlreadyHasExtendsClause(
node: node,
augmentationTarget: element.augmentationTarget,
Expand Down Expand Up @@ -662,6 +670,14 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
element: element,
);

if (element.augmentedIfReally case var augmented?) {
_checkAugmentationTypeParameters(
nameToken: node.name,
typeParameterList: node.typeParameters,
declarationTypeParameters: augmented.declaration.typeParameters,
);
}

var augmented = element.augmented;
var declarationElement = augmented.declaration;
_enclosingClass = declarationElement;
Expand Down Expand Up @@ -738,6 +754,16 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
element: element,
);

if (element.augmentedIfReally case var augmented?) {
if (node.name case var nameToken?) {
_checkAugmentationTypeParameters(
nameToken: nameToken,
typeParameterList: node.typeParameters,
declarationTypeParameters: augmented.declaration.typeParameters,
);
}
}

_enclosingExtension = element;
_duplicateDefinitionVerifier.checkExtension(node);
_checkForConflictingExtensionTypeVariableErrorCodes();
Expand Down Expand Up @@ -771,6 +797,14 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
element: element,
);

if (element.augmentedIfReally case var augmented?) {
_checkAugmentationTypeParameters(
nameToken: node.name,
typeParameterList: node.typeParameters,
declarationTypeParameters: augmented.declaration.typeParameters,
);
}

_enclosingClass = declarationElement;

_checkForBuiltInIdentifierAsName(node.name,
Expand Down Expand Up @@ -1185,6 +1219,14 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
augmentationElement: element,
);

if (element.augmentedIfReally case var augmented?) {
_checkAugmentationTypeParameters(
nameToken: node.name,
typeParameterList: node.typeParameters,
declarationTypeParameters: augmented.declaration.typeParameters,
);
}

var augmented = element.augmented;
var declarationElement = augmented.declaration;
_enclosingClass = declarationElement;
Expand Down Expand Up @@ -1629,6 +1671,45 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
);
}

void _checkAugmentationTypeParameters({
required Token nameToken,
required TypeParameterList? typeParameterList,
required List<TypeParameterElement> declarationTypeParameters,
}) {
if (declarationTypeParameters.isEmpty) {
if (typeParameterList != null) {
errorReporter.atToken(
typeParameterList.leftBracket,
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT,
);
}
} else {
if (typeParameterList == null) {
errorReporter.atToken(
nameToken,
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT,
);
} else {
var declarationCount = declarationTypeParameters.length;
var typeParameters = typeParameterList.typeParameters;
switch (typeParameters.length.compareTo(declarationCount)) {
case < 0:
errorReporter.atToken(
typeParameterList.rightBracket,
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT,
);
case > 0:
errorReporter.atToken(
typeParameters[declarationCount].name,
CompileTimeErrorCode.AUGMENTATION_TYPE_PARAMETER_COUNT,
);
default:
// TODO(scheglov): check names and bounds
}
}
}
}

void _checkClassAugmentationModifiers({
required Token? augmentKeyword,
required ClassDeclarationImpl augmentationNode,
Expand Down
4 changes: 4 additions & 0 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,10 @@ CompileTimeErrorCode:
Parameters:
0: the name of the declaration kind.
1: the name of the augmentation kind.
AUGMENTATION_TYPE_PARAMETER_COUNT:
problemMessage: The augmentation must have the same number of type parameters as the declaration.
correctionMessage: Try changing the augmentation to match the declaration type parameters.
hasPublishedDocs: false
AUGMENTATION_WITHOUT_DECLARATION:
problemMessage: The declaration being augmented doesn't exist.
correctionMessage: Try changing the augmentation to match an existing declaration.
Expand Down
Loading

0 comments on commit 6d62515

Please sign in to comment.