Skip to content

Commit

Permalink
Legacy. Deprecate LibraryElement.isNonNullableByDefault
Browse files Browse the repository at this point in the history
Change-Id: I1b15c59960aa7eb4d27c221febc857a974bae738
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350404
Reviewed-by: Brian Wilkerson <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and Commit Queue committed Feb 6, 2024
1 parent df52948 commit fe0e077
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 288 deletions.
1 change: 1 addition & 0 deletions pkg/analyzer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 6.5.0-dev
* Deprecated `LibraryElement.toLegacyTypeIfOptOut`.
* Deprecated `LibraryElement.toLegacyElementIfOptOut`.
* Deprecated `LibraryElement.isNonNullableByDefault`.

## 6.4.1
* Patch for crash in ffi_verifier.
Expand Down
1 change: 1 addition & 0 deletions pkg/analyzer/lib/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,7 @@ abstract class LibraryOrAugmentationElement implements Element {
/// version override comment at the top of the file.
FeatureSet get featureSet;

@Deprecated('Only non-nullable by default mode is supported')
bool get isNonNullableByDefault;

/// The language version for this library.
Expand Down
1 change: 0 additions & 1 deletion pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ class LibraryAnalyzer {
if (sdkVersionConstraint != null) {
SdkConstraintVerifier verifier = SdkConstraintVerifier(
errorReporter,
_libraryElement,
sdkVersionConstraint.withoutPreRelease,
);
unit.accept(verifier);
Expand Down
28 changes: 13 additions & 15 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ class ConstructorElementImpl extends ExecutableElementImpl
typeFormals: typeParameters,
parameters: parameters,
returnType: returnType,
nullabilitySuffix: _noneOrStarSuffix,
nullabilitySuffix: NullabilitySuffix.none,
);
}

Expand Down Expand Up @@ -2385,12 +2385,6 @@ abstract class ElementImpl implements Element {
return enclosingElement?.source;
}

NullabilitySuffix get _noneOrStarSuffix {
return library!.isNonNullableByDefault == true
? NullabilitySuffix.none
: NullabilitySuffix.star;
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
Expand Down Expand Up @@ -2853,7 +2847,7 @@ abstract class ExecutableElementImpl extends _ExistingElementImpl
typeFormals: typeParameters,
parameters: parameters,
returnType: returnType,
nullabilitySuffix: _noneOrStarSuffix,
nullabilitySuffix: NullabilitySuffix.none,
);
}

Expand Down Expand Up @@ -3276,7 +3270,7 @@ class GenericFunctionTypeElementImpl extends _ExistingElementImpl
parameters: parameters,
returnType: returnType,
nullabilitySuffix:
isNullable ? NullabilitySuffix.question : _noneOrStarSuffix,
isNullable ? NullabilitySuffix.question : NullabilitySuffix.none,
);
}

Expand Down Expand Up @@ -3571,14 +3565,14 @@ abstract class InterfaceElementImpl extends InstanceElementImpl
List<DartType> typeArguments;
if (typeParameters.isNotEmpty) {
typeArguments = typeParameters.map<DartType>((t) {
return t.instantiate(nullabilitySuffix: _noneOrStarSuffix);
return t.instantiate(nullabilitySuffix: NullabilitySuffix.none);
}).toFixedList();
} else {
typeArguments = const <DartType>[];
}
return _thisType = instantiate(
typeArguments: typeArguments,
nullabilitySuffix: _noneOrStarSuffix,
nullabilitySuffix: NullabilitySuffix.none,
);
}
return _thisType!;
Expand Down Expand Up @@ -3993,6 +3987,7 @@ class LibraryAugmentationElementImpl extends LibraryOrAugmentationElementImpl
@override
FeatureSet get featureSet => augmentationTarget.featureSet;

@Deprecated('Only non-nullable by default mode is supported')
@override
bool get isNonNullableByDefault => augmentationTarget.isNonNullableByDefault;

Expand Down Expand Up @@ -4245,6 +4240,7 @@ class LibraryElementImpl extends LibraryOrAugmentationElementImpl
return DartUriResolver.isDartUri(uri);
}

@Deprecated('Only non-nullable by default mode is supported')
@override
bool get isNonNullableByDefault {
return featureSet.isEnabled(Feature.non_nullable);
Expand Down Expand Up @@ -6262,7 +6258,7 @@ class PropertyAccessorElementImpl_ImplicitGetter
typeFormals: const <TypeParameterElement>[],
parameters: const <ParameterElement>[],
returnType: returnType,
nullabilitySuffix: _noneOrStarSuffix,
nullabilitySuffix: NullabilitySuffix.none,
);
}

Expand Down Expand Up @@ -6320,7 +6316,7 @@ class PropertyAccessorElementImpl_ImplicitSetter
typeFormals: const <TypeParameterElement>[],
parameters: parameters,
returnType: returnType,
nullabilitySuffix: _noneOrStarSuffix,
nullabilitySuffix: NullabilitySuffix.none,
);
}

