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

[Compiler] add 'value' label for NSNumber to prevent roundabout err… #39261

Merged
merged 1 commit into from
Sep 16, 2021

Conversation

JiarenWang
Copy link
Contributor

Roundabout error message is viewed when Int is passed to where NSNumber is expected. NSNumber(value: ) should be suggested first.

Resolves SR-15165.

@JiarenWang
Copy link
Contributor Author

@xedin Please review.

lib/Sema/CSDiagnostics.cpp Outdated Show resolved Hide resolved
test/Constraints/rdar82828226.swift Show resolved Hide resolved
test/Constraints/rdar82828226.swift Outdated Show resolved Hide resolved
@JiarenWang
Copy link
Contributor Author

JiarenWang commented Sep 13, 2021

@xedin Please review my new commit. How to write test cases? Enumerate all the types?

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

Looks much better! I added a couple of comments inline.

@@ -701,6 +701,31 @@ class ContextualFailure : public FailureDiagnostic {

static Optional<Diag<Type, Type>>
getDiagnosticFor(ContextualTypePurpose context, Type contextualType);

protected:
bool exprNeedsParensBeforeAddingAs(const Expr *expr) const {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not duplicate the logic here. You could extract these methods from MissingExplicitConversionFailure into a static function in CSDiagnostics.cpp just need to add DeclContext * as an argument to both of them.


func f(_ n: NSNumber) {}
let int: Int = 0
f(int) // expected-error {{cannot convert value of type 'Int' to expected argument type 'NSNumber'}} {{3-3=NSNumber(value: }}
Copy link
Contributor

Choose a reason for hiding this comment

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

You'd have to adjust this test case to make sure that as NSNumber is used instead of NSNumber(value:).
Also please add some more test cases e.g. using NSInteger and as an argument to an operator (you could add a custom one which accepts (NSNumber, NSNumber) to make that easier).

@JiarenWang
Copy link
Contributor Author

@xedin Please review. 'NSInteger' is alias of 'Int', 'Int' passed to 'NSInteger' do not happen anything. 'NSDecimal' renamed 'Decimal', 'Decimal(Int)' works well. 'NSUInteger' produce a error 'Cannot find type 'NSUInteger'. Anything to add about the test?

@xedin
Copy link
Contributor

xedin commented Sep 15, 2021

Hm, I guess our mock SDK doesn't have these types. That's fine though!

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

Thank you!

@xedin
Copy link
Contributor

xedin commented Sep 15, 2021

@swift-ci please smoke test

@xedin
Copy link
Contributor

xedin commented Sep 15, 2021

@swift-ci please test macOS platform

@LucianoPAlmeida
Copy link
Contributor

@swift-ci Please test macOS platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 391c6e0

@LucianoPAlmeida
Copy link
Contributor

@swift-ci Please smoke test MacOS Platform

@xedin
Copy link
Contributor

xedin commented Sep 16, 2021

@swift-ci please test macOS platform

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