-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[NFC] Change diagnostic text mentioning 'undeclared type'/'unresolved identifier' #31315
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
[NFC] Change diagnostic text mentioning 'undeclared type'/'unresolved identifier' #31315
Conversation
@swift-ci please test |
@swift-ci please test |
Build failed |
Build failed |
Tracking rdar://problem/62375243. |
Could this be misleading in the context of protocol requirements? This is the one place I can think of where you're declaring things without providing a definition, and I'm wondering if that might cause confusion.
I think at some point we should re-evaluate the rules laid out in Diagnostics.md. This particular rule does seem to encourage more clinical language, which is not very friendly to the user and often leads to these really jargony messages. |
Like in a function signature |
I just mean in general. Say a user is trying to call a function that is a protocol requirement, and they make a typo. Now, they will get the error message protocol P {
func somethingEasyToMisspell()
}
class C: P {
func somethingEasyToMisspell() {
// do something
}
func test() {
somethingEasyToMispell() // error: couldn't find definition of `somethingEasyToMispell`
}
} |
I see what you mean, that makes sense. How about |
[Note to self: The test failures were due to LLDB -- it also needs to be updated since it is matching on the diagnostics.] |
I agree that dropping FWIW I don't think the word |
5dead8a
to
e2336c3
Compare
Fixes rdar://problem/62375243.
e2336c3
to
4a48c02
Compare
Updated code to use Matching LLDB PR: swiftlang/llvm-project#1128 @swift-ci please test |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@swift-ci please test |
Build failed |
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.
The more I look at the test cases, the more I like cannot find [type/label/etc] X in scope
. The previous message always seemed to indicate that the thing doesn't exist, but in scope
adds another angle – I've definitely hit this error before when the type I'm trying to use isn't in scope
An LLDB test seems to be randomly failing. 😐 @swift-ci please test |
TL;DR: I changed "use of undeclared type %0" -> "couldn't find definition of type %0", and "use of unresolved identifier" -> "couldn't find definition of"
Undeclared type diagnostic
The current diagnostic seems unclear from a user-perspective as to what "undeclared type" means, especially if you have not written C/ObjC/C++ before, which do have a distinct notion of declaring types in the surface language, as opposed to only being able to define types. It also potentially nudges the reader familiar with C/ObjC/C++ but new to Swift in an incorrect direction: "ok, this type is undeclared, can I declare it somehow and get rid of this error?" (The answer is you can't; you need to define it or import it.)
While the newer diagnostic is not technically as accurate (e.g. you can have a declaration of a type in a swiftinterface without a definition), that's not really something people write in Swift code (unless this is a part of the language that I don't know about?). So I think the newer diagnostic matches the user model better.
For comparison, Rust says:
which also seems like a reasonable alternative (although it mentions 'scope' which seems a bit unnecessary perhaps, even though it is technically correct).
Elm is a bit more verbose: (e.g.
type X = MakeX { x : Foo }
)Unresolved identifier diagnostic
The basic reasoning is the same as before: "unresolved identifier" is compiler jargon which a user does not necessarily need to care about. Instead we can simply say that we could not find the definition. We could also do something like Rust where we say "cannot find $thing in scope".
Misc Notes
I don't particularly care about the tense. If you think it should be
cannot
instead ofcouldn't
, I'll change that, nbd.I read through
docs/Diagnostics.md
and I don't think the recommendation of "phrase diagnostics as rules rather than reporting that the compiler failed to do something" is a good one. If we follow that recommendation, the diagnostic would look like:That seems overly aggressive IMO. Moreover, the point of the diagnostic here is not to describe a fundamental language rule (one about scoping and define-before-use), but to gently point out what went wrong. Of course, nothing's perfect, but I think it makes sense to not follow that recommendation here.