Skip to content

Commit ab034e6

Browse files
johnniwinthercommit-bot@chromium.org
authored andcommitted
[dart2js] Fix reduction to declared type in type promotion computation
When reducing the type promotion in case of assignment, the target info wasn't correctly handling the reduction to the trivial state. This caused an error in a following join computation. Closes #42281 Change-Id: I2144f5401e9736308dc37147f7a5cb9ab1be8583 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/150935 Commit-Queue: Stephen Adams <[email protected]> Reviewed-by: Stephen Adams <[email protected]>
1 parent eb21b00 commit ab034e6

File tree

5 files changed

+228
-1
lines changed

5 files changed

+228
-1
lines changed

pkg/compiler/lib/src/ir/static_type.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,13 @@ class TypeMap {
19401940
newTypesOfInterest.add(typeOfInterest);
19411941
}
19421942
}
1943-
if (newTypesOfInterest.isNotEmpty) {
1943+
if (newTypesOfInterest.length > 1 ||
1944+
(newTypesOfInterest.length == 1 &&
1945+
newTypesOfInterest.single != info.declaredType)) {
1946+
// If [newTypesOfInterest] only contains the we have no information
1947+
// about the variable (it is either an instance of its declared type
1948+
// or null) and the canonical way to represent this is to have _no_
1949+
// target info.
19441950
TypeHolder typeHolderIfNonNull =
19451951
new TypeHolder(info.declaredType, newTypesOfInterest, null);
19461952
TypeHolder typeHolderIfNull = new TypeHolder(info.declaredType, null,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 Class {
6+
final Type componentType;
7+
8+
Class(this.componentType);
9+
}
10+
11+
class SubClass extends Class {
12+
SubClass(Type componentType) : super(
13+
/*Type*/ componentType);
14+
}
15+
16+
method1(Class c, Type type, [o]) {
17+
if (/*Class*/ c /*invoke: [Class]->bool*/ == null) {
18+
if (/*dynamic*/ o /*invoke: [dynamic]->bool*/ != null) {
19+
c = new Class(String);
20+
}
21+
/*Class*/ c;
22+
print(/*Type*/ type /*invoke: [Type]->bool*/ == /*Class*/ c?.componentType);
23+
}
24+
}
25+
26+
method2(Class c, Type type, [o]) {
27+
if (/*Class*/ c /*invoke: [Class]->bool*/ == null) {
28+
if (/*dynamic*/ o /*invoke: [dynamic]->bool*/ != null) {
29+
c = new SubClass(String);
30+
}
31+
/*Class*/ c;
32+
print(/*Type*/ type /*invoke: [Type]->bool*/ == /*Class*/ c?.componentType);
33+
}
34+
}
35+
36+
method3(Class c, Type type, [o]) {
37+
if (/*Class*/ c is SubClass) {
38+
if (/*SubClass*/ c /*invoke: [SubClass]->bool*/ == null) {
39+
if (/*dynamic*/ o /*invoke: [dynamic]->bool*/ != null) {
40+
c = new SubClass(String);
41+
}
42+
/*SubClass*/ c;
43+
print(/*Type*/ type /*invoke: [Type]->bool*/ ==
44+
/*SubClass*/ c?.componentType);
45+
}
46+
}
47+
}
48+
49+
main() {
50+
method1(new Class(Object), Object);
51+
method1(new Class(Object), Object, true);
52+
method1(null, Object);
53+
method1(null, Object, true);
54+
55+
method2(new Class(Object), Object);
56+
method2(new Class(Object), Object, true);
57+
method2(null, Object);
58+
method2(null, Object, true);
59+
60+
method3(new Class(Object), Object);
61+
method3(new Class(Object), Object, true);
62+
method3(null, Object);
63+
method3(null, Object, true);
64+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 Class {
6+
final Type componentType;
7+
8+
Class(this.componentType);
9+
}
10+
11+
class SubClass extends Class {
12+
SubClass(Type componentType) : super(/*{}*/ componentType);
13+
}
14+
15+
method1(Class c, Type type, [o]) {
16+
if (/*{}*/ c == null) {
17+
if (/*{c:[{false:Class}|Class]}*/ o != null) {
18+
c = new Class(String);
19+
}
20+
/*{}*/ c;
21+
print(/*{}*/ type == /*{}*/ c?.componentType);
22+
}
23+
}
24+
25+
method2(Class c, Type type, [o]) {
26+
if (/*{}*/ c == null) {
27+
if (/*{c:[{false:Class}|Class]}*/ o != null) {
28+
c = new SubClass(String);
29+
}
30+
/*{}*/ c;
31+
print(/*{}*/ type == /*{}*/ c?.componentType);
32+
}
33+
}
34+
35+
method3(Class c, Type type, [o]) {
36+
if (/*{}*/ c is SubClass) {
37+
if (/*{c:[{true:SubClass}|SubClass]}*/ c == null) {
38+
if (/*{c:[{true:SubClass,false:Class}|SubClass,Class]}*/ o != null) {
39+
c = new SubClass(String);
40+
}
41+
/*{c:[{true:SubClass},{false:Class}|SubClass,Class]}*/ c;
42+
print(
43+
/*{c:[{true:SubClass},{false:Class}|SubClass,Class]}*/ type ==
44+
/*{c:[{true:SubClass},{false:Class}|SubClass,Class]}*/ c
45+
?.componentType);
46+
}
47+
}
48+
}
49+
50+
main() {
51+
method1(new Class(Object), Object);
52+
method1(new Class(Object), Object, true);
53+
method1(null, Object);
54+
method1(null, Object, true);
55+
56+
method2(new Class(Object), Object);
57+
method2(new Class(Object), Object, true);
58+
method2(null, Object);
59+
method2(null, Object, true);
60+
61+
method3(new Class(Object), Object);
62+
method3(new Class(Object), Object, true);
63+
method3(null, Object);
64+
method3(null, Object, true);
65+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 ComponentFactory<T> {
6+
@pragma('dart2js:noInline')
7+
Type get componentType => T;
8+
}
9+
10+
var mm = {'String': String, 'int': int};
11+
var gf1 = ComponentFactory<int>();
12+
var gf2 = ComponentFactory<dynamic>();
13+
Map<String, ComponentFactory?> mf = {'RegExp': ComponentFactory<RegExp>()};
14+
15+
ComponentFactory? test(String s, [Map<String, ComponentFactory>? mf2]) {
16+
var f = mf[s];
17+
if (f == null) {
18+
var t = mm[s];
19+
if (mf2 != null) f = mf2[s];
20+
if (t != null && t != f?.componentType) {
21+
print('not match $t');
22+
f = gf2;
23+
}
24+
}
25+
return f;
26+
}
27+
28+
work() {
29+
Map<String, ComponentFactory> mf2 = {'int': ComponentFactory<num>()};
30+
31+
test('String');
32+
test('String', mf2);
33+
test('int');
34+
test('int', mf2);
35+
test('RegExp');
36+
test('RegExp', mf2);
37+
}
38+
39+
main() {
40+
try {
41+
work();
42+
} catch (e, st) {
43+
print(e);
44+
print(st);
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 ComponentFactory<T> {
6+
@pragma('dart2js:noInline')
7+
Type get componentType => T;
8+
}
9+
10+
var mm = {'String': String, 'int': int};
11+
var gf1 = ComponentFactory<int>();
12+
var gf2 = ComponentFactory<dynamic>();
13+
Map<String, ComponentFactory> mf = {'RegExp': ComponentFactory<RegExp>()};
14+
15+
ComponentFactory test(String s, [Map<String, ComponentFactory> mf2]) {
16+
var f = mf[s];
17+
if (f == null) {
18+
var t = mm[s];
19+
if (mf2 != null) f = mf2[s];
20+
if (t != null && t != f?.componentType) {
21+
print('not match $t');
22+
f = gf2;
23+
}
24+
}
25+
return f;
26+
}
27+
28+
work() {
29+
Map<String, ComponentFactory> mf2 = {'int': ComponentFactory<num>()};
30+
31+
test('String');
32+
test('String', mf2);
33+
test('int');
34+
test('int', mf2);
35+
test('RegExp');
36+
test('RegExp', mf2);
37+
}
38+
39+
main() {
40+
try {
41+
work();
42+
} catch (e, st) {
43+
print(e);
44+
print(st);
45+
}
46+
}

0 commit comments

Comments
 (0)