Skip to content

Commit

Permalink
Test field promotion during top level inference.
Browse files Browse the repository at this point in the history
Bug: dart-lang/language#2020
Change-Id: I58d7f5691a4af614bcd481a109e340ca709fe4ec
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/270760
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
stereotype441 authored and Commit Queue committed Nov 21, 2022
1 parent 22c46e5 commit b848912
Showing 1 changed file with 98 additions and 0 deletions.
@@ -0,0 +1,98 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Tests that field promotion works during top level type inference. In the
// CFE, top level types are inferred earlier than method bodies, so this
// verifies that the data structures necessary to support field promotion have
// been initialized in time.

// SharedOptions=--enable-experiment=inference-update-2

class C {
final int? _promotable;
final int? _notPromotable; // due to D._notPromotable

C(int i)
: _promotable = i,
_notPromotable = i;

static final staticPromotable =
((C c) => c._promotable != null ? c._promotable : 0)(new C(0));

static final staticNotPromotable =
((C c) => c._notPromotable != null ? c._notPromotable : 0)(new C(0));

final instancePromotable =
((C c) => c._promotable != null ? c._promotable : 0)(new C(0));

final instanceNotPromotable =
((C c) => c._notPromotable != null ? c._notPromotable : 0)(new C(0));

late final instancePromotableViaThis = _promotable != null ? _promotable : 0;

late final instanceNotPromotableViaThis =
_notPromotable != null ? _notPromotable : 0;
}

class D {
int? _notPromotable;
}

final topLevelPromotable =
((C c) => c._promotable != null ? c._promotable : 0)(new C(0));

final topLevelNotPromotable =
((C c) => c._notPromotable != null ? c._notPromotable : 0)(new C(0));

void acceptsInt(int x) {}

void testTopLevelPromotable() {
var x = topLevelPromotable;
// `x` has type `int` so this is ok
acceptsInt(x);
}

void testTopLevelNotPromotable() {
var x = topLevelNotPromotable;
// `x` has type `int?` so this is ok
x = null;
}

void testStaticPromotable() {
var x = C.staticPromotable;
// `x` has type `int` so this is ok
acceptsInt(x);
}

void testStaticNotPromotable() {
var x = C.staticNotPromotable;
// `x` has type `int?` so this is ok
x = null;
}

void testInstancePromotable(C c) {
var x = c.instancePromotable;
// `x` has type `int` so this is ok
acceptsInt(x);
}

void testInstanceNotPromotable(C c) {
var x = c.instanceNotPromotable;
// `x` has type `int?` so this is ok
x = null;
}

void testInstancePromotableViaThis(C c) {
var x = c.instancePromotableViaThis;
// `x` has type `int` so this is ok
acceptsInt(x);
}

void testInstanceNotPromotableViaThis(C c) {
var x = c.instanceNotPromotableViaThis;
// `x` has type `int?` so this is ok
x = null;
}

main() {}

0 comments on commit b848912

Please sign in to comment.