Skip to content

Commit d3a4f3d

Browse files
committed
Sema: Diagnose captures of dynamic 'Self' type from default argument expressions
We could actually allow this for local functions, but it's not worth implementing that until the more general ability of local function default arguments to capture values is implemented. Fixes <rdar://problem/55119566>.
1 parent e1e6f0e commit d3a4f3d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,8 @@ ERROR(dynamic_self_invalid_method,none,
26552655
"covariant 'Self' can only appear at the top level of method result type", ())
26562656
ERROR(dynamic_self_stored_property_init,none,
26572657
"covariant 'Self' type cannot be referenced from a stored property initializer", ())
2658+
ERROR(dynamic_self_default_arg,none,
2659+
"covariant 'Self' type cannot be referenced from a default argument expression", ())
26582660

26592661
//------------------------------------------------------------------------------
26602662
// MARK: Type Check Attributes

lib/Sema/TypeCheckCaptures.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,11 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
624624
/*isObjC=*/false);
625625
E->walk(finder);
626626

627+
if (finder.getDynamicSelfCaptureLoc().isValid()) {
628+
Context.Diags.diagnose(finder.getDynamicSelfCaptureLoc(),
629+
diag::dynamic_self_default_arg);
630+
}
631+
627632
auto captures = finder.getCaptureInfo();
628633
if (isGeneric)
629634
captures.setGenericParamCaptures(true);

test/decl/func/dynamic_self.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,11 @@ class Iterable : Sequence {
437437
return DummyIterator()
438438
}
439439
}
440+
441+
// Default arguments of methods cannot capture 'Self' or 'self'
442+
class MathClass {
443+
func invalidDefaultArg(s: Int = Self.intMethod()) {}
444+
// expected-error@-1 {{covariant 'Self' type cannot be referenced from a default argument expression}}
445+
446+
static func intMethod() -> Int { return 0 }
447+
}

0 commit comments

Comments
 (0)