Skip to content

Commit 3377591

Browse files
committed
sema: always record fix for mismatched constness.
Also, this commit has added a test for using _const values in property wrapper.
1 parent 1b6ec37 commit 3377591

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
@@ -1505,19 +1505,17 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
15051505
}
15061506
}
15071507
if (!argument.isCompileTimeConst() && param.isCompileTimeConst()) {
1508-
if (cs.shouldAttemptFixes()) {
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());
1518-
}
1519-
cs.recordFix(NotCompileTimeConst::create(cs, paramTy, locator));
1508+
auto *locator = cs.getConstraintLocator(loc);
1509+
SourceRange range;
1510+
// simplify locator so the anchor is the exact argument.
1511+
locator = simplifyLocator(cs, locator, range);
1512+
if (locator->getPath().empty() &&
1513+
locator->getAnchor().isExpr(ExprKind::UnresolvedMemberChainResult)) {
1514+
locator =
1515+
cs.getConstraintLocator(cast<UnresolvedMemberChainResultExpr>(
1516+
locator->getAnchor().get<Expr*>())->getChainBase());
15201517
}
1518+
cs.recordFix(NotCompileTimeConst::create(cs, paramTy, locator));
15211519
}
15221520

15231521
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)