Skip to content

Commit e047dd6

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Ensure that function elements inside local variable elements have unique locations.
Fixes #44216. Bug: #44216 Change-Id: I5ccf8bb5f90396eabfec470a289a193d2aef16ec Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/173881 Reviewed-by: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 7de6710 commit e047dd6

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4392,7 +4392,7 @@ class FunctionElementImpl extends ExecutableElementImpl
43924392
String get identifier {
43934393
String identifier = super.identifier;
43944394
Element enclosing = enclosingElement;
4395-
if (enclosing is ExecutableElement) {
4395+
if (enclosing is ExecutableElement || enclosing is VariableElement) {
43964396
identifier += "@$nameOffset";
43974397
}
43984398
return identifier;

pkg/analyzer/test/src/dart/element/element_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ main() {
3636
defineReflectiveTests(ElementImplTest);
3737
defineReflectiveTests(LibraryElementImplTest);
3838
defineReflectiveTests(TopLevelVariableElementImplTest);
39+
defineReflectiveTests(UniqueLocationTest);
3940
});
4041
}
4142

@@ -2288,6 +2289,50 @@ class TypeParameterTypeImplTest extends AbstractTypeTest {
22882289
}
22892290
}
22902291

2292+
@reflectiveTest
2293+
class UniqueLocationTest extends PubPackageResolutionTest {
2294+
test_ambiguous_closure_in_executable() async {
2295+
await resolveTestCode('''
2296+
void f() => [() => 0, () => 1];
2297+
''');
2298+
expect(findNode.functionExpression('() => 0').declaredElement.location,
2299+
isNot(findNode.functionExpression('() => 1').declaredElement.location));
2300+
}
2301+
2302+
test_ambiguous_closure_in_local_variable() async {
2303+
await resolveTestCode('''
2304+
void f() {
2305+
var x = [() => 0, () => 1];
2306+
}
2307+
''');
2308+
expect(findNode.functionExpression('() => 0').declaredElement.location,
2309+
isNot(findNode.functionExpression('() => 1').declaredElement.location));
2310+
}
2311+
2312+
test_ambiguous_closure_in_top_level_variable() async {
2313+
await resolveTestCode('''
2314+
var x = [() => 0, () => 1];
2315+
''');
2316+
expect(findNode.functionExpression('() => 0').declaredElement.location,
2317+
isNot(findNode.functionExpression('() => 1').declaredElement.location));
2318+
}
2319+
2320+
test_ambiguous_local_variable_in_executable() async {
2321+
await resolveTestCode('''
2322+
f() {
2323+
{
2324+
int x = 0;
2325+
}
2326+
{
2327+
int x = 1;
2328+
}
2329+
}
2330+
''');
2331+
expect(findNode.variableDeclaration('x = 0').declaredElement.location,
2332+
isNot(findNode.variableDeclaration('x = 1').declaredElement.location));
2333+
}
2334+
}
2335+
22912336
@reflectiveTest
22922337
class VoidTypeImplTest extends AbstractTypeTest {
22932338
/// Reference {code VoidTypeImpl.getInstance()}.

pkg/nnbd_migration/test/api_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,28 @@ void main() {
247247
await _checkSingleFileChanges(content, expected);
248248
}
249249

250+
Future<void> test_ambiguous_closure_parameter_in_local_variable() async {
251+
var content = '''
252+
Object f<T>(Object Function(T) callback, Object obj) => 0;
253+
g() {
254+
var y = f<Map<String, int>>(
255+
(x) => x.keys,
256+
f<List<bool>>(
257+
(x) => x.last, 0));
258+
}
259+
''';
260+
var expected = '''
261+
Object f<T>(Object Function(T) callback, Object obj) => 0;
262+
g() {
263+
var y = f<Map<String, int>>(
264+
(x) => x.keys,
265+
f<List<bool>>(
266+
(x) => x.last, 0));
267+
}
268+
''';
269+
await _checkSingleFileChanges(content, expected);
270+
}
271+
250272
Future<void> test_argumentError_checkNotNull_implies_non_null_intent() async {
251273
var content = '''
252274
void f(int i) {

0 commit comments

Comments
 (0)