Skip to content

Commit dde843c

Browse files
authored
Merge pull request #40377 from nkcsgexi/const-propery-wrapper
sema: always record fix for mismatched constness.
2 parents b8a2d07 + 3377591 commit dde843c

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,19 +1506,17 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
15061506
}
15071507
}
15081508
if (!argument.isCompileTimeConst() && param.isCompileTimeConst()) {
1509-
if (cs.shouldAttemptFixes()) {
1510-
auto *locator = cs.getConstraintLocator(loc);
1511-
SourceRange range;
1512-
// simplify locator so the anchor is the exact argument.
1513-
locator = simplifyLocator(cs, locator, range);
1514-
if (locator->getPath().empty() &&
1515-
locator->getAnchor().isExpr(ExprKind::UnresolvedMemberChainResult)) {
1516-
locator =
1517-
cs.getConstraintLocator(cast<UnresolvedMemberChainResultExpr>(
1518-
locator->getAnchor().get<Expr*>())->getChainBase());
1519-
}
1520-
cs.recordFix(NotCompileTimeConst::create(cs, paramTy, locator));
1509+
auto *locator = cs.getConstraintLocator(loc);
1510+
SourceRange range;
1511+
// simplify locator so the anchor is the exact argument.
1512+
locator = simplifyLocator(cs, locator, range);
1513+
if (locator->getPath().empty() &&
1514+
locator->getAnchor().isExpr(ExprKind::UnresolvedMemberChainResult)) {
1515+
locator =
1516+
cs.getConstraintLocator(cast<UnresolvedMemberChainResultExpr>(
1517+
locator->getAnchor().get<Expr*>())->getChainBase());
15211518
}
1519+
cs.recordFix(NotCompileTimeConst::create(cs, paramTy, locator));
15221520
}
15231521

15241522
cs.addConstraint(
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
enum Colors {
4+
case blue
5+
case red
6+
}
7+
8+
let globalString = ""
9+
let globalInt = 2
10+
let globalColor = Colors.blue
11+
12+
func giveMeBlue() -> Colors {
13+
return .blue
14+
}
15+
16+
@propertyWrapper struct Wrapper<Value> {
17+
let key: String
18+
var wrappedValue: Value? { get { return nil } set { } }
19+
init(_ key: _const String) {self.key = key }
20+
init(_ key: _const Int) {self.key = "" }
21+
init(_ key: _const Colors) { self.key = ""}
22+
}
23+
24+
struct WrapperAdopters {
25+
@Wrapper<Bool>(3)
26+
var wrappedVar_correct_1
27+
28+
@Wrapper<Bool>("")
29+
var wrappedVar_correct_2
30+
31+
@Wrapper<Bool>(.blue)
32+
var wrappedVar_correct_3
33+
34+
@Wrapper<Bool>(Colors.blue)
35+
var wrappedVar_correct_4
36+
}
37+
38+
struct WrapperAdopters_incorrect {
39+
@Wrapper<Bool>(globalInt) // expected-error{{expect a compile-time constant literal}}
40+
var wrappedVar_incorrect_1
41+
42+
@Wrapper<Bool>(globalString) // expected-error{{expect a compile-time constant literal}}
43+
var wrappedVar_incorrect_2
44+
45+
@Wrapper<Bool>(globalColor) // expected-error{{expect a compile-time constant literal}}
46+
var wrappedVar_incorrect_3
47+
48+
@Wrapper<Bool>(giveMeBlue()) // expected-error{{expect a compile-time constant literal}}
49+
var wrappedVar_incorrect_4
50+
}

0 commit comments

Comments
 (0)