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

Index expressible-by-literal expressions. #60432

Merged
merged 1 commit into from
Aug 30, 2022

Conversation

allevato
Copy link
Member

@allevato allevato commented Aug 6, 2022

When a value is initialized or coerced for a type that conforms to one of the ExpressibleBy*Literal protocols (or ExpressibleByStringInterpolation), this change records an implicit call to the corresponding init(...Literal:) in the indexstore, located at the beginning of the literal.

@allevato allevato requested a review from bnbarham August 6, 2022 07:50
@allevato
Copy link
Member Author

allevato commented Aug 6, 2022

@swift-ci please test

@@ -1691,6 +1708,25 @@ bool IndexSwiftASTWalker::initFuncDeclIndexSymbol(FuncDecl *D,
return false;
}

bool IndexSwiftASTWalker::initLiteralInitRefIndexSymbol(ValueDecl *D,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is almost initFuncRefIndexSymbol - is ide::isBeingCalled false and thus causing it to return early?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, isBeingCalled is only checking for (Self)ApplyExpr. Should I update it to support LiteralExprs with non-null initializers?

Copy link
Member Author

Choose a reason for hiding this comment

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

And if I do that, should I update Expr->getReferencedDecl to return the initializer for subclasses of LiteralExpr? Right now they're all using the NO_REFERENCE macro to return null unconditionally.

Copy link
Contributor

Choose a reason for hiding this comment

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

That seems reasonable to me. That combined with SemaAnnotator walking this should mean that IndexSwiftASTWalker will work as is, I believe.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I think I've got this rewritten the way you described now; PTAL!

@@ -698,12 +698,27 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
}
}

void handleLiteralInitRefs(Expr *E) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this isn't being walked by SemaAnnotator because it's implicit? Feels odd to me that visitDeclReference takes a ReferenceMetaData which has IsImplicit and some expressions we walk regardless, but not others. Though that bit looks to have been added a bit later on. Perhaps we should walk LiteralExpr despite being implicit as well?

Any thoughts @benlangmuir?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seem reasonable to me, I don't recall if there's any reason for the current behaviour other than historical.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fair enough, I'd personally prefer doing this in SemaAnnotator then - IMO IndexSwiftASTWalker should be much simpler than it is right now, with all the actual walking done in SemaAnnotator instead.

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 function is now removed; the changes above made this work without special handling here.

@rintaro
Copy link
Member

rintaro commented Aug 9, 2022

Could you add test cases for SE-0213 Literal initialization via coercion? i.e. CustomInteger(42) implicitly calls CustomInteger.init(integerLiteral:)

@rintaro
Copy link
Member

rintaro commented Aug 9, 2022

Also does it make sense to do similar to ExpressibleByArrayLiteral/ExpressibleByDictionaryLiteral too?

@bnbarham
Copy link
Contributor

Also does it make sense to do similar to ExpressibleByArrayLiteral/ExpressibleByDictionaryLiteral too?

There's actually an open bug for this as well - #56165. So @ileitch may be interested in this PR.

@allevato allevato force-pushed the index-expressible-by-literal branch 2 times, most recently from df687ca to 6e51655 Compare August 28, 2022 03:05
When a value is initialized or coerced for a type that conforms to
one of the `ExpressibleBy*Literal` protocols (or
`ExpressibleByStringInterpolation`), this change records an implicit
call to the corresponding `init(...Literal:)` in the indexstore,
located at the beginning of the literal.
@allevato allevato force-pushed the index-expressible-by-literal branch from 6e51655 to 31d1b3a Compare August 28, 2022 03:06
@allevato
Copy link
Member Author

Also does it make sense to do similar to ExpressibleByArrayLiteral/ExpressibleByDictionaryLiteral too?

There's actually an open bug for this as well - #56165. So @ileitch may be interested in this PR.

I've added these two conformances as well now—thanks for mentioning them!

@allevato
Copy link
Member Author

@swift-ci please test

@allevato
Copy link
Member Author

Could you add test cases for SE-0213 Literal initialization via coercion? i.e. CustomInteger(42) implicitly calls CustomInteger.init(integerLiteral:)

Forgot to mention, I also added tests for this. An expression like CustomInteger(42) indexes as an implicit call to CustomInteger(integerLiteral:) at the location of the 42 token due to the way the AST transforms the expression. That might seem a little odd since it looks like an explicit call, but since it's an accurate representation of what's happening here at the language level, I don't know if it's worth treating differently.

@bnbarham
Copy link
Contributor

Forgot to mention, I also added tests for this. An expression like CustomInteger(42) indexes as an implicit call to CustomInteger(integerLiteral:) at the location of the 42 token due to the way the AST transforms the expression. That might seem a little odd since it looks like an explicit call, but since it's an accurate representation of what's happening here at the language level, I don't know if it's worth treating differently.

Seems reasonable for now, we can always change later if necessary.

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 Tony, LGTM!

Comment on lines +323 to +328
SIMPLE_REFERENCE(NilLiteral, getInitializer);
SIMPLE_REFERENCE(IntegerLiteral, getInitializer);
SIMPLE_REFERENCE(FloatLiteral, getInitializer);
SIMPLE_REFERENCE(BooleanLiteral, getInitializer);
SIMPLE_REFERENCE(StringLiteral, getInitializer);
SIMPLE_REFERENCE(InterpolatedStringLiteral, getInitializer);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this change cursor info at all? Could add some tests for that (though I'm not sure anyone would think to jump to definition on a literal 😅). It would also be a little weird for eg. the collection cases since I (think) only the first character (eg. [) would actually return a result.

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like it doesn't; a quick check adding one of these to SourceKit/CursorInfo/cursor_info_expressions came back with <empty cursor info; internal diagnostic: "Resolved to incomplete expression or statement."> at the literal's location.

@allevato allevato merged commit 9c84149 into swiftlang:main Aug 30, 2022
@allevato allevato deleted the index-expressible-by-literal branch August 30, 2022 14:57
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.

4 participants