Files
sdk/tests/language/super/conditional_operator_test.dart
T
Johnni Winther 4b21f58d39 [cfe] Don't emit warnings on null-aware access on non-nullable
Change-Id: I94f3011f69330f5b2d8998f29eb9511517e1fffa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/357301
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
2024-03-20 14:04:29 +00:00

118 lines
4.8 KiB
Dart

// Copyright (c) 2015, 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.
// Verify that the ?. operator cannot be used with "super".
class B {
B();
B.namedConstructor();
var field = 1;
method() => 1;
}
class C extends B {
C()
: super?.namedConstructor()
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] Cannot use '?.' here.
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
;
test() {
super?.field = 1;
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
super?.field += 1;
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
super?.field ??= 1;
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
// ^
// [analyzer] STATIC_WARNING.DEAD_NULL_AWARE_EXPRESSION
super?.field;
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
1 * super?.field;
// ^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.ARGUMENT_TYPE_NOT_ASSIGNABLE
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
-super?.field;
// ^
// [analyzer] COMPILE_TIME_ERROR.UNCHECKED_USE_OF_NULLABLE_VALUE
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
~super?.field;
// ^
// [analyzer] COMPILE_TIME_ERROR.UNCHECKED_USE_OF_NULLABLE_VALUE
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
!super?.field;
// ^^^^^^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.NON_BOOL_NEGATION_EXPRESSION
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
// ^
// [cfe] A value of type 'int' can't be assigned to a variable of type 'bool'.
--super?.field;
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
++super?.field;
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
super?.method();
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
1 * super?.method();
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
-super?.method();
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
~super?.method();
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
!super?.method();
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
--super?.method();
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
// ^
// [cfe] Can't assign to this.
// ^
// [analyzer] SYNTACTIC_ERROR.MISSING_ASSIGNABLE_SELECTOR
++super?.method();
// ^^
// [analyzer] SYNTACTIC_ERROR.INVALID_OPERATOR_QUESTIONMARK_PERIOD_FOR_SUPER
// [cfe] The operator '?.' cannot be used with 'super' because 'super' cannot be null.
// ^
// [cfe] Can't assign to this.
// ^
// [analyzer] SYNTACTIC_ERROR.MISSING_ASSIGNABLE_SELECTOR
}
}
main() {
new C().test();
}