Skip to content

Commit

Permalink
Merge pull request swiftlang#41231 from nkcsgexi/require-static-in-type
Browse files Browse the repository at this point in the history
  • Loading branch information
nkcsgexi authored Feb 6, 2022
2 parents ef6d43e + 48ac9e0 commit 932c45d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,18 @@ PatternBindingEntryRequest::evaluate(Evaluator &eval,
llvm::SmallVector<VarDecl *, 2> vars;
binding->getPattern(entryNumber)->collectVariables(vars);
bool isReq = false;
bool shouldRequireStatic = false;
if (auto *d = binding->getDeclContext()->getAsDecl()) {
isReq = isa<ProtocolDecl>(d);
shouldRequireStatic = isa<NominalTypeDecl>(d);
}
for (auto *sv: vars) {
bool hasConst = sv->getAttrs().getAttribute<CompileTimeConstAttr>();
if (!hasConst)
continue;
bool hasStatic = StaticSpelling != StaticSpellingKind::None;
// only static _const let/var is supported
if (!hasStatic) {
if (shouldRequireStatic && !hasStatic) {
binding->diagnose(diag::require_static_for_const);
continue;
}
Expand Down
9 changes: 9 additions & 0 deletions test/Sema/const_pass_as_arguments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ class ConstFanClassWrong4: ConstFan {
static func giveMeString() -> String { return "" }
static _const let v: String = giveMeString() // expected-error {{_const let should be initialized with a compile-time literal}}
}
_const let globalConst = 3
class ConstFanClassWrong5 {
func foo() -> Int {
_const let localConst = 3
return globalConst + localConst
}
}

0 comments on commit 932c45d

Please sign in to comment.