rename and reword invalid initializer error messages

Change-Id: Ieae7b11f6aee46567247dbc9532098e7541774b7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/97720
Commit-Queue: Dan Rubel <danrubel@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/error/error.dart b/pkg/analyzer/lib/error/error.dart
index 7c86f12..9e464eb 100644
--- a/pkg/analyzer/lib/error/error.dart
+++ b/pkg/analyzer/lib/error/error.dart
@@ -532,10 +532,10 @@
   ParserErrorCode.STATIC_OPERATOR,
   ParserErrorCode.STATIC_SETTER_WITHOUT_BODY,
   ParserErrorCode.STATIC_TOP_LEVEL_DECLARATION,
-  ParserErrorCode.SUPER_AS_EXPRESSION,
+  ParserErrorCode.INVALID_SUPER_IN_INITIALIZER,
   ParserErrorCode.SWITCH_HAS_CASE_AFTER_DEFAULT_CASE,
   ParserErrorCode.SWITCH_HAS_MULTIPLE_DEFAULT_CASES,
-  ParserErrorCode.THIS_ACCESS_FROM_INITIALIZER,
+  ParserErrorCode.INVALID_THIS_IN_INITIALIZER,
   ParserErrorCode.TOP_LEVEL_OPERATOR,
   ParserErrorCode.TYPEDEF_IN_CLASS,
   ParserErrorCode.TYPE_ARGUMENTS_ON_TYPE_VARIABLE,
diff --git a/pkg/analyzer/lib/src/dart/error/syntactic_errors.dart b/pkg/analyzer/lib/src/dart/error/syntactic_errors.dart
index b5adf19..67fbccc 100644
--- a/pkg/analyzer/lib/src/dart/error/syntactic_errors.dart
+++ b/pkg/analyzer/lib/src/dart/error/syntactic_errors.dart
@@ -379,11 +379,17 @@
       "The modifier 'async*' isn't allowed for an expression function body.",
       correction: "Try converting the body to a block.");
 
+  static const ParserErrorCode INVALID_SUPER_IN_INITIALIZER =
+      _INVALID_SUPER_IN_INITIALIZER;
+
   static const ParserErrorCode INVALID_SYNC = const ParserErrorCode(
       'INVALID_SYNC',
       "The modifier 'sync' isn't allowed for an expression function body.",
       correction: "Try converting the body to a block.");
 
+  static const ParserErrorCode INVALID_THIS_IN_INITIALIZER =
+      _INVALID_THIS_IN_INITIALIZER;
+
   static const ParserErrorCode INVALID_UNICODE_ESCAPE = _INVALID_UNICODE_ESCAPE;
 
   static const ParserErrorCode LIBRARY_DIRECTIVE_NOT_FIRST =
@@ -676,17 +682,12 @@
           "Top-level declarations can't be declared to be static.",
           correction: "Try removing the keyword 'static'.");
 
-  static const ParserErrorCode SUPER_AS_EXPRESSION = _SUPER_AS_EXPRESSION;
-
   static const ParserErrorCode SWITCH_HAS_CASE_AFTER_DEFAULT_CASE =
       _SWITCH_HAS_CASE_AFTER_DEFAULT_CASE;
 
   static const ParserErrorCode SWITCH_HAS_MULTIPLE_DEFAULT_CASES =
       _SWITCH_HAS_MULTIPLE_DEFAULT_CASES;
 
-  static const ParserErrorCode THIS_ACCESS_FROM_INITIALIZER =
-      _THIS_ACCESS_FROM_INITIALIZER;
-
   static const ParserErrorCode TOP_LEVEL_OPERATOR = _TOP_LEVEL_OPERATOR;
 
   static const ParserErrorCode TYPE_ARGUMENTS_ON_TYPE_VARIABLE =
diff --git a/pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart b/pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart
index 0bf0763..598d093 100644
--- a/pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart
+++ b/pkg/analyzer/lib/src/dart/error/syntactic_errors.g.dart
@@ -102,8 +102,8 @@
   _MISSING_CATCH_OR_FINALLY,
   _EXPERIMENT_NOT_ENABLED,
   _EXPECTED_ELSE_OR_COMMA,
