Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1d8d2de

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[cfe] Use buildInvalidInitializer to report duplicating initializers
Closes #43363. Bug: dart-lang/sdk#43363 Change-Id: I1de61c2be0d8a30dc184aad7cb392d0c92c5237b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/174403 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Dmitry Stefantsov <[email protected]>
1 parent 52acf2e commit 1d8d2de

15 files changed

+209
-14
lines changed

pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5950,11 +5950,12 @@ class BodyBuilder extends ScopeListener<JumpTarget>
59505950
if (builder?.next != null) {
59515951
// Duplicated name, already reported.
59525952
return <Initializer>[
5953-
new LocalInitializer(new VariableDeclaration.forValue(buildProblem(
5954-
fasta.templateDuplicatedDeclarationUse.withArguments(name),
5955-
fieldNameOffset,
5956-
name.length)))
5957-
..fileOffset = fieldNameOffset
5953+
buildInvalidInitializer(
5954+
buildProblem(
5955+
fasta.templateDuplicatedDeclarationUse.withArguments(name),
5956+
fieldNameOffset,
5957+
name.length),
5958+
fieldNameOffset)
59585959
];
59595960
} else if (builder is FieldBuilder && builder.isDeclarationInstanceMember) {
59605961
initializedFields ??= <String, int>{};

pkg/front_end/testcases/general/duplicated_field_initializer.dart.strong.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class A extends core::Object {
2121
constructor •(core::int* a) → self::A*
2222
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/general/duplicated_field_initializer.dart:8:10: Error: Can't use 'a' because it is declared more than once.
2323
A(this.a);
24-
^", super core::Object::•()
24+
^"
2525
;
2626
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
2727
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf

pkg/front_end/testcases/general/duplicated_field_initializer.dart.strong.transformed.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class A extends core::Object {
2121
constructor •(core::int* a) → self::A*
2222
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/general/duplicated_field_initializer.dart:8:10: Error: Can't use 'a' because it is declared more than once.
2323
A(this.a);
24-
^", super core::Object::•()
24+
^"
2525
;
2626
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
2727
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf

pkg/front_end/testcases/general/issue38938.dart.strong.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class A extends core::Object {
2121
constructor •(core::int* v) → self::A*
2222
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/general/issue38938.dart:8:10: Error: Can't use 'v' because it is declared more than once.
2323
A(this.v);
24-
^", super core::Object::•()
24+
^"
2525
;
2626
constructor second() → self::A*
2727
: super core::Object::•()

pkg/front_end/testcases/general/issue38938.dart.strong.transformed.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class A extends core::Object {
2121
constructor •(core::int* v) → self::A*
2222
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/general/issue38938.dart:8:10: Error: Can't use 'v' because it is declared more than once.
2323
A(this.v);
24-
^", super core::Object::•()
24+
^"
2525
;
2626
constructor second() → self::A*
2727
: super core::Object::•()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
class E {
6+
final int x;
7+
final int y;
8+
E() : this.named(),
9+
this.x = 1;
10+
this.y = 2;
11+
12+
E.named() : this.x = 5,
13+
this.y = 6;
14+
}
15+
16+
main() {}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/general/issue43363.dart:10:9: Error: Expected a class member, but got 'this'.
6+
// this.y = 2;
7+
// ^^^^
8+
//
9+
// pkg/front_end/testcases/general/issue43363.dart:10:13: Error: Expected a class member, but got '.'.
10+
// this.y = 2;
11+
// ^
12+
//
13+
// pkg/front_end/testcases/general/issue43363.dart:10:14: Error: Variables must be declared using the keywords 'const', 'final', 'var' or a type name.
14+
// Try adding the name of the type of the variable or the keyword 'var'.
15+
// this.y = 2;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/general/issue43363.dart:10:14: Error: 'y' is already declared in this scope.
19+
// this.y = 2;
20+
// ^
21+
// pkg/front_end/testcases/general/issue43363.dart:7:13: Context: Previous declaration of 'y'.
22+
// final int y;
23+
// ^
24+
//
25+
import self as self;
26+
import "dart:core" as core;
27+
28+
class E extends core::Object {
29+
final field core::int* x;
30+
final field core::int* y;
31+
constructor •() → self::E*
32+
;
33+
constructor named() → self::E*
34+
;
35+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
36+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
37+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
38+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
39+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
40+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
41+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
42+
abstract member-signature method toString() → core::String*; -> core::Object::toString
43+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
44+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
45+
}
46+
static method main() → dynamic
47+
;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/general/issue43363.dart:10:9: Error: Expected a class member, but got 'this'.
6+
// this.y = 2;
7+
// ^^^^
8+
//
9+
// pkg/front_end/testcases/general/issue43363.dart:10:13: Error: Expected a class member, but got '.'.
10+
// this.y = 2;
11+
// ^
12+
//
13+
// pkg/front_end/testcases/general/issue43363.dart:10:14: Error: Variables must be declared using the keywords 'const', 'final', 'var' or a type name.
14+
// Try adding the name of the type of the variable or the keyword 'var'.
15+
// this.y = 2;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/general/issue43363.dart:10:14: Error: 'y' is already declared in this scope.
19+
// this.y = 2;
20+
// ^
21+
// pkg/front_end/testcases/general/issue43363.dart:7:13: Context: Previous declaration of 'y'.
22+
// final int y;
23+
// ^
24+
//
25+
// pkg/front_end/testcases/general/issue43363.dart:9:16: Error: A redirecting constructor can't have other initializers.
26+
// this.x = 1;
27+
// ^
28+
//
29+
// pkg/front_end/testcases/general/issue43363.dart:13:20: Error: Can't use 'y' because it is declared more than once.
30+
// this.y = 6;
31+
// ^
32+
//
33+
import self as self;
34+
import "dart:core" as core;
35+
36+
class E extends core::Object {
37+
final field core::int* x;
38+
final field core::int* y = null;
39+
constructor •() → self::E*
40+
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/general/issue43363.dart:9:16: Error: A redirecting constructor can't have other initializers.
41+
this.x = 1;
42+
^", this self::E::named()
43+
;
44+
constructor named() → self::E*
45+
: self::E::x = 5, final dynamic #t2 = invalid-expression "pkg/front_end/testcases/general/issue43363.dart:13:20: Error: Can't use 'y' because it is declared more than once.
46+
this.y = 6;
47+
^"
48+
;
49+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
50+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
51+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
52+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
53+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
54+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
55+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
56+
abstract member-signature method toString() → core::String*; -> core::Object::toString
57+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
58+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
59+
}
60+
static method main() → dynamic {}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/general/issue43363.dart:10:9: Error: Expected a class member, but got 'this'.
6+
// this.y = 2;
7+
// ^^^^
8+
//
9+
// pkg/front_end/testcases/general/issue43363.dart:10:13: Error: Expected a class member, but got '.'.
10+
// this.y = 2;
11+
// ^
12+
//
13+
// pkg/front_end/testcases/general/issue43363.dart:10:14: Error: Variables must be declared using the keywords 'const', 'final', 'var' or a type name.
14+
// Try adding the name of the type of the variable or the keyword 'var'.
15+
// this.y = 2;
16+
// ^
17+
//
18+
// pkg/front_end/testcases/general/issue43363.dart:10:14: Error: 'y' is already declared in this scope.
19+
// this.y = 2;
20+
// ^
21+
// pkg/front_end/testcases/general/issue43363.dart:7:13: Context: Previous declaration of 'y'.
22+
// final int y;
23+
// ^
24+
//
25+
// pkg/front_end/testcases/general/issue43363.dart:9:16: Error: A redirecting constructor can't have other initializers.
26+
// this.x = 1;
27+
// ^
28+
//
29+
// pkg/front_end/testcases/general/issue43363.dart:13:20: Error: Can't use 'y' because it is declared more than once.
30+
// this.y = 6;
31+
// ^
32+
//
33+
import self as self;
34+
import "dart:core" as core;
35+
36+
class E extends core::Object {
37+
final field core::int* x;
38+
final field core::int* y = null;
39+
constructor •() → self::E*
40+
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/general/issue43363.dart:9:16: Error: A redirecting constructor can't have other initializers.
41+
this.x = 1;
42+
^", this self::E::named()
43+
;
44+
constructor named() → self::E*
45+
: self::E::x = 5, final dynamic #t2 = invalid-expression "pkg/front_end/testcases/general/issue43363.dart:13:20: Error: Can't use 'y' because it is declared more than once.
46+
this.y = 6;
47+
^"
48+
;
49+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
50+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
51+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
52+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
53+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
54+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
55+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
56+
abstract member-signature method toString() → core::String*; -> core::Object::toString
57+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
58+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
59+
}
60+
static method main() → dynamic {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class E {
2+
final int x;
3+
final int y;
4+
E() : this.named(), this.x = 1;
5+
this.
6+
y = 2;
7+
E.named() : this.x = 5, this.y = 6;
8+
}
9+
main() {}

0 commit comments

Comments
 (0)