Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-13767] Index store reference occurrence incorrectly related for lazy variable #56164

Closed
swift-ci opened this issue Oct 22, 2020 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13767
Radar rdar://problem/70579806
Original Reporter Leitch (JIRA User)
Type Bug
Status Closed
Resolution Duplicate
Environment

Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
Target: x86_64-apple-darwin19.6.0

Additional Detail from JIRA
Votes 1
Component/s Source Tooling
Labels Bug
Assignee None
Priority Medium

md5: a8a7140ca0e0c279aee02176eeb9c4a6

Issue Description:

Given the following code:

class MyClass {
    private var someVar = "test"

    lazy var someLazyVar: String = {
        return someVar
    }()
}

The index store incorrectly relates the `someVar` reference within the lazy var body with `MyClass` as opposed to `someLazyVar`.

The dependency graph is as follows:

Declaration(class, 'MyClass', explicit, internal, [], 's:17RetentionFixtures7MyClassC', testDoesNotRetainLazyProperty.swift:3:7)
··Reference(var.instance, 'someVar', 's:17RetentionFixtures7MyClassC7someVar33_229AED938E4072B048955CC3F10581F2LLSSvp', nil, testDoesNotRetainLazyProperty.swift:7:16)
····Reference(function.accessor.getter, nil, 's:17RetentionFixtures7MyClassC7someVar33_229AED938E4072B048955CC3F10581F2LLSSvg', 's:17RetentionFixtures7MyClassC', testDoesNotRetainLazyProperty.swift:7:16)
··Reference(struct, 'String', 's:SS', nil, testDoesNotRetainLazyProperty.swift:6:27)
··Declaration(var.instance, 'someLazyVar', explicit, internal, [lazy], 's:17RetentionFixtures7MyClassC11someLazyVarSSvp', testDoesNotRetainLazyProperty.swift:6:14)
····Declaration(function.accessor.getter, nil, implicit, internal, [lazy], 's:17RetentionFixtures7MyClassC11someLazyVarSSvg', testDoesNotRetainLazyProperty.swift:6:14)
····Declaration(function.accessor.setter, nil, implicit, internal, [lazy], 's:17RetentionFixtures7MyClassC11someLazyVarSSvs', testDoesNotRetainLazyProperty.swift:6:14)
··Declaration(var.instance, 'someVar', explicit, private, [private], 's:17RetentionFixtures7MyClassC7someVar33_229AED938E4072B048955CC3F10581F2LLSSvp', testDoesNotRetainLazyProperty.swift:4:17)
····Declaration(function.accessor.getter, nil, implicit, internal, [private], 's:17RetentionFixtures7MyClassC7someVar33_229AED938E4072B048955CC3F10581F2LLSSvg', testDoesNotRetainLazyProperty.swift:4:17)
····Declaration(function.accessor.setter, nil, implicit, internal, [private], 's:17RetentionFixtures7MyClassC7someVar33_229AED938E4072B048955CC3F10581F2LLSSvs', testDoesNotRetainLazyProperty.swift:4:17)
··Declaration(function.constructor, nil, implicit, internal, [], 's:17RetentionFixtures7MyClassCACycfc', testDoesNotRetainLazyProperty.swift:3:7)

Note the `someVar` reference is a direct child of `MyClass` rather than `someLazyVar`.

This issue appears to be specific to lazy variables, when `someLazyVar` is changed to be non-lazy, the `someVar` reference is correctly related.

@swift-ci
Copy link
Collaborator Author

Comment by Ian Leitch (JIRA)

/cc @benlangmuir

@benlangmuir
Copy link
Member

> Note the `someVar` reference is a direct child of `MyClass` rather than `someLazyVar`

The reference inside the lazy property initializer has no relations at all. It's the declaration of someVar that has a child relation with MyClass

2:17 | instance-property/Swift | someVar | <usr> | Def,RelChild | rel: 1
  RelChild | class/Swift | MyClass | s:14swift_ide_test7MyClassC
...
5:16 | instance-property/Swift | someVar | <usr> | Ref,Read | rel: 0

For a reference in a computed variable getter, we get

9:16 | instance-property/Swift | someVar | <usr> | Ref,Read,RelCont | rel: 1
  RelCont | instance-method/acc-get/Swift | getter:someComputedVar | <usr>

It seems reasonable that we would have a similar RelCont relation for the lazy variable initializer. That's consistent with what clang indexing does for references in initial values. We would need to decide whether it is contained by the property itself or the getter accessor of the property.

@benlangmuir
Copy link
Member

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Ian Leitch (JIRA)

Sorry for my confusing use terminology, I'm still rather new to the index store.

For my use case I don't think it matters much if it's contained by the property or the getter. Though perhaps the getter is better for consistency sake since that's the behavior of non-lazy computed values.

@swift-ci
Copy link
Collaborator Author

Comment by Ian Leitch (JIRA)

This is actually the same bug as SR-13766.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

2 participants