Skip to content

Commit d07d0da

Browse files
committed
[Parse] Don't propagate static spelling to AST while parsing top level decls
`FuncDecl` of `static func foo() {}` in top level was accidentally `isStatic()`. That causes assertion failure in `ASTPrinter`.
1 parent 66da45b commit d07d0da

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3972,6 +3972,7 @@ ParserStatus Parser::parseDeclVar(ParseDeclOptions Flags,
39723972
diagnose(Tok, diag::static_var_decl_global_scope, StaticSpelling)
39733973
.fixItRemove(StaticLoc);
39743974
StaticLoc = SourceLoc();
3975+
StaticSpelling = StaticSpellingKind::None;
39753976
} else if (Flags.contains(PD_InStruct) || Flags.contains(PD_InEnum) ||
39763977
Flags.contains(PD_InProtocol)) {
39773978
if (StaticSpelling == StaticSpellingKind::KeywordClass)
@@ -4346,6 +4347,7 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
43464347
diagnose(Tok, diag::static_func_decl_global_scope, StaticSpelling)
43474348
.fixItRemove(StaticLoc);
43484349
StaticLoc = SourceLoc();
4350+
StaticSpelling = StaticSpellingKind::None;
43494351
} else if (Flags.contains(PD_InStruct) || Flags.contains(PD_InEnum) ||
43504352
Flags.contains(PD_InProtocol)) {
43514353
if (StaticSpelling == StaticSpellingKind::KeywordClass) {

test/IDE/print_ast_tc_decls_errors.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,10 @@ class OuterContext {
218218
// CHECK: func protocolFunc()
219219
}
220220
}
221+
222+
static func topLevelStaticFunc() {} // expected-error {{static methods may only be declared on a type}}
223+
// NO-TYPEPR: {{^}}func topLevelStaticFunc() -> <<error type>>{{$}}
224+
// TYPEPR: {{^}}func topLevelStaticFunc() {{$}}
225+
226+
static var topLevelStaticVar = 42 // expected-error {{static properties may only be declared on a type}}
227+
// CHECK: {{^}}var topLevelStaticVar: Int{{$}}

0 commit comments

Comments
 (0)