Skip to content

Commit b7aaf90

Browse files
chloestefantsovaCommit Queue
authored andcommitted
[model] Mark erroneous constructors as such in the CFE
This allows to skip some apriori failing checks and avoid cascading errors. Aditionally it instructs the verifier to not check the erroneous constructors. Change-Id: Ie96bbe84d02a96567b3deab65e15e0780a625d19 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/424820 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent 9c3f9b6 commit b7aaf90

12 files changed

+37
-44
lines changed

pkg/front_end/lib/src/kernel/kernel_target.dart

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,18 +1433,23 @@ class KernelTarget {
14331433
Initializer initializer = fieldBuilder.buildImplicitInitializer();
14341434
constructorBuilder.prependInitializer(initializer);
14351435
if (fieldBuilder.isFinal) {
1436-
libraryBuilder.addProblem(
1437-
templateFinalFieldNotInitializedByConstructor
1438-
.withArguments(fieldBuilder.name),
1439-
constructorBuilder.fileOffset,
1440-
constructorBuilder.name.length,
1441-
constructorBuilder.fileUri,
1442-
context: [
1443-
templateMissingImplementationCause
1444-
.withArguments(fieldBuilder.name)
1445-
.withLocation(fieldBuilder.fileUri,
1446-
fieldBuilder.fileOffset, fieldBuilder.name.length)
1447-
]);
1436+
// Avoid cascading error if the constructor is known to be
1437+
// erroneous: such constructors don't initialize the final fields
1438+
// properly.
1439+
if (!constructorBuilder.invokeTarget.isErroneous) {
1440+
libraryBuilder.addProblem(
1441+
templateFinalFieldNotInitializedByConstructor
1442+
.withArguments(fieldBuilder.name),
1443+
constructorBuilder.fileOffset,
1444+
constructorBuilder.name.length,
1445+
constructorBuilder.fileUri,
1446+
context: [
1447+
templateMissingImplementationCause
1448+
.withArguments(fieldBuilder.name)
1449+
.withLocation(fieldBuilder.fileUri,
1450+
fieldBuilder.fileOffset, fieldBuilder.name.length)
1451+
]);
1452+
}
14481453
} else if (fieldBuilder.fieldType is! InvalidType &&
14491454
!fieldBuilder.isLate &&
14501455
fieldBuilder.fieldType.isPotentiallyNonNullable) {

pkg/front_end/lib/src/source/source_constructor_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ class SourceConstructorBuilderImpl extends SourceMemberBuilderImpl
368368
message: message,
369369
kind: UnresolvedKind.Constructor))
370370
..parent = parent);
371+
if (parent is Constructor) {
372+
parent.isErroneous = true;
373+
}
371374
} else {
372375
_initializers.add(initializer..parent = parent);
373376
}

pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart.strong.expect

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ library;
77
// const E2.named(int value) : this(value, value); // Error.
88
// ^
99
//
10-
// pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:22:9: Error: Final field 'foo' is not initialized by this constructor.
11-
// Try to initialize the field using an initializing formal or a field initializer.
12-
// const E2.named(int value) : this(value, value); // Error.
13-
// ^^^^^
14-
// pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:19:13: Context: 'foo' is defined here.
15-
// final int foo;
16-
// ^^^
17-
//
1810
import self as self;
1911
import "dart:core" as core;
2012

