-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Sema] Improves diagnostic when passing an argument as property of an optional class instance (#57437) #68264
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
base: main
Are you sure you want to change the base?
Conversation
A small query, although I have understood certain |
de00dc6
to
3ad2fc6
Compare
The changes need clang-format but I'm going to run tests to see if anything is broken. |
@swift-ci please test |
71f1a65
to
99ac378
Compare
@swift-ci please test |
I haven't updated |
I think some of them are regressions because they loose optionality and there is one test-case in |
Maybe it's just a matter of moving new code after |
99ac378
to
89ab678
Compare
Let's try this. |
@Rajveer100 Have you tried running that test locally to see if this change counters the regression? |
Just to confirm, we are interested in the following
|
Yes, that should be the one. |
Well, the tweak doesn't seem to work: /test/Constraints/parameterized_existentials.swift:39:32: error: incorrect message found
return x // expected-error {{cannot convert return expression of type '(any P)?' to return type '(any P<Int>)?'}}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cannot convert return expression of type 'any P' to return type 'any P<Int>'
/test/Constraints/parameterized_existentials.swift:47:32: error: incorrect message found
return x // expected-error {{cannot convert return expression of type '(any P<Int>)?' to return type '(any P<String>)?'}}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cannot convert return expression of type 'String' to return type 'Int' |
What do you recommend, change it to the desired diagnostic message? |
I think it should be good to strip the optionals but preserve complete types:
and produce a note like other cases do:
|
The notes are already implemented for the struct G<T> {}
// expected-note@-1 {{arguments to generic parameter 'T' ('any P<Int>' and 'any P') are expected to be equal}}
// expected-note@-2 {{arguments to generic parameter 'T' ('any P' and 'any P<Int>') are expected to be equal}}
// expected-note@-3 {{arguments to generic parameter 'T' ('any P<Int>' and 'any P<String>') are expected to be equal}} |
We need to make sure that they are actually emitted for this error, these notes might be attached to the declaration of the struct but they should be emitted as part of the conversion diagnostic. |
What do you suggest in terms of I have stripped the |
89ab678
to
a5e35fa
Compare
For this case the most important thing is to drop the optionals because first of all they match and secondly they are not the problem, the actual problem is that |
b5a7cec
to
bf123ea
Compare
bf123ea
to
37831ac
Compare
… optional class instance (swiftlang#57437)
37831ac
to
7be242a
Compare
@xedin |
@@ -330,7 +330,7 @@ ERROR(cannot_convert_initializer_value_nil,none, | |||
"'nil' cannot initialize specified type %0", (Type)) | |||
|
|||
ERROR(cannot_convert_to_return_type,none, | |||
"cannot convert return expression of type %0 to return type %1", | |||
"return expressions for types %0 and %1 are not interconvertible", |
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.
IIUC the feedback from @xedin, the discussion about rewording was not about only re-phrasing but add more info about the types involved.
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.
Indeed, notes are to be added, as I said above, I have only added in the test file at the moment. Will add it in the code as well to reflect the same.
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.
@LucianoPAlmeida
For this particular case:
struct S {
var str: String
}
func test(s: S?) {
let _: Int? = s?.str
func f(_: Int?) {}
f(s?.str)
}
I don't get the expected error
, it still gives an ambiguous
diagnostic message:
error: type of expression is ambiguous without a type annotation
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.
Seems like fixes are being correctly added in repairFailures but something is happening in the middle resulting in not a solution formed with those fixes.
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.
Yeah, in diagnoseFailureFor
, it's falling through this call:
DE.diagnose(expr->getLoc(), diag::type_of_expression_is_ambiguous)
.highlight(expr->getSourceRange());
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.
Actually I added breakpoints in repairFailures
, it actually never goes through path.back().is<LocatorPathElt::ContextualType>()
as a successful check. Hence, it does through the ambiguous diagnosis.
Resolves #57437
As described in the issue, “Type of expression is ambiguous” error is thrown when passing an argument as property of an optional class instance, which further broken down into an
argument
orcontextual
type.