Skip to content

Commit 13fc048

Browse files
authored
Merge pull request swiftlang#41475 from nkcsgexi/key-path-const
sema: keypath expression should be considered as compile-time constant
2 parents 5974cd0 + d52714f commit 13fc048

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/AST/Expr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ bool Expr::isSemanticallyConstExpr() const {
209209
case ExprKind::BooleanLiteral:
210210
case ExprKind::FloatLiteral:
211211
case ExprKind::StringLiteral:
212+
case ExprKind::KeyPath:
212213
return true;
213214
case ExprKind::Array:
214215
case ExprKind::Dictionary: {

test/Sema/const_keypath.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
struct Article {
4+
let id: String
5+
}
6+
7+
let keypath = \Article.id
8+
func keypath_generator() -> KeyPath<Article, String> { return \.id }
9+
func const_map(_const _ map: KeyPath<Article, String>) {}
10+
11+
const_map(\.id)
12+
const_map(\Article.id)
13+
14+
const_map(keypath_generator()) // expected-error {{expect a compile-time constant literal}}
15+
const_map(keypath) // expected-error {{expect a compile-time constant literal}}

0 commit comments

Comments
 (0)