|
| 1 | +// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx10.15 -swift-version 5 |
| 2 | + |
| 3 | +// REQUIRES: objc_interop |
| 4 | +// REQUIRES: OS=macosx |
| 5 | + |
| 6 | +import SwiftUI |
| 7 | + |
| 8 | +extension [UInt8] { |
| 9 | + var toASCII : String? { nil } |
| 10 | +} |
| 11 | + |
| 12 | +// rdar://111120803 |
| 13 | +do { |
| 14 | + struct MissingOptionalUnwrap : View { |
| 15 | + var data: [UInt8] |
| 16 | + |
| 17 | + var body: some View { |
| 18 | + Group { |
| 19 | + Text("") |
| 20 | + Text(data.toASCII) |
| 21 | + // expected-error@-1 {{value of optional type 'String?' must be unwrapped to a value of type 'String'}} |
| 22 | + // expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}} |
| 23 | + // expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// rdar://112928810 |
| 30 | +do { |
| 31 | + struct DismissAction { |
| 32 | + func callAsFunction() { |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + struct TestConcurrencyMismatch : View { |
| 37 | + private var dismiss : DismissAction |
| 38 | + |
| 39 | + var body: some View { |
| 40 | + List { |
| 41 | + } |
| 42 | + .toolbar { |
| 43 | + ToolbarItem(placement: .cancellationAction) { |
| 44 | + Button("Dismiss") { |
| 45 | + dismiss() |
| 46 | + } |
| 47 | + } |
| 48 | + ToolbarItem(placement: .primaryAction) { |
| 49 | + Button("Done") { |
| 50 | + MainActor.run { |
| 51 | + // expected-error@-1 {{cannot pass function of type '@Sendable () async -> ()' to parameter expecting synchronous function type}} |
| 52 | + await dismiss() |
| 53 | + // expected-note@-1 {{'async' inferred from asynchronous operation used here}} |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// rdar://115784749 |
| 63 | +do { |
| 64 | + struct InvalidArgumentPlacement : View { |
| 65 | + var body: some View { |
| 66 | + let base = RoundedRectangle(cornerRadius: 12) |
| 67 | + Group { |
| 68 | + base.strokeBorder(lineWidth: 2, LinearGradient(gradient: Gradient(colors: [.red, .blue]), startPoint: .top, endPoint: .bottom)) |
| 69 | + // expected-error@-1 {{unnamed argument #2 must precede argument 'lineWidth'}} |
| 70 | + Text("test") |
| 71 | + .font(.system(size: 200)) |
| 72 | + .minimumScaleFactor(0.01) |
| 73 | + .aspectRatio(1, contentMode: .fit) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// rdar://118326796 |
| 80 | +do { |
| 81 | + class Model: ObservableObject { |
| 82 | + } |
| 83 | + |
| 84 | + struct InvalidRef: View { |
| 85 | + @State private var isLoading = true |
| 86 | + |
| 87 | + var body: some View { |
| 88 | + Group { |
| 89 | + if isLoading{ |
| 90 | + EmptyView().onAppear { |
| 91 | + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { |
| 92 | + isLoading = false |
| 93 | + } |
| 94 | + } |
| 95 | + } else { |
| 96 | + NavigationView { |
| 97 | + } |
| 98 | + .environmentObject(Model) |
| 99 | + // expected-error@-1 {{type 'Model.Type' cannot conform to 'ObservableObject'}} |
| 100 | + // expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}} |
| 101 | + // expected-note@-3 {{required by instance method 'environmentObject' where 'T' = 'Model.Type'}} |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// rdar://106804509 |
| 109 | +do { |
| 110 | + struct Entry: Identifiable { |
| 111 | + var id: String { name } |
| 112 | + let name: String |
| 113 | + let type: String = "string" |
| 114 | + } |
| 115 | + |
| 116 | + struct MissingLabelTest: View { |
| 117 | + var entry: [Entry] = [] |
| 118 | + |
| 119 | + var body: some View { |
| 120 | + VStack(alignment: .leading) { |
| 121 | + ForEach(entry) { entry in |
| 122 | + HStack { |
| 123 | + Text(entry.name) |
| 124 | + Text("(\(entry.type))").foregroundColor(.secondary) |
| 125 | + } |
| 126 | + ViewWithTitle("title") |
| 127 | + // expected-error@-1 {{missing argument label 'title:' in call}} |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + struct ViewWithTitle: View { |
| 134 | + let title: String |
| 135 | + |
| 136 | + var body: some View { |
| 137 | + Text(title) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// rdar://119008852 |
| 143 | +do { |
| 144 | + struct LazyPageView: View { |
| 145 | + @State var currentOffset = 0 |
| 146 | + @State private var tabViewSelection = 0 |
| 147 | + let minIndex: Int? |
| 148 | + let maxIndex: Int? |
| 149 | + let contentGen: (Int) -> any View |
| 150 | + |
| 151 | + var body: some View { |
| 152 | + TabView(selection: $tabViewSelection) { |
| 153 | + Group { |
| 154 | + if minIndex == nil || currentOffset - 1 >= minIndex! { |
| 155 | + // expected-error@-1 {{type 'any View' cannot conform to 'View'}} |
| 156 | + // expected-note@-2 {{only concrete types such as structs, enums and classes can conform to protocols}} |
| 157 | + // expected-note@-3 {{required by static method 'buildIf' where 'Content' = 'any View'}} |
| 158 | + contentGen(currentOffset - 1) |
| 159 | + } |
| 160 | + } |
| 161 | + contentGen(currentOffset) |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// rdar://118374670 |
| 168 | +do { |
| 169 | + struct MissingWrapperInnerView: View { |
| 170 | + @State var cond = false |
| 171 | + |
| 172 | + var body: some View { |
| 173 | + Group { |
| 174 | + Group { |
| 175 | + EmptyView() |
| 176 | + } |
| 177 | + .disabled(true) |
| 178 | + Toggle(isOn: cond) { |
| 179 | + // expected-error@-1 {{cannot convert value 'cond' of type 'Bool' to expected type 'Binding<Bool>', use wrapper instead}} {{22-22=$}} |
| 180 | + Text("") |
| 181 | + } |
| 182 | + } |
| 183 | + .disabled(true) |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments