linter: Fix issue with extension type methods as possibly const arguments

Fixes https://github.com/dart-lang/linter/issues/4978

Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try
Change-Id: I532f491355bc34028b19d323102f038dff861445
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371780
Auto-Submit: Sam Rawlins <srawlins@google.com>
Commit-Queue: Sam Rawlins <srawlins@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
Sam Rawlins 2024-06-14 19:42:24 +00:00 committed by Commit Queue
parent 7ec9514093
commit 3c65563735
3 changed files with 33 additions and 0 deletions

View file

@ -341,6 +341,7 @@ class _ConstantAnalysisErrorListener extends AnalysisErrorListener {
case CompileTimeErrorCode
.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZED_BY_NON_CONST:
case CompileTimeErrorCode.CONST_EVAL_EXTENSION_METHOD:
case CompileTimeErrorCode.CONST_EVAL_EXTENSION_TYPE_METHOD:
case CompileTimeErrorCode.CONST_EVAL_METHOD_INVOCATION:
case CompileTimeErrorCode.CONST_EVAL_PROPERTY_ACCESS:
case CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL:

View file

@ -3176,6 +3176,21 @@ void Function(int, {int? b})
''');
}
test_visitUnaryExpression_extensionType() async {
await assertErrorsInCode('''
extension type const A(int it) {
int operator -() => 0;
}
const v1 = A(1);
const v2 = -v1;
''', [
error(CompileTimeErrorCode.CONST_EVAL_EXTENSION_TYPE_METHOD, 89, 3),
]);
var result = _topLevelVar('v2');
_assertNull(result);
}
void _assertHasPrimitiveEqualityFalse(String name) {
var value = _evaluateConstant(name);
var featureSet = result.libraryElement.featureSet;

View file

@ -180,6 +180,23 @@ A f(int i) => A('adjacent' '$i');
''');
}
test_cannotBeConst_argumentIsExtensionTypeMethodCall() async {
await assertNoDiagnostics(r'''
final class A {
final Ex f;
const A(this.f);
void foo() {
var a = A(-f);
}
}
extension type const Ex(int i) implements int {
Ex operator -() => Ex(-i);
}
''');
}
test_cannotBeConst_argumentIsListLiteral_nonLiteralElement() async {
await assertNoDiagnostics(r'''
class A {