-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Diagnostics][Qol] SR-11295 Emit diagnostics for same type coercion. #26710
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
[Diagnostics][Qol] SR-11295 Emit diagnostics for same type coercion. #26710
Conversation
c8be8f9
to
f4736d8
Compare
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.
It would be great if you could convert this into ConstraintFix
format (there are couple of examples already available) which is marked as warning. If you look at CSGen you'll see that when we generate constraints for CoerceExpr
in visitCoerceExpr
it creates Conversion
constraint, you could add a new LocatorPathElt
to uniquely identify that conversion comes from coercion and when it's simplified in matchTypes
add a new warning if both type1
and type2
are equal.
f69525d
to
73b34ed
Compare
b179683
to
d92b5bf
Compare
@xedin Thank's for all the guidance here :)) |
687d233
to
2a0bdf9
Compare
c99aa18
to
c510d35
Compare
Hey guys, sorry for so many questions 😅
And an expression like '_ = 1 as Int64' which should not trigger the warning ends-up doing it. So the question is if we should skip literals or adding the warnigs specials cases like only trigger the warning if coerceExpr->getSub(): Or maybe there is a better way to handle those special cases... edit: Was thinking that maybe the literals could have their own diagnostics like: |
@LucianoPAlmeida Here is the suggestion how to simplify this - instead adding new |
@xedin auto fromType = CS.getType(expr->getSubExpr());
auto locator = [&]() {
if (!isa<LiteralExpr>(expr->getSubExpr()))
return CS.getConstraintLocator(expr, LocatorPathElt::TypeCoercion());
return CS.getConstraintLocator(expr);
}();
CS.addExplicitConversionConstraint(fromType, toType,
/*allowFixes=*/true, locator); ? |
6baadf2
to
479598a
Compare
@LucianoPAlmeida Yes, that's exactly what I mean. Also the problem you are facing in CSApply seems to be related to the fact that logic around determining choice locator has to match logic in CSGen otherwise it'll try to lookup overload without |
fc6d9b6
to
0e168cb
Compare
@xedin Still in progress here, sorry for taking so long to get this one ready (there are some edge cases to handle and many tests breaking) and also sorry for so many questions. But there some few issues that I'm not really sure how to handle. let x: Bool = 3/4 as Float > 1/2 as Float Where the solver binds the type of the expression
And by that the types the handled to match are always equals, which is right for what I could see in TypeChecker.rst since every time the solver makes a guess it re-attempts to simplify the system getting to match types with type1 == Float and type2 == Float, but with the actual logic for this diagnostic it will always endup being emmited. This kinda applies also to the literal cases mentioned before. So I'm not sure what kinda check can be done to avoid emmit it for those cases. |
@LucianoPAlmeida Maybe a sensible thing would be to limit this warning to situation when coerced type comes from |
I end-up limiting only to One other question is if we should handle composition here. Like one of the tests that break is func f(b: Base<Int>) {
let _ = base as Base<Int> & AnyObject
} with the actual logic, the warning is emitted for the Another question is about the tests under the test/ClangImporter directory, where there are a lot of coercion as tests of c/objc types e.g. Other thing is the case with type alias typealias Double1 = Double
typealias Double2 = Double
func f(d: Double1) {
let d2 = d as Double2 // emits: casting expression to 'Double2' (aka 'Double') doesn't change the type
} The question here is since maybe is not obvious that |
0d543fa
to
69c439b
Compare
da0e5f2
to
4278d31
Compare
@LucianoPAlmeida Don't worry about it, I'll take a look! Regarding squash - it be great if you could squash changes which are logically together into one commit. |
b6912f1
to
da65421
Compare
da65421
to
5d1eeac
Compare
ConstraintLocator *locator) | ||
: ContextualFailure(root, cs, fromType, toType, locator) {} | ||
|
||
bool diagnoseAsError() override; |
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.
More of a meta-comment - It looks like we add more and more warnings so it might make sense to do a follow-up by adding diagnoseAsWarning()
method to the FailureDiagnostic
interface and rename classes like this to be Warning
instead of Error
...
@xedin Thank's for the review, I'll make the changes as soon as possible :) |
No problem! |
@xedin All addressed :) |
…-warning-unecessary-casts
@swift-ci please smoke test |
@swift-ci please smoke test macOS platform |
Thank you, @LucianoPAlmeida! |
Emit diagnostic when try to perform a type coercion to the same type.
Resolves SR-11295.
cc @harlanhaskins