From dd3fbffac7ac302a4aed3c033d96dfaf204d54d4 Mon Sep 17 00:00:00 2001 From: Johnni Winther Date: Fri, 26 Apr 2024 14:00:04 +0000 Subject: [PATCH] [cfe,analyzer] Support new as identifier in metadata The parser didn't support 'new' as identifier in this context. The CL also adds the reporting of tear-offs as metadata for the CFE. This was already handled by the analyzer. Change-Id: I7ab5868fa83e5f216d0e7be7ae9cec4a2c865e80 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/364480 Reviewed-by: Jens Johansen Commit-Queue: Johnni Winther Reviewed-by: Erik Ernst --- .../src/parser/identifier_context_impl.dart | 3 + .../lib/src/fasta/kernel/body_builder.dart | 3 +- .../testcases/general/annotation_new.dart | 39 ++++++++ .../general/annotation_new.dart.strong.expect | 90 ++++++++++++++++++ ...otation_new.dart.strong.transformed.expect | 90 ++++++++++++++++++ ...annotation_new.dart.textual_outline.expect | 33 +++++++ ...n_new.dart.textual_outline_modelled.expect | 33 +++++++ .../general/annotation_new.dart.weak.expect | 90 ++++++++++++++++++ .../annotation_new.dart.weak.modular.expect | 90 ++++++++++++++++++ .../annotation_new.dart.weak.outline.expect | 93 +++++++++++++++++++ ...nnotation_new.dart.weak.transformed.expect | 90 ++++++++++++++++++ .../metadata/constructor_new_error_test.dart | 51 ++++++++++ .../metadata/constructor_new_test.dart | 21 +++++ .../mirrors/metadata_allowed_values_test.dart | 6 +- 14 files changed, 730 insertions(+), 2 deletions(-) create mode 100644 pkg/front_end/testcases/general/annotation_new.dart create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.strong.expect create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.strong.transformed.expect create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.textual_outline.expect create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.textual_outline_modelled.expect create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.weak.expect create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.weak.modular.expect create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.weak.outline.expect create mode 100644 pkg/front_end/testcases/general/annotation_new.dart.weak.transformed.expect create mode 100644 tests/language/metadata/constructor_new_error_test.dart create mode 100644 tests/language/metadata/constructor_new_test.dart diff --git a/pkg/_fe_analyzer_shared/lib/src/parser/identifier_context_impl.dart b/pkg/_fe_analyzer_shared/lib/src/parser/identifier_context_impl.dart index 4e3f97a3c20..c4cf992dfa9 100644 --- a/pkg/_fe_analyzer_shared/lib/src/parser/identifier_context_impl.dart +++ b/pkg/_fe_analyzer_shared/lib/src/parser/identifier_context_impl.dart @@ -972,6 +972,9 @@ class MetadataReferenceIdentifierContext extends IdentifierContext { } return identifier; } + + @override + bool get allowsNewAsIdentifier => isContinuation; } /// See [IdentifierContext.methodDeclaration], 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 19729623263..6504eef9b1b 100644 --- a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart +++ b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart @@ -927,7 +927,8 @@ class BodyBuilder extends StackListenerImpl } ConstantContext savedConstantContext = pop() as ConstantContext; - if (expression is! StaticAccessGenerator && + if (!(expression is StaticAccessGenerator && + expression.readTarget is Field) && expression is! VariableUseGenerator && // TODO(johnniwinther): Stop using the type of the generator here. // Ask a property instead. diff --git a/pkg/front_end/testcases/general/annotation_new.dart b/pkg/front_end/testcases/general/annotation_new.dart new file mode 100644 index 00000000000..020487523b5 --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart @@ -0,0 +1,39 @@ +// Copyright (c) 2024, 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 'annotation_new.dart' as self; + +const field = 1; + +void method() {} + +class Class { + const Class(); + + static const field = 1; + + static void method() {} +} + +class GenericClass { + const GenericClass(); +} + +@Class.new() // OK +@GenericClass.new() // OK +@GenericClass.new() // OK +@self.Class.new() // OK +@self.GenericClass.new() // OK +@self.GenericClass.new() // OK +@field // OK +@self.field // OK +@method // Error +@self.method // Error +@Class.field // OK +@Class.method // Error +@Class.new // Error +@self.Class.field // OK +@self.Class.method // Error +@self.Class.new // Error +main() {} diff --git a/pkg/front_end/testcases/general/annotation_new.dart.strong.expect b/pkg/front_end/testcases/general/annotation_new.dart.strong.expect new file mode 100644 index 00000000000..ca726185ea0 --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.strong.expect @@ -0,0 +1,90 @@ +library; +// +// Problems in library: +// +// pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.new // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.new // Error +// ^ +// +import self as self; +import "dart:core" as core; + +import "org-dartlang-testcase:///annotation_new.dart" as self; + +class Class extends core::Object /*hasConstConstructor*/ { + static const field core::int field = #C1; + const constructor •() → self::Class + : super core::Object::•() + ; + static method method() → void {} +} +class GenericClass extends core::Object /*hasConstConstructor*/ { + const constructor •() → self::GenericClass + : super core::Object::•() + ; +} +static const field core::int field = #C1; +static method method() → void {} +@#C2 +@#C3 +@#C4 +@#C2 +@#C3 +@#C4 +@#C1 +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.method // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.new // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.new // Error + ^" +static method main() → dynamic {} + +constants { + #C1 = 1 + #C2 = self::Class {} + #C3 = self::GenericClass {} + #C4 = self::GenericClass {} +} + + +Constructor coverage from constants: +org-dartlang-testcase:///annotation_new.dart: +- Class. (from org-dartlang-testcase:///annotation_new.dart:12:9) +- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart) +- GenericClass. (from org-dartlang-testcase:///annotation_new.dart:20:9) diff --git a/pkg/front_end/testcases/general/annotation_new.dart.strong.transformed.expect b/pkg/front_end/testcases/general/annotation_new.dart.strong.transformed.expect new file mode 100644 index 00000000000..ca726185ea0 --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.strong.transformed.expect @@ -0,0 +1,90 @@ +library; +// +// Problems in library: +// +// pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.new // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.new // Error +// ^ +// +import self as self; +import "dart:core" as core; + +import "org-dartlang-testcase:///annotation_new.dart" as self; + +class Class extends core::Object /*hasConstConstructor*/ { + static const field core::int field = #C1; + const constructor •() → self::Class + : super core::Object::•() + ; + static method method() → void {} +} +class GenericClass extends core::Object /*hasConstConstructor*/ { + const constructor •() → self::GenericClass + : super core::Object::•() + ; +} +static const field core::int field = #C1; +static method method() → void {} +@#C2 +@#C3 +@#C4 +@#C2 +@#C3 +@#C4 +@#C1 +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.method // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.new // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.new // Error + ^" +static method main() → dynamic {} + +constants { + #C1 = 1 + #C2 = self::Class {} + #C3 = self::GenericClass {} + #C4 = self::GenericClass {} +} + + +Constructor coverage from constants: +org-dartlang-testcase:///annotation_new.dart: +- Class. (from org-dartlang-testcase:///annotation_new.dart:12:9) +- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart) +- GenericClass. (from org-dartlang-testcase:///annotation_new.dart:20:9) diff --git a/pkg/front_end/testcases/general/annotation_new.dart.textual_outline.expect b/pkg/front_end/testcases/general/annotation_new.dart.textual_outline.expect new file mode 100644 index 00000000000..b2294c649b4 --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.textual_outline.expect @@ -0,0 +1,33 @@ +import 'annotation_new.dart' as self; + +const field = 1; + +void method() {} + +class Class { + const Class(); + static const field = 1; + static void method() {} +} + +class GenericClass { + const GenericClass(); +} + +@Class.new() +@GenericClass.new() +@GenericClass.new() +@self.Class.new() +@self.GenericClass.new() +@self.GenericClass.new() +@field +@self.field +@method +@self.method +@Class.field +@Class.method +@Class.new +@self.Class.field +@self.Class.method +@self.Class.new +main() {} diff --git a/pkg/front_end/testcases/general/annotation_new.dart.textual_outline_modelled.expect b/pkg/front_end/testcases/general/annotation_new.dart.textual_outline_modelled.expect new file mode 100644 index 00000000000..4adb63a5626 --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.textual_outline_modelled.expect @@ -0,0 +1,33 @@ +import 'annotation_new.dart' as self; + +class Class { + const Class(); + static const field = 1; + static void method() {} +} + +class GenericClass { + const GenericClass(); +} + +const field = 1; + +@Class.new() +@GenericClass.new() +@GenericClass.new() +@self.Class.new() +@self.GenericClass.new() +@self.GenericClass.new() +@field +@self.field +@method +@self.method +@Class.field +@Class.method +@Class.new +@self.Class.field +@self.Class.method +@self.Class.new +main() {} + +void method() {} diff --git a/pkg/front_end/testcases/general/annotation_new.dart.weak.expect b/pkg/front_end/testcases/general/annotation_new.dart.weak.expect new file mode 100644 index 00000000000..6639f762b4f --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.weak.expect @@ -0,0 +1,90 @@ +library; +// +// Problems in library: +// +// pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.new // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.new // Error +// ^ +// +import self as self; +import "dart:core" as core; + +import "org-dartlang-testcase:///annotation_new.dart" as self; + +class Class extends core::Object /*hasConstConstructor*/ { + static const field core::int field = #C1; + const constructor •() → self::Class + : super core::Object::•() + ; + static method method() → void {} +} +class GenericClass extends core::Object /*hasConstConstructor*/ { + const constructor •() → self::GenericClass + : super core::Object::•() + ; +} +static const field core::int field = #C1; +static method method() → void {} +@#C2 +@#C3 +@#C4 +@#C2 +@#C3 +@#C4 +@#C1 +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.method // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.new // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.new // Error + ^" +static method main() → dynamic {} + +constants { + #C1 = 1 + #C2 = self::Class {} + #C3 = self::GenericClass {} + #C4 = self::GenericClass {} +} + + +Constructor coverage from constants: +org-dartlang-testcase:///annotation_new.dart: +- Class. (from org-dartlang-testcase:///annotation_new.dart:12:9) +- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart) +- GenericClass. (from org-dartlang-testcase:///annotation_new.dart:20:9) diff --git a/pkg/front_end/testcases/general/annotation_new.dart.weak.modular.expect b/pkg/front_end/testcases/general/annotation_new.dart.weak.modular.expect new file mode 100644 index 00000000000..6639f762b4f --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.weak.modular.expect @@ -0,0 +1,90 @@ +library; +// +// Problems in library: +// +// pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.new // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.new // Error +// ^ +// +import self as self; +import "dart:core" as core; + +import "org-dartlang-testcase:///annotation_new.dart" as self; + +class Class extends core::Object /*hasConstConstructor*/ { + static const field core::int field = #C1; + const constructor •() → self::Class + : super core::Object::•() + ; + static method method() → void {} +} +class GenericClass extends core::Object /*hasConstConstructor*/ { + const constructor •() → self::GenericClass + : super core::Object::•() + ; +} +static const field core::int field = #C1; +static method method() → void {} +@#C2 +@#C3 +@#C4 +@#C2 +@#C3 +@#C4 +@#C1 +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.method // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.new // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.new // Error + ^" +static method main() → dynamic {} + +constants { + #C1 = 1 + #C2 = self::Class {} + #C3 = self::GenericClass {} + #C4 = self::GenericClass {} +} + + +Constructor coverage from constants: +org-dartlang-testcase:///annotation_new.dart: +- Class. (from org-dartlang-testcase:///annotation_new.dart:12:9) +- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart) +- GenericClass. (from org-dartlang-testcase:///annotation_new.dart:20:9) diff --git a/pkg/front_end/testcases/general/annotation_new.dart.weak.outline.expect b/pkg/front_end/testcases/general/annotation_new.dart.weak.outline.expect new file mode 100644 index 00000000000..751991c4719 --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.weak.outline.expect @@ -0,0 +1,93 @@ +library; +// +// Problems in library: +// +// pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.new // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.new // Error +// ^ +// +import self as self; +import "dart:core" as core; + +import "org-dartlang-testcase:///annotation_new.dart" as self; + +class Class extends core::Object /*hasConstConstructor*/ { + static const field core::int field = 1; + const constructor •() → self::Class + : super core::Object::•() + ; + static method method() → void + ; +} +class GenericClass extends core::Object /*hasConstConstructor*/ { + const constructor •() → self::GenericClass + : super core::Object::•() + ; +} +static const field core::int field = 1; +static method method() → void + ; +@self::Class::•() +@self::GenericClass::•() +@self::GenericClass::•() +@self::Class::•() +@self::GenericClass::•() +@self::GenericClass::•() +@self::field +@self::field +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@method // Error + ^" in self::method +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.method // Error + ^" in self::method +@self::Class::field +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.method // Error + ^" in self::Class::method +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.new // Error + ^" in self::Class::• +@self::Class::field +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.method // Error + ^" in self::Class::method +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.new // Error + ^" in self::Class::• +static method main() → dynamic + ; + + +Extra constant evaluation status: +Evaluated: ConstructorInvocation @ org-dartlang-testcase:///annotation_new.dart:23:2 -> InstanceConstant(const Class{}) +Evaluated: ConstructorInvocation @ org-dartlang-testcase:///annotation_new.dart:24:2 -> InstanceConstant(const GenericClass{}) +Evaluated: ConstructorInvocation @ org-dartlang-testcase:///annotation_new.dart:25:2 -> InstanceConstant(const GenericClass{}) +Evaluated: ConstructorInvocation @ org-dartlang-testcase:///annotation_new.dart:26:2 -> InstanceConstant(const Class{}) +Evaluated: ConstructorInvocation @ org-dartlang-testcase:///annotation_new.dart:27:2 -> InstanceConstant(const GenericClass{}) +Evaluated: ConstructorInvocation @ org-dartlang-testcase:///annotation_new.dart:28:2 -> InstanceConstant(const GenericClass{}) +Evaluated: StaticGet @ org-dartlang-testcase:///annotation_new.dart:29:2 -> IntConstant(1) +Evaluated: StaticGet @ org-dartlang-testcase:///annotation_new.dart:30:7 -> IntConstant(1) +Evaluated: StaticGet @ org-dartlang-testcase:///annotation_new.dart:33:8 -> IntConstant(1) +Evaluated: StaticGet @ org-dartlang-testcase:///annotation_new.dart:36:13 -> IntConstant(1) +Extra constant evaluation: evaluated: 10, effectively constant: 10 diff --git a/pkg/front_end/testcases/general/annotation_new.dart.weak.transformed.expect b/pkg/front_end/testcases/general/annotation_new.dart.weak.transformed.expect new file mode 100644 index 00000000000..6639f762b4f --- /dev/null +++ b/pkg/front_end/testcases/general/annotation_new.dart.weak.transformed.expect @@ -0,0 +1,90 @@ +library; +// +// Problems in library: +// +// pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @Class.new // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.method // Error +// ^ +// +// pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// @self.Class.new // Error +// ^ +// +import self as self; +import "dart:core" as core; + +import "org-dartlang-testcase:///annotation_new.dart" as self; + +class Class extends core::Object /*hasConstConstructor*/ { + static const field core::int field = #C1; + const constructor •() → self::Class + : super core::Object::•() + ; + static method method() → void {} +} +class GenericClass extends core::Object /*hasConstConstructor*/ { + const constructor •() → self::GenericClass + : super core::Object::•() + ; +} +static const field core::int field = #C1; +static method method() → void {} +@#C2 +@#C3 +@#C4 +@#C2 +@#C3 +@#C4 +@#C1 +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:31:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:32:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.method // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:34:8: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:35:2: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@Class.new // Error + ^" +@#C1 +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:37:13: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.method // Error + ^" +@invalid-expression "pkg/front_end/testcases/general/annotation_new.dart:38:7: Error: This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.Class.new // Error + ^" +static method main() → dynamic {} + +constants { + #C1 = 1 + #C2 = self::Class {} + #C3 = self::GenericClass {} + #C4 = self::GenericClass {} +} + + +Constructor coverage from constants: +org-dartlang-testcase:///annotation_new.dart: +- Class. (from org-dartlang-testcase:///annotation_new.dart:12:9) +- Object. (from org-dartlang-sdk:///sdk/lib/core/object.dart) +- GenericClass. (from org-dartlang-testcase:///annotation_new.dart:20:9) diff --git a/tests/language/metadata/constructor_new_error_test.dart b/tests/language/metadata/constructor_new_error_test.dart new file mode 100644 index 00000000000..9ccf03a40f3 --- /dev/null +++ b/tests/language/metadata/constructor_new_error_test.dart @@ -0,0 +1,51 @@ +// Copyright (c) 2024, 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 'constructor_new_error_test.dart' as self; + +class Class { + const Class(); +} + +class GenericClass { + const GenericClass(); +} + +@Class.new +// [error column 1, length 10] +// [analyzer] COMPILE_TIME_ERROR.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS +// [error column 2] +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@GenericClass.new +// [error column 1, length 17] +// [analyzer] COMPILE_TIME_ERROR.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS +// [error column 2] +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@GenericClass.new +// [error column 1, length 30] +// [analyzer] COMPILE_TIME_ERROR.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS +// [error column 2] +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// ^^^ +// [analyzer] SYNTACTIC_ERROR.ANNOTATION_WITH_TYPE_ARGUMENTS_UNINSTANTIATED +// [cfe] An annotation with type arguments must be followed by an argument list. +@self.Class.new +// [error column 1, length 15] +// [analyzer] COMPILE_TIME_ERROR.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS +// ^ +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.GenericClass.new +// [error column 1, length 22] +// [analyzer] COMPILE_TIME_ERROR.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS +// ^ +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +@self.GenericClass.new +// [error column 1, length 35] +// [analyzer] COMPILE_TIME_ERROR.NO_ANNOTATION_CONSTRUCTOR_ARGUMENTS +// ^ +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. +// ^^^ +// [analyzer] SYNTACTIC_ERROR.ANNOTATION_WITH_TYPE_ARGUMENTS_UNINSTANTIATED +// [cfe] An annotation with type arguments must be followed by an argument list. +main() {} diff --git a/tests/language/metadata/constructor_new_test.dart b/tests/language/metadata/constructor_new_test.dart new file mode 100644 index 00000000000..7d4d8206f83 --- /dev/null +++ b/tests/language/metadata/constructor_new_test.dart @@ -0,0 +1,21 @@ +// Copyright (c) 2024, 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 'constructor_new_test.dart' as self; + +class Class { + const Class(); +} + +class GenericClass { + const GenericClass(); +} + +@Class.new() +@GenericClass.new() +@GenericClass.new() +@self.Class.new() +@self.GenericClass.new() +@self.GenericClass.new() +main() {} diff --git a/tests/lib/mirrors/metadata_allowed_values_test.dart b/tests/lib/mirrors/metadata_allowed_values_test.dart index 61a3640bfb1..3e8e458de8b 100644 --- a/tests/lib/mirrors/metadata_allowed_values_test.dart +++ b/tests/lib/mirrors/metadata_allowed_values_test.dart @@ -117,6 +117,8 @@ class Q {} @V.tearOff // ^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ANNOTATION +// ^ +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. class V { static tearOff() {} } @@ -126,6 +128,8 @@ topLevelTearOff() => 4; @topLevelTearOff // ^^^^^^^^^^^^^^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ANNOTATION +// ^ +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. class W {} @TypeParameter @@ -254,7 +258,7 @@ class JJ {} // ^^^ // [analyzer] COMPILE_TIME_ERROR.INVALID_ANNOTATION // ^ -// [cfe] Constant evaluation error: +// [cfe] This can't be used as an annotation; an annotation should be a reference to a compile-time constant variable, or a call to a constant constructor. class KK { const KK(); }