Skip to content

Commit 8b1c9c9

Browse files
committed
[Parse] InitAccessors: Parse initializer exprs associated with computed properties that have init accessor
Initialization expressions are not allowed on computed properties but if a property has `init` accessor it should be allowed because it could be used by a memberwise initializer.
1 parent 612a2e7 commit 8b1c9c9

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,14 +1059,14 @@ bool Parser::isStartOfGetSetAccessor() {
10591059
// The only case this can happen is if the accessor label is immediately after
10601060
// a brace (possibly preceded by attributes). "get" is implicit, so it can't
10611061
// be checked for. Conveniently however, get/set properties are not allowed
1062-
// to have initializers, so we don't have an ambiguity, we just have to check
1063-
// for observing accessors.
1062+
// to have initializers unless they have `init` accessor, so we don't have an
1063+
// ambiguity, we just have to check for observing accessors and init accessor.
10641064
//
10651065
// If we have a 'didSet' or a 'willSet' label, disambiguate immediately as
10661066
// an accessor block.
10671067
Token NextToken = peekToken();
10681068
if (NextToken.isContextualKeyword("didSet") ||
1069-
NextToken.isContextualKeyword("willSet"))
1069+
NextToken.isContextualKeyword("willSet") || NextToken.is(tok::kw_init))
10701070
return true;
10711071

10721072
// If we don't have attributes, then it cannot be an accessor block.
@@ -1087,9 +1087,9 @@ bool Parser::isStartOfGetSetAccessor() {
10871087
skipSingle();
10881088
}
10891089

1090-
// Check if we have 'didSet'/'willSet' after attributes.
1090+
// Check if we have 'didSet'/'willSet' or 'init' after attributes.
10911091
return Tok.isContextualKeyword("didSet") ||
1092-
Tok.isContextualKeyword("willSet");
1092+
Tok.isContextualKeyword("willSet") || Tok.is(tok::kw_init);
10931093
}
10941094

10951095
/// Recover invalid uses of trailing closures in a situation

test/decl/var/init_accessors.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,24 @@ func test_memberwise_ordering() {
444444

445445
_ = Test5(_a: 0, _b: 1, c: 2) // Ok
446446
}
447+
448+
func test_default_arguments_are_analyzed() {
449+
struct Test {
450+
var pair: (Int, Int) = (0, 1) { // Ok
451+
init {}
452+
}
453+
454+
var other: (Int, String) = ("", 42) {
455+
// expected-error@-1 {{cannot convert value of type '(String, Int)' to specified type '(Int, String)'}}
456+
init(initialValue) {}
457+
}
458+
459+
var otherPair = (0, 1) {
460+
// expected-error@-1 {{computed property must have an explicit type}}
461+
init(initalValue) {}
462+
463+
get { 42 }
464+
// expected-error@-1 {{cannot convert return expression of type 'Int' to return type '(Int, Int)'}}
465+
}
466+
}
467+
}

0 commit comments

Comments
 (0)