Skip to content

Commit 11d24d6

Browse files
scheglovCommit Queue
authored andcommitted
Elements. TypeParameterElementImpl2.name3 is nullable.
Change-Id: I3a62ddeae72c0c830855a139ac0d020eb0c01380 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403909 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent f91050c commit 11d24d6

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10851,7 +10851,7 @@ class TypeParameterElementImpl extends ElementImpl
1085110851
// chain will have their `_element` set to the newly created element.
1085210852
return TypeParameterElementImpl2(
1085310853
firstFragment: firstFragment,
10854-
name3: firstFragment.name,
10854+
name3: firstFragment.name.nullIfEmpty,
1085510855
bound: firstFragment.bound,
1085610856
);
1085710857
}
@@ -10971,7 +10971,7 @@ class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1097110971
final TypeParameterElementImpl firstFragment;
1097210972

1097310973
@override
10974-
final String name3;
10974+
final String? name3;
1097510975

1097610976
@override
1097710977
DartType? bound;

pkg/analyzer/lib/src/dart/element/generic_inferrer.dart

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,14 @@ class GenericInferrer {
237237

238238
// If everything else succeeded, check the `extends` constraint.
239239
if (success) {
240+
var name = parameter.name3;
240241
var parameterBoundRaw = parameter.bound;
241-
if (parameterBoundRaw != null) {
242+
if (name != null && parameterBoundRaw != null) {
242243
var parameterBound =
243244
Substitution.fromPairs2(_typeFormals, inferredTypes)
244245
.substituteType(parameterBoundRaw);
245246
var extendsConstraint = MergedTypeConstraint.fromExtends(
246-
typeParameterName: parameter.name3,
247+
typeParameterName: name,
247248
boundType: SharedTypeView(parameterBoundRaw),
248249
extendsType: SharedTypeView(parameterBound),
249250
typeAnalyzerOperations: _typeSystemOperations,
@@ -260,13 +261,16 @@ class GenericInferrer {
260261
return null;
261262
}
262263
hasErrorReported = true;
264+
265+
var name = parameter.name3;
266+
if (name == null) {
267+
return null;
268+
}
269+
263270
errorReporter?.atEntity(
264271
errorEntity!,
265272
CompileTimeErrorCode.COULD_NOT_INFER,
266-
arguments: [
267-
parameter.name3,
268-
_formatError(parameter, inferred, constraints)
269-
],
273+
arguments: [name, _formatError(parameter, inferred, constraints)],
270274
);
271275

272276
// Heuristic: even if we failed, keep the erroneous type.
@@ -284,13 +288,19 @@ class GenericInferrer {
284288
return null;
285289
}
286290
hasErrorReported = true;
291+
292+
var name = parameter.name3;
293+
if (name == null) {
294+
return null;
295+
}
296+
287297
var typeFormals = inferred.typeFormals;
288298
var typeFormalsStr = typeFormals.map(_elementStr).join(', ');
289299
errorReporter!.atEntity(
290300
errorEntity!,
291301
CompileTimeErrorCode.COULD_NOT_INFER,
292302
arguments: [
293-
parameter.name3,
303+
name,
294304
' Inferred candidate type ${_typeStr(inferred)} has type parameters'
295305
' [$typeFormalsStr], but a function with'
296306
' type parameters cannot be used as a type argument.'
@@ -329,6 +339,12 @@ class GenericInferrer {
329339
}
330340
hasErrorReported = true;
331341
TypeParameterElementImpl2 typeParam = _typeFormals[i];
342+
343+
var name = typeParam.name3;
344+
if (name == null) {
345+
return null;
346+
}
347+
332348
var typeParamBound =
333349
Substitution.fromPairs2(_typeFormals, inferredTypes)
334350
.substituteType(typeParam.bound ?? typeProvider.objectType);
@@ -337,7 +353,7 @@ class GenericInferrer {
337353
errorEntity!,
338354
CompileTimeErrorCode.COULD_NOT_INFER,
339355
arguments: [
340-
typeParam.name3,
356+
name,
341357
"\nRecursive bound cannot be instantiated: '$typeParamBound'."
342358
"\nConsider passing explicit type argument(s) "
343359
"to the generic.\n\n'"
@@ -374,6 +390,11 @@ class GenericInferrer {
374390
continue;
375391
}
376392

393+
var name = parameter.name3;
394+
if (name == null) {
395+
continue;
396+
}
397+
377398
var substitution = Substitution.fromPairs2(
378399
_typeFormals.map((e) => e).toList(), typeArguments);
379400
var bound = substitution.substituteType(rawBound);
@@ -382,7 +403,7 @@ class GenericInferrer {
382403
errorEntity!,
383404
CompileTimeErrorCode.COULD_NOT_INFER,
384405
arguments: [
385-
parameter.name3,
406+
name,
386407
"\n'${_typeStr(argument)}' doesn't conform to "
387408
"the bound '${_typeStr(bound)}'"
388409
", instantiated from '${_typeStr(rawBound)}'"
@@ -472,10 +493,11 @@ class GenericInferrer {
472493
// variance is added to the interface.
473494
var typeParam = _typeFormals[i];
474495
MergedTypeConstraint? extendsClause;
496+
var name = typeParam.name3;
475497
var bound = typeParam.bound;
476-
if (bound != null) {
498+
if (name != null && bound != null) {
477499
extendsClause = MergedTypeConstraint.fromExtends(
478-
typeParameterName: typeParam.name3,
500+
typeParameterName: name,
479501
boundType: SharedTypeView(bound),
480502
extendsType: SharedTypeView(
481503
Substitution.fromPairs2(_typeFormals, inferredTypes)

pkg/analyzer/lib/src/dart/resolver/extension_member_resolver.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,15 @@ class ExtensionMemberResolver {
269269
for (var i = 0; i < typeArgumentTypes.length; i++) {
270270
var argument = typeArgumentTypes[i];
271271
var parameter = typeParameters[i];
272+
var name = parameter.name3;
272273
var parameterBound = parameter.bound;
273-
if (parameterBound != null) {
274+
if (name != null && parameterBound != null) {
274275
parameterBound = substitution.substituteType(parameterBound);
275276
if (!_typeSystem.isSubtypeOf(argument, parameterBound)) {
276277
_errorReporter.atNode(
277278
typeArgumentList.arguments[i],
278279
CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS,
279-
arguments: [argument, parameter.name3, parameterBound],
280+
arguments: [argument, name, parameterBound],
280281
);
281282
}
282283
}

pkg/analyzer/lib/src/utilities/extensions/string.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ extension Pluralized on String {
111111
}
112112

113113
extension StringExtension on String {
114+
String? get nullIfEmpty {
115+
return isNotEmpty ? this : null;
116+
}
117+
114118
/// If [length] is above the [limit], replace the middle with `...`.
115119
String elideTo(int limit) {
116120
if (length > limit) {

pkg/analyzer/test/src/summary/elements/class_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26597,7 +26597,7 @@ library
2659726597
firstFragment: <testLibraryFragment>::@class::A
2659826598
typeParameters
2659926599
T
26600-
26600+
<null-name>
2660126601
constructors
2660226602
synthetic new
2660326603
firstFragment: <testLibraryFragment>::@class::A::@constructor::new

0 commit comments

Comments
 (0)