-  _SUPER_AS_EXPRESSION,
-  _THIS_ACCESS_FROM_INITIALIZER,
+  _INVALID_SUPER_IN_INITIALIZER,
+  _INVALID_THIS_IN_INITIALIZER,
 ];
 
 const ParserErrorCode _ABSTRACT_CLASS_MEMBER = const ParserErrorCode(
@@ -388,6 +388,14 @@
         r"The operator '?.' cannot be used with 'super' because 'super' cannot be null.",
         correction: "Try replacing '?.' with '.'");
 
+const ParserErrorCode _INVALID_SUPER_IN_INITIALIZER = const ParserErrorCode(
+    'INVALID_SUPER_IN_INITIALIZER',
+    r"Can only use 'super' in an initializer for calling the superclass constructor (e.g. 'super()' or 'super.namedConstructor()')");
+
+const ParserErrorCode _INVALID_THIS_IN_INITIALIZER = const ParserErrorCode(
+    'INVALID_THIS_IN_INITIALIZER',
+    r"Can only use 'this' in an initializer for field initialization (e.g. 'this.x = something') and constructor redirection (e.g. 'this()' or 'this.namedConstructor())");
+
 const ParserErrorCode _INVALID_UNICODE_ESCAPE = const ParserErrorCode(
     'INVALID_UNICODE_ESCAPE',
     r"An escape sequence starting with '\u' must be followed by 4 hexadecimal digits or from 1 to 6 digits between '{' and '}'.");
@@ -517,11 +525,6 @@
     'STATIC_OPERATOR', r"Operators can't be static.",
     correction: "Try removing the keyword 'static'.");
 
-const ParserErrorCode _SUPER_AS_EXPRESSION = const ParserErrorCode(
-    'SUPER_AS_EXPRESSION', r"Can't use 'super' as an expression.",
-    correction:
-        "To delegate a constructor to a super constructor, put the super call as an initializer.");
-
 const ParserErrorCode _SWITCH_HAS_CASE_AFTER_DEFAULT_CASE =
     const ParserErrorCode('SWITCH_HAS_CASE_AFTER_DEFAULT_CASE',
         r"The default case should be the last case in a switch statement.",
@@ -533,9 +536,6 @@
         r"The 'default' case can only be declared once.",
         correction: "Try removing all but one default case.");
 
-const ParserErrorCode _THIS_ACCESS_FROM_INITIALIZER = const ParserErrorCode(
-    'THIS_ACCESS_FROM_INITIALIZER', r"Can't access 'this' in an initializer.");
-
 const ParserErrorCode _TOP_LEVEL_OPERATOR = const ParserErrorCode(
     'TOP_LEVEL_OPERATOR', r"Operators must be declared within a class.",
     correction:
diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart
index ddf4af8..746666b 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -29,11 +29,11 @@
         messageFieldInitializerOutsideConstructor,
         messageIllegalAssignmentToNonAssignable,
         messageInterpolationInUri,
+        messageInvalidSuperInInitializer,
+        messageInvalidThisInInitializer,
         messageMissingAssignableSelector,
         messageNativeClauseShouldBeAnnotation,
         messageStaticConstructor,
-        messageSuperAsExpression,
-        messageThisAccessInInitializer,
         messageTypedefNotFunction,
         templateDuplicateLabelInSwitchStatement,
         templateExpectedButGot,
@@ -1133,14 +1133,14 @@
                   targetFunct.superKeyword, null, null, target.argumentList));
               // TODO(danrubel): Consider generating this error in the parser
               // This error is also reported in the body builder
