-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Implement SE-0068: Expanding Swift Self
to class members and value types
#3866
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1428,7 +1428,7 @@ ERROR(requires_conformance_nonprotocol,none, | |
ERROR(requires_not_suitable_archetype,none, | ||
"%select{|first |second }0type %1 in %select{conformance|same-type}2 " | ||
"requirement does not refer to a generic parameter or associated type", | ||
(int, TypeLoc, int)) | ||
(int, Type, int)) | ||
ERROR(requires_no_same_type_archetype,none, | ||
"neither type in same-type refers to a generic parameter or " | ||
"associated type", | ||
|
@@ -1713,13 +1713,9 @@ ERROR(broken_equatable_eq_operator,none, | |
ERROR(no_equal_overload_for_int,none, | ||
"no overload of '==' for Int", ()) | ||
|
||
// Dynamic Self | ||
ERROR(dynamic_self_non_method,none, | ||
// Self | ||
ERROR(self_non_method,none, | ||
"%select{global|local}0 function cannot return 'Self'", (bool)) | ||
ERROR(dynamic_self_struct_enum,none, | ||
"%select{struct|enum}0 method cannot return 'Self'; " | ||
"did you mean to use the %select{struct|enum}0 type %1?", | ||
(int, Identifier)) | ||
|
||
// Duplicate declarations | ||
ERROR(duplicate_enum_element,none, | ||
|
@@ -2572,9 +2568,8 @@ ERROR(array_literal_intrinsics_not_found,none, | |
"Array<T>", ()) | ||
ERROR(bool_intrinsics_not_found,none, | ||
"broken standard library: cannot find intrinsic operations on Bool", ()) | ||
ERROR(self_in_nominal,none, | ||
"'Self' is only available in a protocol or as the result of a " | ||
"method in a class; did you mean %0?", (Identifier)) | ||
ERROR(self_outside_nominal,none, | ||
"'Self' is only available in a type", ()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: "within" instead of "in" |
||
ERROR(class_super_access,none, | ||
"class %select{must be declared " | ||
"%select{private|fileprivate|internal|PUBLIC}2" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -431,6 +431,18 @@ resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *DC) { | |
auto Lookup = lookupUnqualified(DC, Name, Loc, LookupOptions); | ||
|
||
if (!Lookup) { | ||
// Recover from not finding 'Self' in a nominal type, if we are | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it makes sense to do this as recovery rather than unilaterally. Recovery means that a top-level value named "Self" will take precedence over the language feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That precedence is used currently so the protocol's dynamic |
||
// in a type context we can return the TypeExpr | ||
if (Name.getBaseName() == Context.Id_Self) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: this should also check that there are no argument labels ( |
||
if (auto nominal = DC->getEnclosingNominalContext()) | ||
return TypeExpr::createForDecl(Loc, nominal, false); | ||
|
||
// Warn if 'Self' is referenced outside a nominal type context | ||
diagnose(Loc, diag::self_outside_nominal) | ||
.highlight(UDRE->getSourceRange()); | ||
return new (Context) ErrorExpr(UDRE->getSourceRange()); | ||
} | ||
|
||
// If we failed lookup of an operator, check to see it to see if it is | ||
// because two operators are juxtaposed e.g. (x*-4) that needs whitespace. | ||
// If so, emit specific diagnostics for it. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4511,7 +4511,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> { | |
|
||
// Dynamic 'Self' is only permitted on methods. | ||
if (!dc->isTypeContext()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about local functions within types? |
||
TC.diagnose(simpleRepr->getIdLoc(), diag::dynamic_self_non_method, | ||
TC.diagnose(simpleRepr->getIdLoc(), diag::self_non_method, | ||
dc->isLocalContext()); | ||
simpleRepr->setInvalid(); | ||
return true; | ||
|
@@ -4522,26 +4522,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> { | |
auto declaredType = dc->getDeclaredTypeOfContext(); | ||
if (declaredType->is<ErrorType>()) | ||
return false; | ||
|
||
auto nominal = declaredType->getAnyNominal(); | ||
if (!isa<ClassDecl>(nominal) && !isa<ProtocolDecl>(nominal)) { | ||
int which; | ||
if (isa<StructDecl>(nominal)) | ||
which = 0; | ||
else if (isa<EnumDecl>(nominal)) | ||
which = 1; | ||
else | ||
llvm_unreachable("Unknown nominal type"); | ||
TC.diagnose(simpleRepr->getIdLoc(), diag::dynamic_self_struct_enum, | ||
which, nominal->getName()) | ||
.fixItReplace(simpleRepr->getIdLoc(), nominal->getName().str()); | ||
simpleRepr->setInvalid(); | ||
return true; | ||
|
||
// 'Self' return types in classes or protocols are dynamic 'Self' | ||
if (isa<ClassDecl>(nominal) || isa<ProtocolDecl>(nominal)) { | ||
// Note that the function has a dynamic Self return type and set | ||
// the return type component to the dynamic self type. | ||
func->setDynamicSelf(true); | ||
} | ||
|
||
// Note that the function has a dynamic Self return type and set | ||
// the return type component to the dynamic self type. | ||
func->setDynamicSelf(true); | ||
return false; | ||
} | ||
|
||
|
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.
Nitpick: I don't think it's correct to say a nominal is "enclosing" when you're in an extension.
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.
getInnermostTypeContext()->getDeclaredTypeInContext()
?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 was a refactor of a global static function, but I will remove it and replace it with that, thanks