Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 0fedb9b

Browse files
Dmitry Stefantsovcommit-bot@chromium.org
authored andcommitted
[fasta] Disable upwards new/const completion
Change-Id: If2c14bf402bbcbacbf4cf2da351eb7234e1e0944 Reviewed-on: https://dart-review.googlesource.com/49900 Commit-Queue: Dmitry Stefantsov <[email protected]> Reviewed-by: Kevin Millikin <[email protected]>
1 parent 3d8524b commit 0fedb9b

29 files changed

+107
-193
lines changed

pkg/front_end/lib/src/fasta/constant_context.dart

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,4 @@ enum ConstantContext {
2323
/// This means that `Object()` and `[]` are equivalent to `const Object()` and
2424
/// `const []` respectively. `new Object()` is a compile-time error.
2525
inferred,
26-
27-
/// In a context that allows only constant values, but requires them to be
28-
/// defined as `const` explicitly. For example, in default values of optional
29-
/// and named parameters.
30-
///
31-
/// The following code should emit a compile-time error:
32-
///
33-
/// class Bar { const Bar(); }
34-
/// class Foo { void foo({Bar bar: Bar()}) {} }
35-
///
36-
/// The following code should compile without errors:
37-
///
38-
/// class Bar { const Bar(); }
39-
/// class Foo { void foo({Bar bar: const Bar()}) {} }
40-
needsExplicitConst,
4126
}

pkg/front_end/lib/src/fasta/fasta_codes_generated.dart

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,18 +3761,6 @@ const MessageCode messageNativeClauseShouldBeAnnotation = const MessageCode(
37613761
tip:
37623762
r"""Try removing this native clause and adding @native() or @native('native-name') before the declaration.""");
37633763

3764-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
3765-
const Code<Null> codeNeedExplicitConst = messageNeedExplicitConst;
3766-
3767-
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
3768-
const MessageCode messageNeedExplicitConst = const MessageCode(
3769-
"NeedExplicitConst",
3770-
analyzerCode: "NON_CONSTANT_DEFAULT_VALUE",
3771-
dart2jsCode: "*fatal*",
3772-
severity: Severity.error,
3773-
message: r"""An explicit 'const' keyword is expected here.""",
3774-
tip: r"""Put the 'const' keyword at this position.""");
3775-
37763764
// DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE.
37773765
const Template<
37783766
Message Function(Token token)> templateNoFormals = const Template<

pkg/front_end/lib/src/fasta/kernel/body_builder.dart

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ import 'redirecting_factory_body.dart'
7676

7777
import '../names.dart';
7878

79-
import 'constness_evaluator.dart' show evaluateConstness;
80-
8179
import 'fasta_accessors.dart';
8280

8381
import 'kernel_api.dart';
@@ -185,12 +183,6 @@ class BodyBuilder<Arguments> extends ScopeListener<JumpTarget>
185183
/// and where that was.
186184
Map<String, int> initializedFields;
187185

188-
/// Constructor invocations (either generative or factory) with not specified
189-
/// `new` or `const` keywords. The constness for these should be inferred
190-
/// based on the subexpressions.
191-
List<Expression> constructorInvocationsWithImplicitConstness =
192-
new List<Expression>();
193-
194186
BodyBuilder(
195187
KernelLibraryBuilder library,
196188
this.member,
@@ -497,8 +489,6 @@ class BodyBuilder<Arguments> extends ScopeListener<JumpTarget>
497489
}
498490
}
499491
}
500-
501-
inferConstness();
502492
}
503493

