Skip to content

Commit 932c45d

Browse files
authored
Merge pull request #41231 from nkcsgexi/require-static-in-type
2 parents ef6d43e + 48ac9e0 commit 932c45d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/Sema/TypeCheckStorage.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,18 @@ PatternBindingEntryRequest::evaluate(Evaluator &eval,
257257
llvm::SmallVector<VarDecl *, 2> vars;
258258
binding->getPattern(entryNumber)->collectVariables(vars);
259259
bool isReq = false;
260+
bool shouldRequireStatic = false;
260261
if (auto *d = binding->getDeclContext()->getAsDecl()) {
261262
isReq = isa<ProtocolDecl>(d);
263+
shouldRequireStatic = isa<NominalTypeDecl>(d);
262264
}
263265
for (auto *sv: vars) {
264266
bool hasConst = sv->getAttrs().getAttribute<CompileTimeConstAttr>();
265267
if (!hasConst)
266268
continue;
267269
bool hasStatic = StaticSpelling != StaticSpellingKind::None;
268270
// only static _const let/var is supported
269-
if (!hasStatic) {
271+
if (shouldRequireStatic && !hasStatic) {
270272
binding->diagnose(diag::require_static_for_const);
271273
continue;
272274
}

test/Sema/const_pass_as_arguments.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,12 @@ class ConstFanClassWrong4: ConstFan {
7171
static func giveMeString() -> String { return "" }
7272
static _const let v: String = giveMeString() // expected-error {{_const let should be initialized with a compile-time literal}}
7373
}
74+
75+
_const let globalConst = 3
76+
77+
class ConstFanClassWrong5 {
78+
func foo() -> Int {
79+
_const let localConst = 3
80+
return globalConst + localConst
81+
}
82+
}

0 commit comments

Comments
 (0)