Skip to content

Commit 7bc1dc5

Browse files
committed
[AST] getParameterAt should check index before retrieving it
`getParameterAt` needs to be more defensive about the input because caller cannot always perform all the checks upfront. Resolves: rdar://102085039
1 parent 42ca1c4 commit 7bc1dc5

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7815,7 +7815,7 @@ const ParamDecl *swift::getParameterAt(ConcreteDeclRef declRef,
78157815
const ParamDecl *swift::getParameterAt(const ValueDecl *source,
78167816
unsigned index) {
78177817
if (auto *params = getParameterList(const_cast<ValueDecl *>(source))) {
7818-
return params->get(index);
7818+
return index < params->size() ? params->get(index) : nullptr;
78197819
}
78207820
return nullptr;
78217821
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
protocol ResultType {
4+
associatedtype E
5+
associatedtype F
6+
7+
func failure() -> F?
8+
}
9+
10+
extension Result: ResultType {
11+
typealias E = Success
12+
typealias F = Failure
13+
14+
func failure() -> F? { fatalError() }
15+
}
16+
17+
protocol ObservableType {
18+
associatedtype Element
19+
}
20+
21+
class Observable<Element> : ObservableType {
22+
func flatMap<Source: ObservableType>(_ selector: @escaping (Element) throws -> Source) -> Observable<Source.Element> {
23+
fatalError()
24+
}
25+
26+
static func just(_ element: Element) -> Observable<Element> {
27+
fatalError()
28+
}
29+
}
30+
31+
extension Observable where Element: ResultType {
32+
func mapGenericError() -> Observable<Result<Element.E, Error>> {
33+
flatMap { result -> Observable<Result<Element.E, Error>> in
34+
if let error = result.failure() as? Error {
35+
return .just(Result.failure(error)) // Ok
36+
}
37+
fatalError()
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)