Fix indexing constructors with generic parameters (#65597)

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: https://github.com/apple/swift/issues/54532
This commit is contained in:
Keith Smiley
2023-05-05 15:20:07 -07:00
committed by GitHub
parent 1566eccbad
commit f5fbee2f26
3 changed files with 96 additions and 4 deletions

View File

@@ -827,9 +827,22 @@ passReference(ValueDecl *D, Type Ty, SourceLoc BaseNameLoc, SourceRange Range,
if (auto *TD = dyn_cast<TypeDecl>(D)) {
if (!CtorRefs.empty() && BaseNameLoc.isValid()) {
Expr *Fn = CtorRefs.back()->getFn();
if (Fn->getLoc() == BaseNameLoc) {
D = ide::getReferencedDecl(Fn).second.getDecl();
ConstructorRefCallExpr *Ctor = CtorRefs.back();
SourceLoc CtorLoc = Ctor->getFn()->getLoc();
// Get the location of the type, ignoring parens, rather than the start of
// the Expr, to match the lookup.
if (auto *TE = dyn_cast<TypeExpr>(Ctor->getBase()))
CtorLoc = TE->getTypeRepr()->getWithoutParens()->getLoc();
bool isImplicit = false;
Expr *Fn = Ctor->getFn();
while (auto *ICE = dyn_cast<ImplicitConversionExpr>(Fn))
Fn = ICE->getSubExpr();
if (auto *DRE = dyn_cast<DeclRefExpr>(Fn))
isImplicit = DRE->isImplicit();
if (isImplicit && CtorLoc == BaseNameLoc) {
D = ide::getReferencedDecl(Ctor->getFn()).second.getDecl();
if (D == nullptr) {
assert(false && "Unhandled constructor reference");
return true;