Skip to content

Fix indexing constructors with generic parameters #65597

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

Conversation

keith
Copy link
Member

@keith keith commented May 2, 2023

Previously in the case of a constructor like A<Int>(value: 1) Fn->getLoc() returned the location of >(value: 1) while the actual location we're looking for is correctly the start of A<Int>(value: 1). This adjusts the location we're looking up to use the start location of the constructor instead.

Fixes: #54532

@keith keith requested review from bnbarham, ahoppen and rintaro as code owners May 2, 2023 21:54
@keith
Copy link
Member Author

keith commented May 2, 2023

@swift-ci please test

@bnbarham bnbarham requested a review from hamishknight May 2, 2023 21:55
Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@keith
Copy link
Member Author

keith commented May 3, 2023

the failure here is actually real, the problem is with nested types it moves the initializer to the parent type, which is wrong 😕

@keith keith force-pushed the ks/fix-indexing-constructors-with-generic-parameters branch from 89cc2ff to 00cf28a Compare May 3, 2023 17:53
@@ -129,7 +129,7 @@ class GenCls<T> {
}

func test2() {
// CHECK: <Class@[[@LINE-19]]:7>GenCls</Class><<iStruct@>Int</iStruct>>()
// CHECK: <Ctor@[[@LINE-17]]:3-Class@[[@LINE-19]]:7>GenCls</Ctor><<iStruct@>Int</iStruct>>()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was wrong before for the same reason

@keith
Copy link
Member Author

keith commented May 3, 2023

fixed that failure with a new way to compute the correct location that seems to work in all cases

@keith
Copy link
Member Author

keith commented May 3, 2023

@swift-ci please test

@keith
Copy link
Member Author

keith commented May 3, 2023

this breaks the Foo.init case by putting a reference to the constructor on both Foo and .init. New test incoming

@hamishknight hamishknight self-requested a review May 4, 2023 11:36
@keith keith force-pushed the ks/fix-indexing-constructors-with-generic-parameters branch from 00cf28a to 6ce9562 Compare May 4, 2023 17:30
@keith
Copy link
Member Author

keith commented May 4, 2023

pushed a version w/ that new case fixed, and ((A))(value: 1) 🙃

@keith
Copy link
Member Author

keith commented May 4, 2023

@swift-ci please test

@keith keith force-pushed the ks/fix-indexing-constructors-with-generic-parameters branch from 6ce9562 to c807ab8 Compare May 4, 2023 17:35
@keith
Copy link
Member Author

keith commented May 4, 2023

@swift-ci please test

@keith
Copy link
Member Author

keith commented May 4, 2023

I hope re-pushing cancels previously running tests 😬

@hamishknight
Copy link
Contributor

I hope re-pushing cancels previously running tests

It does (or more accurately, triggering the tests again cancels any previous runs) :)

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Keith!

@keith keith force-pushed the ks/fix-indexing-constructors-with-generic-parameters branch from c807ab8 to 9d2de8e Compare May 5, 2023 00:42
@keith
Copy link
Member Author

keith commented May 5, 2023

@swift-ci please test

Previously in the case of a constructor like `A<Int>(value: 1)`
`Fn->getLoc()` returned the location of `>(value: 1)` while the actual
location we're looking for is correctly the start of `A<Int>(value: 1)`.
This adjusts the location we're looking up to use the start location of
the constructor instead.

Fixes: swiftlang#54532
@keith keith force-pushed the ks/fix-indexing-constructors-with-generic-parameters branch from 9d2de8e to 9c42f7a Compare May 5, 2023 15:39
@keith
Copy link
Member Author

keith commented May 5, 2023

@swift-ci please test

Comment on lines +822 to +823
if (auto *TE = dyn_cast<TypeExpr>(Ctor->getBase()))
CtorLoc = TE->getTypeRepr()->getWithoutParens()->getLoc();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may be some other TypeReprs that this doesn't quite handle correctly e.g X?(y), I believe the location in that case will be on the ?. That being said, I'm still happy to take this given it's a strict improvement over what we have currently. I've started looking into refactoring this so we can walk the references separately, which should avoid the need to do this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm what's the scenario more specifically? this code:

protocol P {
    init(value: Int)
}

func foo<T: P>(A: T.Type?) {
    _ = A?(value: 0)
}

Forces you to do A?.init so it ends up working fine

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean like this:

struct S {}

func foo(_ x: S) {
  _ = S?(x)
}

Optional has an init that takes the wrapped value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yep, that case doesn't work you're right, i'll file that example

Copy link
Member Author

@keith keith May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@keith keith merged commit f5fbee2 into swiftlang:main May 5, 2023
@keith keith deleted the ks/fix-indexing-constructors-with-generic-parameters branch May 5, 2023 22:20
keith added a commit to keith/swift that referenced this pull request May 5, 2023
)

Previously in the case of a constructor like `A<Int>(value: 1)`
`Fn->getLoc()` returned the location of `>(value: 1)` while the actual
location we're looking for is correctly the start of `A<Int>(value: 1)`.
This adjusts the location we're looking up to use the start location of
the constructor instead.

Fixes: swiftlang#54532

(cherry picked from commit f5fbee2 / swiftlang#65597)
keith added a commit that referenced this pull request May 30, 2023
)

Previously in the case of a constructor like `A<Int>(value: 1)`
`Fn->getLoc()` returned the location of `>(value: 1)` while the actual
location we're looking for is correctly the start of `A<Int>(value: 1)`.
This adjusts the location we're looking up to use the start location of
the constructor instead.

Fixes: #54532

(cherry picked from commit f5fbee2 / #65597)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SR-12096] Index missing references to initializers in Result extensions
3 participants