Changing error diagnosed for when self isn’t available

Hi all, I just wanted to get everyone’s thoughts on something:

Wouldn’t it be better to change the current error thrown when the keyword self isn’t available? Currently, the error thrown would be cannot find 'self’ in scope, however I believe a better error would be self can only be used in the declaration or extension of a type, as it would make the usage of self clearer, especially to beginners.

Please let me know your thoughts on this :slightly_smiling_face:


The phrase self can only be used in the declaration or extension of a type wouldn't be true, strictly speaking:

func foo() { // <- a global function
    let `self` = 5
    print(self) // <- `self` is used outside of a type declaration or extension
1 Like

Given that the vast majority of use cases intend to actually use self on a type (or maybe treat Swift functions as those in JavaScript), I think the diagnostic could be more helpful. Perhaps there could be a suggestion after the main error, prompting the user to extend the type they want to use self on. Maybe a suggestion to write let 'self' = <code> could also be offered, but I'm not sure if that would actually be useful.


There is nothing illegal or objectively bad about a custom variable named self, so the proposed message would be actively harmful for those cases, where the current message is merely "not necessarily clear enough" in the demonstrated case. For anyone who knows what self means in Swift (which should arguably be everyone who's writing Swift code), the existing message would be perfectly clear

1 Like

What about changing the wording then? Ie, improper use of self, or something similar?

I think it's even more vague than "cannot find self in scope", since "use" can refer to anything at all. I'd argue that the issue of the current wording seeming not very helpful for beginners is better solved by possibly improving the Swift book, as necessary, to mention that self is a variable that, in many cases is declared implicitly by the complier, but can also be declared explicitly.

1 Like

Beginner programmers do not know what self is until they have learned about and internalized writing their own types. It is completely reasonable for a Swift programmer to not have a good understanding of what self is.


Agreed. That's why I suggested to focus on the documentation and the learning instead.

I’m thinking of something like self used in a context where it is not explicitly declared or in the context of a type?, though may want to reword the context of a type to something better sounding..

1 Like

When something would be helpful in the context of a diagnostic but cannot be accurately phrased as a rule (“can,” “cannot,” “must”), I find it helpful to phrase it instead as a question. This has the effect of being able to suggest something to the user without coming off as saying it’s the only thing a user is allowed to do.

I agree with @Serena and @filip-sakel that a little more help here could be useful for beginners. So one might try something like:

cannot find ‘self’ in scope; did you mean to declare or extend a type?


That sounds very reasonable to me! As long as the message doesn't imply that an otherwise perfectly reasonable piece of code is illegal, a clarification in the form of a question could work!


@xwu @technogen cannot find ‘self’ in scope; did you mean to declare or extend a type? sounds good! That would make it way less ambiguous than it is right now and hopefully clear it up to newer programmers, I’ll get working on a pr for this soon, though the discussion is still open for anyone to recommend changes with this wording :upside_down_face:

1 Like

I like the question approach! There's also the option of phrasing a declarative error in a way that gets at the heart of the problem without explicitly saying that the let `self` = ... approach is invalid:

attempt to use 'self' outside of a type declaration or extension


cannot find 'self' in scope; not currently within a type declaration or extension

The first one seems great and I think I’d actually use it however I fear others may think it invalidates the cases where self could be a user defined variable, @technogen thoughts?

1 Like

@Serena So far, the phrasing suggested by @xwu sounds best to me:

Cannot find `self` in scope, did you mean to declare or extend a type?

This kind of extraneous self reference feels more likely to mean that someone misplaced it in terms of context, rather than forgot to declare or extend a type. Perhaps did you mean to use it within a type or extension context? or similar would be more helpful.


Heavily agree with this! I’d go with something like did you mean to use it within a type declaration or extension context? instead, though both get across the same point across and are good enough

As an update: a Pull Request has been made with edits and tests, and the tests are passing :partying_face:, still open for any changes recommended