Skip to content

Commit a125754

Browse files
srawlinsCommit Bot
authored and
Commit Bot
committed
analyzer: skip CodeChecker for null safe libraries
In some profiling, I saw that CodeChecker accounted for ~10% of the time spent in `LibraryAnalyzer._computeVerifyErrors`. The only errors which are reported any longer in CodeChecker are pre-null safe errors, and the TOP_LEVEL_CYCLE error, which is moved to resolution. Change-Id: I9ad38a1c76490a95cbb8464465e37a07622b4f97 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/240643 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 20bb5ac commit a125754

File tree

6 files changed

+75
-56
lines changed

6 files changed

+75
-56
lines changed

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

+8-6
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,14 @@ class LibraryAnalyzer {
440440
void _computeVerifyErrors(FileState file, CompilationUnit unit) {
441441
ErrorReporter errorReporter = _getErrorReporter(file);
442442

443-
CodeChecker checker = CodeChecker(
444-
_typeProvider,
445-
_typeSystem,
446-
errorReporter,
447-
);
448-
checker.visitCompilationUnit(unit);
443+
if (!unit.featureSet.isEnabled(Feature.non_nullable)) {
444+
CodeChecker checker = CodeChecker(
445+
_typeProvider,
446+
_typeSystem,
447+
errorReporter,
448+
);
449+
checker.visitCompilationUnit(unit);
450+
}
449451

450452
//
451453
// Validate the directives.

pkg/analyzer/lib/src/dart/micro/library_analyzer.dart

+8-6
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,14 @@ class LibraryAnalyzer {
361361
void _computeVerifyErrors(FileState file, CompilationUnit unit) {
362362
ErrorReporter errorReporter = _getErrorReporter(file);
363363

364-
CodeChecker checker = CodeChecker(
365-
_typeProvider,
366-
_typeSystem,
367-
errorReporter,
368-
);
369-
checker.visitCompilationUnit(unit);
364+
if (!unit.featureSet.isEnabled(Feature.non_nullable)) {
365+
CodeChecker checker = CodeChecker(
366+
_typeProvider,
367+
_typeSystem,
368+
errorReporter,
369+
);
370+
checker.visitCompilationUnit(unit);
371+
}
370372

371373
//
372374
// Validate the directives.

pkg/analyzer/lib/src/error/codes.g.dart

+16
Original file line numberDiff line numberDiff line change
@@ -7688,6 +7688,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
76887688
* Parameters:
76897689
* 0: the type of the function
76907690
* 1: the expected function type
7691+
*
7692+
* This error is only reported in libraries which are not null safe.
76917693
*/
76927694
static const CompileTimeErrorCode INVALID_CAST_FUNCTION =
76937695
CompileTimeErrorCode(
@@ -7700,6 +7702,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
77007702
* Parameters:
77017703
* 0: the type of the torn-off function expression
77027704
* 1: the expected function type
7705+
*
7706+
* This error is only reported in libraries which are not null safe.
77037707
*/
77047708
static const CompileTimeErrorCode INVALID_CAST_FUNCTION_EXPR =
77057709
CompileTimeErrorCode(
@@ -7713,6 +7717,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
77137717
* Parameters:
77147718
* 0: the type of the literal
77157719
* 1: the expected type
7720+
*
7721+
* This error is only reported in libraries which are not null safe.
77167722
*/
77177723
static const CompileTimeErrorCode INVALID_CAST_LITERAL = CompileTimeErrorCode(
77187724
'INVALID_CAST_LITERAL',
@@ -7723,6 +7729,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
77237729
* Parameters:
77247730
* 0: the type of the list literal
77257731
* 1: the expected type
7732+
*
7733+
* This error is only reported in libraries which are not null safe.
77267734
*/
77277735
static const CompileTimeErrorCode INVALID_CAST_LITERAL_LIST =
77287736
CompileTimeErrorCode(
@@ -7736,6 +7744,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
77367744
* Parameters:
77377745
* 0: the type of the map literal
77387746
* 1: the expected type
7747+
*
7748+
* This error is only reported in libraries which are not null safe.
77397749
*/
77407750
static const CompileTimeErrorCode INVALID_CAST_LITERAL_MAP =
77417751
CompileTimeErrorCode(
@@ -7749,6 +7759,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
77497759
* Parameters:
77507760
* 0: the type of the set literal
77517761
* 1: the expected type
7762+
*
7763+
* This error is only reported in libraries which are not null safe.
77527764
*/
77537765
static const CompileTimeErrorCode INVALID_CAST_LITERAL_SET =
77547766
CompileTimeErrorCode(
@@ -7762,6 +7774,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
77627774
* Parameters:
77637775
* 0: the type of the torn-off method
77647776
* 1: the expected function type
7777+
*
7778+
* This error is only reported in libraries which are not null safe.
77657779
*/
77667780
static const CompileTimeErrorCode INVALID_CAST_METHOD = CompileTimeErrorCode(
77677781
'INVALID_CAST_METHOD',
@@ -7774,6 +7788,8 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
77747788
* Parameters:
77757789
* 0: the type of the instantiated object
77767790
* 1: the expected type
7791+
*
7792+
* This error is only reported in libraries which are not null safe.
77777793
*/
77787794
static const CompileTimeErrorCode INVALID_CAST_NEW_EXPR =
77797795
CompileTimeErrorCode(

pkg/analyzer/lib/src/generated/resolver.dart

+23
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import 'package:analyzer/src/generated/static_type_analyzer.dart';
7373
import 'package:analyzer/src/generated/this_access_tracker.dart';
7474
import 'package:analyzer/src/generated/utilities_dart.dart';
7575
import 'package:analyzer/src/generated/variable_type_provider.dart';
76+
import 'package:analyzer/src/task/inference_error.dart';
7677
import 'package:analyzer/src/util/ast_data_extractor.dart';
7778
import 'package:meta/meta.dart';
7879

@@ -2488,6 +2489,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
24882489
isLate: parent.isLate,
24892490
isImplicitlyTyped: declaredType == null);
24902491
}
2492+
_checkTopLevelCycle(node);
24912493
}
24922494

24932495
@override
@@ -2537,6 +2539,27 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
25372539
_yieldStatementResolver.resolve(node);
25382540
}
25392541

2542+
void _checkTopLevelCycle(VariableDeclaration node) {
2543+
var element = node.declaredElement;
2544+
if (element is! PropertyInducingElementImpl) {
2545+
return;
2546+
}
2547+
// Errors on const are reported separately with
2548+
// [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT].
2549+
if (element.isConst) {
2550+
return;
2551+
}
2552+
var error = element.typeInferenceError;
2553+
if (error == null) {
2554+
return;
2555+
}
2556+
if (error.kind == TopLevelInferenceErrorKind.dependencyCycle) {
2557+
var argumentsText = error.arguments.join(', ');
2558+
errorReporter.reportErrorForNode(CompileTimeErrorCode.TOP_LEVEL_CYCLE,
2559+
node.name, [node.name.name, argumentsText]);
2560+
}
2561+
}
2562+
25402563
/// Creates a union of `T | Future<T>`, unless `T` is already a
25412564
/// future-union, in which case it simply returns `T`.
25422565
DartType _createFutureOr(DartType type) {

pkg/analyzer/lib/src/task/strong/checker.dart

+4-44
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
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/ast/ast.dart';
76
import 'package:analyzer/dart/ast/token.dart';
87
import 'package:analyzer/dart/ast/visitor.dart';
@@ -13,11 +12,9 @@ import 'package:analyzer/dart/element/type_provider.dart';
1312
import 'package:analyzer/error/error.dart';
1413
import 'package:analyzer/error/listener.dart';
1514
import 'package:analyzer/src/dart/ast/extensions.dart';
16-
import 'package:analyzer/src/dart/element/element.dart';
1715
import 'package:analyzer/src/dart/element/type.dart';
1816
import 'package:analyzer/src/dart/element/type_system.dart';
1917
import 'package:analyzer/src/error/codes.dart' show CompileTimeErrorCode;
20-
import 'package:analyzer/src/task/inference_error.dart';
2118

2219
Element? _getKnownElement(Expression expression) {
2320
if (expression is ParenthesizedExpression) {
@@ -34,25 +31,15 @@ Element? _getKnownElement(Expression expression) {
3431
return null;
3532
}
3633

37-
/// Checks the body of functions and properties.
34+
/// Checks the body of functions and properties for implicit cast errors in
35+
/// pre-null safe libraries.
3836
class CodeChecker extends RecursiveAstVisitor {
3937
final TypeSystemImpl _typeSystem;
4038
final TypeProvider _typeProvider;
4139
final ErrorReporter _errorReporter;
4240

43-
late final FeatureSet _featureSet;
44-
4541
CodeChecker(this._typeProvider, this._typeSystem, this._errorReporter);
4642

47-
bool get _isNonNullableByDefault =>
48-
_featureSet.isEnabled(Feature.non_nullable);
49-
50-
NullabilitySuffix get _noneOrStarSuffix {
51-
return _isNonNullableByDefault
52-
? NullabilitySuffix.none
53-
: NullabilitySuffix.star;
54-
}
55-
5643
void checkArgument(Expression arg, DartType expectedType) {
5744
// Preserve named argument structure, so their immediate parent is the
5845
// method invocation.
@@ -172,7 +159,6 @@ class CodeChecker extends RecursiveAstVisitor {
172159

173160
@override
174161
void visitCompilationUnit(CompilationUnit node) {
175-
_featureSet = node.featureSet;
176162
node.visitChildren(this);
177163
}
178164

@@ -385,28 +371,6 @@ class CodeChecker extends RecursiveAstVisitor {
385371
node.visitChildren(this);
386372
}
387373

388-
@override
389-
void visitVariableDeclaration(VariableDeclaration node) {
390-
var element = node.declaredElement;
391-
if (element is PropertyInducingElementImpl) {
392-
var error = element.typeInferenceError;
393-
if (error != null) {
394-
if (error.kind == TopLevelInferenceErrorKind.dependencyCycle) {
395-
// Errors on const should have been reported with
396-
// [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT].
397-
if (!element.isConst) {
398-
_recordMessage(
399-
node.name,
400-
CompileTimeErrorCode.TOP_LEVEL_CYCLE,
401-
[element.name, error.arguments],
402-
);
403-
}
404-
}
405-
}
406-
}
407-
node.visitChildren(this);
408-
}
409-
410374
@override
411375
void visitVariableDeclarationList(VariableDeclarationList node) {
412376
var type = node.type;
@@ -520,10 +484,6 @@ class CodeChecker extends RecursiveAstVisitor {
520484
bool forSpread = false,
521485
bool forSpreadKey = false,
522486
bool forSpreadValue = false}) {
523-
if (_isNonNullableByDefault) {
524-
return;
525-
}
526-
527487
expr = expr.unParenthesized;
528488
if (_needsImplicitCast(expr, to: to, from: from) == true) {
529489
_recordImplicitCast(expr, to,
@@ -627,7 +587,7 @@ class CodeChecker extends RecursiveAstVisitor {
627587
// Ensure it's at least a Stream / Iterable.
628588
return expectedElement.instantiate(
629589
typeArguments: [_typeProvider.dynamicType],
630-
nullabilitySuffix: _noneOrStarSuffix,
590+
nullabilitySuffix: NullabilitySuffix.star,
631591
);
632592
} else {
633593
// Analyzer will provide a separate error if expected type
@@ -836,7 +796,7 @@ class CodeChecker extends RecursiveAstVisitor {
836796
if (elementType == null) {
837797
var sequenceType = sequenceElement.instantiate(
838798
typeArguments: [_typeProvider.dynamicType],
839-
nullabilitySuffix: _noneOrStarSuffix,
799+
nullabilitySuffix: NullabilitySuffix.star,
840800
);
841801

842802
if (_typeSystem.isSubtypeOf(sequenceType, iterableType)) {

pkg/analyzer/messages.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -6771,48 +6771,64 @@ CompileTimeErrorCode:
67716771
Parameters:
67726772
0: the type of the function
67736773
1: the expected function type
6774+
6775+
This error is only reported in libraries which are not null safe.
67746776
INVALID_CAST_FUNCTION_EXPR:
67756777
problemMessage: "The function expression type '{0}' isn't of type '{1}'. This means its parameter or return type doesn't match what is expected. Consider changing parameter type(s) or the returned type(s)."
67766778
comment: |-
67776779
Parameters:
67786780
0: the type of the torn-off function expression
67796781
1: the expected function type
6782+
6783+
This error is only reported in libraries which are not null safe.
67806784
INVALID_CAST_LITERAL:
67816785
problemMessage: "The literal '{0}' with type '{1}' isn't of expected type '{2}'."
67826786
comment: |-
67836787
Parameters:
67846788
0: the type of the literal
67856789
1: the expected type
6790+
6791+
This error is only reported in libraries which are not null safe.
67866792
INVALID_CAST_LITERAL_LIST:
67876793
problemMessage: "The list literal type '{0}' isn't of expected type '{1}'. The list's type can be changed with an explicit generic type argument or by changing the element types."
67886794
comment: |-
67896795
Parameters:
67906796
0: the type of the list literal
67916797
1: the expected type
6798+
6799+
This error is only reported in libraries which are not null safe.
67926800
INVALID_CAST_LITERAL_MAP:
67936801
problemMessage: "The map literal type '{0}' isn't of expected type '{1}'. The map's type can be changed with an explicit generic type arguments or by changing the key and value types."
67946802
comment: |-
67956803
Parameters:
67966804
0: the type of the map literal
67976805
1: the expected type
6806+
6807+
This error is only reported in libraries which are not null safe.
67986808
INVALID_CAST_LITERAL_SET:
67996809
problemMessage: "The set literal type '{0}' isn't of expected type '{1}'. The set's type can be changed with an explicit generic type argument or by changing the element types."
68006810
comment: |-
68016811
Parameters:
68026812
0: the type of the set literal
68036813
1: the expected type
6814+
6815+
This error is only reported in libraries which are not null safe.
68046816
INVALID_CAST_METHOD:
68056817
problemMessage: "The method tear-off '{0}' has type '{1}' that isn't of expected type '{2}'. This means its parameter or return type doesn't match what is expected."
68066818
comment: |-
68076819
Parameters:
68086820
0: the type of the torn-off method
68096821
1: the expected function type
6822+
6823+
This error is only reported in libraries which are not null safe.
68106824
INVALID_CAST_NEW_EXPR:
68116825
problemMessage: "The constructor returns type '{0}' that isn't of expected type '{1}'."
68126826
comment: |-
68136827
Parameters:
68146828
0: the type of the instantiated object
68156829
1: the expected type
6830+
6831+
This error is only reported in libraries which are not null safe.
68166832
INVALID_CONSTANT:
68176833
problemMessage: Invalid constant value.
68186834
comment: |-

0 commit comments

Comments
 (0)