Skip to content

Commit aaab579

Browse files
author
Dart CI
committed
Version 2.11.0-231.0.dev
Merge commit '89dbc35fca4c994d5ff1c076e1c564519c687f0c' into 'dev'
2 parents b58cfe5 + 89dbc35 commit aaab579

File tree

89 files changed

+4286
-3089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+4286
-3089
lines changed

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ vars = {
133133
"quiver-dart_tag": "246e754fe45cecb6aa5f3f13b4ed61037ff0d784",
134134
"resource_rev": "f8e37558a1c4f54550aa463b88a6a831e3e33cd6",
135135
"root_certificates_rev": "7e5ec82c99677a2e5b95ce296c4d68b0d3378ed8",
136-
"rust_revision": "cbe7c5ce705896d4e22bf6096590bc1f17993b78",
136+
"rust_revision": "b7856f695d65a8ebc846754f97d15814bcb1c244",
137137
"shelf_static_rev": "v0.2.8",
138138
"shelf_packages_handler_tag": "2.0.0",
139139
"shelf_proxy_tag": "0.1.0+7",

pkg/analyzer/lib/src/error/best_practices_verifier.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,10 +1657,12 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
16571657
Set<TargetKind> _targetKindsFor(ElementAnnotation annotation) {
16581658
var element = annotation.element;
16591659
ClassElement classElement;
1660-
if (element is VariableElement) {
1661-
var type = element.type;
1662-
if (type is InterfaceType) {
1663-
classElement = type.element;
1660+
if (element is PropertyAccessorElement) {
1661+
if (element.isGetter) {
1662+
var type = element.returnType;
1663+
if (type is InterfaceType) {
1664+
classElement = type.element;
1665+
}
16641666
}
16651667
} else if (element is ConstructorElement) {
16661668
classElement = element.enclosingElement;

pkg/analyzer/test/src/dart/element/nullable_test.dart

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/analysis/features.dart';
65
import 'package:analyzer/dart/element/element.dart';
76
import 'package:analyzer/dart/element/nullability_suffix.dart';
87
import 'package:analyzer/dart/element/type.dart';
9-
import 'package:analyzer/dart/element/type_provider.dart';
108
import 'package:analyzer/src/dart/element/type.dart';
11-
import 'package:analyzer/src/dart/element/type_system.dart';
129
import 'package:meta/meta.dart';
1310
import 'package:test/test.dart';
1411
import 'package:test_reflective_loader/test_reflective_loader.dart';
1512

16-
import '../../../generated/elements_types_mixin.dart';
17-
import '../../../generated/test_analysis_context.dart';
13+
import '../../../generated/type_system_test.dart';
1814

1915
main() {
2016
defineReflectiveSuite(() {
@@ -28,7 +24,7 @@ main() {
2824
}
2925

3026
@reflectiveTest
31-
class IsNonNullableTest extends _NullableBase {
27+
class IsNonNullableTest extends AbstractTypeSystemNullSafetyTest {
3228
void isNonNullable(DartType type) {
3329
expect(typeSystem.isNonNullable(type), isTrue);
3430
}
@@ -193,7 +189,7 @@ class IsNonNullableTest extends _NullableBase {
193189
}
194190

195191
@reflectiveTest
196-
class IsNullableTest extends _NullableBase {
192+
class IsNullableTest extends AbstractTypeSystemNullSafetyTest {
197193
void isNotNullable(DartType type) {
198194
expect(typeSystem.isNullable(type), isFalse);
199195
}
@@ -358,7 +354,7 @@ class IsNullableTest extends _NullableBase {
358354
}
359355

360356
@reflectiveTest
361-
class IsPotentiallyNonNullableTest extends _NullableBase {
357+
class IsPotentiallyNonNullableTest extends AbstractTypeSystemNullSafetyTest {
362358
void isNotPotentiallyNonNullable(DartType type) {
363359
expect(typeSystem.isPotentiallyNonNullable(type), isFalse);
364360
}
@@ -405,7 +401,7 @@ class IsPotentiallyNonNullableTest extends _NullableBase {
405401
}
406402

407403
@reflectiveTest
408-
class IsPotentiallyNullableTest extends _NullableBase {
404+
class IsPotentiallyNullableTest extends AbstractTypeSystemNullSafetyTest {
409405
void isNotPotentiallyNullable(DartType type) {
410406
expect(typeSystem.isPotentiallyNullable(type), isFalse);
411407
}
@@ -452,7 +448,7 @@ class IsPotentiallyNullableTest extends _NullableBase {
452448
}
453449

454450
@reflectiveTest
455-
class IsStrictlyNonNullableTest extends _NullableBase {
451+
class IsStrictlyNonNullableTest extends AbstractTypeSystemNullSafetyTest {
456452
void isNotStrictlyNonNullable(DartType type) {
457453
expect(typeSystem.isStrictlyNonNullable(type), isFalse);
458454
}
@@ -599,7 +595,7 @@ class IsStrictlyNonNullableTest extends _NullableBase {
599595
}
600596

601597
@reflectiveTest
602-
class PromoteToNonNullTest extends _NullableBase {
598+
class PromoteToNonNullTest extends AbstractTypeSystemNullSafetyTest {
603599
test_dynamic() {
604600
_check(dynamicType, dynamicType);
605601
}
@@ -797,22 +793,3 @@ class PromoteToNonNullTest extends _NullableBase {
797793
expect(actual.nullabilitySuffix, NullabilitySuffix.none);
798794
}
799795
}
800-
801-
abstract class _NullableBase with ElementsTypesMixin {
802-
@override
803-
TypeProvider typeProvider;
804-
805-
TypeSystemImpl typeSystem;
806-
807-
FeatureSet get testFeatureSet {
808-
return FeatureSet.forTesting();
809-
}
810-
811-
void setUp() {
812-
var analysisContext = TestAnalysisContext(
813-
featureSet: testFeatureSet,
814-
);
815-
typeProvider = analysisContext.typeProviderLegacy;
816-
typeSystem = analysisContext.typeSystemLegacy;
817-
}
818-
}

pkg/analyzer/test/src/diagnostics/invalid_annotation_target_test.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mixin M {}
4747
]);
4848
}
4949

50-
void test_classType_topLevelVariable() async {
50+
void test_classType_topLevelVariable_constructor() async {
5151
writeTestPackageConfigWithMeta();
5252
await assertErrorsInCode('''
5353
import 'package:meta/meta_meta.dart';
@@ -64,6 +64,25 @@ int x = 0;
6464
]);
6565
}
6666

67+
void test_classType_topLevelVariable_topLevelConstant() async {
68+
writeTestPackageConfigWithMeta();
69+
await assertErrorsInCode('''
70+
import 'package:meta/meta_meta.dart';
71+
72+
@Target({TargetKind.classType})
73+
class A {
74+
const A();
75+
}
76+
77+
const a = A();
78+
79+
@a
80+
int x = 0;
81+
''', [
82+
error(HintCode.INVALID_ANNOTATION_TARGET, 114, 1),
83+
]);
84+
}
85+
6786
void test_enumType_class() async {
6887
writeTestPackageConfigWithMeta();
6988
await assertErrorsInCode('''

pkg/dev_compiler/lib/src/kernel/compiler.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
250250
types,
251251
hierarchy,
252252
jsTypeRep,
253-
NullableInference(jsTypeRep, staticTypeContext),
253+
NullableInference(jsTypeRep, staticTypeContext, options: options),
254254
staticTypeContext,
255255
options,
256256
importToSummary,
@@ -4271,9 +4271,9 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
42714271

42724272
if (_reifyTearoff(member)) {
42734273
return runtimeCall('bind(#, #)', [jsReceiver, jsName]);
4274-
} else if (isJsMember(member) &&
4275-
member is Procedure &&
4276-
!member.isAccessor) {
4274+
} else if (member is Procedure &&
4275+
!member.isAccessor &&
4276+
isJsMember(member)) {
42774277
return runtimeCall(
42784278
'tearoffInterop(#)', [js_ast.PropertyAccess(jsReceiver, jsName)]);
42794279
} else {
@@ -4472,7 +4472,9 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
44724472
}
44734473

44744474
bool _isDynamicOrFunction(DartType t) =>
4475-
t == _coreTypes.functionLegacyRawType || t == const DynamicType();
4475+
DartTypeEquivalence(_coreTypes, ignoreTopLevelNullability: true)
4476+
.areEqual(t, _coreTypes.functionNonNullableRawType) ||
4477+
t == const DynamicType();
44764478

44774479
js_ast.Expression _emitUnaryOperator(
44784480
Expression expr, Member target, InvocationExpression node) {
@@ -5329,8 +5331,9 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
53295331
if (jsExpr is js_ast.LiteralString && jsExpr.valueWithoutQuotes.isEmpty) {
53305332
continue;
53315333
}
5332-
parts.add(e.getStaticType(_staticTypeContext) ==
5333-
_types.coreTypes.stringLegacyRawType &&
5334+
var type = e.getStaticType(_staticTypeContext);
5335+
parts.add(DartTypeEquivalence(_coreTypes, ignoreTopLevelNullability: true)
5336+
.areEqual(type, _coreTypes.stringNonNullableRawType) &&
53345337
!isNullable(e)
53355338
? jsExpr
53365339
: runtimeCall('str(#)', [jsExpr]));

pkg/dev_compiler/lib/src/kernel/nullable_inference.dart

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
// @dart = 2.9
66

77
import 'dart:collection';
8+
89
import 'package:kernel/core_types.dart';
910
import 'package:kernel/kernel.dart';
1011
import 'package:kernel/type_environment.dart';
12+
13+
import '../compiler/shared_command.dart' show SharedCompilerOptions;
1114
import 'js_typerep.dart';
1215
import 'kernel_helpers.dart';
1316

@@ -40,8 +43,12 @@ class NullableInference extends ExpressionVisitor<bool> {
4043

4144
final _variableInference = _NullableVariableInference();
4245

43-
NullableInference(this.jsTypeRep, this._staticTypeContext)
44-
: coreTypes = jsTypeRep.coreTypes {
46+
final bool _soundNullSafety;
47+
48+
NullableInference(this.jsTypeRep, this._staticTypeContext,
49+
{SharedCompilerOptions options})
50+
: coreTypes = jsTypeRep.coreTypes,
51+
_soundNullSafety = options?.soundNullSafety ?? false {
4552
_variableInference._nullInference = this;
4653
}
4754

@@ -85,59 +92,71 @@ class NullableInference extends ExpressionVisitor<bool> {
8592

8693
@override
8794
bool visitPropertyGet(PropertyGet node) =>
88-
_getterIsNullable(node.interfaceTarget);
95+
_getterIsNullable(node.interfaceTarget, node);
8996

9097
@override
9198
bool visitPropertySet(PropertySet node) => isNullable(node.value);
9299

93100
@override
94101
bool visitSuperPropertyGet(SuperPropertyGet node) =>
95-
_getterIsNullable(node.interfaceTarget);
102+
_getterIsNullable(node.interfaceTarget, node);
96103

97104
@override
98105
bool visitSuperPropertySet(SuperPropertySet node) => isNullable(node.value);
99106

100107
@override
101-
bool visitStaticGet(StaticGet node) => _getterIsNullable(node.target);
108+
bool visitStaticGet(StaticGet node) => _getterIsNullable(node.target, node);
102109

103110
@override
104111
bool visitStaticSet(StaticSet node) => isNullable(node.value);
105112

106113
@override
107114
bool visitMethodInvocation(MethodInvocation node) => _invocationIsNullable(
108-
node.interfaceTarget, node.name.text, node.receiver);
115+
node.interfaceTarget, node.name.text, node, node.receiver);
109116

110117
@override
111118
bool visitSuperMethodInvocation(SuperMethodInvocation node) =>
112-
_invocationIsNullable(node.interfaceTarget, node.name.text);
119+
_invocationIsNullable(node.interfaceTarget, node.name.text, node);
113120

114-
bool _invocationIsNullable(Member target, String name,
121+
bool _invocationIsNullable(
122+
Member target, String name, InvocationExpression node,
115123
[Expression receiver]) {
116124
// TODO(jmesserly): this is not a valid assumption for user-defined equality
117125
// but it is added to match the behavior of the Analyzer backend.
118126
// https://github.com/dart-lang/sdk/issues/31854
119127
if (name == '==') return false;
128+
if (_staticallyNonNullable(node.getStaticType(_staticTypeContext))) {
129+
return false;
130+
}
120131
if (target == null) return true; // dynamic call
121132
if (target.name.text == 'toString' &&
122133
receiver != null &&
123134
receiver.getStaticType(_staticTypeContext) ==
124135
coreTypes.stringLegacyRawType) {
125136
// TODO(jmesserly): `class String` in dart:core does not explicitly
126137
// declare `toString`, which results in a target of `Object.toString` even
127-
// when the reciever type is known to be `String`. So we work around it.
138+
// when the receiver type is known to be `String`. So we work around it.
128139
// (The Analyzer backend of DDC probably has the same issue.)
129140
return false;
130141
}
131142
return _returnValueIsNullable(target);
132143
}
133144

134-
bool _getterIsNullable(Member target) {
145+
bool _getterIsNullable(Member target, Expression node) {
146+
if (_staticallyNonNullable(node.getStaticType(_staticTypeContext))) {
147+
return false;
148+
}
135149
if (target == null) return true;
136150
// tear-offs are not null
137151
if (target is Procedure && !target.isAccessor) return false;
138152
return _returnValueIsNullable(target);
139153
}
140154

155+
bool _staticallyNonNullable(DartType type) =>
156+
_soundNullSafety &&
157+
type != null &&
158+
type.nullability == Nullability.nonNullable;
159+
141160
bool _returnValueIsNullable(Member target) {
142161
var targetClass = target.enclosingClass;
143162
if (targetClass != null) {
@@ -179,7 +198,7 @@ class NullableInference extends ExpressionVisitor<bool> {
179198
typeString.split('|').contains('Null');
180199
}
181200
}
182-
return _invocationIsNullable(target, node.name.text);
201+
return _invocationIsNullable(target, node.name.text, node);
183202
}
184203

185204
@override
@@ -202,7 +221,10 @@ class NullableInference extends ExpressionVisitor<bool> {
202221
bool visitIsExpression(IsExpression node) => false;
203222

204223
@override
205-
bool visitAsExpression(AsExpression node) => isNullable(node.operand);
224+
bool visitAsExpression(AsExpression node) =>
225+
_staticallyNonNullable(node.getStaticType(_staticTypeContext))
226+
? false
227+
: isNullable(node.operand);
206228

207229
@override
208230
bool visitSymbolLiteral(SymbolLiteral node) => false;
@@ -226,7 +248,8 @@ class NullableInference extends ExpressionVisitor<bool> {
226248
bool visitMapLiteral(MapLiteral node) => false;
227249

228250
@override
229-
bool visitAwaitExpression(AwaitExpression node) => true;
251+
bool visitAwaitExpression(AwaitExpression node) =>
252+
!_staticallyNonNullable(node.getStaticType(_staticTypeContext));
230253

231254
@override
232255
bool visitFunctionExpression(FunctionExpression node) => false;
@@ -336,7 +359,8 @@ class _NullableVariableInference extends RecursiveVisitor<void> {
336359
@override
337360
void visitFunctionNode(FunctionNode node) {
338361
_functions.add(node);
339-
if (_nullInference.allowNotNullDeclarations) {
362+
if (_nullInference.allowNotNullDeclarations ||
363+
_nullInference._soundNullSafety) {
340364
visitList(node.positionalParameters, this);
341365
visitList(node.namedParameters, this);
342366
}
@@ -362,9 +386,14 @@ class _NullableVariableInference extends RecursiveVisitor<void> {
362386
}
363387
}
364388
var initializer = node.initializer;
365-
// A Variable declaration with a FunctionNode as a parent is a function
366-
// parameter so we can't trust the initializer as a nullable check.
367-
if (node.parent is! FunctionNode) {
389+
if (_nullInference._soundNullSafety &&
390+
node.type.nullability == Nullability.nonNullable) {
391+
// Avoid null checks for variables when the type system guarantees they
392+
// can never be null.
393+
_notNullLocals.add(node);
394+
} else if (node.parent is! FunctionNode) {
395+
// A variable declaration with a FunctionNode as a parent is a function
396+
// parameter so we can't trust the initializer as a nullable check.
368397
if (initializer != null) {
369398
var savedVariable = _variableAssignedTo;
370399
_variableAssignedTo = node;
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|3725|5|94|Const constructors can't throw exceptions.
2-
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|7913|5|97|Const constructors can't throw exceptions.
2+
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|7925|5|97|Const constructors can't throw exceptions.
33
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|939|5|95|Const constructors can't throw exceptions.
44
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|972|5|94|Const constructors can't throw exceptions.
5-
ERROR|COMPILE_TIME_ERROR|INVALID_ASSIGNMENT|lib/_internal/js_dev_runtime/private/interceptors.dart|1348|18|27|A value of type 'double' can't be assigned to a variable of type 'int'.
6-
ERROR|COMPILE_TIME_ERROR|RETURN_OF_INVALID_TYPE|lib/_internal/js_dev_runtime/private/interceptors.dart|1215|14|38|A value of type 'double' can't be returned from method '%' because it has a return type of 'JSNumber'.
7-
ERROR|COMPILE_TIME_ERROR|RETURN_OF_INVALID_TYPE|lib/_internal/js_dev_runtime/private/interceptors.dart|1217|14|38|A value of type 'double' can't be returned from method '%' because it has a return type of 'JSNumber'.
5+
ERROR|COMPILE_TIME_ERROR|INVALID_ASSIGNMENT|lib/_internal/js_dev_runtime/private/interceptors.dart|1358|18|27|A value of type 'double' can't be assigned to a variable of type 'int'.
6+
ERROR|COMPILE_TIME_ERROR|RETURN_OF_INVALID_TYPE|lib/_internal/js_dev_runtime/private/interceptors.dart|1225|14|38|A value of type 'double' can't be returned from method '%' because it has a return type of 'JSNumber'.
7+
ERROR|COMPILE_TIME_ERROR|RETURN_OF_INVALID_TYPE|lib/_internal/js_dev_runtime/private/interceptors.dart|1227|14|38|A value of type 'double' can't be returned from method '%' because it has a return type of 'JSNumber'.
88
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|3723|3|5|Only redirecting factory constructors can be declared to be 'const'.
9-
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|7911|3|5|Only redirecting factory constructors can be declared to be 'const'.
9+
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|7923|3|5|Only redirecting factory constructors can be declared to be 'const'.
1010
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|937|3|5|Only redirecting factory constructors can be declared to be 'const'.
1111
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|970|3|5|Only redirecting factory constructors can be declared to be 'const'.

0 commit comments

Comments
 (0)