File: const_pass_as_arguments.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (82 lines) | stat: -rw-r--r-- 3,397 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// RUN: %target-typecheck-verify-swift

public func takeIntConst(_ a: _const Int) {}
public func takeStringConst(_ a: _const String) {}
public func takeDoubleConst(_ a: _const Double) {}
public func takeArrayConst(_ a: _const [String]) {}
public func takeDictConst(_ a: _const [Int: String]) {}

func main(_ i: Int, _ d: Double, _ s: String, arr: [String], dict: [Int: String]) {
	takeIntConst(2)
	takeDoubleConst(3.3)
	takeStringConst("")
	takeArrayConst([""])
	takeDictConst([1: "", 2: "text"])
	
	takeIntConst(i) // expected-error {{expect a compile-time constant literal}}
	takeDoubleConst(d) // expected-error {{expect a compile-time constant literal}}
	takeStringConst("\(d)") // expected-error {{expect a compile-time constant literal}}
	takeStringConst(s) // expected-error {{expect a compile-time constant literal}}
	takeArrayConst(arr) // expected-error {{expect a compile-time constant literal}}
	takeArrayConst([s]) // expected-error {{expect a compile-time constant literal}}
	takeArrayConst(["", s]) // expected-error {{expect a compile-time constant literal}}
	takeDictConst([1: "", 2: s]) // expected-error {{expect a compile-time constant literal}}
	takeDictConst([1: "", i: "text"]) // expected-error {{expect a compile-time constant literal}}
}

public struct Utils {
	public func takeIntConst(_ a: _const Int) {}
	public func takeStringConst(_ a: _const String) {}
	public func takeDoubleConst(_ a: _const Double) {}
}

func main_member(_ u: Utils, _ i: Int, _ d: Double, _ s: String) {
	u.takeIntConst(2)
	u.takeDoubleConst(3.3)
	u.takeStringConst("")

	u.takeIntConst(i) // expected-error {{expect a compile-time constant literal}}
	u.takeDoubleConst(d) // expected-error {{expect a compile-time constant literal}}
	u.takeStringConst("\(d)") // expected-error {{expect a compile-time constant literal}}
	u.takeStringConst(s) // expected-error {{expect a compile-time constant literal}}
}

protocol ConstFan {
	static _const var v: String { get }  // expected-note {{protocol requires property 'v' with type 'String'}}
}

class ConstFanClass1: ConstFan { // expected-error {{type 'ConstFanClass1' does not conform to protocol 'ConstFan'}} expected-note {{add stubs for conformance}}
	static let v: String = "" // expected-note {{candidate operates as non-const, not const as required}}
}

class ConstFanClassCorrect: ConstFan {
	static _const let v: String = ""
}

class ConstFanClassWrong1: ConstFan {
	static _const let v: String // expected-error {{_const let should be initialized with a compile-time literal}}
	// expected-error@-1 {{'static let' declaration requires an initializer expression or an explicitly stated getter}}
	// expected-note@-2 {{add an initializer to silence this error}}
}

class ConstFanClassWrong2: ConstFan {
	static _const let v: String = "\(v)" // expected-error {{_const let should be initialized with a compile-time literal}}
}

class ConstFanClassWrong3: ConstFan {
	static _const var v: String = "" // expected-error {{let is required for a _const variable declaration}}
}

class ConstFanClassWrong4: ConstFan {
	static func giveMeString() -> String { return "" }
	static _const let v: String = giveMeString() // expected-error {{_const let should be initialized with a compile-time literal}}
}

_const let globalConst = 3

class ConstFanClassWrong5 {
	func foo() -> Int {
		_const let localConst = 3
		return globalConst + localConst
	}
}