Skip to content

Commit 9f23661

Browse files
jensjohaCommit Queue
authored andcommitted
[CFE] Know about '#this' if finding scopes *on* an extension type
member. See #56911 (comment) for context. Bug: #56911 Change-Id: If990f41c58268b592d575e1280831b1ed120d54d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392341 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Jens Johansen <[email protected]>
1 parent 97871ae commit 9f23661

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

pkg/front_end/lib/src/base/incremental_compiler.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ import '../api_prototype/incremental_kernel_generator.dart'
6666
IncrementalCompilerResult,
6767
IncrementalKernelGenerator,
6868
isLegalIdentifier;
69-
import '../api_prototype/lowering_predicates.dart' show isExtensionThisName;
69+
import '../api_prototype/lowering_predicates.dart'
70+
show isExtensionThisName, syntheticThisName;
7071
import '../api_prototype/memory_file_system.dart' show MemoryFileSystem;
7172
import '../base/nnbd_mode.dart';
7273
import '../base/processed_options.dart' show ProcessedOptions;
@@ -1917,6 +1918,17 @@ class IncrementalCompiler implements IncrementalKernelGenerator {
19171918
builder.lookupLocalMember(afterDot, setter: false);
19181919
if (subBuilder is MemberBuilder) {
19191920
if (subBuilder.isExtensionTypeInstanceMember) {
1921+
List<VariableDeclaration>? positionals =
1922+
subBuilder.invokeTarget?.function?.positionalParameters;
1923+
if (positionals != null &&
1924+
positionals.isNotEmpty &&
1925+
isExtensionThisName(positionals.first.name) &&
1926+
usedDefinitions.containsKey(syntheticThisName)) {
1927+
// If we setup the extensionType (and later the
1928+
// `extensionThis`) we should also set the type correctly
1929+
// (at least in a non-static setting).
1930+
usedDefinitions[syntheticThisName] = positionals.first.type;
1931+
}
19201932
isStatic = false;
19211933
}
19221934
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) 2024, 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+
# https://github.com/dart-lang/sdk/issues/56911
6+
7+
# Definition, offset, method etc extracted by starting the VM with
8+
# `-DDFE_VERBOSE=false`, e.g.
9+
# ```
10+
# out/ReleaseX64/dart -DDFE_VERBOSE=false --enable-vm-service \
11+
# --serve-observatory --disable-service-auth-codes --pause_isolates_on_start \
12+
# inputFile.dart
13+
# ```
14+
# and then issuing the expression compilation. It will then print stuff like
15+
# `DFE: request[6]: [dart:core, _OneByteString, 1, 0]` in the terminal.
16+
17+
sources: |
18+
void main() {
19+
List<ExtensionType> list = [new ExtensionType(0)];
20+
list.forEach((ExtensionType input) {
21+
print(input.value);
22+
});
23+
}
24+
25+
extension type ExtensionType._(String s) {
26+
ExtensionType(int i) : this._("$i");
27+
int get value => s.codeUnitAt(0);
28+
}
29+
30+
definitions: ["input"]
31+
# String
32+
definition_types: ["dart:core", "_OneByteString", "1", "0"]
33+
type_definitions: []
34+
type_bounds: []
35+
type_defaults: []
36+
method: "<anonymous closure>"
37+
static: true
38+
offset: 122 # at the 'value' of 'print(input.value)' line.
39+
scriptUri: main.dart
40+
expression: |
41+
input.value
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Errors: {
2+
}
3+
static method /* from org-dartlang-debug:synthetic_debug_expression */ debugExpr(#lib1::ExtensionType% /* erasure=dart.core::String, declared=! */ input, dart.core::List<#lib1::ExtensionType% /* erasure=dart.core::String, declared=! */> list) → dynamic
4+
return #lib1::ExtensionType|get#value(input);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) 2024, 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+
# https://github.com/dart-lang/sdk/issues/56911
6+
7+
sources: |
8+
void main() {
9+
List<ExtensionType> list = [new ExtensionType(0)];
10+
list.forEach((ExtensionType input) {
11+
print(input.value);
12+
});
13+
}
14+
15+
extension type ExtensionType._(String s) {
16+
ExtensionType(int i) : this._("$i");
17+
int get value => s.codeUnitAt(0);
18+
}
19+
20+
definitions: ["#this"]
21+
# String
22+
definition_types: ["dart:core", "_OneByteString", "1", "0"]
23+
type_definitions: []
24+
type_bounds: []
25+
type_defaults: []
26+
method: "ExtensionType.value"
27+
static: true
28+
offset: 231 # at the 'value' of 'int get value => s.codeUnitAt(0);' line.
29+
scriptUri: main.dart
30+
expression: |
31+
s
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Errors: {
2+
}
3+
method /* from org-dartlang-debug:synthetic_debug_expression */ debugExpr(lowered #lib1::ExtensionType% /* erasure=dart.core::String, declared=! */ #this) → dynamic
4+
return #this as{Unchecked} dart.core::String;

0 commit comments

Comments
 (0)