blob: c65fe3bb029cfb9cb8f428d40add6e99a0aafc38 [file] [log] [blame]
// Copyright (c) 2016, 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.
/// @assertion Evaluation of a compound assignment of the form e1?.v op = e2 is
/// equivalent to ((x) => x?.v = x.v op e2)(e1) where x is a variable that is not
/// used in e2.
/// @description Checks that an expression of the form e1?.v *= e2 is equivalent
/// to ((x) => x?.v = x.v * e2)(e1) where x is a variable that is not used in e2.
/// @author sgrekhov@unipro.ru
import '../../../../Utils/expect.dart';
class C {
int? _v;
int getterInvocation = 0;
int setterInvocation = 0;
C(val) {
_v = val;
}
get v {
++getterInvocation;
return _v;
}
set v(val) {
++setterInvocation;
_v = val;
}
}
main() {
C? c1 = null;
var res1 = (c1?.v *= 2);
Expect.isNull(res1);
Expect.isNull(c1);
C c2 = new C(null);
Expect.throws(() {c2?.v *= 2;});
// ^^
// [analyzer] STATIC_WARNING.INVALID_NULL_AWARE_OPERATOR
// ^
// [cfe] Operand of null-aware operation '?.' has type 'C' which excludes null.
C c3 = new C(3);
var res3 = (c3?.v *= 3);
// ^^
// [analyzer] STATIC_WARNING.INVALID_NULL_AWARE_OPERATOR
// ^
// [cfe] Operand of null-aware operation '?.' has type 'C' which excludes null.
Expect.equals(1, c3.getterInvocation);
Expect.equals(1, c3.setterInvocation);
Expect.equals(9, res3);
Expect.equals(9, c3.v);
}