From 5930cd6d55636935899408d56aea50860bf77a84 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Tue, 8 Aug 2023 20:23:38 +0000 Subject: [PATCH] Extension types. Report TYPE_PARAMETER_SUPERTYPE_OF_ITS_BOUND when via extension type. Change-Id: I79d77e251fc4107e8d8496f9b4baea4b8a2b8bf0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/319200 Reviewed-by: Brian Wilkerson Commit-Queue: Konstantin Shcheglov --- .../lib/src/dart/element/extensions.dart | 13 +++++++++++ .../lib/src/generated/error_verifier.dart | 9 +++++--- ...parameter_supertype_of_its_bound_test.dart | 23 ++++++++++++++++++- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/element/extensions.dart b/pkg/analyzer/lib/src/dart/element/extensions.dart index 795163b6655..5992a08094f 100644 --- a/pkg/analyzer/lib/src/dart/element/extensions.dart +++ b/pkg/analyzer/lib/src/dart/element/extensions.dart @@ -5,10 +5,23 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/src/dart/element/element.dart'; +import 'package:analyzer/src/dart/element/type.dart'; import 'package:analyzer/src/generated/utilities_dart.dart'; import 'package:collection/collection.dart'; import 'package:meta/meta_meta.dart'; +extension DartTypeExtension on DartType { + /// If `this` is an [InterfaceType] that is an instantiation of an extension + /// type, returns its representation type erasure. Otherwise, returns self. + DartType get representationTypeErasureOrSelf { + final self = this; + if (self is InterfaceTypeImpl) { + return self.representationTypeErasure ?? self; + } + return self; + } +} + extension ElementAnnotationExtensions on ElementAnnotation { static final Map _targetKindsByName = { for (final kind in TargetKind.values) kind.name: kind, diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index f11b6b84010..27f3e8f1f39 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -20,6 +20,7 @@ import 'package:analyzer/src/dart/ast/ast.dart'; import 'package:analyzer/src/dart/ast/extensions.dart'; import 'package:analyzer/src/dart/element/class_hierarchy.dart'; import 'package:analyzer/src/dart/element/element.dart'; +import 'package:analyzer/src/dart/element/extensions.dart'; import 'package:analyzer/src/dart/element/inheritance_manager3.dart'; import 'package:analyzer/src/dart/element/non_covariant_type_parameter_position.dart'; import 'package:analyzer/src/dart/element/type.dart'; @@ -4763,9 +4764,11 @@ class ErrorVerifier extends RecursiveAstVisitor TypeParameter? current = parameter; for (var step = 0; current != null; step++) { - var bound = current.bound; - if (bound is NamedType) { - current = elementToNode[bound.element]; + final boundNode = current.bound; + if (boundNode is NamedType) { + var boundType = boundNode.typeOrThrow; + boundType = boundType.representationTypeErasureOrSelf; + current = elementToNode[boundType.element]; } else { current = null; } diff --git a/pkg/analyzer/test/src/diagnostics/type_parameter_supertype_of_its_bound_test.dart b/pkg/analyzer/test/src/diagnostics/type_parameter_supertype_of_its_bound_test.dart index fdfa18277cc..0d6a1e90be9 100644 --- a/pkg/analyzer/test/src/diagnostics/type_parameter_supertype_of_its_bound_test.dart +++ b/pkg/analyzer/test/src/diagnostics/type_parameter_supertype_of_its_bound_test.dart @@ -17,7 +17,28 @@ main() { @reflectiveTest class TypeParameterSupertypeOfItsBoundTest extends PubPackageResolutionTest - with TypeParameterSupertypeOfItsBoundTestCases {} + with TypeParameterSupertypeOfItsBoundTestCases { + test_1of1_viaExtensionType() async { + await assertErrorsInCode(r''' +extension type A(T it) {} + +class B> {} +''', [ + error(CompileTimeErrorCode.TYPE_PARAMETER_SUPERTYPE_OF_ITS_BOUND, 38, 1), + ]); + } + + test_2of2_viaExtensionType() async { + await assertErrorsInCode(r''' +extension type A(T it) {} + +class B, T2 extends T1> {} +''', [ + error(CompileTimeErrorCode.TYPE_PARAMETER_SUPERTYPE_OF_ITS_BOUND, 38, 2), + error(CompileTimeErrorCode.TYPE_PARAMETER_SUPERTYPE_OF_ITS_BOUND, 56, 2), + ]); + } +} mixin TypeParameterSupertypeOfItsBoundTestCases on PubPackageResolutionTest { test_1of1() async {