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

Commit 3575b81

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Only add const in a constant context (issue 32819)
Change-Id: Ia149d812d2540fa98d76765ffb8b54fe32ecf010 Reviewed-on: https://dart-review.googlesource.com/50120 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 09f7d2a commit 3575b81

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

pkg/analysis_server/test/analysis/get_hover_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ class A {
556556
const A(int i);
557557
}
558558
main() {
559-
var a = A(0);
559+
const a = A(0);
560560
}
561561
''');
562562
HoverInformation hover = await prepareHover('A(0)');

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6558,7 +6558,7 @@ class InstanceCreationExpressionImpl extends ExpressionImpl
65586558
if (!isImplicit) {
65596559
return keyword.keyword == Keyword.CONST;
65606560
} else {
6561-
return inConstantContext || canBeConst();
6561+
return inConstantContext;
65626562
}
65636563
}
65646564

pkg/analyzer/test/src/dart/ast/ast_test.dart

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,40 @@ class C {
4949
assertInContext("C(0", true);
5050
}
5151

52-
test_inConstantContext_instanceCreation_functionLiteral() {
52+
test_inConstantContext_instanceCreation_fieldWithConstConstructor() {
5353
parse('''
54-
const V = () => C();
5554
class C {
55+
final d = D();
5656
const C();
5757
}
58+
class D {
59+
const D();
60+
}
5861
''');
59-
assertInContext("C()", false);
62+
assertInContext("D()", false);
6063
}
6164

62-
test_inConstantContext_instanceCreation_initializer_false() {
65+
test_inConstantContext_instanceCreation_fieldWithoutConstConstructor() {
6366
parse('''
64-
var c = C();
6567
class C {
66-
const C();
68+
final d = D();
69+
C();
70+
}
71+
class D {
72+
const D();
6773
}
6874
''');
69-
assertInContext("C()", false);
75+
assertInContext("D()", false);
7076
}
7177

72-
test_inConstantContext_instanceCreation_initializer_true() {
78+
test_inConstantContext_instanceCreation_functionLiteral() {
7379
parse('''
74-
const c = C();
80+
const V = () => C();
7581
class C {
7682
const C();
7783
}
7884
''');
79-
assertInContext("C()", true);
85+
assertInContext("C()", false);
8086
}
8187

8288
test_inConstantContext_instanceCreation_instanceCreation_false() {
@@ -216,6 +222,26 @@ class C {
216222
assertInContext("C()", true);
217223
}
218224

225+
test_inConstantContext_instanceCreation_topLevelVariable_false() {
226+
parse('''
227+
var c = C();
228+
class C {
229+
const C();
230+
}
231+
''');
232+
assertInContext("C()", false);
233+
}
234+
235+
test_inConstantContext_instanceCreation_topLevelVariable_true() {
236+
parse('''
237+
const c = C();
238+
class C {
239+
const C();
240+
}
241+
''');
242+
assertInContext("C()", true);
243+
}
244+
219245
test_inConstantContext_listLiteral_annotation_true() {
220246
parse('''
221247
@C([])
@@ -475,7 +501,7 @@ class C {
475501
const C.c();
476502
}
477503
''');
478-
assertIsConst("C(C", true);
504+
assertIsConst("C(C", false);
479505
}
480506

481507
void test_isConst_notInContext_constructor_const_constParam_named() async {
@@ -486,7 +512,7 @@ class C {
486512
const C({c});
487513
}
488514
''');
489-
assertIsConst("C(c", true);
515+
assertIsConst("C(c", false);
490516
}
491517

492518
void
@@ -498,7 +524,7 @@ class C {
498524
const C({c});
499525
}
500526
''');
501-
assertIsConst("C(c", true);
527+
assertIsConst("C(c", false);
502528
}
503529

504530
void test_isConst_notInContext_constructor_const_constParam_parens() async {
@@ -510,7 +536,7 @@ class C {
510536
const C.c();
511537
}
512538
''');
513-
assertIsConst("C( (", true);
539+
assertIsConst("C( (", false);
514540
}
515541

516542
void test_isConst_notInContext_constructor_const_generic_named() async {
@@ -521,7 +547,7 @@ class C<E> {
521547
const C.n();
522548
}
523549
''');
524-
assertIsConst("C<int>.n", true);
550+
assertIsConst("C<int>.n", false);
525551
}
526552

527553
void
@@ -536,7 +562,7 @@ class C<E> {
536562
import 'c.dart' as p;
537563
f() => <Object>[p.C<int>.n()];
538564
''');
539-
assertIsConst("C<int>", true);
565+
assertIsConst("C<int>", false);
540566
}
541567

542568
void test_isConst_notInContext_constructor_const_generic_unnamed() async {
@@ -547,7 +573,7 @@ class C<E> {
547573
const C();
548574
}
549575
''');
550-
assertIsConst("C<int>", true);
576+
assertIsConst("C<int>", false);
551577
}
552578

553579
void
@@ -562,7 +588,7 @@ class C<E> {
562588
import 'c.dart' as p;
563589
f() => <Object>[p.C<int>()];
564590
''');
565-
assertIsConst("C<int>", true);
591+
assertIsConst("C<int>", false);
566592
}
567593

568594
void
@@ -605,7 +631,7 @@ class C<E> {
605631
const C.n();
606632
}
607633
''');
608-
assertIsConst("C.n()", true);
634+
assertIsConst("C.n()", false);
609635
}
610636

611637
void
@@ -620,7 +646,7 @@ class C {
620646
import 'c.dart' as p;
621647
f() => <Object>[p.C.n()];
622648
''');
623-
assertIsConst("C.n()", true);
649+
assertIsConst("C.n()", false);
624650
}
625651

626652
void test_isConst_notInContext_constructor_const_nonGeneric_unnamed() async {
@@ -631,7 +657,7 @@ class C {
631657
const C();
632658
}
633659
''');
634-
assertIsConst("C()", true);
660+
assertIsConst("C()", false);
635661
}
636662

637663
void
@@ -646,7 +672,7 @@ class C {
646672
import 'c.dart' as p;
647673
f() => <Object>[p.C()];
648674
''');
649-
assertIsConst("C()", true);
675+
assertIsConst("C()", false);
650676
}
651677

652678
void test_isConst_notInContext_constructor_nonConst() async {

tests/language_2/language_2_dartdevc.status

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ implicit_creation/implicit_const_context_constructor_generic_named_test: Compile
7070
implicit_creation/implicit_const_context_constructor_generic_test: CompileTimeError
7171
implicit_creation/implicit_const_context_prefix_constructor_generic_named_test: CompileTimeError
7272
implicit_creation/implicit_const_context_prefix_constructor_generic_test: CompileTimeError
73-
implicit_creation/implicit_new_or_const_composite_test: RuntimeError
74-
implicit_creation/implicit_new_or_const_generic_test: RuntimeError
75-
implicit_creation/implicit_new_or_const_test: RuntimeError
7673
implicit_downcast_during_compound_assignment_test: RuntimeError
7774
implicit_downcast_during_indexed_compound_assignment_test: RuntimeError
7875
implicit_downcast_during_indexed_if_null_assignment_test: RuntimeError

0 commit comments

Comments
 (0)