Skip to content

Commit e64597a

Browse files
srawlinscommit-bot@chromium.org
authored and
commit-bot@chromium.org
committed
analyzer: Report duplicate unnamed and "new"-named constructors
Bug: #46020 Change-Id: I5a737b8b227fa205e66650dd40d697e4e3ef0125 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/208821 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
1 parent 1796160 commit e64597a

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

pkg/analyzer/lib/src/error/duplicate_definition_verifier.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,18 @@ class DuplicateDefinitionVerifier {
253253

254254
/// Check that there are no members with the same name.
255255
void _checkClassMembers(ClassElement element, List<ClassMember> members) {
256-
Set<String> constructorNames = HashSet<String>();
257-
Map<String, Element> instanceGetters = HashMap<String, Element>();
258-
Map<String, Element> instanceSetters = HashMap<String, Element>();
259-
Map<String, Element> staticGetters = HashMap<String, Element>();
260-
Map<String, Element> staticSetters = HashMap<String, Element>();
256+
var constructorNames = HashSet<String>();
257+
var instanceGetters = HashMap<String, Element>();
258+
var instanceSetters = HashMap<String, Element>();
259+
var staticGetters = HashMap<String, Element>();
260+
var staticSetters = HashMap<String, Element>();
261261

262262
for (ClassMember member in members) {
263263
if (member is ConstructorDeclaration) {
264264
var name = member.name?.name ?? '';
265+
if (name == 'new') {
266+
name = '';
267+
}
265268
if (!constructorNames.add(name)) {
266269
if (name.isEmpty) {
267270
_errorReporter.reportErrorForName(

pkg/analyzer/test/src/dart/resolution/class_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,28 @@ class C {
338338
]);
339339
}
340340

341+
test_error_duplicateConstructorDefault_bothNew() async {
342+
await assertErrorsInCode(r'''
343+
class C {
344+
C.new();
345+
C.new();
346+
}
347+
''', [
348+
error(CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT, 23, 5),
349+
]);
350+
}
351+
352+
test_error_duplicateConstructorDefault_oneNew() async {
353+
await assertErrorsInCode(r'''
354+
class C {
355+
C();
356+
C.new();
357+
}
358+
''', [
359+
error(CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT, 19, 5),
360+
]);
361+
}
362+
341363
test_error_duplicateConstructorName() async {
342364
await assertErrorsInCode(r'''
343365
class C {

0 commit comments

Comments
 (0)