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

Prevent unrelated casts using as #2092

Open
ahoppen opened this issue Aug 23, 2023 · 11 comments
Open

Prevent unrelated casts using as #2092

ahoppen opened this issue Aug 23, 2023 · 11 comments
Assignees
Labels
enhancement New feature or request

Comments

@ahoppen
Copy link
Member

ahoppen commented Aug 23, 2023

Currently, SyntaxProtocol has an as method that allows casting to any other syntax node type. Because of this, it’s possible to e.g. cast a FunctionDeclSyntax to an IdentifierExprSyntax without any compiler errors or warnings, even though we know that the cast will always fail.

What we should do instead, is to only have a as(_: ExprSyntaxProtocol.Type) function on ExprSyntax (analogous for StmtSyntax and the other base nodes).

The as function on SyntaxProtocol can then be marked as deprecated and produce a warning that the cast will always fail.

is and cast will need to be updated accordingly.

@ahoppen ahoppen added the enhancement New feature or request label Aug 23, 2023
@ahoppen
Copy link
Member Author

ahoppen commented Aug 23, 2023

Tracked in Apple’s issue tracker as rdar://114330180

@Matejkob
Copy link
Contributor

What does the term "as well as as(_: SyntaxProtocol.Type)" mean? Does it suggest that every base node should have this method? Would this be equivalent to having the as method directly on SyntaxProtocol?

@ahoppen
Copy link
Member Author

ahoppen commented Aug 23, 2023

Oh, that was an artifact of a sentence I started writing but then didn’t finish. Ignore it – and I removed it from the description

@grynspan
Copy link
Contributor

If I have a Syntax, how do I then cast it to something else?

@Matejkob
Copy link
Contributor

If I have a Syntax, how do I then cast it to something else?

I believe we should keep this part of the casting API. If you find a while you could review my pull request #2108, which contains a draft proposal on casting constraints?

@ahoppen
Copy link
Member Author

ahoppen commented Aug 28, 2023

If I have a Syntax, how do I then cast it to something else?

The proposal here is to remove the as methods on all types that are not base nodes, e.g. FunctionDeclSyntax.as. Syntax.as will continue to exist.

@grynspan
Copy link
Contributor

Okay, so a Syntax can still be cast, but an arbitrary some SyntaxProtocol can't be? And if I find myself needing to cast in that case, I can do Syntax(foo).as(...)?

@ahoppen
Copy link
Member Author

ahoppen commented Aug 29, 2023

@Matejkob also noted that in #2108. The only case where you won’t be able to cast is on anything that’s statically known to be a leaf syntax node type. Casting will still be possible on some SyntaxProtocol, some DeclSyntaxProtocol, any SyntaxProtocol etc.

@grynspan
Copy link
Contributor

Oh, I get it. Sorry, I misunderstood what was being described here. I'll be quiet. :)

@Matejkob
Copy link
Contributor

Matejkob commented Sep 13, 2023

@ahoppen if I remember correctly, we previously discussed having some follow-up PRs related to the main one (#2108). Should we keep closed the current issue and create new ones, or would you prefer to continue using this issue to track all follow-ups?

Things to do:

@ahoppen
Copy link
Member Author

ahoppen commented Sep 13, 2023

Oh, sorry. I shouldn’t have closed this issue just yet.

@ahoppen ahoppen reopened this Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants