Skip to content

Commit d5d6988

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
More tests for DUPLICATE_DEFINITION in extensions, fixes for static fields.
[email protected] Change-Id: Ib3657cd76d3edc06452618b7f7423163ada3a51d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/112746 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent b31df28 commit d5d6988

File tree

3 files changed

+295
-30
lines changed

3 files changed

+295
-30
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,7 +1750,16 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
17501750
var staticSetters = <String, Element>{};
17511751

17521752
for (var member in members) {
1753-
if (member is MethodDeclaration) {
1753+
if (member is FieldDeclaration) {
1754+
for (var field in member.fields.variables) {
1755+
var identifier = field.name;
1756+
_checkDuplicateIdentifier(
1757+
member.isStatic ? staticGetters : instanceGetters,
1758+
identifier,
1759+
setterScope: member.isStatic ? staticSetters : instanceSetters,
1760+
);
1761+
}
1762+
} else if (member is MethodDeclaration) {
17541763
_checkDuplicateIdentifier(
17551764
member.isStatic ? staticGetters : instanceGetters,
17561765
member.name,
@@ -1761,7 +1770,22 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
17611770

17621771
// Check for local static members conflicting with local instance members.
17631772
for (var member in members) {
1764-
if (member is MethodDeclaration) {
1773+
if (member is FieldDeclaration) {
1774+
if (member.isStatic) {
1775+
for (var field in member.fields.variables) {
1776+
var identifier = field.name;
1777+
var name = identifier.name;
1778+
if (instanceGetters.containsKey(name) ||
1779+
instanceSetters.containsKey(name)) {
1780+
_errorReporter.reportErrorForNode(
1781+
CompileTimeErrorCode.EXTENSION_CONFLICTING_STATIC_AND_INSTANCE,
1782+
identifier,
1783+
[_enclosingExtension.name, name],
1784+
);
1785+
}
1786+
}
1787+
}
1788+
} else if (member is MethodDeclaration) {
17651789
if (member.isStatic) {
17661790
var identifier = member.name;
17671791
var name = identifier.name;
@@ -1901,7 +1925,13 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
19011925
}
19021926
}
19031927
for (CompilationUnitMember member in node.declarations) {
1904-
if (member is NamedCompilationUnitMember) {
1928+
if (member is ExtensionDeclaration) {
1929+
var identifier = member.name;
1930+
if (identifier != null) {
1931+
_checkDuplicateIdentifier(definedGetters, identifier,
1932+
setterScope: definedSetters);
1933+
}
1934+
} else if (member is NamedCompilationUnitMember) {
19051935
_checkDuplicateIdentifier(definedGetters, member.name,
19061936
setterScope: definedSetters);
19071937
} else if (member is TopLevelVariableDeclaration) {

pkg/analyzer/test/src/diagnostics/duplicate_definition_test.dart

Lines changed: 215 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,6 @@ class DuplicateDefinitionExtensionTest extends DriverResolutionTest {
325325
..contextFeatures = new FeatureSet.forTesting(
326326
sdkVersion: '2.3.0', additionalFeatures: [Feature.extension_methods]);
327327

328-
CompileTimeErrorCode get _errorCode =>
329-
CompileTimeErrorCode.DUPLICATE_DEFINITION;
330-
331328
test_extendedType_instance() async {
332329
await assertNoErrorsInCode('''
333330
class A {
@@ -361,86 +358,277 @@ extension E on A {
361358
}
362359

363360
test_instance_getter_getter() async {
364-
await assertErrorsInCode('''
365-
extension E on String {
361+
await assertErrorsInCode(r'''
362+
class A {}
363+
extension E on A {
364+
int get foo => 0;
366365
int get foo => 0;
366+
}
367+
''', [
368+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 60, 3),
369+
]);
370+
}
371+
372+
test_instance_getter_method() async {
373+
await assertErrorsInCode(r'''
374+
class A {}
375+
extension E on A {
367376
int get foo => 0;
377+
void foo() {}
368378
}
369379
''', [
370-
error(_errorCode, 54, 3),
380+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 57, 3),
371381
]);
372382
}
373383

374384
test_instance_getter_setter() async {
375-
await assertNoErrorsInCode('''
376-
extension E on String {
385+
await assertNoErrorsInCode(r'''
386+
class A {}
387+
extension E on A {
377388
int get foo => 0;
378389
set foo(_) {}
379390
}
380391
''');
381392
}
382393

394+
test_instance_method_getter() async {
395+
await assertErrorsInCode(r'''
396+
class A {}
397+
extension E on A {
398+
void foo() {}
399+
int get foo => 0;
400+
}
401+
''', [
402+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 56, 3),
403+
]);
404+
}
405+
383406
test_instance_method_method() async {
384-
await assertErrorsInCode('''
385-
extension E on String {
407+
await assertErrorsInCode(r'''
408+
class A {}
409+
extension E on A {
386410
void foo() {}
387411
void foo() {}
388412
}
389413
''', [
390-
error(_errorCode, 47, 3),
414+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 53, 3),
391415
]);
392416
}
393417

394-
test_instance_setter_setter() async {
395-
await assertErrorsInCode('''
396-
extension E on String {
418+
test_instance_method_setter() async {
419+
await assertErrorsInCode(r'''
420+
class A {}
421+
extension E on A {
422+
void foo() {}
397423
set foo(_) {}
424+
}
425+
''', [
426+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 52, 3),
427+
]);
428+
}
429+
430+
test_instance_setter_getter() async {
431+
await assertNoErrorsInCode(r'''
432+
class A {}
433+
extension E on A {
398434
set foo(_) {}
435+
int get foo => 0;
436+
}
437+
''');
438+
}
439+
440+
test_instance_setter_method() async {
441+
await assertErrorsInCode(r'''
442+
class A {}
443+
extension E on A {
444+
set foo(_) {}
445+
void foo() {}
446+
}
447+
''', [
448+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 53, 3),
449+
]);
450+
}
451+
452+
test_instance_setter_setter() async {
453+
await assertErrorsInCode(r'''
454+
class A {}
455+
extension E on A {
456+
void set foo(_) {}
457+
void set foo(_) {}
399458
}
400459
''', [
401-
error(_errorCode, 46, 3),
460+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 62, 3),
402461
]);
403462
}
404463

464+
test_static_field_field() async {
465+
await assertErrorsInCode(r'''
466+
class A {}
467+
extension E on A {
468+
static int foo;
469+
static int foo;
470+
}
471+
''', [
472+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 61, 3),
473+
]);
474+
}
475+
476+
test_static_field_getter() async {
477+
await assertErrorsInCode(r'''
478+
class A {}
479+
extension E on A {
480+
static int foo;
481+
static int get foo => 0;
482+
}
483+
''', [
484+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 65, 3),
485+
]);
486+
}
487+
488+
test_static_field_method() async {
489+
await assertErrorsInCode(r'''
490+
class A {}
491+
extension E on A {
492+
static int foo;
493+
static void foo() {}
494+
}
495+
''', [
496+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 62, 3),
497+
]);
498+
}
499+
500+
test_static_fieldFinal_getter() async {
501+
await assertErrorsInCode(r'''
502+
class A {}
503+
extension E on A {
504+
static final int foo = 0;
505+
static int get foo => 0;
506+
}
507+
''', [
508+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 75, 3),
509+
]);
510+
}
511+
512+
test_static_fieldFinal_setter() async {
513+
await assertNoErrorsInCode(r'''
514+
class A {}
515+
extension E on A {
516+
static final int foo = 0;
517+
static set foo(int x) {}
518+
}
519+
''');
520+
}
521+
405522
test_static_getter_getter() async {
406-
await assertErrorsInCode('''
407-
extension E on String {
523+
await assertErrorsInCode(r'''
524+
class A {}
525+
extension E on A {
408526
static int get foo => 0;
409527
static int get foo => 0;
410528
}
411529
''', [
412-
error(_errorCode, 68, 3),
530+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 74, 3),
531+
]);
532+
}
533+
534+
test_static_getter_method() async {
535+
await assertErrorsInCode(r'''
536+
class A {}
537+
extension E on A {
538+
static int get foo => 0;
539+
static void foo() {}
540+
}
541+
''', [
542+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 71, 3),
413543
]);
414544
}
415545

416546
test_static_getter_setter() async {
417-
await assertNoErrorsInCode('''
418-
extension E on String {
547+
await assertNoErrorsInCode(r'''
548+
class A {}
549+
extension E on A {
419550
static int get foo => 0;
420551
static set foo(_) {}
421552
}
422553
''');
423554
}
424555

556+
test_static_method_getter() async {
557+
await assertErrorsInCode(r'''
558+
class A {}
559+
extension E on A {
560+
static void foo() {}
561+
static int get foo => 0;
562+
}
563+
''', [
564+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 70, 3),
565+
]);
566+
}
567+
425568
test_static_method_method() async {
426-
await assertErrorsInCode('''
427-
extension E on String {
569+
await assertErrorsInCode(r'''
570+
class A {}
571+
extension E on A {
428572
static void foo() {}
429573
static void foo() {}
430574
}
431575
''', [
432-
error(_errorCode, 61, 3),
576+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 67, 3),
433577
]);
434578
}
435579

436-
test_static_setter_setter() async {
437-
await assertErrorsInCode('''
438-
extension E on String {
580+
test_static_method_setter() async {
581+
await assertErrorsInCode(r'''
582+
class A {}
583+
extension E on A {
584+
static void foo() {}
439585
static set foo(_) {}
586+
}
587+
''', [
588+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 66, 3),
589+
]);
590+
}
591+
592+
test_static_setter_getter() async {
593+
await assertNoErrorsInCode(r'''
594+
mixin M {
440595
static set foo(_) {}
596+
static int get foo => 0;
441597
}
598+
''');
599+
}
600+
601+
test_static_setter_method() async {
602+
await assertErrorsInCode(r'''
603+
class A {}
604+
extension E on A {
605+
static set foo(_) {}
606+
static void foo() {}
607+
}
608+
''', [
609+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 67, 3),
610+
]);
611+
}
612+
613+
test_static_setter_setter() async {
614+
await assertErrorsInCode(r'''
615+
class A {}
616+
extension E on A {
617+
static void set foo(_) {}
618+
static void set foo(_) {}
619+
}
620+
''', [
621+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 76, 3),
622+
]);
623+
}
624+
625+
test_unitMembers_extension() async {
626+
await assertErrorsInCode('''
627+
class A {}
628+
extension E on A {}
629+
extension E on A {}
442630
''', [
443-
error(_errorCode, 60, 3),
631+
error(CompileTimeErrorCode.DUPLICATE_DEFINITION, 41, 1),
444632
]);
445633
}
446634
}

0 commit comments

Comments
 (0)