Revert "fix #26141, add support for type arguments to constants"
This reverts commit 5d155000af4285db51976d0722989081e27a415f.
Seems to have lost an error regarding type literals used in non-strong mode. Probably an easy fix, but reverting for simplicity.
Review URL: https://codereview.chromium.org/2188403002 .
diff --git a/pkg/analyzer/lib/src/dart/ast/utilities.dart b/pkg/analyzer/lib/src/dart/ast/utilities.dart
index 59a51e9..53f9109 100644
--- a/pkg/analyzer/lib/src/dart/ast/utilities.dart
+++ b/pkg/analyzer/lib/src/dart/ast/utilities.dart
@@ -72,7 +72,7 @@
* [nodes].
*/
List<AstNode/*=E*/ > cloneNodeList/*<E extends AstNode>*/(
- List/*<E>*/ nodes) {
+ NodeList/*<E>*/ nodes) {
int count = nodes.length;
List/*<E>*/ clonedNodes = new List/*<E>*/();
for (int i = 0; i < count; i++) {
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index 77cf158..57eee89 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -75,9 +75,6 @@
*/
final ConstantEvaluationValidator validator;
- /** Whether we are running in strong mode. */
- final bool strongMode;
-
/**
* Initialize a newly created [ConstantEvaluationEngine]. The [typeProvider]
* is used to access known types. [_declaredVariables] is the set of
@@ -85,12 +82,9 @@
* given, is used to verify correct dependency analysis when running unit
* tests.
*/
- ConstantEvaluationEngine(TypeProvider typeProvider, this._declaredVariables,
+ ConstantEvaluationEngine(this.typeProvider, this._declaredVariables,
{ConstantEvaluationValidator validator, TypeSystem typeSystem})
- : typeProvider = typeProvider,
- strongMode =
- typeProvider.objectType.element.context.analysisOptions.strongMode,
- validator =
+ : validator =
validator ?? new ConstantEvaluationValidator_ForProduction(),
typeSystem = typeSystem ?? new TypeSystemImpl();
@@ -415,7 +409,7 @@
DartObjectImpl evaluateConstructorCall(
AstNode node,
- List<Expression> arguments,
+ NodeList<Expression> arguments,
ConstructorElement constructor,
ConstantVisitor constantVisitor,
ErrorReporter errorReporter) {
@@ -521,49 +515,25 @@
// so consider it an unknown value to suppress further errors.
return new DartObjectImpl.validWithUnknownValue(definingClass);
}
-
- // In strong mode, we allow constants to have type arguments.
- //
- // They will be added to the lexical environment when evaluating
- // subexpressions.
- HashMap<String, DartObjectImpl> typeArgumentMap;
- if (strongMode) {
- // Instantiate the constructor with the in-scope type arguments.
- definingClass = constantVisitor.evaluateType(definingClass);
- constructor = ConstructorMember.from(constructorBase, definingClass);
-
- typeArgumentMap = new HashMap<String, DartObjectImpl>.fromIterables(
- definingClass.typeParameters.map((t) => t.name),
- definingClass.typeArguments.map(constantVisitor.typeConstant));
- }
-
- var fieldMap = new HashMap<String, DartObjectImpl>();
- var fieldInitVisitor = new ConstantVisitor(this, errorReporter,
- lexicalEnvironment: typeArgumentMap);
+ HashMap<String, DartObjectImpl> fieldMap =
+ new HashMap<String, DartObjectImpl>();
// Start with final fields that are initialized at their declaration site.
- List<FieldElement> fields = constructor.enclosingElement.fields;
- for (int i = 0; i < fields.length; i++) {
- FieldElement field = fields[i];
+ for (FieldElement field in constructor.enclosingElement.fields) {
if ((field.isFinal || field.isConst) &&
!field.isStatic &&
field is ConstFieldElementImpl) {
validator.beforeGetFieldEvaluationResult(field);
-
- DartObjectImpl fieldValue;
- if (strongMode) {
- fieldValue = field.constantInitializer.accept(fieldInitVisitor);
- } else {
- fieldValue = field.evaluationResult?.value;
- }
+ EvaluationResultImpl evaluationResult = field.evaluationResult;
// It is possible that the evaluation result is null.
// This happens for example when we have duplicate fields.
// class Test {final x = 1; final x = 2; const Test();}
- if (fieldValue == null) {
+ if (evaluationResult == null) {
continue;
}
// Match the value and the type.
DartType fieldType =
FieldMember.from(field, constructor.returnType).type;
+ DartObjectImpl fieldValue = evaluationResult.value;
if (fieldValue != null && !runtimeTypeMatch(fieldValue, fieldType)) {
errorReporter.reportErrorForNode(
CheckedModeCompileTimeErrorCode
@@ -579,7 +549,6 @@
new HashMap<String, DartObjectImpl>();
List<ParameterElement> parameters = constructor.parameters;
int parameterCount = parameters.length;
-
for (int i = 0; i < parameterCount; i++) {
ParameterElement parameter = parameters[i];
ParameterElement baseParameter = parameter;
@@ -605,23 +574,12 @@
// The parameter is an optional positional parameter for which no value
// was provided, so use the default value.
validator.beforeGetParameterDefault(baseParameter);
- if (strongMode && baseParameter is ConstVariableElement) {
- var defaultValue =
- (baseParameter as ConstVariableElement).constantInitializer;
- if (defaultValue == null) {
- argumentValue = typeProvider.nullObject;
- } else {
- argumentValue = defaultValue.accept(fieldInitVisitor);
- }
- } else {
- EvaluationResultImpl evaluationResult =
- baseParameter.evaluationResult;
- if (evaluationResult == null) {
- // No default was provided, so the default value is null.
- argumentValue = typeProvider.nullObject;
- } else if (evaluationResult.value != null) {
- argumentValue = evaluationResult.value;
- }
+ EvaluationResultImpl evaluationResult = baseParameter.evaluationResult;
+ if (evaluationResult == null) {
+ // No default was provided, so the default value is null.
+ argumentValue = typeProvider.nullObject;
+ } else if (evaluationResult.value != null) {
+ argumentValue = evaluationResult.value;
}
}
if (argumentValue != null) {
@@ -719,7 +677,6 @@
if (superArguments == null) {
superArguments = new NodeList<Expression>(null);
}
-
evaluateSuperConstructorCall(node, fieldMap, superConstructor,
superArguments, initializerVisitor, errorReporter);
}
@@ -731,7 +688,7 @@
AstNode node,
HashMap<String, DartObjectImpl> fieldMap,
ConstructorElement superConstructor,
- List<Expression> superArguments,
+ NodeList<Expression> superArguments,
ConstantVisitor initializerVisitor,
ErrorReporter errorReporter) {
if (superConstructor != null && superConstructor.isConst) {
@@ -1274,7 +1231,6 @@
// problem - the error has already been reported.
return null;
}
-
return evaluationEngine.evaluateConstructorCall(
node, node.argumentList.arguments, constructor, this, _errorReporter);
}
@@ -1318,9 +1274,9 @@
return null;
}
DartType elementType = _typeProvider.dynamicType;
- NodeList<TypeName> typeArgs = node.typeArguments?.arguments;
- if (typeArgs?.length == 1) {
- DartType type = visitTypeName(typeArgs[0]).toTypeValue();
+ if (node.typeArguments != null &&
+ node.typeArguments.arguments.length == 1) {
+ DartType type = node.typeArguments.arguments[0].type;
if (type != null) {
elementType = type;
}
@@ -1353,13 +1309,13 @@
}
DartType keyType = _typeProvider.dynamicType;
DartType valueType = _typeProvider.dynamicType;
- NodeList<TypeName> typeArgs = node.typeArguments?.arguments;
- if (typeArgs?.length == 2) {
- DartType keyTypeCandidate = visitTypeName(typeArgs[0]).toTypeValue();
+ if (node.typeArguments != null &&
+ node.typeArguments.arguments.length == 2) {
+ DartType keyTypeCandidate = node.typeArguments.arguments[0].type;
if (keyTypeCandidate != null) {
keyType = keyTypeCandidate;
}
- DartType valueTypeCandidate = visitTypeName(typeArgs[1]).toTypeValue();
+ DartType valueTypeCandidate = node.typeArguments.arguments[1].type;
if (valueTypeCandidate != null) {
valueType = valueTypeCandidate;
}
@@ -1509,48 +1465,6 @@
_typeProvider.symbolType, new SymbolState(buffer.toString()));
}
- @override
- DartObjectImpl visitTypeName(TypeName node) {
- return typeConstant(evaluateType(node.type));
- }
-
- /**
- * Given a [type], returns the constant value that contains that type value.
- */
- DartObjectImpl typeConstant(DartType type) {
- return new DartObjectImpl(_typeProvider.typeType, new TypeState(type));
- }
-
- /**
- * Given a [type] that may contain free type variables, evaluate them against
- * the current lexical environment and return the substituted type.
- */
- DartType evaluateType(DartType type) {
- if (type is TypeParameterType) {
- String name = type.name;
- if (_lexicalEnvironment != null) {
- return _lexicalEnvironment[name]?.toTypeValue() ?? type;
- }
- return type;
- }
- if (type is ParameterizedType) {
- List<DartType> typeArguments;
- for (int i = 0; i < type.typeArguments.length; i++) {
- DartType ta = type.typeArguments[i];
- DartType t = evaluateType(ta);
- if (!identical(t, ta)) {
- if (typeArguments == null) {
- typeArguments = type.typeArguments.toList(growable: false);
- }
- typeArguments[i] = t;
- }
- }
- if (typeArguments == null) return type;
- return type.substitute2(typeArguments, type.typeArguments);
- }
- return type;
- }
-
/**
* Create an error associated with the given [node]. The error will have the
* given error [code].
@@ -1583,9 +1497,10 @@
}
return new DartObjectImpl(functionType, new FunctionState(function));
}
- } else if (variableElement is TypeDefiningElement) {
- return new DartObjectImpl(
- _typeProvider.typeType, new TypeState(variableElement.type));
+ } else if (variableElement is ClassElement ||
+ variableElement is FunctionTypeAliasElement ||
+ variableElement is DynamicElementImpl) {
+ return new DartObjectImpl(_typeProvider.typeType, new TypeState(element));
}
// TODO(brianwilkerson) Figure out which error to report.
_error(node, null);
diff --git a/pkg/analyzer/lib/src/dart/constant/value.dart b/pkg/analyzer/lib/src/dart/constant/value.dart
index a0d1162..a269925 100644
--- a/pkg/analyzer/lib/src/dart/constant/value.dart
+++ b/pkg/analyzer/lib/src/dart/constant/value.dart
@@ -720,7 +720,10 @@
DartType toTypeValue() {
InstanceState state = _state;
if (state is TypeState) {
- return state._type;
+ Element element = state._element;
+ if (element is TypeDefiningElement) {
+ return element.type;
+ }
}
return null;
}
@@ -2768,29 +2771,29 @@
/**
* The element representing the type being modeled.
*/
- final DartType _type;
+ final Element _element;
/**
* Initialize a newly created state to represent the given [value].
*/
- TypeState(this._type);
+ TypeState(this._element);
@override
- int get hashCode => _type?.hashCode ?? 0;
+ int get hashCode => _element == null ? 0 : _element.hashCode;
@override
String get typeName => "Type";
@override
bool operator ==(Object object) =>
- object is TypeState && (_type == object._type);
+ object is TypeState && (_element == object._element);
@override
StringState convertToString() {
- if (_type == null) {
+ if (_element == null) {
return StringState.UNKNOWN_VALUE;
}
- return new StringState(_type.displayName);
+ return new StringState(_element.name);
}
@override
@@ -2801,15 +2804,15 @@
@override
BoolState isIdentical(InstanceState rightOperand) {
- if (_type == null) {
+ if (_element == null) {
return BoolState.UNKNOWN_VALUE;
}
if (rightOperand is TypeState) {
- DartType rightType = rightOperand._type;
- if (rightType == null) {
+ Element rightElement = rightOperand._element;
+ if (rightElement == null) {
return BoolState.UNKNOWN_VALUE;
}
- return BoolState.from(_type == rightType);
+ return BoolState.from(_element == rightElement);
} else if (rightOperand is DynamicState) {
return BoolState.UNKNOWN_VALUE;
}
@@ -2817,5 +2820,5 @@
}
@override
- String toString() => _type?.toString() ?? "-unknown-";
+ String toString() => _element == null ? "-unknown-" : _element.name;
}
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index 5e5a20e..0a54caa 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -868,9 +868,7 @@
_checkForConstWithNonConst(node);
_checkForConstWithUndefinedConstructor(
node, constructorName, typeName);
- if (!_options.strongMode) {
- _checkForConstWithTypeParameters(typeName);
- }
+ _checkForConstWithTypeParameters(typeName);
_checkForConstDeferredClass(node, constructorName, typeName);
} else {
_checkForNewWithUndefinedConstructor(node, constructorName, typeName);
@@ -893,9 +891,9 @@
Object visitListLiteral(ListLiteral node) {
TypeArgumentList typeArguments = node.typeArguments;
if (typeArguments != null) {
- if (!_options.strongMode && node.constKeyword != null) {
+ if (node.constKeyword != null) {
NodeList<TypeName> arguments = typeArguments.arguments;
- if (arguments.isNotEmpty) {
+ if (arguments.length != 0) {
_checkForInvalidTypeArgumentInConstTypedLiteral(arguments,
CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST);
}
@@ -912,7 +910,7 @@
TypeArgumentList typeArguments = node.typeArguments;
if (typeArguments != null) {
NodeList<TypeName> arguments = typeArguments.arguments;
- if (!_options.strongMode && arguments.isNotEmpty) {
+ if (arguments.length != 0) {
if (node.constKeyword != null) {
_checkForInvalidTypeArgumentInConstTypedLiteral(arguments,
CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP);
diff --git a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart
index dd3c4f3..cf6d9cd 100644
--- a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart
+++ b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart
@@ -30,7 +30,6 @@
initializeTestEnvironment();
runReflectiveTests(ConstantValueComputerTest);
runReflectiveTests(ConstantVisitorTest);
- runReflectiveTests(StrongConstantValueComputerTest);
}
/**
@@ -1601,11 +1600,3 @@
return result;
}
}
-
-@reflectiveTest
-class StrongConstantValueComputerTest extends ConstantValueComputerTest {
- void setUp() {
- super.setUp();
- resetWithOptions(new AnalysisOptionsImpl()..strongMode = true);
- }
-}
diff --git a/pkg/analyzer/test/src/task/strong/checker_test.dart b/pkg/analyzer/test/src/task/strong/checker_test.dart
index 8bdd4ce..343a2ae 100644
--- a/pkg/analyzer/test/src/task/strong/checker_test.dart
+++ b/pkg/analyzer/test/src/task/strong/checker_test.dart
@@ -2164,58 +2164,6 @@
check(implicitDynamic: false);
}
- void test_constantGenericTypeArg_infer() {
- // Regression test for https://github.com/dart-lang/sdk/issues/26141
- checkFile('''
-abstract class Equality<Q> {}
-abstract class EqualityBase<R> implements Equality<R> {
- final C<R> c = /*info:INFERRED_TYPE_ALLOCATION*/const C();
- const EqualityBase();
-}
-class DefaultEquality<S> extends EqualityBase<S> {
- const DefaultEquality();
-}
-class SetEquality<T> implements Equality<T> {
- final Equality<T> field = const DefaultEquality();
- const SetEquality([Equality<T> inner = const DefaultEquality()]);
-}
-class C<Q> {
- final List<Q> list = /*info:INFERRED_TYPE_LITERAL*/const [];
- final Map<Q, Iterable<Q>> m = /*info:INFERRED_TYPE_LITERAL*/const {};
- const C();
-}
-main() {
- const SetEquality<String>();
-}
- ''');
- }
-
- void test_constantGenericTypeArg_explict() {
- // Regression test for https://github.com/dart-lang/sdk/issues/26141
- checkFile('''
-abstract class Equality<R> {}
-abstract class EqualityBase<R> implements Equality<R> {
- final C<R> c = const C<R>();
- const EqualityBase();
-}
-class DefaultEquality<S> extends EqualityBase<S> {
- const DefaultEquality();
-}
-class SetEquality<T> implements Equality<T> {
- final Equality<T> field = const DefaultEquality<T>();
- const SetEquality([Equality<T> inner = const DefaultEquality<T>()]);
-}
-class C<Q> {
- final List<Q> list = const <Q>[];
- final Map<Q, Iterable<Q>> m = const <Q, Iterable<Q>>{};
- const C();
-}
-main() {
- const SetEquality<String>();
-}
- ''');
- }
-
void test_invalidOverrides_baseClassOverrideToChildInterface() {
checkFile('''
class A {}