Skip to content

Commit

Permalink
Legacy. Remove uses of WithoutNullSafetyMixin, part 4.
Browse files Browse the repository at this point in the history
Change-Id: I78429ada7a65f4efa07675408a0075ae5fa3d408
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346182
Reviewed-by: Samuel Rawlins <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Jan 14, 2024
1 parent f9ba7a9 commit e871af6
Show file tree
Hide file tree
Showing 75 changed files with 3,664 additions and 9,155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1506,8 +1506,6 @@ CompileTimeErrorCode.SUPER_INVOCATION_NOT_LAST:
status: hasFix
CompileTimeErrorCode.SWITCH_CASE_COMPLETES_NORMALLY:
status: hasFix
CompileTimeErrorCode.SWITCH_EXPRESSION_NOT_ASSIGNABLE:
status: noFix
CompileTimeErrorCode.TEAROFF_OF_GENERATIVE_CONSTRUCTOR_OF_ABSTRACT_CLASS:
status: noFix
since: 2.15
Expand Down Expand Up @@ -3702,8 +3700,6 @@ WarningCode.MISSING_REQUIRED_PARAM:
status: hasFix
WarningCode.MISSING_REQUIRED_PARAM_WITH_DETAILS:
status: hasFix
WarningCode.MISSING_RETURN:
status: hasFix
WarningCode.MIXIN_ON_SEALED_CLASS:
status: noFix
notes: |-
Expand Down Expand Up @@ -3770,9 +3766,6 @@ WarningCode.SDK_VERSION_CONSTRUCTOR_TEAROFFS:
since: 2.15
WarningCode.SDK_VERSION_GT_GT_GT_OPERATOR:
status: hasFix
WarningCode.SDK_VERSION_NEVER:
status: noFix
notes: Deprecated
WarningCode.SDK_VERSION_SINCE:
status: needsFix
notes: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1620,9 +1620,6 @@ class FixProcessor extends BaseProcessor {
WarningCode.MISSING_REQUIRED_PARAM_WITH_DETAILS: [
AddMissingRequiredArgument.new,
],
WarningCode.MISSING_RETURN: [
AddAsync.missingReturn,
],
WarningCode.MUST_CALL_SUPER: [
AddCallSuper.new,
],
Expand Down
13 changes: 0 additions & 13 deletions pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/analysis/declared_variables.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
Expand Down Expand Up @@ -47,7 +46,6 @@ import 'package:analyzer/src/ignore_comments/ignore_info.dart';
import 'package:analyzer/src/lint/linter.dart';
import 'package:analyzer/src/lint/linter_visitor.dart';
import 'package:analyzer/src/services/lint.dart';
import 'package:analyzer/src/task/strong/checker.dart';
import 'package:analyzer/src/util/performance/operation_performance.dart';
import 'package:analyzer/src/utilities/extensions/version.dart';
import 'package:analyzer/src/workspace/pub.dart';
Expand Down Expand Up @@ -380,16 +378,6 @@ class LibraryAnalyzer {
void _computeVerifyErrors(FileState file, CompilationUnit unit) {
ErrorReporter errorReporter = _getErrorReporter(file);

if (!unit.featureSet.isEnabled(Feature.non_nullable)) {
CodeChecker checker = CodeChecker(
_typeProvider,
_typeSystem,
errorReporter,
strictCasts: _analysisOptions.strictCasts,
);
checker.visitCompilationUnit(unit);
}

//
// Use the ConstantVerifier to compute errors.
//
Expand Down Expand Up @@ -497,7 +485,6 @@ class LibraryAnalyzer {
SdkConstraintVerifier verifier = SdkConstraintVerifier(
errorReporter,
_libraryElement,
_typeProvider,
sdkVersionConstraint.withoutPreRelease,
);
unit.accept(verifier);
Expand Down
59 changes: 1 addition & 58 deletions pkg/analyzer/lib/src/dart/constant/constant_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,8 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
_typeSystem.isAlwaysExhaustive(node.expression.typeOrThrow),
isSwitchExpression: false,
);
} else if (_currentLibrary.isNonNullableByDefault) {
_validateSwitchStatement_nullSafety(node);
} else {
_validateSwitchStatement_legacy(node);
_validateSwitchStatement_nullSafety(node);
}
});
}
Expand Down Expand Up @@ -971,61 +969,6 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
}
}

void _validateSwitchStatement_legacy(SwitchStatement node) {
// TODO(paulberry): to minimize error messages, it would be nice to
// compare all types with the most popular type rather than the first
// type.
bool foundError = false;
DartObjectImpl? firstValue;
DartType? firstType;
for (var switchMember in node.members) {
if (switchMember is SwitchCase) {
Expression expression = switchMember.expression;

var expressionValue = _evaluateAndReportError(
expression,
CompileTimeErrorCode.NON_CONSTANT_CASE_EXPRESSION,
);
if (expressionValue is! DartObjectImpl) {
continue;
}
firstValue ??= expressionValue;

var expressionValueType = _typeSystem.toLegacyTypeIfOptOut(
expressionValue.type,
);

if (firstType == null) {
firstType = expressionValueType;
} else {
if (firstType != expressionValueType) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES,
expression,
[expression.toSource(), firstType],
);
foundError = true;
}
}
}
}