-              handleRecoverableError(messageSuperAsExpression,
+              handleRecoverableError(messageInvalidSuperInInitializer,
                   targetFunct.superKeyword, targetFunct.superKeyword);
             } else if (targetFunct is ThisExpression) {
               initializers.add(ast.redirectingConstructorInvocation(
                   targetFunct.thisKeyword, null, null, target.argumentList));
               // TODO(danrubel): Consider generating this error in the parser
               // This error is also reported in the body builder
-              handleRecoverableError(messageThisAccessInInitializer,
+              handleRecoverableError(messageInvalidThisInInitializer,
                   targetFunct.thisKeyword, targetFunct.thisKeyword);
             } else {
               throw new UnsupportedError(
@@ -1193,14 +1193,14 @@
                 targetFunct.superKeyword, null, null, target.argumentList));
             // TODO(danrubel): Consider generating this error in the parser
             // This error is also reported in the body builder
-            handleRecoverableError(messageSuperAsExpression,
+            handleRecoverableError(messageInvalidSuperInInitializer,
                 targetFunct.superKeyword, targetFunct.superKeyword);
           } else if (targetFunct is ThisExpression) {
             initializers.add(ast.redirectingConstructorInvocation(
                 targetFunct.thisKeyword, null, null, target.argumentList));
             // TODO(danrubel): Consider generating this error in the parser
             // This error is also reported in the body builder
-            handleRecoverableError(messageThisAccessInInitializer,
+            handleRecoverableError(messageInvalidThisInInitializer,
                 targetFunct.thisKeyword, targetFunct.thisKeyword);
           } else {
             throw new UnsupportedError(
diff --git a/pkg/analyzer/test/generated/parser_fasta_test.dart b/pkg/analyzer/test/generated/parser_fasta_test.dart
index 14663b1..27264e0 100644
--- a/pkg/analyzer/test/generated/parser_fasta_test.dart
+++ b/pkg/analyzer/test/generated/parser_fasta_test.dart
@@ -835,7 +835,7 @@
     // https://github.com/dart-lang/sdk/issues/36262
     // https://github.com/dart-lang/sdk/issues/31198
     parseCompilationUnit('class B extends A { B(): super().foo {} }', errors: [
-      expectedError(ParserErrorCode.SUPER_AS_EXPRESSION, 25, 5),
+      expectedError(ParserErrorCode.INVALID_SUPER_IN_INITIALIZER, 25, 5),
     ]);
   }
 
@@ -844,7 +844,7 @@
     // https://github.com/dart-lang/sdk/issues/31198
     parseCompilationUnit('class B extends A { B(): super().foo() {} }',
         errors: [
-          expectedError(ParserErrorCode.SUPER_AS_EXPRESSION, 25, 5),
+          expectedError(ParserErrorCode.INVALID_SUPER_IN_INITIALIZER, 25, 5),
         ]);
   }
 
@@ -852,7 +852,7 @@
     // https://github.com/dart-lang/sdk/issues/36262
     // https://github.com/dart-lang/sdk/issues/31198
     parseCompilationUnit('class B extends A { B(): this().foo; }', errors: [
-      expectedError(ParserErrorCode.THIS_ACCESS_FROM_INITIALIZER, 25, 4),
+      expectedError(ParserErrorCode.INVALID_THIS_IN_INITIALIZER, 25, 4),
     ]);
   }
 
@@ -860,7 +860,7 @@
     // https://github.com/dart-lang/sdk/issues/36262
     // https://github.com/dart-lang/sdk/issues/31198
     parseCompilationUnit('class B extends A { B(): this().foo(); }', errors: [
-      expectedError(ParserErrorCode.THIS_ACCESS_FROM_INITIALIZER, 25, 4),
+      expectedError(ParserErrorCode.INVALID_THIS_IN_INITIALIZER, 25, 4),
     ]);
   }
 
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 3939f16..9925cfd 100644
--- a/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart
+++ b/pkg/front_end/lib/src/fasta/fasta_codes_generated.dart
@@ -6178,6 +6178,17 @@
 }
 
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
+const Code<Null> codeInvalidSuperInInitializer =
+    messageInvalidSuperInInitializer;
+
+// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
+const MessageCode messageInvalidSuperInInitializer = const MessageCode(
+    "InvalidSuperInInitializer",
+    index: 95,
+    message:
+        r"""Can only use 'super' in an initializer for calling the superclass constructor (e.g. 'super()' or 'super.namedConstructor()')""");
+
+// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
 const Code<Null> codeInvalidSyncModifier = messageInvalidSyncModifier;
 
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
@@ -6188,6 +6199,16 @@
     tip: r"""Try replacing 'sync' with 'sync*'.""");
 
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
+const Code<Null> codeInvalidThisInInitializer = messageInvalidThisInInitializer;
+
+// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
+const MessageCode messageInvalidThisInInitializer = const MessageCode(
+    "InvalidThisInInitializer",
+    index: 96,
+    message:
+        r"""Can only use 'this' in an initializer for field initialization (e.g. 'this.x = something') and constructor redirection (e.g. 'this()' or 'this.namedConstructor())""");
+
+// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
 const Code<Null> codeInvalidUnicodeEscape = messageInvalidUnicodeEscape;
 
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
@@ -8616,7 +8637,7 @@
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
 const MessageCode messageSuperAsExpression = const MessageCode(
     "SuperAsExpression",
-    index: 95,
+    analyzerCodes: <String>["SUPER_AS_EXPRESSION"],
     message: r"""Can't use 'super' as an expression.""",
     tip:
         r"""To delegate a constructor to a super constructor, put the super call as an initializer.""");
