Skip to content

Commit 9802875

Browse files
authored
Merge pull request swiftlang#23187 from theblixguy/fix/SR-10062
[Typechecker] Disallow default argument to inout parameter
2 parents 2924be7 + e21430a commit 9802875

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ NOTE(inout_change_var_type_if_possible,none,
238238
ERROR(cannot_pass_rvalue_inout,none,
239239
"cannot pass immutable value of type %0 as inout argument",
240240
(Type))
241+
ERROR(cannot_provide_default_value_inout,none,
242+
"cannot provide default value to inout parameter %0", (Identifier))
241243

242244
ERROR(cannot_assign_to_literal,none,
243245
"cannot assign to a literal value", ())

lib/Sema/TypeCheckPattern.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,14 @@ bool TypeChecker::typeCheckParameterList(ParameterList *PL,
863863
param->setSpecifier(VarDecl::Specifier::Owned);
864864
}
865865
}
866+
867+
if (param->isInOut() && param->isDefaultArgument()) {
868+
diagnose(param->getDefaultValue()->getLoc(),
869+
swift::diag::cannot_provide_default_value_inout,
870+
param->getName());
871+
param->markInvalid();
872+
hadError = true;
873+
}
866874
}
867875

868876
return hadError;

test/decl/func/default-values.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,16 @@ struct X<T> {
122122

123123
let testXa: X<Int> = .foo(i: 0)
124124
let testXb: X<Int> = .bar
125+
126+
// SR-10062
127+
128+
var aLiteral = 1
129+
let bLiteral = 2
130+
131+
func inoutFuncWithDefaultArg1(x: inout Int = 1) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
132+
func inoutFuncWithDefaultArg2(x: inout Int = bLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
133+
func inoutFuncWithDefaultArg3(x: inout Int = aLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
134+
func inoutFuncWithDefaultArg4(x: inout Int = &aLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
135+
func inoutFuncWithDefaultArg5(x: inout Int = &bLiteral) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
136+
func inoutFuncWithDefaultArg6(x: inout Int = #file) {} // expected-error {{cannot provide default value to inout parameter 'x'}}
137+
func inoutFuncWithDefaultArg7(_: inout Int = 1) {} // expected-error {{cannot provide default value to inout parameter '_'}}

test/expr/expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ func inoutTests(_ arr: inout Int) {
844844

845845
// <rdar://problem/20802757> Compiler crash in default argument & inout expr
846846
var g20802757 = 2
847-
func r20802757(_ z: inout Int = &g20802757) { // expected-error {{use of extraneous '&'}}
847+
func r20802757(_ z: inout Int = &g20802757) { // expected-error {{cannot provide default value to inout parameter 'z'}}
848848
print(z)
849849
}
850850

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: not %target-swift-frontend -emit-silgen %s
2+
3+
// Just make sure we don't crash.
4+
5+
func foo(x: inout Int = 0) {}
6+
foo()

0 commit comments

Comments
 (0)