504494
@override
@@ -696,24 +686,6 @@ class BodyBuilder<Arguments> extends ScopeListener<JumpTarget>
696686
unhandled("${builder.runtimeType}", "finishFunction", builder.charOffset,
697687
builder.fileUri);
698688
}
699-
700-
inferConstness();
701-
}
702-
703-
// Infers constness of the constructor invocations collected so far in
704-
// [constructorInvocationsWithImplicitConstness], then clears out the list.
705-
void inferConstness() {
706-
for (Expression invocation in constructorInvocationsWithImplicitConstness) {
707-
if (invocation is ConstructorInvocation) {
708-
invocation.isConst = evaluateConstness(invocation, coreTypes, uri);
709-
} else if (invocation is StaticInvocation) {
710-
invocation.isConst = evaluateConstness(invocation, coreTypes, uri);
711-
} else {
712-
unhandled("${invocation.runtimeType}", "inferConstness",
713-
invocation.fileOffset, invocation.location.file);
714-
}
715-
}
716-
constructorInvocationsWithImplicitConstness.clear();
717689
}
718690

719691
@override
@@ -2170,7 +2142,7 @@ class BodyBuilder<Arguments> extends ScopeListener<JumpTarget>
21702142
@override
21712143
void beginFormalParameterDefaultValueExpression() {
21722144
super.push(constantContext);
2173-
constantContext = ConstantContext.needsExplicitConst;
2145+
constantContext = ConstantContext.none;
21742146
}
21752147

