Skip to content

Commit 54b3ca9

Browse files
jensjohaCommit Queue
authored andcommitted
[CFE] Skip processing of redirecting factories inside extensions as it will crash the compiler
Change-Id: I58b8cc2e62bd182bfb5c5a6a5efb33297c7dd5a9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395520 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 31e324b commit 54b3ca9

9 files changed

+80
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,6 +2948,13 @@ severity: $severity
29482948
if (redirectingFactoryBuilders != null) {
29492949
for (RedirectingFactoryBuilder redirectingFactoryBuilder
29502950
in redirectingFactoryBuilders) {
2951+
if (redirectingFactoryBuilder.parent.isExtension) {
2952+
// Extensions don't build their redirecting factories so we can't
2953+
// process them. Once they are added in
2954+
// [DeclarationNameSpaceBuilder.buildNameSpace] this skipping can
2955+
// likely be removed.
2956+
continue;
2957+
}
29512958
redirectingFactoryBuilder.buildOutlineExpressions(
29522959
classHierarchy, delayedDefaultValueCloners);
29532960
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,10 @@ class DeclarationNameSpaceBuilder {
14591459
// TODO(johnniwinther): Handle constructors in extensions consistently.
14601460
// Currently they are not part of the name space but still processed
14611461
// for instance when inferring redirecting factories.
1462+
// They are part of the name space for extension types though.
1463+
// Note that we have to remove [RedirectingFactoryBuilder]s in
1464+
// [SourceLoader.inferRedirectingFactories] as we don't build them
1465+
// because we don't add them here.
14621466
constructors: includeConstructors ? constructors : null);
14631467
}
14641468
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2024, 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+
extension Foo on String {
6+
factory Foo({ this.fooX }) = Foobar;
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/extension_with_redirecting_factory.dart:6:3: Error: Extensions can't declare constructors.
6+
// Try removing the constructor declaration.
7+
// factory Foo({ this.fooX }) = Foobar;
8+
// ^^^^^^^
9+
//
10+
import self as self;
11+
import "dart:core" as core;
12+
13+
extension Foo on core::String {
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/extension_with_redirecting_factory.dart:6:3: Error: Extensions can't declare constructors.
6+
// Try removing the constructor declaration.
7+
// factory Foo({ this.fooX }) = Foobar;
8+
// ^^^^^^^
9+
//
10+
import self as self;
11+
import "dart:core" as core;
12+
13+
extension Foo on core::String {
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/extension_with_redirecting_factory.dart:6:3: Error: Extensions can't declare constructors.
6+
// Try removing the constructor declaration.
7+
// factory Foo({ this.fooX }) = Foobar;
8+
// ^^^^^^^
9+
//
10+
import self as self;
11+
import "dart:core" as core;
12+
13+
extension Foo on core::String {
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/regress/extension_with_redirecting_factory.dart:6:3: Error: Extensions can't declare constructors.
6+
// Try removing the constructor declaration.
7+
// factory Foo({ this.fooX }) = Foobar;
8+
// ^^^^^^^
9+
//
10+
import self as self;
11+
import "dart:core" as core;
12+
13+
extension Foo on core::String {
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extension Foo on String {
2+
factory Foo({ this.fooX }) = Foobar;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
extension Foo on String {
2+
factory Foo({ this.fooX }) = Foobar;
3+
}

0 commit comments

Comments
 (0)