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

[Diag] Improve diagnostics for optional unwrapping #22050

Merged
merged 7 commits into from Jan 26, 2019
Merged

[Diag] Improve diagnostics for optional unwrapping #22050

merged 7 commits into from Jan 26, 2019

Conversation

theblixguy
Copy link
Collaborator

@theblixguy theblixguy commented Jan 22, 2019

This PR resolves an issue where incorrect diagnostics would be generated when diagnosing optional unwrapping.

struct Foo {
  func bar() -> String??? { return "bar" }
}

let foo: Foo? = Foo()

// incorrect error: Value of optional type 'String??' must be unwrapped to a value of type 'String?'
let _: String?? = foo?.bar()

Resolves SR-9201.

@theblixguy
Copy link
Collaborator Author

cc @xedin

@xedin xedin self-requested a review January 22, 2019 22:16
@xedin
Copy link
Member

xedin commented Jan 22, 2019

Thanks, @theblixguy! I'll take a look tonight.

Copy link
Member

@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.

LGTM! I have left a couple of minor comments, and regarding diagnostic you mentioned - I think it makes more sense to report new optional unwrap diagnostic there, so let's update all other test and see if there are any true regressions.

lib/Sema/CSFix.h Outdated Show resolved Hide resolved
lib/Sema/CSDiagnostics.cpp Outdated Show resolved Hide resolved
@theblixguy
Copy link
Collaborator Author

theblixguy commented Jan 25, 2019

@xedin I have updated the diagnostic messages in the test files, can you take a look?

Also, the one in optional_context_member.swift throws an "unexpected error produced" on line 16 but the error matches what the // expected-error {{ ... }} says. Not sure what's going on there, but it could just be an issue with my build of the compiler.

Copy link
Member

@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.

New errors are great! Regarding the problem in optional_context_member.swift - that means that the same error is produced twice now, I think that's worth investigating.

lib/Sema/CSDiagnostics.cpp Outdated Show resolved Hide resolved
test/ClangImporter/cfuncs_parse.swift Outdated Show resolved Hide resolved
@theblixguy
Copy link
Collaborator Author

theblixguy commented Jan 25, 2019

@xedin Ok, so after some debugging, it's happening in matchTypes(). In this example:

struct Foo {
  static var someOptVar: Foo? = Foo()
}

func nonOptContext() -> Foo {
  switch () {
  case ():
    return .someOptVar 
  }
}

matchTypes() is called several times, which I think is expected.

if (objectType1->getOptionalObjectType()) { .. } is called 3 times and the check succeeds 2 times, hence why we see 2 fixes.

It's a bit strange because I don't think I have touched any code that would cause this to happen. So it must've been happening from the start, but then that doesn't explain why the diagnostic wasn't being emitted twice previously.

@theblixguy
Copy link
Collaborator Author

theblixguy commented Jan 25, 2019

Dumping the fixes shows:

[fix: force optional] @ locator@0x7fe865818a20 [UnresolvedMember@/Users/suyashsrijan/Desktop/test.swift:8:13 -> unresolved member]

[fix: force optional] @ locator@0x7fe865818b10 [UnresolvedMember@/Users/suyashsrijan/Desktop/test.swift:8:13 -> rvalue adjustment]

@xedin
Copy link
Member

xedin commented Jan 25, 2019

@theblixguy That's interesting, in recordFix we have logic which check if we already have a fix for a particular locator. As you can see in your output - it's anchor but different path - one is unresolved member and another is rvalue adjustment. Maybe for ForceOptional fixes we'd want to record only anchor for locator instead of the whole path to avoid this kind of a situation?

@xedin
Copy link
Member

xedin commented Jan 25, 2019

@theblixguy Sorry it wasn't clear - I mean use anchor from locator when we construct ForceOptional fix, I think logic in recordFix should stay the same.

@theblixguy
Copy link
Collaborator Author

@xedin ah okay, that works 🎉

@xedin
Copy link
Member

xedin commented Jan 25, 2019

@swift-ci please test

@swift-ci
Copy link
Collaborator

Build failed
Swift Test OS X Platform
Git Sha - f5264b9

@swift-ci
Copy link
Collaborator

Build failed
Swift Test Linux Platform
Git Sha - f5264b9

@theblixguy
Copy link
Collaborator Author

@xedin I was running a validation test on my machine and it found 2 more failures. I have adjusted the diagnostics in them, but could you take a look at 18d4da1?

test/Constraints/fixes.swift Outdated Show resolved Hide resolved
@xedin
Copy link
Member

xedin commented Jan 26, 2019

@swift-ci please test

@swift-ci
Copy link
Collaborator

Build failed
Swift Test Linux Platform
Git Sha - 8845fcb

@swift-ci
Copy link
Collaborator

Build failed
Swift Test OS X Platform
Git Sha - 8845fcb

@theblixguy
Copy link
Collaborator Author

@xedin all tests have passed, but seems like there's an unrelated failure: Assertion failed: (e == 0), function ~recursive_mutex, file /BuildRoot/Library/Caches/com.apple.xbs/Sources/libcxx/libcxx-400.9/src/mutex.cpp, line 64.

@xedin
Copy link
Member

xedin commented Jan 26, 2019

@swift-ci please test macOS platform

@xedin xedin merged commit b4fb25f into apple:master Jan 26, 2019
@xedin
Copy link
Member

xedin commented Jan 26, 2019

@theblixguy Thanks!

@theblixguy theblixguy deleted the fix/SR-9201 branch January 26, 2019 09:38
@theblixguy theblixguy changed the title [Diag] Fix incorrect diagnostic when unwrapping double optional [Diag] Improve diagnostics for optional unwrapping Jan 26, 2019
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.

None yet

3 participants