Issue 46514. Report an error for using extension methods in a prefix or binary constant expressions.

Bug: https://github.com/dart-lang/sdk/issues/46514
Change-Id: I59ed3bd754043302632f31aedeaedd7824b8a8c2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301505
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index 93e3a8b..7bc29df 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -590,6 +590,11 @@
 
   @override
   DartObjectImpl? visitBinaryExpression(BinaryExpression node) {
+    if (node.staticElement?.enclosingElement is ExtensionElement) {
+      _error(node, null);
+      return null;
+    }
+
     TokenType operatorType = node.operator.type;
     var leftResult = node.leftOperand.accept(this);
     // evaluate lazy operators
@@ -1021,6 +1026,10 @@
       _error(node, CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION);
       return null;
     }
+    if (node.staticElement?.enclosingElement is ExtensionElement) {
+      _error(node, null);
+      return null;
+    }
     if (node.operator.type == TokenType.BANG) {
       return _dartObjectComputer.logicalNot(node, operand);
     } else if (node.operator.type == TokenType.TILDE) {
diff --git a/pkg/analyzer/test/generated/constant_test.dart b/pkg/analyzer/test/generated/constant_test.dart
index e1b85ef..a19e30a 100644
--- a/pkg/analyzer/test/generated/constant_test.dart
+++ b/pkg/analyzer/test/generated/constant_test.dart
@@ -388,6 +388,26 @@
     await _assertValueInt(-42, "-42");
   }
 
+  test_negated_object_hasExtension() async {
+    await assertErrorsInCode('''
+extension on Object {
+  int operator -() => 0;
+}
+
+const Object v1 = 1;
+const v2 = -v1;
+''', [
+      error(CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE, 82,
+          3),
+    ]);
+
+    final v2 = findElement.topVar('v2');
+    final evaluationResult = v2.evaluationResult;
+    assertDartObjectText(evaluationResult.value, r'''
+<null>
+''');
+  }
+
   /// Even though it is an error to specify a default value for a required
   /// parameter, we still can evaluate it.
   test_normalParameter_requiredNamed_hasDefault() async {
@@ -577,6 +597,26 @@
     await _assertValueInt(5, "2 + 3");
   }
 
+  test_plus_object_hasExtension() async {
+    await assertErrorsInCode('''
+extension on Object {
+  int operator +(Object other) => 0;
+}
+
+const Object v1 = 0;
+const v2 = v1 + v1;
+''', [
+      error(CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE, 94,
+          7),
+    ]);
+
+    final v2 = findElement.topVar('v2');
+    final evaluationResult = v2.evaluationResult;
+    assertDartObjectText(evaluationResult.value, r'''
+<null>
+''');
+  }
+
   test_plus_string_string() async {
     await _assertValueString("ab", "'a' + 'b'");
   }