Expand Down Expand Up @@ -6708,14 +6704,16 @@ class TypeAliasElementImpl extends _ExistingElementImpl
final List<DartType> typeArguments;
if (typeParameters.isNotEmpty) {
typeArguments = typeParameters.map<DartType>((t) {
return t.instantiate(nullabilitySuffix: _noneOrStarSuffix);
return t.instantiate(
nullabilitySuffix: NullabilitySuffix.none,
);
}).toList();
} else {
typeArguments = const <DartType>[];
}
return instantiate(
typeArguments: typeArguments,
nullabilitySuffix: _noneOrStarSuffix,
nullabilitySuffix: NullabilitySuffix.none,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class PropertyElementResolver with ScopeHelpers {
writeElementRequested = _resolver.toLegacyElement(writeLookup?.requested);
writeElementRecovery = _resolver.toLegacyElement(writeLookup?.recovery);

AssignmentVerifier(_resolver.definingLibrary, errorReporter).verify(
AssignmentVerifier(errorReporter).verify(
node: node,
requested: writeElementRequested,
recovery: writeElementRecovery,
Expand Down Expand Up @@ -509,7 +509,7 @@ class PropertyElementResolver with ScopeHelpers {
if (hasWrite) {
_checkForStaticMember(target, propertyName, result.setter);
if (result.needsSetterError) {
AssignmentVerifier(_definingLibrary, errorReporter).verify(
AssignmentVerifier(errorReporter).verify(
node: propertyName,
requested: null,
recovery: result.getter,
Expand Down Expand Up @@ -721,7 +721,7 @@ class PropertyElementResolver with ScopeHelpers {
} else {
// Recovery, try to use getter.
writeElementRecovery = augmented.getGetter(propertyName.name);
AssignmentVerifier(_definingLibrary, errorReporter).verify(
AssignmentVerifier(errorReporter).verify(
node: propertyName,
requested: null,
recovery: writeElementRecovery,
Expand Down
13 changes: 1 addition & 12 deletions pkg/analyzer/lib/src/error/assignment_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import 'package:analyzer/src/error/codes.dart';
/// an [AssignmentExpression], or a [PrefixExpression] or [PostfixExpression]
/// when the operator is an increment operator.
class AssignmentVerifier {
final LibraryElement _definingLibrary;
final ErrorReporter _errorReporter;

AssignmentVerifier(this._definingLibrary, this._errorReporter);
AssignmentVerifier(this._errorReporter);

/// We resolved [node] and found that it references the [requested] element.
/// Verify that this element is actually writable.
Expand All @@ -41,16 +40,6 @@ class AssignmentVerifier {
node,
CompileTimeErrorCode.ASSIGNMENT_TO_CONST,
);
} else if (requested.isFinal) {
if (_definingLibrary.isNonNullableByDefault) {
// Handled during resolution, with flow analysis.
} else {
_errorReporter.atNode(
node,
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL_LOCAL,
arguments: [requested.name],
);
}
}
}
return;
Expand Down
15 changes: 0 additions & 15 deletions pkg/analyzer/lib/src/error/bool_expression_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,6 @@ class BoolExpressionVerifier {
arguments: arguments,
);
}
} else if (!_resolver.definingLibrary.isNonNullableByDefault) {
if (expression is InstanceCreationExpression) {
// In pre-null safety code, an implicit cast from a supertype is allowed
// unless the expression is an explicit instance creation expression,
// with the idea that the cast would likely fail at runtime.
var constructor = expression.constructorName.staticElement;
if (constructor == null || !constructor.isFactory) {
_errorReporter.atNode(
expression,
errorCode,
arguments: arguments,
);
return;
}
}
}
}

Expand Down
12 changes: 1 addition & 11 deletions pkg/analyzer/lib/src/generated/error_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3704,10 +3704,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
}

void _checkForMainFunction1(Token nameToken, Element declaredElement) {
if (!_currentLibrary.isNonNullableByDefault) {
return;
}

// We should only check exported declarations, i.e. top-level.
if (declaredElement.enclosingElement is! CompilationUnitElement) {
return;
Expand All @@ -3726,10 +3722,6 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
}

void _checkForMainFunction2(FunctionDeclaration functionDeclaration) {
if (!_currentLibrary.isNonNullableByDefault) {
return;
}

if (functionDeclaration.name.lexeme != 'main') {
return;
}
Expand Down Expand Up @@ -3984,9 +3976,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
InterfaceType mixinType = mixinName.type as InterfaceType;
for (var constraint in mixinType.superclassConstraints) {
var superType = _enclosingClass!.supertype as InterfaceTypeImpl;
if (_currentLibrary.isNonNullableByDefault) {
superType = superType.withNullability(NullabilitySuffix.none);
}
superType = superType.withNullability(NullabilitySuffix.none);

bool isSatisfied = typeSystem.isSubtypeOf(superType, constraint);
if (!isSatisfied) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/analyzer/lib/src/generated/resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
genericMetadataIsEnabled =
definingLibrary.featureSet.isEnabled(Feature.generic_metadata),
options = TypeAnalyzerOptions(
nullSafetyEnabled: definingLibrary.isNonNullableByDefault,
nullSafetyEnabled: true,
patternsEnabled:
definingLibrary.featureSet.isEnabled(Feature.patterns)) {
nullableDereferenceVerifier = NullableDereferenceVerifier(
Expand Down
10 changes: 1 addition & 9 deletions pkg/analyzer/lib/src/hint/sdk_constraint_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
/// The error reporter to be used to report errors.
final ErrorReporter _errorReporter;

/// The element representing the library containing the unit to be verified.
final LibraryElement _containingLibrary;

/// The version constraint for the SDK.
final VersionConstraint _versionConstraint;

Expand All @@ -31,8 +28,7 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {

/// Initialize a newly created verifier to use the given [_errorReporter] to
/// report errors.
SdkConstraintVerifier(
this._errorReporter, this._containingLibrary, this._versionConstraint);
SdkConstraintVerifier(this._errorReporter, this._versionConstraint);

/// Return a range covering every version up to, but not including, 2.14.0.
VersionRange get before_2_14_0 =>
Expand All @@ -58,10 +54,6 @@ class SdkConstraintVerifier extends RecursiveAstVisitor<void> {
VersionRange get before_2_6_0 =>
VersionRange(max: Version.parse('2.6.0'), includeMax: false);

/// Return `true` if references to the non-nullable features need to be
/// checked.
bool get checkNnbd => !_containingLibrary.isNonNullableByDefault;

/// Return `true` if references to the constant-update-2018 features need to
/// be checked.
bool get checkTripleShift => _checkTripleShift ??=
Expand Down
6 changes: 1 addition & 5 deletions pkg/analyzer/lib/src/summary2/default_types_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,8 @@ class DefaultTypesBuilder {
) {
if (parameterList == null) return;

var library = declarationElement.library!;
var typeProvider = library.typeProvider;
var dynamicType = DynamicTypeImpl.instance;
var bottomType = library.isNonNullableByDefault
? NeverTypeImpl.instance
: typeProvider.nullType;
var bottomType = NeverTypeImpl.instance;

var nodes = parameterList.typeParameters;
var length = nodes.length;
Expand Down
18 changes: 5 additions & 13 deletions pkg/analyzer/lib/src/summary2/reference_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
final TypeSystemImpl _typeSystem;
final NodesToBuildType nodesToBuildType;

/// Indicates whether the library is opted into NNBD.
final bool isNNBD;

Scope scope;

ReferenceResolver(
this.linker,
this.nodesToBuildType,
LibraryOrAugmentationElementImpl container,
) : _typeSystem = container.library.typeSystem,
scope = container.scope,
isNNBD = container.isNonNullableByDefault;
scope = container.scope;

@override
void visitBlockFunctionBody(BlockFunctionBody node) {}
Expand Down Expand Up @@ -278,7 +274,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
node.parameters.accept(this);

var nullabilitySuffix = _getNullabilitySuffix(node.question != null);
var builder = FunctionTypeBuilder.of(isNNBD, nodeImpl, nullabilitySuffix);
var builder = FunctionTypeBuilder.of(true, nodeImpl, nullabilitySuffix);
nodeImpl.type = builder;
nodesToBuildType.addTypeBuilder(builder);

Expand Down Expand Up @@ -499,14 +495,10 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
}

NullabilitySuffix _getNullabilitySuffix(bool hasQuestion) {
if (isNNBD) {
if (hasQuestion) {
return NullabilitySuffix.question;
} else {
return NullabilitySuffix.none;
}
if (hasQuestion) {
return NullabilitySuffix.question;
} else {
return NullabilitySuffix.star;
return NullabilitySuffix.none;
}
}
}
12 changes: 4 additions & 8 deletions pkg/analyzer/messages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3079,8 +3079,7 @@ CompileTimeErrorCode:
`null`, which is neither a list nor a set:

```dart
%language=2.9
const List<int> list1 = null;
const dynamic list1 = 42;
const List<int> list2 = [...[!list1!]];
```

Expand All @@ -3090,8 +3089,7 @@ CompileTimeErrorCode:
or a constant set:

```dart
%language=2.9
const List<int> list1 = [];
const dynamic list1 = [42];
const List<int> list2 = [...list1];
```
CONST_SPREAD_EXPECTED_MAP:
Expand All @@ -3110,8 +3108,7 @@ CompileTimeErrorCode:
`null`, which isn't a map:

```dart
%language=2.9
const Map<String, int> map1 = null;
const dynamic map1 = 42;
const Map<String, int> map2 = {...[!map1!]};
```

Expand All @@ -3120,8 +3117,7 @@ CompileTimeErrorCode:
Change the expression to something that evaluates to a constant map:

```dart
%language=2.9
const Map<String, int> map1 = {};
const dynamic map1 = {'answer': 42};
const Map<String, int> map2 = {...map1};
```
CONST_TYPE_PARAMETER:
Expand Down
Loading

0 comments on commit fe0e077

Please sign in to comment.