meta: add TargetKind to @factory

Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try,pkg-win-release-try
Change-Id: I5c0608637f2feb9986e890f3c83cf02884bef7f9
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369789
Commit-Queue: Sam Rawlins <srawlins@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Sam Rawlins 2024-06-06 15:39:56 +00:00 committed by Commit Queue
parent 3f47c93b8c
commit e04c53d5ac
12 changed files with 17 additions and 102 deletions

View file

@ -3534,8 +3534,6 @@ WarningCode.INVALID_EXPORT_OF_INTERNAL_ELEMENT_INDIRECTLY:
The fix is to update the export directive so that the member using the name
is no longer exported.
WarningCode.INVALID_FACTORY_ANNOTATION:
status: hasFix
WarningCode.INVALID_FACTORY_METHOD_DECL:
status: noFix
WarningCode.INVALID_FACTORY_METHOD_IMPL:

View file

@ -1525,9 +1525,6 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
WarningCode.INVALID_ANNOTATION_TARGET: [
RemoveAnnotation.new,
],
WarningCode.INVALID_FACTORY_ANNOTATION: [
RemoveAnnotation.new,
],
WarningCode.INVALID_INTERNAL_ANNOTATION: [
RemoveAnnotation.new,
],

View file

@ -69,10 +69,7 @@ class AnnotationVerifier {
void _checkFactory(Annotation node) {
var parent = node.parent;
if (parent is! MethodDeclaration) {
_errorReporter.atNode(
node.name,
WarningCode.INVALID_FACTORY_ANNOTATION,
);
// Warning reported by `_checkKinds`.
return;
}
var returnType = parent.returnType?.type;

View file

@ -6599,13 +6599,6 @@ class WarningCode extends AnalyzerErrorCode {
hasPublishedDocs: true,
);
/// This warning is generated anywhere a @factory annotation is associated
/// with anything other than a method.
static const WarningCode INVALID_FACTORY_ANNOTATION = WarningCode(
'INVALID_FACTORY_ANNOTATION',
"Only methods can be annotated as factories.",
);
/// Parameters:
/// 0: The name of the method
static const WarningCode INVALID_FACTORY_METHOD_DECL = WarningCode(

View file

@ -1010,7 +1010,6 @@ const List<ErrorCode> errorCodeValues = [
WarningCode.INVALID_ANNOTATION_TARGET,
WarningCode.INVALID_EXPORT_OF_INTERNAL_ELEMENT,
WarningCode.INVALID_EXPORT_OF_INTERNAL_ELEMENT_INDIRECTLY,
WarningCode.INVALID_FACTORY_ANNOTATION,
WarningCode.INVALID_FACTORY_METHOD_DECL,
WarningCode.INVALID_FACTORY_METHOD_IMPL,
WarningCode.INVALID_INTERNAL_ANNOTATION,

View file

@ -227,6 +227,9 @@ class _Experimental {
const _Experimental();
}
@Target({
TargetKind.method,
})
class _Factory {
const _Factory();
}

View file

@ -24014,12 +24014,6 @@ WarningCode:
If the function doesn't need to be exported, then stop exporting it,
either by removing it from the `show` clause, adding it to the `hide`
clause, or by removing the export.
INVALID_FACTORY_ANNOTATION:
problemMessage: Only methods can be annotated as factories.
hasPublishedDocs: false
comment: |-
This warning is generated anywhere a @factory annotation is associated
with anything other than a method.
INVALID_FACTORY_METHOD_DECL:
problemMessage: "Factory method '{0}' must have a return type."
hasPublishedDocs: true

View file

@ -1,67 +0,0 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/src/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(InvalidFactoryAnnotationTest);
});
}
@reflectiveTest
class InvalidFactoryAnnotationTest extends PubPackageResolutionTest {
@override
void setUp() {
super.setUp();
writeTestPackageConfigWithMeta();
}
test_class() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
@factory
class X {
}
''', [
error(WarningCode.INVALID_FACTORY_ANNOTATION, 34, 7),
]);
}
test_extensionType() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
@factory
extension type E(int i) {
}
''', [
error(WarningCode.INVALID_FACTORY_ANNOTATION, 34, 7),
]);
}
test_field() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
class X {
@factory
int x = 0;
}
''', [
error(WarningCode.INVALID_FACTORY_ANNOTATION, 46, 7),
]);
}
test_topLevelFunction() async {
await assertErrorsInCode(r'''
import 'package:meta/meta.dart';
@factory
main() { }
''', [
error(WarningCode.INVALID_FACTORY_ANNOTATION, 34, 7),
]);
}
}

View file

@ -428,7 +428,6 @@ import 'invalid_export_of_internal_element_test.dart'
as invalid_export_of_internal_element;
import 'invalid_extension_argument_count_test.dart'
as invalid_extension_argument_count;
import 'invalid_factory_annotation_test.dart' as invalid_factory_annotation;
import 'invalid_factory_method_impl_test.dart' as invalid_factory_method_impl;
import 'invalid_factory_name_not_a_class_test.dart'
as invalid_factory_name_not_a_class;
@ -1190,7 +1189,6 @@ main() {
invalid_exception_value.main();
invalid_export_of_internal_element.main();
invalid_extension_argument_count.main();
invalid_factory_annotation.main();
invalid_factory_method_impl.main();
invalid_factory_name_not_a_class.main();
invalid_field_type_in_struct.main();

View file

@ -642,7 +642,9 @@ class _Experimental {
const _Experimental();
}
// TODO(srawlins): Enforce with `TargetKind.method`.
@Target({
TargetKind.method,
})
class _Factory {
const _Factory();
}

View file

@ -1,13 +1,12 @@
## 1.16.0-dev
- Require that `@Immutable` is only used on classes, extensions, and mixins.
Previously, this behavior was custom-wired into the Dart analyzer, but now it
is specified with TargetKinds.
- Require that `@mustBeOverridden` and `@mustCallSuper` are only used on
overridable members. Previously, this behavior was custom-wired into the Dart
analyzer, but now it is specified with TargetKinds.
- Require that `@sealed` is only used on classes. Previously, this behavior was
custom-wired into the Dart analyzer, but now it is specified with TargetKinds.
- Add `TargetKind`s to a few annotations to match custom-wired behavior that the
Dart analyzer has been providing:
- Require that `@factory` is only used on methods.
- Require that `@Immutable` is only used on classes, extensions, and mixins.
- Require that `@mustBeOverridden` and `@mustCallSuper` are only used on
overridable members.
- Require that `@sealed` is only used on classes.
## 1.15.0

View file

@ -673,7 +673,9 @@ class _Experimental {
const _Experimental();
}
// TODO(srawlins): Enforce with `TargetKind.method`.
@Target({
TargetKind.method,
})
class _Factory {
const _Factory();
}