Skip to content

Commit 53dde32

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[fasta] Fix override-based type inference for setters
If both the getter and the setter are present in the superclass, the type should be taken from the setter. Change-Id: Ie0d84e45d15341151af4a688c508da0f1d7b522d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/101988 Reviewed-by: Aske Simon Christensen <askesc@google.com>
1 parent e92bd97 commit 53dde32

14 files changed

+209
-36
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ class ClassHierarchyNodeBuilder {
771771
VariableDeclaration bParameter =
772772
bTarget.function.positionalParameters.single;
773773
bType = bParameter.type;
774-
if (!hasExplictlyTypedFormalParameter(b, 0)) {
774+
if (!hasExplicitlyTypedFormalParameter(b, 0)) {
775775
debug?.log("Giving up (type may be inferred)");
776776
return false;
777777
}
@@ -794,7 +794,7 @@ class ClassHierarchyNodeBuilder {
794794

795795
bool inferSetterType(KernelProcedureBuilder a, Declaration b) {
796796
debug?.log(
797-
"Inferring getter types for ${fullName(a)} based on ${fullName(b)}");
797+
"Inferring setter types for ${fullName(a)} based on ${fullName(b)}");
798798
Member bTarget = b.target;
799799
Procedure aProcedure = a.target;
800800
VariableDeclaration aParameter =
@@ -810,7 +810,8 @@ class ClassHierarchyNodeBuilder {
810810
bTarget.function.positionalParameters.single;
811811
bType = bParameter.type;
812812
copyParameterCovariance(a.parent, aParameter, bParameter);
813-
if (!hasExplictlyTypedFormalParameter(b, 0)) {
813+
if (!hasExplicitlyTypedFormalParameter(b, 0) ||
814+
!hasExplicitlyTypedFormalParameter(a, 0)) {
814815
debug?.log("Giving up (type may be inferred)");
815816
return false;
816817
}
@@ -876,7 +877,7 @@ class ClassHierarchyNodeBuilder {
876877
bTarget.function.positionalParameters.single;
877878
// inheritedType = parameter.type;
878879
copyFieldCovarianceFromParameter(a.parent, a.target, parameter);
879-
if (!hasExplictlyTypedFormalParameter(b, 0)) {
880+
if (!hasExplicitlyTypedFormalParameter(b, 0)) {
880881
debug?.log("Giving up (type may be inferred)");
881882
return false;
882883
}
@@ -2012,7 +2013,7 @@ class DelayedOverrideCheck {
20122013
inferReturnType(cls, a, type, a.hadTypesInferred, hierarchy);
20132014
}
20142015
}
2015-
} else if (a.isSetter && !hasExplictlyTypedFormalParameter(a, 0)) {
2016+
} else if (a.isSetter && !hasExplicitlyTypedFormalParameter(a, 0)) {
20162017
DartType type;
20172018
if (b.isGetter) {
20182019
Procedure bTarget = b.target;
@@ -2448,11 +2449,14 @@ void addDeclarationIfDifferent(
24482449
}
24492450

24502451
String fullName(Declaration declaration) {
2451-
if (declaration is DelayedMember) return declaration.fullNameForErrors;
2452+
String suffix = declaration.isSetter ? "=" : "";
2453+
if (declaration is DelayedMember) {
2454+
return "${declaration.fullNameForErrors}$suffix";
2455+
}
24522456
Declaration parent = declaration.parent;
24532457
return parent == null
2454-
? declaration.fullNameForErrors
2455-
: "${parent.fullNameForErrors}.${declaration.fullNameForErrors}";
2458+
? "${declaration.fullNameForErrors}$suffix"
2459+
: "${parent.fullNameForErrors}.${declaration.fullNameForErrors}$suffix";
24562460
}
24572461

24582462
int compareNamedParameters(VariableDeclaration a, VariableDeclaration b) {
@@ -2594,7 +2598,7 @@ bool hasExplicitReturnType(Declaration declaration) {
25942598
: true;
25952599
}
25962600

2597-
bool hasExplictlyTypedFormalParameter(Declaration declaration, int index) {
2601+
bool hasExplicitlyTypedFormalParameter(Declaration declaration, int index) {
25982602
assert(
25992603
declaration is KernelProcedureBuilder || declaration is DillMemberBuilder,
26002604
"${declaration.runtimeType}");

pkg/front_end/testcases/issue129167943.dart.hierarchy.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ H3:
387387
Object._simpleInstanceOfTrue
388388
Object.==
389389
interfaceSetters:
390-
H3.E.foo%G.foo
390+
H3.E.foo=%G.foo=
391391

392392
H4:
393393
Longest path to Object: 3

pkg/front_end/testcases/mixin_application_override.dart.hierarchy.expect

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ A0:
115115
Object.runtimeType
116116
Object._simpleInstanceOf
117117
Object._instanceOf
118-
A0.M.foo%S.foo%M.foo
118+
A0.M.foo%S.foo
119119
Object.noSuchMethod
120120
Object._identityHashCode
121121
Object.hashCode
@@ -128,7 +128,7 @@ A0:
128128
Object.runtimeType
129129
Object._simpleInstanceOf
130130
Object._instanceOf
131-
A0.M.foo%S.foo%M.foo
131+
A0.M.foo%S.foo
132132
Object.noSuchMethod
133133
Object._identityHashCode
134134
Object.hashCode
@@ -311,7 +311,7 @@ S with M:
311311
Object.runtimeType
312312
Object._simpleInstanceOf
313313
Object._instanceOf
314-
S with M.M.foo%S.foo%M.foo
314+
S with M.M.foo%S.foo
315315
Object.noSuchMethod
316316
Object._identityHashCode
317317
Object.hashCode
@@ -324,7 +324,7 @@ S with M:
324324
Object.runtimeType
325325
Object._simpleInstanceOf
326326
Object._instanceOf
327-
S with M.M.foo%S.foo%M.foo
327+
S with M.M.foo%S.foo
328328
Object.noSuchMethod
329329
Object._identityHashCode
330330
Object.hashCode
@@ -344,7 +344,7 @@ A0X:
344344
Object.runtimeType
345345
Object._simpleInstanceOf
346346
Object._instanceOf
347-
S with M.M.foo%S.foo%M.foo
347+
S with M.M.foo%S.foo
348348
Object.noSuchMethod
349349
Object._identityHashCode
350350
Object.hashCode
@@ -357,7 +357,7 @@ A0X:
357357
Object.runtimeType
358358
Object._simpleInstanceOf
359359
Object._instanceOf
360-
S with M.M.foo%S.foo%M.foo
360+
S with M.M.foo%S.foo
361361
Object.noSuchMethod
362362
Object._identityHashCode
363363
Object.hashCode
@@ -609,7 +609,7 @@ S with M:
609609
Object.runtimeType
610610
Object._simpleInstanceOf
611611
Object._instanceOf
612-
S with M.M.foo%S.foo%M.foo
612+
S with M.M.foo%S.foo
613613
Object.noSuchMethod
614614
Object._identityHashCode
615615
Object.hashCode
@@ -622,7 +622,7 @@ S with M:
622622
Object.runtimeType
623623
Object._simpleInstanceOf
624624
Object._instanceOf
625-
S with M.M.foo%S.foo%M.foo
625+
S with M.M.foo%S.foo
626626
Object.noSuchMethod
627627
Object._identityHashCode
628628
Object.hashCode
@@ -642,7 +642,7 @@ B0:
642642
Object.runtimeType
643643
Object._simpleInstanceOf
644644
Object._instanceOf
645-
S with M.M.foo%S.foo%M.foo
645+
S with M.M.foo%S.foo
646646
Object.noSuchMethod
647647
Object._identityHashCode
648648
Object.hashCode
@@ -655,7 +655,7 @@ B0:
655655
Object.runtimeType
656656
Object._simpleInstanceOf
657657
Object._instanceOf
658-
S with M.M.foo%S.foo%M.foo
658+
S with M.M.foo%S.foo
659659
Object.noSuchMethod
660660
Object._identityHashCode
661661
Object.hashCode
@@ -907,7 +907,7 @@ S with M:
907907
Object.runtimeType
908908
Object._simpleInstanceOf
909909
Object._instanceOf
910-
S with M.M.foo%S.foo%M.foo
910+
S with M.M.foo%S.foo
911911
Object.noSuchMethod
912912
Object._identityHashCode
913913
Object.hashCode
@@ -920,7 +920,7 @@ S with M:
920920
Object.runtimeType
921921
Object._simpleInstanceOf
922922
Object._instanceOf
923-
S with M.M.foo%S.foo%M.foo
923+
S with M.M.foo%S.foo
924924
Object.noSuchMethod
925925
Object._identityHashCode
926926
Object.hashCode
@@ -940,7 +940,7 @@ _B0X&S&M with MX:
940940
Object.runtimeType
941941
Object._simpleInstanceOf
942942
Object._instanceOf
943-
S with M.M.foo%S.foo%M.foo
943+
S with M.M.foo%S.foo
944944
Object.noSuchMethod
945945
Object._identityHashCode
946946
Object.hashCode
@@ -953,7 +953,7 @@ _B0X&S&M with MX:
953953
Object.runtimeType
954954
Object._simpleInstanceOf
955955
Object._instanceOf
956-
S with M.M.foo%S.foo%M.foo
956+
S with M.M.foo%S.foo
957957
Object.noSuchMethod
958958
Object._identityHashCode
959959
Object.hashCode
@@ -974,7 +974,7 @@ B0X:
974974
Object.runtimeType
975975
Object._simpleInstanceOf
976976
Object._instanceOf
977-
S with M.M.foo%S.foo%M.foo
977+
S with M.M.foo%S.foo
978978
Object.noSuchMethod
979979
Object._identityHashCode
980980
Object.hashCode
@@ -987,7 +987,7 @@ B0X:
987987
Object.runtimeType
988988
Object._simpleInstanceOf
989989
Object._instanceOf
990-
S with M.M.foo%S.foo%M.foo
990+
S with M.M.foo%S.foo
991991
Object.noSuchMethod
992992
Object._identityHashCode
993993
Object.hashCode
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2019, 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 B {
6+
num get foo => null;
7+
set foo(dynamic newFoo) {}
8+
}
9+
10+
class A extends B {
11+
set foo(newFoo) {}
12+
}
13+
14+
main() {}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Object:
2+
superclasses:
3+
interfaces:
4+
classMembers:
5+
Object._haveSameRuntimeType
6+
Object.toString
7+
Object.runtimeType
8+
Object._toString
9+
Object._simpleInstanceOf
10+
Object._hashCodeRnd
11+
Object._instanceOf
12+
Object.noSuchMethod
13+
Object._objectHashCode
14+
Object._identityHashCode
15+
Object.hashCode
16+
Object._simpleInstanceOfFalse
17+
Object._simpleInstanceOfTrue
18+
Object.==
19+
classSetters:
20+
21+
B:
22+
superclasses:
23+
Object
24+
interfaces:
25+
classMembers:
26+
Object.toString
27+
Object.runtimeType
28+
Object._simpleInstanceOf
29+
Object._instanceOf
30+
B.foo
31+
Object.noSuchMethod
32+
Object._identityHashCode
33+
Object.hashCode
34+
Object._simpleInstanceOfFalse
35+
Object._simpleInstanceOfTrue
36+
Object.==
37+
classSetters:
38+
B.foo
39+
40+
A:
41+
superclasses:
42+
Object
43+
-> B
44+
interfaces:
45+
classMembers:
46+
Object.toString
47+
Object.runtimeType
48+
Object._simpleInstanceOf
49+
Object._instanceOf
50+
B.foo
51+
Object.noSuchMethod
52+
Object._identityHashCode
53+
Object.hashCode
54+
Object._simpleInstanceOfFalse
55+
Object._simpleInstanceOfTrue
56+
Object.==
57+
classSetters:
58+
A.foo
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class B extends core::Object {
6+
synthetic constructor •() → self::B
7+
: super core::Object::•()
8+
;
9+
get foo() → core::num
10+
return null;
11+
set foo(dynamic newFoo) → void {}
12+
}
13+
class A extends self::B {
14+
synthetic constructor •() → self::A
15+
: super self::B::•()
16+
;
17+
set foo(dynamic newFoo) → void {}
18+
}
19+
static method main() → dynamic {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class B extends core::Object {
6+
synthetic constructor •() → self::B
7+
: super core::Object::•()
8+
;
9+
get foo() → core::num
10+
return null;
11+
set foo(dynamic newFoo) → void {}
12+
}
13+
class A extends self::B {
14+
synthetic constructor •() → self::A
15+
: super self::B::•()
16+
;
17+
set foo(dynamic newFoo) → void {}
18+
}
19+
static method main() → dynamic {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class B extends core::Object {
6+
synthetic constructor •() → self::B
7+
;
8+
get foo() → core::num
9+
;
10+
set foo(dynamic newFoo) → void
11+
;
12+
}
13+
class A extends self::B {
14+
synthetic constructor •() → self::A
15+
;
16+
set foo(dynamic newFoo) → void
17+
;
18+
}
19+
static method main() → dynamic
20+
;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
library;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class B extends core::Object {
6+
synthetic constructor •() → self::B
7+
: super core::Object::•()
8+
;
9+
get foo() → core::num
10+
return null;
11+
set foo(dynamic newFoo) → void {}
12+
}
13+
class A extends self::B {
14+
synthetic constructor •() → self::A
15+
: super self::B::•()
16+
;
17+
set foo(dynamic newFoo) → void {}
18+
}
19+
static method main() → dynamic {}

0 commit comments

Comments
 (0)