diff --git a/pkg/analyzer/lib/error/error.dart b/pkg/analyzer/lib/error/error.dart index c93b47de552..d2a16770a03 100644 --- a/pkg/analyzer/lib/error/error.dart +++ b/pkg/analyzer/lib/error/error.dart @@ -250,6 +250,7 @@ const List errorCodeValues = const [ CompileTimeErrorCode.THIS_ACCESS_FROM_INITIALIZER, CompileTimeErrorCode.TYPE_ALIAS_CANNOT_REFERENCE_ITSELF, CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS, + CompileTimeErrorCode.TYPE_PARAMETER_IN_CONST_EXPRESSION, CompileTimeErrorCode.TYPE_PARAMETER_ON_CONSTRUCTOR, CompileTimeErrorCode.UNDEFINED_ANNOTATION, CompileTimeErrorCode.UNDEFINED_CLASS, diff --git a/pkg/analyzer/lib/src/error/codes.dart b/pkg/analyzer/lib/src/error/codes.dart index 8f9692a6ad5..6fe436cc9ce 100644 --- a/pkg/analyzer/lib/src/error/codes.dart +++ b/pkg/analyzer/lib/src/error/codes.dart @@ -2474,6 +2474,10 @@ class CompileTimeErrorCode extends ErrorCode { "Typedefs can't reference themselves directly or recursively via " "another typedef."); + static const CompileTimeErrorCode TYPE_PARAMETER_IN_CONST_EXPRESSION = + const CompileTimeErrorCode.fromFasta( + 'TYPE_PARAMETER_IN_CONST_EXPRESSION'); + static const CompileTimeErrorCode TYPE_PARAMETER_ON_CONSTRUCTOR = const CompileTimeErrorCode.fromFasta('TYPE_PARAMETER_ON_CONSTRUCTOR'); diff --git a/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart b/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart index fc453d56014..7b1f75b2c29 100644 --- a/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart +++ b/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart @@ -6533,6 +6533,40 @@ Message _withArgumentsTypeVariableDuplicatedNameCause(String name) { arguments: {'name': name}); } +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +const Template< + Message Function( + String name, + DartType + _type)> templateTypeVariableInConstExpression = const Template< + Message Function(String name, DartType _type)>( + messageTemplate: + r"""Type variable '#name' can't be used as a constant expression '#type'.""", + withArguments: _withArgumentsTypeVariableInConstExpression); + +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +const Code + codeTypeVariableInConstExpression = + const Code( + "TypeVariableInConstExpression", templateTypeVariableInConstExpression, + analyzerCode: "TYPE_PARAMETER_IN_CONST_EXPRESSION", + dart2jsCode: "*fatal*", + severity: Severity.error); + +// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. +Message _withArgumentsTypeVariableInConstExpression( + String name, DartType _type) { + NameSystem nameSystem = new NameSystem(); + StringBuffer buffer = new StringBuffer(); + new Printer(buffer, syntheticNames: nameSystem).writeNode(_type); + String type = '$buffer'; + + return new Message(codeTypeVariableInConstExpression, + message: + """Type variable '${name}' can't be used as a constant expression '${type}'.""", + arguments: {'name': name, 'type': _type}); +} + // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. const Code codeTypeVariableInStaticContext = messageTypeVariableInStaticContext; diff --git a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart index efaef2473f1..8d3f23acbaa 100644 --- a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart +++ b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart @@ -4203,10 +4203,12 @@ abstract class BodyBuilder extends ScopeListener } return const InvalidType(); } else if (constantContext != ConstantContext.none) { - deprecated_addCompileTimeError( + int length = type.parameter.name.length; + addCompileTimeError( + fasta.templateTypeVariableInConstExpression + .withArguments(type.parameter.name, type), offset, - "Type variable '${type.parameter.name}' can't be used as a constant " - "expression $type."); + length); } return type; } diff --git a/pkg/front_end/messages.status b/pkg/front_end/messages.status index 1a69d933185..65870f693cd 100644 --- a/pkg/front_end/messages.status +++ b/pkg/front_end/messages.status @@ -348,6 +348,7 @@ TypeArgumentMismatch/example: Fail TypeArgumentsOnTypeVariable/script1: Fail TypeNotFound/example: Fail TypeVariableDuplicatedName/example: Fail +TypeVariableInConstExpression/example: Fail TypeVariableInStaticContext/example: Fail TypeVariableSameNameAsEnclosing/example: Fail TypedefNotFunction/example: Fail diff --git a/pkg/front_end/messages.yaml b/pkg/front_end/messages.yaml index 7c48b348f5b..486ca73c011 100644 --- a/pkg/front_end/messages.yaml +++ b/pkg/front_end/messages.yaml @@ -2147,6 +2147,12 @@ FinalInstanceVariableAlreadyInitializedCause: template: "'#name' was initialized here." severity: CONTEXT +TypeVariableInConstExpression: + template: "Type variable '#name' can't be used as a constant expression '#type'." + severity: ERROR + analyzerCode: TYPE_PARAMETER_IN_CONST_EXPRESSION + dart2jsCode: "*fatal*" + TypeVariableInStaticContext: template: "Type variables can't be used in static members." severity: ERROR_LEGACY_WARNING