Skip to content

[NFC] Introduce isType and getDeclType #27195

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 2 commits into from
Closed

Conversation

Azoy
Copy link
Contributor

@Azoy Azoy commented Sep 16, 2019

I've noticed that there are a lot of cases where we dance around types by grabbing their nominal type and comparing that to some stdlib decl. Let's clean this up a little bit and introduce isType so that instead of dancing around this:

if (type->getAnyNominal() == C.getIntDecl())

it becomes:

if (type->isInt())

Note: this is only applied to those stdlib types defined in KnownStdlibTypes.

I've also introduced getDeclType which is shorthand for e.g. C.getBoolDecl()->getDeclaredInterfaceType() == C.getBoolType().

@slavapestov @jrose-apple (Sorry if this is a lot to review!)

@xwu
Copy link
Collaborator

xwu commented Sep 16, 2019

For consistency, shouldn't these be called isIntType(), etc.?

@Azoy
Copy link
Contributor Author

Azoy commented Sep 16, 2019

For consistency, shouldn't these be called isIntType(), etc.?

They can be, but there was already a isBool and isVoid for precedent. It also seems kind of redundant because you're saying type twice in a sentence. "If this type is equal to Int type." It doesn't really matter to me though at the end of the day which ever way this goes.

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

I wasn't sure at first but looking through the diff shows that (a) an awful lot of code has gotten cleaner, and (b) we were already writing helpers to do basically this anyway. I do have comments but the general approach seems reasonable.

@LucianoPAlmeida
Copy link
Contributor

Would make sense or be useful to add something like this isStdlibType here too?

@Azoy
Copy link
Contributor Author

Azoy commented Sep 21, 2019

Would make sense or be useful to add something like this isStdlibType here too?

It might make sense, maybe not in this patch though.

@Azoy
Copy link
Contributor Author

Azoy commented Sep 27, 2019

Will fix conflict soon.

@Azoy Azoy force-pushed the types-need-love branch 2 times, most recently from 4ca62f1 to 02111d3 Compare September 30, 2019 14:55
@Azoy Azoy force-pushed the types-need-love branch 2 times, most recently from 0569522 to 1fc2b5a Compare October 6, 2019 14:36
@Azoy Azoy requested a review from jrose-apple October 6, 2019 14:37
Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

Last comment, I think!

@Azoy Azoy force-pushed the types-need-love branch from 1fc2b5a to c2c66bf Compare October 8, 2019 01:02
@Azoy
Copy link
Contributor Author

Azoy commented Oct 8, 2019

@jrose-apple alright, should be good to go!

@Azoy Azoy force-pushed the types-need-love branch from c2c66bf to f69d772 Compare October 8, 2019 01:04
@jrose-apple
Copy link
Contributor

@swift-ci Please test

@jrose-apple
Copy link
Contributor

@swift-ci Please test source compatibility

@jrose-apple jrose-apple self-assigned this Oct 8, 2019
@swift-ci
Copy link
Contributor

swift-ci commented Oct 8, 2019

Build failed
Swift Test OS X Platform
Git Sha - c2c66bf6387049607d5b32780394973afa6a6f3e

@swift-ci
Copy link
Contributor

swift-ci commented Oct 8, 2019

Build failed
Swift Test Linux Platform
Git Sha - c2c66bf6387049607d5b32780394973afa6a6f3e

@Azoy
Copy link
Contributor Author

Azoy commented Oct 8, 2019

Are the failures related?

@jrose-apple
Copy link
Contributor

Looks like it:

/Users/buildnode/jenkins/workspace/swift-PR-osx/branch-master/swift/stdlib/public/Darwin/Dispatch/Time.swift:19:22: error: cannot convert value of type 'mach_timebase_info_data_t' (aka 'mach_timebase_info') to expected argument type 'mach_timebase_info_t?' (aka 'Optional<UnsafeMutablePointer<mach_timebase_info>>')

mach_timebase_info(&info)
                   ^

/home/buildnode/jenkins/workspace/swift-PR-Linux/branch-master/swift-corelibs-foundation/Foundation/NSAttributedString.swift:75:60: error: cannot convert value of type 'NSRange' (aka '_NSRange') to expected argument type 'NSRangePointer?' (aka 'Optional<UnsafeMutablePointer<_NSRange>>')

var dict = attributes(at: loc, effectiveRange: &range) as NSDictionary
                                               ^

Maybe something around the pointer conversions is being too strict?

@CodaFi
Copy link
Contributor

CodaFi commented Apr 15, 2020

We got this partially done in the linked PR. Do you have the time to rebase this and push on with the rest?

@Azoy
Copy link
Contributor Author

Azoy commented Apr 15, 2020

@CodaFi I can update this weekend if that's fine.

@Azoy Azoy force-pushed the types-need-love branch from f69d772 to 94b7298 Compare April 22, 2020 18:07
Accidentally forgot a negation

address review notes

Address more of Jordan's feedback
@Azoy Azoy force-pushed the types-need-love branch from 94b7298 to 690b0c0 Compare April 22, 2020 18:10
@Azoy
Copy link
Contributor Author

Azoy commented Apr 22, 2020

@CodaFi sorry this is a little later than last weekend, but I've rebased and fixed the issue upthread. Please take a look! I'll do another patch after this lands to find other places that have added more of the old way and update those.

\
Type ASTContext::get##NAME##Type() const { \
if (!get##NAME##Decl()) \
return Type(); \
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 the one thing I'm worried about. Clients of these accessors are historically terrible at handling the case where the lookup fails. That's why some of the TypeChecker entrypoints try to detect this, emit a diagnostic, and map the null type to an ErrorType. There shouldn't be any clients of this function before Sema, so maybe we should just look into doing that everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Slava pointed out in a previous commit that I did that perhaps if these accessors fail to just fail loudly because other pipelines of the compiler aren't great at handling whether or not they failed. Would that ease your concern, or should we still diagnose and provide an error type still?

@CodaFi
Copy link
Contributor

CodaFi commented Apr 23, 2020

@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 690b0c0

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 690b0c0

@Azoy
Copy link
Contributor Author

Azoy commented Apr 23, 2020

@CodaFi I've fixed the test issues related to function builders!

@CodaFi
Copy link
Contributor

CodaFi commented Apr 24, 2020

@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 2b735c4

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 2b735c4

@Azoy
Copy link
Contributor Author

Azoy commented Apr 24, 2020

Ok, I’m going to take another look at the failing tests and resolve them.

@@ -303,12 +303,12 @@ ClangTypeConverter::reverseBuiltinTypeMapping(IRGenModule &IGM,
// Handle Int and UInt specially. On Apple platforms, these correspond to
// the NSInteger and NSUInteger typedefs, so map them back to those typedefs
// if they're available, to ensure we get consistent ObjC @encode strings.
if (swiftType->getAnyNominal() == IGM.Context.getIntDecl()) {
if (swiftType->isInt()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For changes in this file, please also update the corresponding code in ClangTypeConverter.cpp, so that they don't get out of sync. 🙂 See my comment here in case you're wondering why there is this duplication.

@shahmishal
Copy link
Member

Please update the base branch to main by Oct 5th otherwise the pull request will be closed automatically.

  • How to change the base branch: (Link)
  • More detail about the branch update: (Link)

@Azoy Azoy changed the base branch from master to main October 1, 2020 06:59
@Azoy Azoy closed this Apr 16, 2021
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.

8 participants