Skip to content

[NFC][ClangImporter] Alter importDecl API to return an optional. #41117

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

Closed
wants to merge 1 commit into from

Conversation

plotfi
Copy link
Contributor

@plotfi plotfi commented Feb 1, 2022

Inspired by #36747. I thought I'd go a little further. I have found
there is quite a lot of usage of dyn_cast_or_null in place of checking
for nullptr or using an optional. I think using an llvm::Optional is
a lot cleaner.

Work in progress (Still doesn't compile), but I wanted to post this here for now so it doesn't get lost.

@zoecarver @hyp

Inspired by swiftlang#36747. I thought I'd go a little further. I have found
there is quite a lot of usage of dyn_cast_or_null in place of checking
for nullptr or using an optional. I think using an llvm::Optional<T> is
  a lot cleaner.
@plotfi plotfi force-pushed the importDecl-Optional-plotfi branch from cebf885 to 6cb7f22 Compare February 2, 2022 05:05
@CodaFi
Copy link
Contributor

CodaFi commented Feb 2, 2022

Not sure I agree with this choice since it turns the clang importer's result into a tri-state (what does Optional(nullptr) signal?)

Perhaps this should be swift::NullablePtr<Decl *> or some equivalent to Optional<NonNull<Decl *>> if we can make that guarantee...

@plotfi
Copy link
Contributor Author

plotfi commented Feb 2, 2022

Not sure I agree with this choice since it turns the clang importer's result into a tri-state (what does Optional(nullptr) signal?)

Perhaps this should be swift::NullablePtr<Decl *> or some equivalent to Optional<NonNull<Decl *>> if we can make that guarantee...

I agree. Was wondering how I could avoid this "tri-state." I can try those too and push an update.

@zoecarver
Copy link
Contributor

@plotfi let's think about how we use the API importDecl today. It returns a potentially null pointer. If the pointer is null, it means we failed to import the decl.

If I understand correctly, this patch gives importDecl two failure modes: if anything fails (including importing a decl) importDecl will return None, but if we have a cached failed imported, we will return {nullptr}? The distinction between someone we failed to import now and something we failed to import a while ago doesn't seem relevant to me, so I agree with Robert, this change doesn't make a lot of sense.

Further, I don't think we get a lot of benefit from having this return an optional, what's the rational there? Maybe if we were starting a new project, using optional and always having non-null pointers would make sense, but I think we're too deep into the convention of using pointers as optionals (I could certainly be swayed on this, though). Regardless: let's factor this change out into a separate commit/PR. We can land the caching change independently (I think roughly what Thomas has is good).

@plotfi
Copy link
Contributor Author

plotfi commented Feb 2, 2022

@plotfi let's think about how we use the API importDecl today. It returns a potentially null pointer. If the pointer is null, it means we failed to import the decl.

If I understand correctly, this patch gives importDecl two failure modes: if anything fails (including importing a decl) importDecl will return None, but if we have a cached failed imported, we will return {nullptr}? The distinction between someone we failed to import now and something we failed to import a while ago doesn't seem relevant to me, so I agree with Robert, this change doesn't make a lot of sense.

Further, I don't think we get a lot of benefit from having this return an optional, what's the rational there? Maybe if we were starting a new project, using optional and always having non-null pointers would make sense, but I think we're too deep into the convention of using pointers as optionals (I could certainly be swayed on this, though). Regardless: let's factor this change out into a separate commit/PR. We can land the caching change independently (I think roughly what Thomas has is good).

Sounds good. I went a little overboard off of the caching changes and wanted to see how far up things go. Is there anything particularly you want different in the caching change or just land it? I like Robert's suggestion of using Optional<NonNull<Decl *>> or something similar.

@plotfi plotfi closed this Feb 4, 2022
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.

3 participants