Skip to content

Commit 535a9be

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
analyzer: Report bad accesses to member named 'new'
Fixes #47077 Change-Id: Ib344204b28adbaf7a9dfa92e2ddb157930cc6547 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/212500 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 7495184 commit 535a9be

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

pkg/analyzer/lib/src/dart/resolver/ast_rewrite.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ class AstRewriter {
227227
// This isn't a constructor reference.
228228
return node;
229229
}
230+
if (node.parent is AssignmentExpression) {
231+
return node;
232+
}
230233
var prefix = node.prefix;
231234
var element = nameScope.lookup(prefix.name).getter;
232235
if (element is ClassElement) {

pkg/analyzer/lib/src/dart/resolver/type_property_resolver.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ class TypePropertyResolver {
7272

7373
receiverType = _resolveTypeParameter(receiverType, ifLegacy: true);
7474

75+
if (name == 'new') {
76+
_needsGetterError = true;
77+
_needsSetterError = true;
78+
return _toResult();
79+
}
80+
7581
if (_typeSystem.isDynamicBounded(receiverType)) {
7682
_lookupInterfaceType(
7783
_typeProvider.objectType,

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,88 @@ class A {
441441
int f() => A.x;
442442
''');
443443
}
444+
445+
test_new_cascade() async {
446+
await assertErrorsInCode('''
447+
class C {}
448+
449+
f(C? c) {
450+
c..new;
451+
}
452+
''', [
453+
error(CompileTimeErrorCode.UNDEFINED_GETTER, 27, 3),
454+
]);
455+
}
456+
457+
test_new_dynamic() async {
458+
await assertErrorsInCode('''
459+
f(dynamic d) {
460+
d.new;
461+
}
462+
''', [
463+
error(CompileTimeErrorCode.UNDEFINED_GETTER, 19, 3),
464+
]);
465+
}
466+
467+
test_new_expression() async {
468+
await assertErrorsInCode('''
469+
class C {}
470+
471+
f(C? c1, C c2) {
472+
(c1 ?? c2).new;
473+
}
474+
''', [
475+
error(CompileTimeErrorCode.UNDEFINED_GETTER, 42, 3),
476+
]);
477+
}
478+
479+
test_new_nullAware() async {
480+
await assertErrorsInCode('''
481+
class C {}
482+
483+
f(C? c) {
484+
c?.new;
485+
}
486+
''', [
487+
error(CompileTimeErrorCode.UNDEFINED_GETTER, 27, 3),
488+
]);
489+
}
490+
491+
test_new_prefixedIdentifier() async {
492+
await assertErrorsInCode('''
493+
class C {}
494+
495+
abstract class D {
496+
C get c;
497+
}
498+
499+
f(D d) {
500+
d.c.new;
501+
}
502+
''', [
503+
error(CompileTimeErrorCode.UNDEFINED_GETTER, 60, 3),
504+
]);
505+
}
506+
507+
test_new_simpleIdentifier() async {
508+
await assertErrorsInCode('''
509+
class C {}
510+
511+
f(C c) {
512+
c.new;
513+
}
514+
''', [
515+
error(CompileTimeErrorCode.UNDEFINED_GETTER, 25, 3),
516+
]);
517+
}
518+
519+
test_new_typeVariable() async {
520+
await assertErrorsInCode('''
521+
f<T>(T t) {
522+
t.new;
523+
}
524+
''', [
525+
error(CompileTimeErrorCode.UNDEFINED_GETTER, 16, 3),
526+
]);
527+
}
444528
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ f() => p.c;
2525
]);
2626
}
2727

28+
test_new() async {
29+
newFile('$testPackageLibPath/lib.dart', content: '');
30+
await assertErrorsInCode(r'''
31+
import 'lib.dart' as p;
32+
void f() {
33+
p.new;
34+
}
35+
''', [
36+
error(CompileTimeErrorCode.UNDEFINED_PREFIXED_NAME, 39, 3),
37+
]);
38+
}
39+
2840
test_setterContext() async {
2941
newFile('$testPackageLibPath/lib.dart');
3042
await assertErrorsInCode('''

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,74 @@ f(C c) {
143143
@reflectiveTest
144144
class UndefinedSetterWithNullSafetyTest extends PubPackageResolutionTest
145145
with UndefinedSetterTestCases {
146+
test_new_cascade() async {
147+
await assertErrorsInCode('''
148+
class C {}
149+
150+
f(C? c) {
151+
c..new = 1;
152+
}
153+
''', [
154+
error(CompileTimeErrorCode.UNDEFINED_SETTER, 27, 3),
155+
]);
156+
}
157+
158+
test_new_dynamic() async {
159+
await assertErrorsInCode('''
160+
f(dynamic d) {
161+
d.new = 1;
162+
}
163+
''', [
164+
error(CompileTimeErrorCode.UNDEFINED_SETTER, 19, 3),
165+
]);
166+
}
167+
168+
test_new_instance() async {
169+
await assertErrorsInCode('''
170+
class C {}
171+
172+
f(C c) {
173+
c.new = 1;
174+
}
175+
''', [
176+
error(CompileTimeErrorCode.UNDEFINED_SETTER, 25, 3),
177+
]);
178+
}
179+
180+
test_new_interfaceType() async {
181+
await assertErrorsInCode('''
182+
class C {}
183+
184+
f() {
185+
C.new = 1;
186+
}
187+
''', [
188+
error(CompileTimeErrorCode.UNDEFINED_SETTER, 22, 3),
189+
]);
190+
}
191+
192+
test_new_nullAware() async {
193+
await assertErrorsInCode('''
194+
class C {}
195+
196+
f(C? c) {
197+
c?.new = 1;
198+
}
199+
''', [
200+
error(CompileTimeErrorCode.UNDEFINED_SETTER, 27, 3),
201+
]);
202+
}
203+
204+
test_new_typeVariable() async {
205+
await assertErrorsInCode('''
206+
f<T>(T t) {
207+
t.new = 1;
208+
}
209+
''', [
210+
error(CompileTimeErrorCode.UNDEFINED_SETTER, 16, 3),
211+
]);
212+
}
213+
146214
test_set_abstract_field_valid() async {
147215
await assertNoErrorsInCode('''
148216
abstract class A {

0 commit comments

Comments
 (0)