if (foundError) {
return;
}

if (firstValue != null) {
final featureSet = _currentLibrary.featureSet;
if (!firstValue.hasPrimitiveEquality(featureSet)) {
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.CASE_EXPRESSION_TYPE_IMPLEMENTS_EQUALS,
node.switchKeyword,
[firstValue.type],
);
}
}
}

void _validateSwitchStatement_nullSafety(SwitchStatement node) {
void validateExpression(Expression expression) {
var expressionValue = _evaluateAndReportError(
Expand Down
28 changes: 5 additions & 23 deletions pkg/analyzer/lib/src/dart/resolver/shared_type_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:collection/collection.dart';

typedef SharedPatternField
= shared.RecordPatternField<PatternFieldImpl, DartPatternImpl>;
Expand All @@ -36,28 +35,11 @@ class SharedTypeAnalyzerErrors
required DartType scrutineeType,
required DartType caseExpressionType,
required bool nullSafetyEnabled}) {
if (nullSafetyEnabled) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode
.CASE_EXPRESSION_TYPE_IS_NOT_SWITCH_EXPRESSION_SUBTYPE,
caseExpression,
[caseExpressionType, scrutineeType]);
} else {
// We only report the error if it occurs on the first case; otherwise
// separate logic will report that different cases have different types.
var switchStatement = scrutinee.parent as SwitchStatement;
if (identical(
switchStatement.members
.whereType<SwitchCase>()
.firstOrNull
?.expression,
caseExpression)) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.SWITCH_EXPRESSION_NOT_ASSIGNABLE,
scrutinee,
[scrutineeType, caseExpressionType]);
}
}
_errorReporter.reportErrorForNode(
CompileTimeErrorCode
.CASE_EXPRESSION_TYPE_IS_NOT_SWITCH_EXPRESSION_SUBTYPE,
caseExpression,
[caseExpressionType, scrutineeType]);
}

