Skip to content

Commit 2e6d7bf

Browse files
johnniwintherCommit Bot
authored andcommitted
[cfe] Report error on instantiation of enums from .dill
Closes #48350 Change-Id: I0cedf8a5a12e1a8dee546eec73e012519a054e13 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/232384 Reviewed-by: Chloe Stefantsova <cstefantsova@google.com> Commit-Queue: Johnni Winther <johnniwinther@google.com>
1 parent 7263b35 commit 2e6d7bf

16 files changed

+403
-1
lines changed

pkg/front_end/lib/src/fasta/builder/type_declaration_builder.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ abstract class TypeDeclarationBuilder implements ModifierBuilder {
2828
@override
2929
TypeDeclarationBuilder get origin;
3030

31+
/// Return `true` if this type declaration is an enum.
32+
bool get isEnum;
33+
3134
/// Creates the [DartType] corresponding to this declaration applied with
3235
/// [arguments] in [library] with the syntactical nullability defined by
3336
/// [nullabilityBuilder].
@@ -74,6 +77,9 @@ abstract class TypeDeclarationBuilderImpl extends ModifierBuilderImpl
7477
@override
7578
bool get isTypeDeclaration => true;
7679

80+
@override
81+
bool get isEnum => false;
82+
7783
@override
7884
String get fullNameForErrors => name;
7985

pkg/front_end/lib/src/fasta/dill/dill_class_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class DillClassBuilder extends ClassBuilderImpl {
4646
parent,
4747
cls.fileOffset);
4848

49+
@override
50+
bool get isEnum => cls.isEnum;
51+
4952
@override
5053
DillClassBuilder get origin => this;
5154

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5371,7 +5371,7 @@ class BodyBuilder extends ScopeListener<JumpTarget>
53715371
} else {
53725372
target = b.member;
53735373
}
5374-
if (type is SourceEnumBuilder &&
5374+
if (type.isEnum &&
53755375
!(libraryBuilder.enableEnhancedEnumsInLibrary &&
53765376
target is Procedure &&
53775377
target.kind == ProcedureKind.Factory)) {

pkg/front_end/lib/src/fasta/source/source_enum_builder.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ class SourceEnumBuilder extends SourceClassBuilder {
455455
return enumBuilder;
456456
}
457457

458+
@override
459+
bool get isEnum => true;
460+
458461
@override
459462
TypeBuilder? get mixedInTypeBuilder => null;
460463

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright (c) 2022, 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+
entry_point: "main.dart"
6+
definitions: []
7+
position: "main.dart"
8+
expression: |
9+
En(123, 'foo')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Errors: {
2+
org-dartlang-debug:synthetic_debug_expression:1:1: Error: Enums can't be instantiated.
3+
En(123, 'foo')
4+
^^
5+
}
6+
method /* from org-dartlang-debug:synthetic_debug_expression */ debugExpr() → dynamic
7+
return invalid-expression "org-dartlang-debug:synthetic_debug_expression:1:1: Error: Enums can't be instantiated.\nEn(123, 'foo')\n^^";

pkg/front_end/testcases/expression/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ void hasClosure() {
127127
.fold<int>(0, (previousValue, element) => previousValue + element.length);
128128
print("xCombinedLength = $xCombinedLength");
129129
}
130+
131+
enum En { a, b, c }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2022, 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+
import 'main_lib.dart';
6+
7+
enum Enum1 { a, b, c }
8+
9+
typedef Alias1 = Enum1;
10+
11+
test() {
12+
Enum1(123, 'foo');
13+
Enum2(123, 'foo');
14+
Alias1(123, 'foo');
15+
Alias2(123, 'foo');
16+
}
17+
18+
main() {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'main_lib.dart';
2+
3+
enum Enum1 { a, b, c }
4+
typedef Alias1 = Enum1;
5+
test() {}
6+
main() {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import 'main_lib.dart';
2+
3+
enum Enum1 { a, b, c }
4+
main() {}
5+
test() {}
6+
typedef Alias1 = Enum1;

0 commit comments

Comments
 (0)