[Sema] PreCheck: Diagnose standalone self within init accessors

'self' within init accessor could only be used to refer to
properties listed in `initializes` and `accesses` attributes.
This commit is contained in:
Pavel Yaskevich
2023-06-07 14:31:32 -07:00
parent 4e2f3b1c8c
commit 5613006944
5 changed files with 90 additions and 2 deletions

View File

@@ -82,6 +82,7 @@ namespace swift {
class SerializedDefaultArgumentInitializer;
class SerializedTopLevelCodeDecl;
class StructDecl;
class AccessorDecl;
namespace serialization {
using DeclID = llvm::PointerEmbeddedInt<unsigned, 31>;
@@ -457,6 +458,18 @@ public:
return const_cast<DeclContext*>(this)->getInnermostMethodContext();
}
/// Returns the innermost accessor context that belongs to a property.
///
/// This routine looks through closure, initializer, and local function
/// contexts to find the innermost accessor declaration.
///
/// \returns the innermost accessor, or null if there is no such context.
LLVM_READONLY
AccessorDecl *getInnermostPropertyAccessorContext();
const AccessorDecl *getInnermostPropertyAccessorContext() const {
return const_cast<DeclContext*>(this)->getInnermostPropertyAccessorContext();
}
/// Returns the innermost type context.
///
/// This routine looks through closure, initializer, and local function

View File

@@ -7342,6 +7342,10 @@ ERROR(init_accessor_accesses_attribute_on_other_declaration,none,
ERROR(init_accessor_property_both_init_and_accessed,none,
"property %0 cannot be both initialized and accessed",
(DeclName))
ERROR(invalid_use_of_self_in_init_accessor,none,
"'self' within init accessors can only be used to reference "
"properties listed in 'initializes' and 'accesses'; "
"init accessors are run before 'self' is fully available", ())
#define UNDEFINE_DIAGNOSTIC_MACROS