Files
swift-mirror/test/expr/closure/default_args.swift
Doug Gregor 6e64ca66f0 Treat '|' as a delimiter while parsing the signature of a closure.
'|' is part of the character set for operators, but within the
signature of a closure we need to treat the first non-nested '|' as
the closing delimiter for the closure parameter list. For example,

  { |x = 1| 2 + x}

parses with the default value of '1' for x, with the body 2 + x. If
the '|' operator is needed in the default value, it can be wrapped in
parentheses:

  { |x = (1|2)| x }

Note that we have problems with both name binding and type checking
for default values in closures (<rdar://problem/13372694>), so they
aren't actually enabled. However, this allows us to parse them and
recover better in their presence.



Swift SVN r5202
2013-05-17 16:02:44 +00:00

28 lines
1.3 KiB
Swift

// RUN: %swift -parse %s -verify
// FIXME: This test makes sure we parse the closing '|' of the closure
// signature properly in the presence of default arguments. We do not
// yet actually support default arguments.
func simple_default_args() {
var f1 : (Int) -> Int = {|x : Int = 1| x+1} // expected-error{{default initializer}}
var f2 : () -> Int = {|x : Int = 1| x+1} // expected-error{{default initializer}} expected-note{{while converting}} expected-error{{different number}}
}
func inferred_default_args() {
var f1 : (Int) -> Int = {|x = 1| x+1} // expected-error{{default initializer}}
var f2 : () -> Int = {|x = 1| x+1} // expected-error{{default initializer}} expected-note{{while converting}} expected-error{{different number}}
}
func nested_pipes() {
var f1 : (Int[]) -> Int[] = {|x = new Int[1|2]| x } // expected-error{{default initializer}}
var f2 : (Int) -> Int = {|x = (1|2)| x } // expected-error{{default initializer}}
var f3 : (Int, Int) -> Int = {|x,y| x|y }
}
func fun_with_pipes() {
var f1 : (Int) -> Int = {|x = 1 | 2 | 3} // expected-error{{default initializer}}
var f2 : (Int) -> Int = {|x = 1 + | 2 | 3} // expected-error{{default initializer}} expected-error{{after operator}}
var f3 : (Int) -> Int = {|x = 1|-x} // expected-error{{default initializer}}
}