Skip to content

[ASTVerifier] Fix pre-check to look at subscript arguments #20354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,24 @@ struct is_apply_expr
std::is_same<Ty, DotSyntaxCallExpr>::value ||
std::is_same<Ty, ConstructorRefCallExpr>::value> {};

template <typename Ty>
struct is_subscript_expr
: public std::integral_constant<
bool, std::is_same<Ty, SubscriptExpr>::value ||
std::is_same<Ty, DynamicSubscriptExpr>::value> {};

template <typename Ty>
struct is_autoclosure_expr
: public std::integral_constant<bool,
std::is_same<Ty, AutoClosureExpr>::value> {
};

template <typename Ty>
struct is_apply_or_autoclosure_expr
: public std::integral_constant<
bool, is_apply_expr<Ty>::value || is_autoclosure_expr<Ty>::value> {};
struct is_apply_subscript_or_autoclosure_expr
: public std::integral_constant<bool, is_apply_expr<Ty>::value ||
is_subscript_expr<Ty>::value ||
is_autoclosure_expr<Ty>::value> {
};

template <typename Verifier, typename Kind>
std::pair<bool, Expr *> dispatchVisitPreExprHelper(
Expand All @@ -112,6 +120,22 @@ std::pair<bool, Expr *> dispatchVisitPreExprHelper(
return {false, node};
}

template <typename Verifier, typename Kind>
std::pair<bool, Expr *> dispatchVisitPreExprHelper(
Verifier &V,
typename std::enable_if<
is_subscript_expr<typename std::remove_pointer<Kind>::type>::value,
Kind>::type node) {
if (V.shouldVerify(node)) {
// Record any inout_to_pointer or array_to_pointer that we see in
// the proper position.
V.maybeRecordValidPointerConversion(node, node->getIndex());
return {true, node};
}
V.cleanup(node);
return {false, node};
}

template <typename Verifier, typename Kind>
std::pair<bool, Expr *> dispatchVisitPreExprHelper(
Verifier &V,
Expand All @@ -131,7 +155,7 @@ std::pair<bool, Expr *> dispatchVisitPreExprHelper(
template <typename Verifier, typename Kind>
std::pair<bool, Expr *> dispatchVisitPreExprHelper(
Verifier &V, typename std::enable_if<
!is_apply_or_autoclosure_expr<
!is_apply_subscript_or_autoclosure_expr<
typename std::remove_pointer<Kind>::type>::value,
Kind>::type node) {
if (V.shouldVerify(node)) {
Expand Down
12 changes: 12 additions & 0 deletions test/Constraints/subscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,15 @@ func rdar_45819956() {
_ = s[takesPtr: &i]
// expected-error@-1 {{cannot pass an inout argument to a subscript; use 'withUnsafeMutablePointer' to explicitly convert argument to a pointer}}
}

// rdar://problem/45825806 - [SR-7190] Array-to-pointer in subscript arg crashes compiler
func rdar_45825806() {
struct S {
subscript(takesPtr ptr: UnsafePointer<Int>) -> Int {
get { return 0 }
}
}

let s = S()
_ = s[takesPtr: [1, 2, 3]] // Ok
}