@@ -46,7 +38,7 @@ Try removing the extra positional arguments.
4638
const constructor •(core::int #index, core::String #name, core::int foo) → self::E2
4739
: self::E2::foo = foo, super core::_Enum::•(#index, #name)
4840
;
49-
const constructor named(core::int #index, core::String #name, core::int value) → self::E2
41+
const erroneous constructor named(core::int #index, core::String #name, core::int value) → self::E2
5042
: self::E2::foo = null, final dynamic #t1 = invalid-expression "pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:22:35: Error: Too many positional arguments: 3 allowed, but 4 found.
5143
Try removing the extra positional arguments.
5244
const E2.named(int value) : this(value, value); // Error.

pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart.strong.modular.expect

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ library;
77
// const E2.named(int value) : this(value, value); // Error.
88
// ^
99
//
10-
// pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:22:9: Error: Final field 'foo' is not initialized by this constructor.
11-
// Try to initialize the field using an initializing formal or a field initializer.
12-
// const E2.named(int value) : this(value, value); // Error.
13-
// ^^^^^
14-
// pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:19:13: Context: 'foo' is defined here.
15-
// final int foo;
16-
// ^^^
17-
//
1810
import self as self;
1911
import "dart:core" as core;
2012

@@ -46,7 +38,7 @@ Try removing the extra positional arguments.
4638
const constructor •(core::int #index, core::String #name, core::int foo) → self::E2
4739
: self::E2::foo = foo, super core::_Enum::•(#index, #name)
4840
;
49-
const constructor named(core::int #index, core::String #name, core::int value) → self::E2
41+
const erroneous constructor named(core::int #index, core::String #name, core::int value) → self::E2
5042
: self::E2::foo = null, final dynamic #t1 = invalid-expression "pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:22:35: Error: Too many positional arguments: 3 allowed, but 4 found.
5143
Try removing the extra positional arguments.
5244
const E2.named(int value) : this(value, value); // Error.

pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart.strong.outline.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class E2 extends core::_Enum /*isEnum*/ {
3232
const constructor •(core::int #index, core::String #name, core::int foo) → self::E2
3333
: self::E2::foo = foo, super core::_Enum::•(#index, #name)
3434
;
35-
const constructor named(core::int #index, core::String #name, core::int value) → self::E2
35+
const erroneous constructor named(core::int #index, core::String #name, core::int value) → self::E2
3636
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:22:35: Error: Too many positional arguments: 3 allowed, but 4 found.
3737
Try removing the extra positional arguments.
3838
const E2.named(int value) : this(value, value); // Error.

pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart.strong.transformed.expect

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@ library;
77
// const E2.named(int value) : this(value, value); // Error.
88
// ^
99
//
10-
// pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:22:9: Error: Final field 'foo' is not initialized by this constructor.
11-
// Try to initialize the field using an initializing formal or a field initializer.
12-
// const E2.named(int value) : this(value, value); // Error.
13-
// ^^^^^
14-
// pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:19:13: Context: 'foo' is defined here.
15-
// final int foo;
16-
// ^^^
17-
//
1810
import self as self;
1911
import "dart:core" as core;
2012

@@ -46,7 +38,7 @@ Try removing the extra positional arguments.
4638
const constructor •(core::int #index, core::String #name, core::int foo) → self::E2
4739
: self::E2::foo = foo, super core::_Enum::•(#index, #name)
4840
;
49-
const constructor named(core::int #index, core::String #name, core::int value) → self::E2
41+
const erroneous constructor named(core::int #index, core::String #name, core::int value) → self::E2
5042
: self::E2::foo = null, final dynamic #t1 = invalid-expression "pkg/front_end/testcases/enhanced_enums/redirecting_initializers.dart:22:35: Error: Too many positional arguments: 3 allowed, but 4 found.
5143
Try removing the extra positional arguments.
5244
const E2.named(int value) : this(value, value); // Error.

pkg/front_end/testcases/rasta/bad_constructor_redirection.dart.strong.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import self as self;
1111
import "dart:core" as core;
1212

1313
class C extends core::Object /*hasConstConstructor*/ {
14-
const constructor •() → self::C
14+
const erroneous constructor •() → self::C
1515
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/rasta/bad_constructor_redirection.dart:6:21: Error: Too many positional arguments: 0 allowed, but 1 found.
1616
Try removing the extra positional arguments.
1717
const C() : this.x(1);

pkg/front_end/testcases/rasta/bad_constructor_redirection.dart.strong.modular.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import self as self;
1111
import "dart:core" as core;
1212

1313
class C extends core::Object /*hasConstConstructor*/ {
14-
const constructor •() → self::C
14+
const erroneous constructor •() → self::C
1515
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/rasta/bad_constructor_redirection.dart:6:21: Error: Too many positional arguments: 0 allowed, but 1 found.
1616
Try removing the extra positional arguments.
1717
const C() : this.x(1);

pkg/front_end/testcases/rasta/bad_constructor_redirection.dart.strong.outline.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import self as self;
1111
import "dart:core" as core;
1212

1313
class C extends core::Object /*hasConstConstructor*/ {
14-
const constructor •() → self::C
14+
const erroneous constructor •() → self::C
1515
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/rasta/bad_constructor_redirection.dart:6:21: Error: Too many positional arguments: 0 allowed, but 1 found.
1616
Try removing the extra positional arguments.
1717
const C() : this.x(1);

pkg/front_end/testcases/rasta/bad_constructor_redirection.dart.strong.transformed.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import self as self;
1111
import "dart:core" as core;
1212

1313
class C extends core::Object /*hasConstConstructor*/ {
14-
const constructor •() → self::C
14+
const erroneous constructor •() → self::C
1515
: final dynamic #t1 = invalid-expression "pkg/front_end/testcases/rasta/bad_constructor_redirection.dart:6:21: Error: Too many positional arguments: 0 allowed, but 1 found.
1616
Try removing the extra positional arguments.
1717
const C() : this.x(1);

0 commit comments

Comments
 (0)