21762148
@override
@@ -2497,31 +2469,17 @@ class BodyBuilder<Arguments> extends ScopeListener<JumpTarget>
24972469
argMessage: argMessage);
24982470
}
24992471
if (target is Constructor) {
2500-
if (constantContext == ConstantContext.needsExplicitConst &&
2501-
constness == Constness.implicit) {
2502-
return buildCompileTimeError(
2503-
fasta.messageNeedExplicitConst, charOffset, noLength);
2504-
}
25052472
isConst =
25062473
isConst || constantContext != ConstantContext.none && target.isConst;
25072474
if ((isConst || constantContext == ConstantContext.inferred) &&
25082475
!target.isConst) {
25092476
return deprecated_buildCompileTimeError(
25102477
"Not a const constructor.", charOffset);
25112478
}
2512-
ShadowConstructorInvocation invocation = new ShadowConstructorInvocation(
2513-
target,
2514-
targetTypeArguments,
2515-
initialTarget,
2516-
forest.castArguments(arguments),
2479+
return new ShadowConstructorInvocation(target, targetTypeArguments,
2480+
initialTarget, forest.castArguments(arguments),
25172481
isConst: isConst)
25182482
..fileOffset = charOffset;
2519-
if (constness == Constness.implicit &&
2520-
target.isConst &&
2521-
constantContext != ConstantContext.inferred) {
2522-
constructorInvocationsWithImplicitConstness.add(invocation);
2523-
}
2524-
return invocation;
25252483
} else {
25262484
Procedure procedure = target;
25272485
if (procedure.isFactory) {
@@ -2532,22 +2490,10 @@ class BodyBuilder<Arguments> extends ScopeListener<JumpTarget>
25322490
return deprecated_buildCompileTimeError(
25332491
"Not a const factory.", charOffset);
25342492
}
2535-
if (constantContext == ConstantContext.needsExplicitConst &&
2536-
constness == Constness.implicit) {
2537-
return buildCompileTimeError(
2538-
fasta.messageNeedExplicitConst, charOffset, noLength);
2539-
}
2540-
ShadowFactoryConstructorInvocation invocation =
2541-
new ShadowFactoryConstructorInvocation(target, targetTypeArguments,
2542-
initialTarget, forest.castArguments(arguments),
2543-
isConst: isConst)
2544-
..fileOffset = charOffset;
2545-
if (constness == Constness.implicit &&
2546-
procedure.isConst &&
2547-
constantContext != ConstantContext.inferred) {
2548-
constructorInvocationsWithImplicitConstness.add(invocation);
2549-
}
2550-
return invocation;
2493+
return new ShadowFactoryConstructorInvocation(target,
2494+
targetTypeArguments, initialTarget, forest.castArguments(arguments),
2495+
isConst: isConst)
2496+
..fileOffset = charOffset;
25512497
} else {
25522498
return new ShadowStaticInvocation(
25532499
target, forest.castArguments(arguments),

pkg/front_end/messages.yaml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,17 +2061,3 @@ ImplicitCallOfNonMethod:
20612061
template: "Can't invoke the type '#type' because its declaration of `.call` is not a method."
20622062
tip: "Change .call to a method or explicitly invoke .call."
20632063
severity: ERROR
2064-
2065-
NeedExplicitConst:
2066-
template: "An explicit 'const' keyword is expected here."
2067-
tip: "Put the 'const' keyword at this position."
2068-
severity: ERROR
2069-
analyzerCode: NON_CONSTANT_DEFAULT_VALUE # TODO(dmitryas): Ask the analyzer team if the code is correct.
2070-
dart2jsCode: "*fatal*"
2071-
script: >
2072-
class Foo {
2073-
const Foo();
2074-
}
2075-
class Bar {
2076-
const Bar([Foo foo = Foo()]);
2077-
}

pkg/front_end/testcases/implicit_const_with_static_fields.dart.direct.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ class C extends core::Object {
1010
}
1111
static const field dynamic constTopLevelField = 42;
1212
static method main() → dynamic {
13-
const self::C::•(self::C::constField);
14-
const self::C::•(self::constTopLevelField);
13+
new self::C::•(self::C::constField);
14+
new self::C::•(self::constTopLevelField);
1515
}

pkg/front_end/testcases/implicit_const_with_static_fields.dart.direct.transformed.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ class C extends core::Object {
1010
}
1111
static const field dynamic constTopLevelField = 42;
1212
static method main() → dynamic {
13-
const self::C::•(self::C::constField);
14-
const self::C::•(self::constTopLevelField);
13+
new self::C::•(self::C::constField);
14+
new self::C::•(self::constTopLevelField);
1515
}

pkg/front_end/testcases/implicit_const_with_static_fields.dart.strong.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ class C extends core::Object {
1010
}
1111
static const field core::int constTopLevelField = 42;
1212
static method main() → dynamic {
13-
const self::C::•(self::C::constField);
14-
const self::C::•(self::constTopLevelField);
13+
new self::C::•(self::C::constField);
14+
new self::C::•(self::constTopLevelField);
1515
}

pkg/front_end/testcases/implicit_const_with_static_fields.dart.strong.transformed.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ class C extends core::Object {
1010
}
1111
static const field core::int constTopLevelField = 42;
1212
static method main() → dynamic {
13-
const self::C::•(self::C::constField);
14-
const self::C::•(self::constTopLevelField);
13+
new self::C::•(self::C::constField);
14+
new self::C::•(self::constTopLevelField);
1515
}

pkg/front_end/testcases/magic_const.dart.direct.expect

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ class NotConstant extends core::Object {
1212
: super core::Object::•()
1313
;
1414
}
15-
static method foo({dynamic a = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:9: Error: An explicit 'const' keyword is expected here.
16-
Put the 'const' keyword at this position.
17-
foo({a: Constant(), b: Constant(), c: []}) {}
18-
^", dynamic b = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:24: Error: An explicit 'const' keyword is expected here.
19-
Put the 'const' keyword at this position.
20-
foo({a: Constant(), b: Constant(), c: []}) {}
21-
^", dynamic c = <dynamic>[]}) → dynamic {}
15+
static method foo({dynamic a = new self::Constant::•(), dynamic b = new self::Constant::•(), dynamic c = <dynamic>[]}) → dynamic {}
2216
static method test() → dynamic {
2317
invalid-expression "pkg/front_end/testcases/magic_const.dart:18:9: Error: Not a const constructor.
2418
const NotConstant();
2519
^";
26-
const self::Constant::•();
20+
new self::Constant::•();
2721
const dynamic x = const self::Constant::•();
28-
const core::bool::fromEnvironment("fisk");
22+
core::bool::fromEnvironment("fisk");
2923
const dynamic b = const core::bool::fromEnvironment("fisk");
3024
}
3125
static method main() → dynamic {}

pkg/front_end/testcases/magic_const.dart.direct.transformed.expect

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ class NotConstant extends core::Object {
1212
: super core::Object::•()
1313
;
1414
}
15-
static method foo({dynamic a = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:9: Error: An explicit 'const' keyword is expected here.
16-
Put the 'const' keyword at this position.
17-
foo({a: Constant(), b: Constant(), c: []}) {}
18-
^", dynamic b = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:24: Error: An explicit 'const' keyword is expected here.
19-
Put the 'const' keyword at this position.
20-
foo({a: Constant(), b: Constant(), c: []}) {}
21-
^", dynamic c = <dynamic>[]}) → dynamic {}
15+
static method foo({dynamic a = new self::Constant::•(), dynamic b = new self::Constant::•(), dynamic c = <dynamic>[]}) → dynamic {}
2216
static method test() → dynamic {
2317
invalid-expression "pkg/front_end/testcases/magic_const.dart:18:9: Error: Not a const constructor.
2418
const NotConstant();
2519
^";
26-
const self::Constant::•();
20+
new self::Constant::•();
2721
const dynamic x = const self::Constant::•();
28-
const core::bool::fromEnvironment("fisk");
22+
core::bool::fromEnvironment("fisk");
2923
const dynamic b = const core::bool::fromEnvironment("fisk");
3024
}
3125
static method main() → dynamic {}

pkg/front_end/testcases/magic_const.dart.strong.expect

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ class NotConstant extends core::Object {
1212
: super core::Object::•()
1313
;
1414
}
15-
static method foo({dynamic a = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:9: Error: An explicit 'const' keyword is expected here.
16-
Put the 'const' keyword at this position.
17-
foo({a: Constant(), b: Constant(), c: []}) {}
18-
^", dynamic b = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:24: Error: An explicit 'const' keyword is expected here.
19-
Put the 'const' keyword at this position.
20-
foo({a: Constant(), b: Constant(), c: []}) {}
21-
^", dynamic c = <dynamic>[]}) → dynamic {}
15+
static method foo({dynamic a = new self::Constant::•(), dynamic b = new self::Constant::•(), dynamic c = <dynamic>[]}) → dynamic {}
2216
static method test() → dynamic {
2317
invalid-expression "pkg/front_end/testcases/magic_const.dart:18:9: Error: Not a const constructor.
2418
const NotConstant();
2519
^";
26-
const self::Constant::•();
20+
new self::Constant::•();
2721
const self::Constant x = const self::Constant::•();
28-
const core::bool::fromEnvironment("fisk");
22+
core::bool::fromEnvironment("fisk");
2923
const core::bool b = const core::bool::fromEnvironment("fisk");
3024
}
3125
static method main() → dynamic {}

pkg/front_end/testcases/magic_const.dart.strong.transformed.expect

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ class NotConstant extends core::Object {
1212
: super core::Object::•()
1313
;
1414
}
15-
static method foo({dynamic a = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:9: Error: An explicit 'const' keyword is expected here.
16-
Put the 'const' keyword at this position.
17-
foo({a: Constant(), b: Constant(), c: []}) {}
18-
^", dynamic b = invalid-expression "pkg/front_end/testcases/magic_const.dart:15:24: Error: An explicit 'const' keyword is expected here.
19-
Put the 'const' keyword at this position.
20-
foo({a: Constant(), b: Constant(), c: []}) {}
21-
^", dynamic c = <dynamic>[]}) → dynamic {}
15+
static method foo({dynamic a = new self::Constant::•(), dynamic b = new self::Constant::•(), dynamic c = <dynamic>[]}) → dynamic {}
2216
static method test() → dynamic {
2317
invalid-expression "pkg/front_end/testcases/magic_const.dart:18:9: Error: Not a const constructor.
2418
const NotConstant();
2519
^";
26-
const self::Constant::•();
20+
new self::Constant::•();
2721
const self::Constant x = const self::Constant::•();
28-
const core::bool::fromEnvironment("fisk");
22+
core::bool::fromEnvironment("fisk");
2923
const core::bool b = const core::bool::fromEnvironment("fisk");
3024
}
3125
static method main() → dynamic {}

pkg/front_end/testcases/nested_implicit_const_with_env_var.dart.direct.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class C extends core::Object {
1919
: super core::Object::•()
2020
;
2121
method fun() → dynamic {
22-
const self::B::•(const self::A::•(self::foo));
22+
new self::B::•(new self::A::•(self::foo));
2323
}
2424
}
2525
static const field core::int foo = const core::int::fromEnvironment("fisk");

pkg/front_end/testcases/nested_implicit_const_with_env_var.dart.direct.transformed.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class C extends core::Object {
1919
: super core::Object::•()
2020
;
2121
method fun() → dynamic {
22-
const self::B::•(const self::A::•(self::foo));
22+
new self::B::•(new self::A::•(self::foo));
2323
}
2424
}
2525
static const field core::int foo = const core::int::fromEnvironment("fisk");

pkg/front_end/testcases/nested_implicit_const_with_env_var.dart.strong.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class C extends core::Object {
1919
: super core::Object::•()
2020
;
2121
method fun() → dynamic {
22-
const self::B::•(const self::A::•(self::foo));
22+
new self::B::•(new self::A::•(self::foo));
2323
}
2424
}
2525
static const field core::int foo = const core::int::fromEnvironment("fisk");

pkg/front_end/testcases/nested_implicit_const_with_env_var.dart.strong.transformed.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class C extends core::Object {
1919
: super core::Object::•()
2020
;
2121
method fun() → dynamic {
22-
const self::B::•(const self::A::•(self::foo));
22+
new self::B::•(new self::A::•(self::foo));
2323
}
2424
}
2525
static const field core::int foo = const core::int::fromEnvironment("fisk");

pkg/front_end/testcases/new_const_insertion/simple.dart.direct.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class A extends core::Object {
1010
}
1111
static method main() → dynamic {
1212
core::int foo = 42;
13-
const self::A::•(5);
14-
const self::A::•(5.+(5));
13+
new self::A::•(5);
14+
new self::A::•(5.+(5));
1515
new self::A::•(foo);
1616
new self::A::•(5.+(foo));
1717
}

pkg/front_end/testcases/new_const_insertion/simple.dart.direct.transformed.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class A extends core::Object {
1010
}
1111
static method main() → dynamic {
1212
core::int foo = 42;
13-
const self::A::•(5);
14-
const self::A::•(5.+(5));
13+
new self::A::•(5);
14+
new self::A::•(5.+(5));
1515
new self::A::•(foo);
1616
new self::A::•(5.+(foo));
1717
}

pkg/front_end/testcases/new_const_insertion/simple.dart.strong.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class A extends core::Object {
1010
}
1111
static method main() → dynamic {
1212
core::int foo = 42;
13-
const self::A::•(5);
14-
const self::A::•(5.{core::num::+}(5));
13+
new self::A::•(5);
14+
new self::A::•(5.{core::num::+}(5));
1515
new self::A::•(foo);
1616
new self::A::•(5.{core::num::+}(foo));
1717
}

pkg/front_end/testcases/new_const_insertion/simple.dart.strong.transformed.expect

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class A extends core::Object {
1010
}
1111
static method main() → dynamic {
1212
core::int foo = 42;
13-
const self::A::•(5);
14-
const self::A::•(5.{core::num::+}(5));
13+
new self::A::•(5);
14+
new self::A::•(5.{core::num::+}(5));
1515
new self::A::•(foo);
1616
new self::A::•(5.{core::num::+}(foo));
1717
}

0 commit comments

Comments
 (0)