Skip to content

Commit 615e448

Browse files
chloestefantsovaCommit Queue
authored and
Commit Queue
committed
[cfe] Support private field names in object patterns
Part of #49749 Closes #51490 Change-Id: I712ad8b5aa87a27614f76da42fb10d4c9e9a03da Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/287740 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Chloe Stefantsova <[email protected]>
1 parent d9e8662 commit 615e448

10 files changed

+194
-3
lines changed

pkg/front_end/lib/src/fasta/type_inference/inference_visitor.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10551,7 +10551,8 @@ class InferenceVisitorImpl extends InferenceVisitorBase
1055110551
}
1055210552

1055310553
if (fieldNameString != null) {
10554-
field.fieldName = new Name(fieldNameString);
10554+
field.fieldName = new Name(fieldNameString,
10555+
fieldNameString.startsWith("_") ? libraryBuilder.library : null);
1055510556

1055610557
ObjectAccessTarget fieldTarget = findInterfaceMember(
1055710558
node.objectType, field.fieldName, field.fileOffset,
@@ -11225,9 +11226,12 @@ class InferenceVisitorImpl extends InferenceVisitorBase
1122511226
required DartType receiverType,
1122611227
required shared.RecordPatternField<TreeNode, Pattern> field,
1122711228
}) {
11228-
// TODO(cstefantsova): Provide a better fileOffset.
11229+
String fieldName = field.name!;
1122911230
ObjectAccessTarget fieldAccessTarget = findInterfaceMember(
11230-
receiverType, new Name(field.name!), field.pattern.fileOffset,
11231+
receiverType,
11232+
new Name(fieldName,
11233+
fieldName.startsWith("_") ? libraryBuilder.library : null),
11234+
field.pattern.fileOffset,
1123111235
callSiteAccessKind: CallSiteAccessKind.getterInvocation);
1123211236
return fieldAccessTarget.getGetterType(this);
1123311237
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
class A {
6+
int _foo = 0;
7+
}
8+
9+
test(dynamic x) {
10+
switch(x) {
11+
case A(_foo: 42):
12+
return x;
13+
default:
14+
return null;
15+
}
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class A extends core::Object {
6+
field core::int _foo = 0;
7+
synthetic constructor •() → self::A
8+
: super core::Object::•()
9+
;
10+
}
11+
static method test(dynamic x) → dynamic {
12+
#L1:
13+
{
14+
final dynamic #0#0 = x;
15+
if(#0#0 is{ForNonNullableByDefault} self::A && #C1 =={core::num::==}{(core::Object) → core::bool} #0#0{self::A}.{self::A::_foo}{core::int}) {
16+
{
17+
return x{self::A};
18+
}
19+
}
20+
else {
21+
{
22+
return null;
23+
}
24+
}
25+
}
26+
}
27+
28+
constants {
29+
#C1 = 42
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class A extends core::Object {
6+
field core::int _foo = 0;
7+
synthetic constructor •() → self::A
8+
: super core::Object::•()
9+
;
10+
}
11+
static method test(dynamic x) → dynamic {
12+
#L1:
13+
{
14+
final dynamic #0#0 = x;
15+
if(#0#0 is{ForNonNullableByDefault} self::A && #C1 =={core::num::==}{(core::Object) → core::bool} #0#0{self::A}.{self::A::_foo}{core::int}) {
16+
{
17+
return x{self::A};
18+
}
19+
}
20+
else {
21+
{
22+
return null;
23+
}
24+
}
25+
}
26+
}
27+
28+
constants {
29+
#C1 = 42
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A {
2+
int _foo = 0;
3+
}
4+
5+
test(dynamic x) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class A {
2+
int _foo = 0;
3+
}
4+
5+
test(dynamic x) {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class A extends core::Object {
6+
field core::int _foo = 0;
7+
synthetic constructor •() → self::A
8+
: super core::Object::•()
9+
;
10+
}
11+
static method test(dynamic x) → dynamic {
12+
#L1:
13+
{
14+
final dynamic #0#0 = x;
15+
if(#0#0 is{ForNonNullableByDefault} self::A && #C1 =={core::num::==}{(core::Object) → core::bool} #0#0{self::A}.{self::A::_foo}{core::int}) {
16+
{
17+
return x{self::A};
18+
}
19+
}
20+
else {
21+
{
22+
return null;
23+
}
24+
}
25+
}
26+
}
27+
28+
constants {
29+
#C1 = 42
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class A extends core::Object {
6+
field core::int _foo = 0;
7+
synthetic constructor •() → self::A
8+
: super core::Object::•()
9+
;
10+
}
11+
static method test(dynamic x) → dynamic {
12+
#L1:
13+
{
14+
final dynamic #0#0 = x;
15+
if(#0#0 is{ForNonNullableByDefault} self::A && #C1 =={core::num::==}{(core::Object) → core::bool} #0#0{self::A}.{self::A::_foo}{core::int}) {
16+
{
17+
return x{self::A};
18+
}
19+
}
20+
else {
21+
{
22+
return null;
23+
}
24+
}
25+
}
26+
}
27+
28+
constants {
29+
#C1 = 42
30+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class A extends core::Object {
6+
field core::int _foo;
7+
synthetic constructor •() → self::A
8+
;
9+
}
10+
static method test(dynamic x) → dynamic
11+
;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
library /*isNonNullableByDefault*/;
2+
import self as self;
3+
import "dart:core" as core;
4+
5+
class A extends core::Object {
6+
field core::int _foo = 0;
7+
synthetic constructor •() → self::A
8+
: super core::Object::•()
9+
;
10+
}
11+
static method test(dynamic x) → dynamic {
12+
#L1:
13+
{
14+
final dynamic #0#0 = x;
15+
if(#0#0 is{ForNonNullableByDefault} self::A && #C1 =={core::num::==}{(core::Object) → core::bool} #0#0{self::A}.{self::A::_foo}{core::int}) {
16+
{
17+
return x{self::A};
18+
}
19+
}
20+
else {
21+
{
22+
return null;
23+
}
24+
}
25+
}
26+
}
27+
28+
constants {
29+
#C1 = 42
30+
}

0 commit comments

Comments
 (0)