@override
Expand Down
70 changes: 0 additions & 70 deletions pkg/analyzer/lib/src/error/best_practices_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import 'package:analyzer/src/dart/element/member.dart' show ExecutableMember;
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_provider.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/dart/resolver/body_inference_context.dart';
import 'package:analyzer/src/dart/resolver/exit_detector.dart';
import 'package:analyzer/src/dart/resolver/scope.dart';
import 'package:analyzer/src/error/annotation_verifier.dart';
import 'package:analyzer/src/error/codes.dart';
Expand Down Expand Up @@ -281,15 +279,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
@override
void visitConstructorDeclaration(ConstructorDeclaration node) {
var element = node.declaredElement as ConstructorElementImpl;
if (!_isNonNullableByDefault && element.isFactory) {
if (node.body is BlockFunctionBody) {
// Check the block for a return statement.
if (!ExitDetector.exits(node.body)) {
_errorReporter.reportErrorForNode(
WarningCode.MISSING_RETURN, node, [node.returnType.name]);
}
}
}
_checkStrictInferenceInParameters(node.parameters,
body: node.body, initializers: node.initializers);
_deprecatedVerifier.pushInDeprecatedValue(element.hasDeprecated);
Expand Down Expand Up @@ -459,8 +448,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
_inDoNotStoreMember = true;
}
try {
_checkForMissingReturn(node.functionExpression.body, node);

// Return types are inferred only on non-recursive local functions.
if (node.parent is CompilationUnit && !node.isSetter) {
_checkStrictInferenceReturnType(
Expand All @@ -485,9 +472,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
@override
void visitFunctionExpression(FunctionExpression node) {
var body = node.body;
if (node.parent is! FunctionDeclaration) {
_checkForMissingReturn(body, node);
}
if (!(node as FunctionExpressionImpl).wasFunctionTypeSupplied) {
_checkStrictInferenceInParameters(node.parameters, body: node.body);
}
Expand Down Expand Up @@ -592,7 +576,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
_inDoNotStoreMember = true;
}
try {
_checkForMissingReturn(node.body, node);
_mustCallSuperVerifier.checkMethodDeclaration(node);
_checkForUnnecessaryNoSuchMethod(node);
_checkForNullableEqualsParameterType(node);
Expand Down Expand Up @@ -1212,59 +1195,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
return false;
}

/// Generates a warning for functions that have a potentially non-nullable
/// return type, but do not have a return statement on all branches. At the
/// end of blocks with no return, Dart implicitly returns `null`. Avoiding
/// these implicit returns is considered a best practice.
///
/// See [WarningCode.MISSING_RETURN].
void _checkForMissingReturn(FunctionBody body, AstNode functionNode) {
if (_isNonNullableByDefault) {
return;
}

// Generators always return.
if (body.isGenerator) {
return;
}

if (body is! BlockFunctionBody) {
return;
}

var bodyContext = BodyInferenceContext.of(body)!;
// TODO(scheglov): Update InferenceContext to record any type, dynamic.
var returnType = bodyContext.contextType ?? DynamicTypeImpl.instance;

if (_typeSystem.isNullable(returnType)) {
return;
}

if (ExitDetector.exits(body)) {
return;
}

if (functionNode is FunctionDeclaration) {
_errorReporter.reportErrorForToken(
WarningCode.MISSING_RETURN,
functionNode.name,
[returnType],
);
} else if (functionNode is MethodDeclaration) {
_errorReporter.reportErrorForToken(
WarningCode.MISSING_RETURN,
functionNode.name,
[returnType],
);
} else {
_errorReporter.reportErrorForNode(
WarningCode.MISSING_RETURN,
functionNode,
[returnType],
);
}
}

void _checkForNullableEqualsParameterType(MethodDeclaration node) {
if (!_typeSystem.isNonNullableByDefault) {
// Cannot specify non-nullable types before null safety.
Expand Down
31 changes: 0 additions & 31 deletions pkg/analyzer/lib/src/error/codes.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4928,17 +4928,6 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);

/// Parameters:
/// 0: the static type of the switch expression
/// 1: the static type of the case expressions
static const CompileTimeErrorCode SWITCH_EXPRESSION_NOT_ASSIGNABLE =
CompileTimeErrorCode(
'SWITCH_EXPRESSION_NOT_ASSIGNABLE',
"Type '{0}' of the switch expression isn't assignable to the type '{1}' of "
"case expressions.",
hasPublishedDocs: true,
);

/// No parameters.
static const CompileTimeErrorCode
TEAROFF_OF_GENERATIVE_CONSTRUCTOR_OF_ABSTRACT_CLASS =
Expand Down Expand Up @@ -6817,17 +6806,6 @@ class WarningCode extends AnalyzerErrorCode {
uniqueName: 'MISSING_REQUIRED_PARAM_WITH_DETAILS',
);

/// Parameters:
/// 0: the name of the declared return type
static const WarningCode MISSING_RETURN = WarningCode(
'MISSING_RETURN',
"This function has a return type of '{0}', but doesn't end with a return "
"statement.",
correctionMessage:
"Try adding a return statement, or changing the return type to 'void'.",
hasPublishedDocs: true,
);

/// This warning is generated anywhere where a `@sealed` class is used as a
/// a superclass constraint of a mixin.
///
Expand Down Expand Up @@ -7113,15 +7091,6 @@ class WarningCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);

/// No parameters.
static const WarningCode SDK_VERSION_NEVER = WarningCode(
'SDK_VERSION_NEVER',
"The type 'Never' wasn't supported until version 2.12.0, but this code is "
"required to be able to run on earlier versions.",
correctionMessage: "Try updating the SDK constraints.",
hasPublishedDocs: true,
);

/// Parameters:
/// 0: the version specified in the `@Since()` annotation
/// 1: the SDK version constraints
Expand Down
3 changes: 0 additions & 3 deletions pkg/analyzer/lib/src/error/error_code_values.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ const List<ErrorCode> errorCodeValues = [
CompileTimeErrorCode.SUPER_IN_INVALID_CONTEXT,
CompileTimeErrorCode.SUPER_IN_REDIRECTING_CONSTRUCTOR,
CompileTimeErrorCode.SWITCH_CASE_COMPLETES_NORMALLY,
CompileTimeErrorCode.SWITCH_EXPRESSION_NOT_ASSIGNABLE,
CompileTimeErrorCode.TEAROFF_OF_GENERATIVE_CONSTRUCTOR_OF_ABSTRACT_CLASS,
CompileTimeErrorCode.THROW_OF_INVALID_TYPE,
CompileTimeErrorCode.TOP_LEVEL_CYCLE,
Expand Down Expand Up @@ -1040,7 +1039,6 @@ const List<ErrorCode> errorCodeValues = [
WarningCode.MISSING_OVERRIDE_OF_MUST_BE_OVERRIDDEN_TWO,
WarningCode.MISSING_REQUIRED_PARAM,
WarningCode.MISSING_REQUIRED_PARAM_WITH_DETAILS,
WarningCode.MISSING_RETURN,
WarningCode.MIXIN_ON_SEALED_CLASS,
WarningCode.MUST_BE_IMMUTABLE,
WarningCode.MUST_CALL_SUPER,
Expand All @@ -1065,7 +1063,6 @@ const List<ErrorCode> errorCodeValues = [
WarningCode.RETURN_TYPE_INVALID_FOR_CATCH_ERROR,
WarningCode.SDK_VERSION_CONSTRUCTOR_TEAROFFS,
WarningCode.SDK_VERSION_GT_GT_GT_OPERATOR,
WarningCode.SDK_VERSION_NEVER,
WarningCode.SDK_VERSION_SINCE,
WarningCode.STRICT_RAW_TYPE,
WarningCode.SUBTYPE_OF_SEALED_CLASS,
Expand Down
Loading

0 comments on commit e871af6

Please sign in to comment.