Skip to content

Commit f2f0c1a

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[cfe] Create const constructor in mixin application with only static fields
Closes #40982 Change-Id: I2fc426735ece176cc81a9da33e33fd3ad14e756a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/171592 Reviewed-by: Jens Johansen <[email protected]> Commit-Queue: Johnni Winther <[email protected]>
1 parent d03b7e3 commit f2f0c1a

7 files changed

+454
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,15 @@ class KernelTarget extends TargetImplementation {
749749

750750
Class cls = classBuilder.cls;
751751
Constructor constructor = memberBuilder.member;
752-
bool isConst = constructor.isConst && mixin.fields.isEmpty;
752+
bool isConst = constructor.isConst;
753+
if (isConst && mixin.fields.isNotEmpty) {
754+
for (Field field in mixin.fields) {
755+
if (!field.isStatic) {
756+
isConst = false;
757+
break;
758+
}
759+
}
760+
}
753761
List<VariableDeclaration> positionalParameters = <VariableDeclaration>[];
754762
List<VariableDeclaration> namedParameters = <VariableDeclaration>[];
755763
List<Expression> positional = <Expression>[];
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 A {
6+
const A();
7+
}
8+
9+
mixin B {
10+
static const int value = 1;
11+
}
12+
13+
class C1 extends A with B {
14+
const C1();
15+
}
16+
17+
class C2 = A with B;
18+
19+
class C3 extends C2 {
20+
const C3();
21+
}
22+
23+
mixin D {
24+
int value = 1;
25+
}
26+
27+
class E1 extends A with D {
28+
const E1();
29+
}
30+
31+
class E2 = A with D;
32+
33+
class E3 extends E2 {
34+
const E3();
35+
}
36+
37+
main() {}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/general/issue40982.dart:28:9: Error: A constant constructor can't call a non-constant super constructor.
6+
// const E1();
7+
// ^
8+
//
9+
// pkg/front_end/testcases/general/issue40982.dart:34:9: Error: A constant constructor can't call a non-constant super constructor.
10+
// const E3();
11+
// ^
12+
//
13+
import self as self;
14+
import "dart:core" as core;
15+
16+
class A extends core::Object /*hasConstConstructor*/ {
17+
const constructor •() → self::A*
18+
: super core::Object::•()
19+
;
20+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
21+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
22+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
23+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
24+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
25+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
26+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
27+
abstract member-signature method toString() → core::String*; -> core::Object::toString
28+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
29+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
30+
}
31+
abstract class B extends core::Object /*isMixinDeclaration*/ {
32+
static const field core::int* value = 1;
33+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
34+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
35+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
36+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
37+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
38+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
39+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
40+
abstract member-signature method toString() → core::String*; -> core::Object::toString
41+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
42+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
43+
}
44+
abstract class _C1&A&B = self::A with self::B /*isAnonymousMixin,hasConstConstructor*/ {
45+
const synthetic constructor •() → self::_C1&A&B*
46+
: super self::A::•()
47+
;
48+
}
49+
class C1 extends self::_C1&A&B /*hasConstConstructor*/ {
50+
const constructor •() → self::C1*
51+
: super self::_C1&A&B::•()
52+
;
53+
}
54+
class C2 = self::A with self::B /*hasConstConstructor*/ {
55+
const synthetic constructor •() → self::C2*
56+
: super self::A::•()
57+
;
58+
}
59+
class C3 extends self::C2 /*hasConstConstructor*/ {
60+
const constructor •() → self::C3*
61+
: super self::C2::•()
62+
;
63+
}
64+
abstract class D extends core::Object /*isMixinDeclaration*/ {
65+
field core::int* value;
66+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
67+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
68+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
69+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
70+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
71+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
72+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
73+
abstract member-signature method toString() → core::String*; -> core::Object::toString
74+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
75+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
76+
}
77+
abstract class _E1&A&D = self::A with self::D /*isAnonymousMixin*/ {
78+
synthetic constructor •() → self::_E1&A&D*
79+
: super self::A::•()
80+
;
81+
}
82+
class E1 extends self::_E1&A&D /*hasConstConstructor*/ {
83+
const constructor •() → self::E1*
84+
: super self::_E1&A&D::•()
85+
;
86+
}
87+
class E2 = self::A with self::D {
88+
synthetic constructor •() → self::E2*
89+
: super self::A::•()
90+
;
91+
}
92+
class E3 extends self::E2 /*hasConstConstructor*/ {
93+
const constructor •() → self::E3*
94+
: super self::E2::•()
95+
;
96+
}
97+
static method main() → dynamic
98+
;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
library;
2+
//
3+
// Problems in library:
4+
//
5+
// pkg/front_end/testcases/general/issue40982.dart:28:9: Error: A constant constructor can't call a non-constant super constructor.
6+
// const E1();
7+
// ^
8+
//
9+
// pkg/front_end/testcases/general/issue40982.dart:34:9: Error: A constant constructor can't call a non-constant super constructor.
10+
// const E3();
11+
// ^
12+
//
13+
import self as self;
14+
import "dart:core" as core;
15+
16+
class A extends core::Object /*hasConstConstructor*/ {
17+
const constructor •() → self::A*
18+
: super core::Object::•()
19+
;
20+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
21+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
22+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
23+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
24+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
25+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
26+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
27+
abstract member-signature method toString() → core::String*; -> core::Object::toString
28+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
29+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
30+
}
31+
abstract class B extends core::Object /*isMixinDeclaration*/ {
32+
static const field core::int* value = #C1;
33+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
34+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
35+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
36+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
37+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
38+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
39+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
40+
abstract member-signature method toString() → core::String*; -> core::Object::toString
41+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
42+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
43+
}
44+
abstract class _C1&A&B = self::A with self::B /*isAnonymousMixin,hasConstConstructor*/ {
45+
const synthetic constructor •() → self::_C1&A&B*
46+
: super self::A::•()
47+
;
48+
}
49+
class C1 extends self::_C1&A&B /*hasConstConstructor*/ {
50+
const constructor •() → self::C1*
51+
: super self::_C1&A&B::•()
52+
;
53+
}
54+
class C2 = self::A with self::B /*hasConstConstructor*/ {
55+
const synthetic constructor •() → self::C2*
56+
: super self::A::•()
57+
;
58+
}
59+
class C3 extends self::C2 /*hasConstConstructor*/ {
60+
const constructor •() → self::C3*
61+
: super self::C2::•()
62+
;
63+
}
64+
abstract class D extends core::Object /*isMixinDeclaration*/ {
65+
field core::int* value = 1;
66+
abstract member-signature get _identityHashCode() → core::int*; -> core::Object::_identityHashCode
67+
abstract member-signature method _instanceOf(dynamic instantiatorTypeArguments, dynamic functionTypeArguments, dynamic type) → core::bool*; -> core::Object::_instanceOf
68+
abstract member-signature method _simpleInstanceOf(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOf
69+
abstract member-signature method _simpleInstanceOfTrue(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfTrue
70+
abstract member-signature method _simpleInstanceOfFalse(dynamic type) → core::bool*; -> core::Object::_simpleInstanceOfFalse
71+
abstract member-signature operator ==(dynamic other) → core::bool*; -> core::Object::==
72+
abstract member-signature get hashCode() → core::int*; -> core::Object::hashCode
73+
abstract member-signature method toString() → core::String*; -> core::Object::toString
74+
abstract member-signature method noSuchMethod(core::Invocation* invocation) → dynamic; -> core::Object::noSuchMethod
75+
abstract member-signature get runtimeType() → core::Type*; -> core::Object::runtimeType
76+
}
77+
abstract class _E1&A&D = self::A with self::D /*isAnonymousMixin*/ {
78+
synthetic constructor •() → self::_E1&A&D*
79+
: super self::A::•()
80+
;
81+
}
82+
class E1 extends self::_E1&A&D /*hasConstConstructor*/ {
83+
const constructor •() → self::E1*
84+
: super self::_E1&A&D::•()
85+
;
86+
}
87+
class E2 = self::A with self::D {
88+
synthetic constructor •() → self::E2*
89+
: super self::A::•()
90+
;
91+
}
92+
class E3 extends self::E2 /*hasConstConstructor*/ {
93+
const constructor •() → self::E3*
94+
: super self::E2::•()
95+
;
96+
}
97+
static method main() → dynamic {}
98+
99+
constants {
100+
#C1 = 1
101+
}

0 commit comments

Comments
 (0)