Skip to content

Commit

Permalink
fix: prevent references to following parameters from default values (#…
Browse files Browse the repository at this point in the history
…707)

Closes partially #540

### Summary of Changes

References to parameters that are defined later in the parameter list
are no longer resolved from default values. Example:

```txt
fun f(p: Int = q, q: Int = p)
```

The reference to `q` does not get resolved anymore. The reference to `p`
is still possible.
  • Loading branch information
lars-reimann authored Oct 30, 2023
1 parent 4518aee commit 182d64b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
isSdsModule,
isSdsNamedType,
isSdsNamedTypeDeclaration,
isSdsParameter,
isSdsPlaceholder,
isSdsQualifiedImport,
isSdsReference,
Expand All @@ -35,12 +36,14 @@ import {
isSdsWildcardImport,
isSdsYield,
SdsArgument,
type SdsCallable,
SdsDeclaration,
SdsExpression,
SdsImportedDeclaration,
SdsMemberAccess,
SdsMemberType,
SdsNamedTypeDeclaration,
type SdsParameter,
SdsPlaceholder,
SdsReference,
SdsStatement,
Expand Down Expand Up @@ -285,9 +288,17 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
}

private localDeclarations(node: AstNode, outerScope: Scope): Scope {
// Parameters
const containingCallable = getContainerOfType(node.$container, isSdsCallable);
const parameters = getParameters(containingCallable);

// Parameters (up to the containing parameter)
const containingParameter = getContainerOfType(node, isSdsParameter);

let parameters: Iterable<SdsParameter>;
if (containingCallable && containingParameter) {
parameters = this.parametersUpToParameter(containingCallable, containingParameter);
} else {
parameters = getParameters(containingCallable);
}

// Placeholders up to the containing statement
const containingStatement = getContainerOfType(node.$container, isSdsStatement);
Expand All @@ -311,6 +322,19 @@ export class SafeDsScopeProvider extends DefaultScopeProvider {
}
}

private *parametersUpToParameter(
callable: SdsCallable,
parameter: SdsParameter,
): Generator<SdsParameter, void, undefined> {
for (const current of getParameters(callable)) {
if (current === parameter) {
return;
}

yield current;
}
}

private *placeholdersUpToStatement(
statement: SdsStatement | undefined,
): Generator<SdsPlaceholder, void, undefined> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tests.scoping.references.inSameFile.toParameters.fromDefaultValueOfParameter

annotation MyAnnotation(
// $TEST$ target annotation_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references annotation_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)

class MyClass(
// $TEST$ target class_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references class_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)

enum MyEnum {
MyEnumVariant(
// $TEST$ target enum_variant_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references enum_variant_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)
}

fun myFunction(
// $TEST$ target function_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references function_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
)

segment mySegment1(
// $TEST$ target segment_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references segment_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) {}

segment mySegment2(
p: (
// $TEST$ target callable_type_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references callable_type_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) -> ()
) {
(
// $TEST$ target block_lambda_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references block_lambda_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) {};
(
// $TEST$ target block_function_param
// $TEST$ unresolved
»p«: String = »q«,
// $TEST$ references block_function_param
q: String = »p«,
// $TEST$ unresolved
r: String = »r«,
) -> 1;
}

0 comments on commit 182d64b

Please sign in to comment.