Skip to content

Commit 934980b

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 44780. Fix reporing accessing instance from static when enclosed in local variable.
Bug: #44780 Change-Id: Iec59d928161b68ecf77f95792bd6e9ac8ca0d462 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/181305 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent b23be6b commit 934980b

File tree

3 files changed

+101
-39
lines changed

3 files changed

+101
-39
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,28 @@ class EnclosingExecutableContext {
114114

115115
DartType get returnType => catchErrorOnErrorReturnType ?? element.returnType;
116116

117-
static bool _inFactoryConstructor(ExecutableElement element) {
117+
static bool _inFactoryConstructor(Element element) {
118+
var enclosing = element?.enclosingElement;
119+
if (enclosing == null) {
120+
return false;
121+
}
118122
if (element is ConstructorElement) {
119123
return element.isFactory;
120124
}
121-
var enclosing = element?.enclosingElement;
122-
if (enclosing is ExecutableElement) {
123-
return _inFactoryConstructor(enclosing);
124-
}
125-
return false;
125+
return _inFactoryConstructor(enclosing);
126126
}
127127

128-
static bool _inStaticMethod(ExecutableElement element) {
128+
static bool _inStaticMethod(Element element) {
129129
var enclosing = element?.enclosingElement;
130-
if (enclosing is ClassElement || enclosing is ExtensionElement) {
131-
return element.isStatic;
130+
if (enclosing == null) {
131+
return false;
132132
}
133-
if (enclosing is ExecutableElement) {
134-
return _inStaticMethod(enclosing);
133+
if (enclosing is ClassElement || enclosing is ExtensionElement) {
134+
if (element is ExecutableElement) {
135+
return element.isStatic;
136+
}
135137
}
136-
return false;
138+
return _inStaticMethod(enclosing);
137139
}
138140
}
139141

pkg/analyzer/test/src/diagnostics/instance_member_access_from_factory_test.dart

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,66 +15,97 @@ main() {
1515

1616
@reflectiveTest
1717
class InstanceMemberAccessFromFactoryTest extends PubPackageResolutionTest {
18-
test_named() async {
18+
test_named_getter() async {
1919
await assertErrorsInCode(r'''
2020
class A {
21-
m() {}
22-
A();
21+
int get foo => 0;
22+
2323
factory A.make() {
24-
m();
25-
return new A();
24+
foo;
25+
throw 0;
2626
}
2727
}
2828
''', [
29-
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 51, 1),
29+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 56, 3),
3030
]);
3131
}
3232

33-
test_property() async {
33+
test_named_getter_localFunction() async {
3434
await assertErrorsInCode(r'''
3535
class A {
36-
int m;
37-
A();
36+
int get foo => 0;
37+
3838
factory A.make() {
39-
m;
40-
return new A();
39+
void f() {
40+
foo;
41+
}
42+
f();
43+
throw 0;
4144
}
4245
}
4346
''', [
44-
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 51, 1),
47+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 73, 3),
4548
]);
4649
}
4750

48-
test_property_fromClosure() async {
51+
test_named_method() async {
4952
await assertErrorsInCode(r'''
5053
class A {
51-
int m;
52-
A();
54+
void foo() {}
55+
5356
factory A.make() {
54-
void f() {
55-
m;
56-
}
57-
f();
58-
return new A();
57+
foo();
58+
throw 0;
59+
}
60+
}
61+
''', [
62+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 52, 3),
63+
]);
64+
}
65+
66+
test_named_method_functionExpression() async {
67+
await assertErrorsInCode(r'''
68+
class A {
69+
void foo() {}
70+
71+
factory A.make() {
72+
() => foo();
73+
throw 0;
5974
}
6075
}
6176
''', [
62-
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 68, 1),
77+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 58, 3),
6378
]);
6479
}
6580

66-
test_unnamed() async {
81+
test_named_method_functionExpression_localVariable() async {
6782
await assertErrorsInCode(r'''
6883
class A {
69-
m() {}
70-
A._();
84+
void foo() {}
85+
86+
factory A.make() {
87+
// ignore:unused_local_variable
88+
var x = () => foo();
89+
throw 0;
90+
}
91+
}
92+
''', [
93+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 102, 3),
94+
]);
95+
}
96+
97+
test_unnamed_method() async {
98+
await assertErrorsInCode(r'''
99+
class A {
100+
void foo() {}
101+
71102
factory A() {
72-
m();
73-
return new A._();
103+
foo();
104+
throw 0;
74105
}
75106
}
76107
''', [
77-
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 48, 1),
108+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, 47, 3),
78109
]);
79110
}
80111
}

pkg/analyzer/test/src/diagnostics/instance_member_access_from_static_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,35 @@ class A {
6161
]);
6262
}
6363

64+
test_class_thisGetter_fromMethod_functionExpression() async {
65+
await assertErrorsInCode(r'''
66+
class A {
67+
int get foo => 0;
68+
69+
static void bar() {
70+
() => foo;
71+
}
72+
}
73+
''', [
74+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC, 63, 3),
75+
]);
76+
}
77+
78+
test_class_thisGetter_fromMethod_functionExpression_localVariable() async {
79+
await assertErrorsInCode(r'''
80+
class A {
81+
int get foo => 0;
82+
83+
static void bar() {
84+
// ignore:unused_local_variable
85+
var x = () => foo;
86+
}
87+
}
88+
''', [
89+
error(CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC, 107, 3),
90+
]);
91+
}
92+
6493
test_class_thisMethod_fromMethod() async {
6594
await assertErrorsInCode(r'''
6695
class A {

0 commit comments

Comments
 (0)