Skip to content

Commit 8970b71

Browse files
scheglovCommit Queue
authored andcommitted
Fine. Formal parameters are not in scope of the signature of a function expression.
Specification: 9.2 Formal Parameters The formal parameter list of a function declaration introduces a new scope known as the function’s formal parameter scope. The formal parameter scope △ of a non-generic function f is enclosed in the scope where f is declared. The formal parameter scope of a generic function f is enclosed in the type parameter scope of f. Every formal parameter introduces a local variable into the formal parameter scope. The current scope for the function’s signature is the scope that encloses the formal parameter scope. This means that in a generic function declaration, the return type and parameter type annotations can use the formal type parameters, but the formal parameters are not in scope in the signature. The body of a function declaration introduces a new scope known as the function’s body scope. The body scope of a function f is enclosed in the scope △ introduced by the formal parameter scope of f. Change-Id: Icc795c11f489fadd6ed25b1a5904c5dfd1601d09 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/457885 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent b0de09e commit 8970b71

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,11 +5178,12 @@ class ScopeResolverVisitor extends UnifyingAstVisitor<void> {
51785178
return;
51795179
}
51805180

5181-
nameScope = FormalParameterScope(
5182-
TypeParameterScope(nameScope, element.typeParameters),
5183-
element.formalParameters,
5184-
);
5185-
super.visitFunctionExpression(node);
5181+
nameScope = TypeParameterScope(nameScope, element.typeParameters);
5182+
node.typeParameters?.accept(this);
5183+
node.parameters?.accept(this);
5184+
5185+
nameScope = FormalParameterScope(nameScope, element.formalParameters);
5186+
node.body.accept(this);
51865187
} finally {
51875188
nameScope = outerScope;
51885189
_enclosingClosure = outerClosure;

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75167,6 +75167,47 @@ const a = -'' + 2;
7516775167
);
7516875168
}
7516975169

75170+
test_manifest_constInitializer_functionExpression_formalParameter_defaultValue_formalParameter() async {
75171+
await _runLibraryManifestScenario(
75172+
initialCode: r'''
75173+
final f = ({int x = x}) {};
75174+
''',
75175+
expectedInitialEvents: r'''
75176+
[operation] linkLibraryCycle SDK
75177+
[operation] linkLibraryCycle
75178+
package:test/test.dart
75179+
hashForRequirements: #H0
75180+
declaredGetters
75181+
f: #M0
75182+
declaredVariables
75183+
f: #M1
75184+
exportMapId: #M2
75185+
exportMap
75186+
f: #M0
75187+
''',
75188+
updatedCode: r'''
75189+
final f = ({int x = x}) {};
75190+
class A {}
75191+
''',
75192+
expectedUpdatedEvents: r'''
75193+
[operation] linkLibraryCycle
75194+
package:test/test.dart
75195+
hashForRequirements: #H1
75196+
declaredClasses
75197+
A: #M3
75198+
interface: #M4
75199+
declaredGetters
75200+
f: #M0
75201+
declaredVariables
75202+
f: #M1
75203+
exportMapId: #M5
75204+
exportMap
75205+
A: #M3
75206+
f: #M0
75207+
''',
75208+
);
75209+
}
75210+
7517075211
test_manifest_constInitializer_identifier_addIdentifier() async {
7517175212
configuration.withElementManifests = true;
7517275213
await _runLibraryManifestScenario(

pkg/analyzer/test/src/dart/resolution/function_expression_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/src/error/codes.dart';
56
import 'package:test_reflective_loader/test_reflective_loader.dart';
67

78
import 'context_collection_resolution.dart';
@@ -93,6 +94,42 @@ FunctionExpression
9394
element: null@null
9495
type: Null Function<T extends num>()
9596
staticType: Null Function<T extends num>()
97+
''');
98+
}
99+
100+
test_signatureScope_noFormalParameters() async {
101+
await assertErrorsInCode(
102+
'''
103+
var f = ({int x = x}) {};
104+
''',
105+
[error(CompileTimeErrorCode.undefinedIdentifier, 18, 1)],
106+
);
107+
108+
var node = findNode.singleFormalParameterList;
109+
assertResolvedNodeText(node, r'''
110+
FormalParameterList
111+
leftParenthesis: (
112+
leftDelimiter: {
113+
parameter: DefaultFormalParameter
114+
parameter: SimpleFormalParameter
115+
type: NamedType
116+
name: int
117+
element: dart:core::@class::int
118+
type: int
119+
name: x
120+
declaredElement: <testLibraryFragment> x@14
121+
element: isPublic
122+
type: int
123+
separator: =
124+
defaultValue: SimpleIdentifier
125+
token: x
126+
element: <null>
127+
staticType: InvalidType
128+
declaredElement: <testLibraryFragment> x@14
129+
element: isPublic
130+
type: int
131+
rightDelimiter: }
132+
rightParenthesis: )
96133
''');
97134
}
98135
}

0 commit comments

Comments
 (0)