Skip to content

Commit c17d35d

Browse files
authored
Merge pull request swiftlang#62146 from xedin/rdar-102085039
[AST] `getParameterAt` should check index before retrieving it
2 parents 527d9a8 + 77fb90e commit c17d35d

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7822,7 +7822,7 @@ const ParamDecl *swift::getParameterAt(ConcreteDeclRef declRef,
78227822
const ParamDecl *swift::getParameterAt(const ValueDecl *source,
78237823
unsigned index) {
78247824
if (auto *params = getParameterList(const_cast<ValueDecl *>(source))) {
7825-
return params->get(index);
7825+
return index < params->size() ? params->get(index) : nullptr;
78267826
}
78277827
return nullptr;
78287828
}

lib/SILGen/SILGen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
907907
case SILDeclRef::Kind::DefaultArgGenerator: {
908908
auto *decl = constant.getDecl();
909909
auto *param = getParameterAt(decl, constant.defaultArgIndex);
910+
assert(param);
911+
910912
auto *initDC = param->getDefaultArgumentInitContext();
911913

912914
switch (param->getDefaultArgumentKind()) {

lib/SILGen/SILGenFunction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ void SILGenFunction::emitCaptures(SILLocation loc,
254254
if (closure.kind == SILDeclRef::Kind::DefaultArgGenerator) {
255255
auto *param = getParameterAt(closure.getDecl(),
256256
closure.defaultArgIndex);
257+
assert(param);
257258
loc = param->getLoc();
258259
} else {
259260
auto f = *closure.getAnyFunctionRef();

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5497,6 +5497,7 @@ Solution::resolveLocatorToDecl(ConstraintLocator *locator) const {
54975497
static ConcreteDeclRef getDefaultArgOwner(ConcreteDeclRef owner,
54985498
unsigned index) {
54995499
auto *param = getParameterAt(owner.getDecl(), index);
5500+
assert(param);
55005501
if (param->getDefaultArgumentKind() == DefaultArgumentKind::Inherited) {
55015502
return getDefaultArgOwner(owner.getOverriddenDecl(), index);
55025503
}
@@ -6034,6 +6035,8 @@ ArgumentList *ExprRewriter::coerceCallArguments(
60346035

60356036
if (paramInfo.hasExternalPropertyWrapper(paramIdx)) {
60366037
auto *paramDecl = getParameterAt(callee.getDecl(), paramIdx);
6038+
assert(paramDecl);
6039+
60376040
auto appliedWrapper = appliedPropertyWrappers[appliedWrapperIndex++];
60386041
auto wrapperType = solution.simplifyType(appliedWrapper.wrapperType);
60396042
auto initKind = appliedWrapper.initKind;

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,7 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
18981898
auto argLabel = argument.getLabel();
18991899
if (paramInfo.hasExternalPropertyWrapper(argIdx) || argLabel.hasDollarPrefix()) {
19001900
auto *param = getParameterAt(callee, argIdx);
1901+
assert(param);
19011902
if (cs.applyPropertyWrapperToParameter(paramTy, argTy, const_cast<ParamDecl *>(param),
19021903
argLabel, subKind, loc).isFailure()) {
19031904
return cs.getTypeMatchFailure(loc);
@@ -10534,6 +10535,8 @@ static Type getOpenedResultBuilderTypeFor(ConstraintSystem &cs,
1053410535
return Type();
1053510536

1053610537
auto *PD = getParameterAt(choice, argToParamElt->getParamIdx());
10538+
assert(PD);
10539+
1053710540
auto builderType = PD->getResultBuilderType();
1053810541
if (!builderType)
1053910542
return Type();

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -697,12 +697,16 @@ CheckRedeclarationRequest::evaluate(Evaluator &eval, ValueDecl *current) const {
697697

698698
if (currParams[i].getPlainType()->getOptionalObjectType()) {
699699
optionalRedecl = true;
700-
if (swift::getParameterAt(current, i)->isImplicitlyUnwrappedOptional())
700+
auto *param = swift::getParameterAt(current, i);
701+
assert(param);
702+
if (param->isImplicitlyUnwrappedOptional())
701703
currIsIUO = true;
702704
}
703705

704706
if (otherParams[i].getPlainType()->getOptionalObjectType()) {
705-
if (swift::getParameterAt(other, i)->isImplicitlyUnwrappedOptional())
707+
auto *param = swift::getParameterAt(other, i);
708+
assert(param);
709+
if (param->isImplicitlyUnwrappedOptional())
706710
otherIsIUO = true;
707711
}
708712
else {
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)