-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Diagnostics] Improve error when type parameters aren't equal #17210
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
Conversation
@@ -1432,6 +1432,9 @@ ERROR(types_not_equal,none, | |||
ERROR(types_not_equal_in_call,none, | |||
"%0 requires the types %1 and %2 be equivalent to use %3", | |||
(Type, Type, Type, DeclName)) | |||
ERROR(types_not_equal_in_call_dependent,none, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ID is not my best naming, but nothing better jumped out at me.
lib/Sema/CSDiag.cpp
Outdated
} else { | ||
TC.diagnose(UDE->getLoc(), diag::types_not_equal_in_call, ownerType, lhs, | ||
rhs, UDE->getName()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is… not as clean as I'd like. I'm very open to suggestions about how it might be improved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DougGregor might want to take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least to me new messages don't seem to be much clearer about what is going on especially for.popFirst()
, maybe a better solution here would be to print the actual requirement which got broken like we do in other cases e.g. https://github.com/apple/swift/blob/master/test/Constraints/generics.swift#L251, WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm… it does seem like it be nice to have parity there 🤔
Unfortunately, I'm not sure it's possible without a somewhat major change to this diagnostic. (I'd love to be wrong on this.)
That information is available on the Requirement
. I can get to that through member
's GenericContext
. It looks like the Constraint
is generated from the Requirement
, but I don't see a way to connect that Requirement
back to the current Constraint
to know which of the Requirement
s failed.
I tried moving the diagnoseArgumentGenericRequirements
earlier—to be before this check—but it didn't hit.
It sure seems like there ought to be a way to unify these two things. But there's quite a bit of code between them, and I don't understand enough of the big picture to know how to connect them.
This PR seems like an improvement to me, so I'd probably take this improvement for now. But I'm happy to spend some more time looking—especially if you have any suggestions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constraint locator should tell you exactly what requirement is this constraint related to and you can get it by index from generic signature of the context, take a look at how open{Unbound}Generic
/openGenericRequirements
generates such constraints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this look?
error: value of type '[Int]' has no member 'popFirst'
var b = a.popFirst()
^
Swift.Collection:2:37: note: candidate requires that the types '[Int]' and 'ArraySlice<Int>' be equivalent (requirement specified as 'Self' == 'Self.SubSequence')
@inlinable public mutating func popFirst() -> Self.Element?
^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mdiep I'm not sure what is going on but for some reason I can't see any new changes... But the new node looks great though!
8a52f3f
to
3a21b46
Compare
Okay, just pushed the updated code and tests. Thanks for the help on this one, @xedin! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! One last nit request - could you please use clang-format
on the commit before we merge this?
Done! |
@swift-ci please test and merge |
1 similar comment
@swift-ci please test and merge |
This looks fantastic, thank you! |
This improves the error for code like the following:
Before it would give this error:
I found that to be very unclear about why that was a requirement.
Now it gives this error, with note about where the method was declared:
That seems much clearer to me.
I'm not sure if it's possible to use a locally-built copy of Swift in Xcode, so I'm not sure if that location is enough for tools like Xcode to properly link to the declaration of
popFirst
.Resolves SR-7177.