@@ -8955,15 +8976,6 @@
 }
 
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
-const Code<Null> codeThisAccessInInitializer = messageThisAccessInInitializer;
-
-// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
-const MessageCode messageThisAccessInInitializer = const MessageCode(
-    "ThisAccessInInitializer",
-    index: 96,
-    message: r"""Can't access 'this' in an initializer.""");
-
-// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
 const Code<Null> codeThisAsIdentifier = messageThisAsIdentifier;
 
 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
diff --git a/pkg/front_end/messages.status b/pkg/front_end/messages.status
index f4317d0..89791bc 100644
--- a/pkg/front_end/messages.status
+++ b/pkg/front_end/messages.status
@@ -239,6 +239,8 @@
 InvalidInitializer/example: Fail
 InvalidPackageUri/analyzerCode: Fail
 InvalidPackageUri/example: Fail
+InvalidSuperInInitializer/example: Fail
+InvalidThisInInitializer/example: Fail
 InvalidUseOfNullAwareAccess/example: Fail
 InvalidVoid/script1: Fail
 InvalidVoid/script2: Fail
@@ -370,7 +372,6 @@
 SwitchCaseFallThrough/example: Fail
 SyntheticToken/example: Fail # Can't be tested, used to recover from other errors.
 ThisAccessInFieldInitializer/example: Fail
-ThisAccessInInitializer/example: Fail
 ThisAsIdentifier/example: Fail
 ThisOrSuperAccessInFieldInitializer/example: Fail
 TooFewArguments/example: Fail
diff --git a/pkg/front_end/messages.yaml b/pkg/front_end/messages.yaml
index 27acf2a..dbcc070 100644
--- a/pkg/front_end/messages.yaml
+++ b/pkg/front_end/messages.yaml
@@ -2510,10 +2510,15 @@
   template: "SDK libraries specification not found: #uri."
   tip: "Normally, the specification is a file named 'libraries.json' in the Dart SDK install location."
 
-ThisAccessInInitializer:
+InvalidSuperInInitializer:
+  index: 95
+  template: "Can only use 'super' in an initializer for calling the superclass constructor (e.g. 'super()' or 'super.namedConstructor()')"
+  analyzerCode: ParserErrorCode.INVALID_SUPER_IN_INITIALIZER
+
+InvalidThisInInitializer:
   index: 96
-  template: "Can't access 'this' in an initializer."
-  analyzerCode: ParserErrorCode.THIS_ACCESS_FROM_INITIALIZER
+  template: "Can only use 'this' in an initializer for field initialization (e.g. 'this.x = something') and constructor redirection (e.g. 'this()' or 'this.namedConstructor())"
+  analyzerCode: ParserErrorCode.INVALID_THIS_IN_INITIALIZER
 
 ThisAccessInFieldInitializer:
   template: "Can't access 'this' in a field initializer to read '#name'."
@@ -2532,10 +2537,9 @@
   analyzerCode: SUPER_AS_EXPRESSION
 
 SuperAsExpression:
-  index: 95
   template: "Can't use 'super' as an expression."
   tip: "To delegate a constructor to a super constructor, put the super call as an initializer."
-  analyzerCode: ParserErrorCode.SUPER_AS_EXPRESSION
+  analyzerCode: SUPER_AS_EXPRESSION
 
 SwitchExpressionNotAssignable:
   template: "Type '#type' of the switch expression isn't assignable to the type '#type2' of this case expression."