Skip to content

Commit 2ddba9c

Browse files
authored
Merge pull request #27528 from rintaro/syntaxparse-rdar55952739
[SyntaxParse] Fix attribute generation in ASTGen
2 parents 33770fa + edbf24c commit 2ddba9c

File tree

5 files changed

+27
-37
lines changed

5 files changed

+27
-37
lines changed

include/swift/Parse/ASTGen.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ class ASTGen {
6767

6868
private:
6969
DeclAttributes
70-
generateDeclAttributes(const syntax::DeclSyntax &D,
71-
const Optional<syntax::AttributeListSyntax> &attrs,
72-
const Optional<syntax::ModifierListSyntax> &modifiers,
73-
SourceLoc Loc, bool includeComments);
70+
generateDeclAttributes(const syntax::Syntax &D, SourceLoc Loc,
71+
bool includeComments);
7472

7573
void generateFreeStandingGenericWhereClause(
7674
const syntax::GenericWhereClauseSyntax &syntax,

lib/Parse/ASTGen.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,14 @@ Decl *ASTGen::generate(const DeclSyntax &D, const SourceLoc Loc) {
5757
}
5858

5959
DeclAttributes
60-
ASTGen::generateDeclAttributes(const DeclSyntax &D,
61-
const Optional<AttributeListSyntax> &attrs,
62-
const Optional<ModifierListSyntax> &modifiers,
63-
SourceLoc Loc, bool includeComments) {
64-
SourceLoc attrsLoc;
65-
if (attrs) {
66-
attrsLoc = advanceLocBegin(Loc, *attrs->getFirstToken());
67-
} else if (modifiers) {
68-
attrsLoc = advanceLocBegin(Loc, *modifiers->getFirstToken());
69-
} else {
70-
// We might have comment attributes.
71-
attrsLoc = advanceLocBegin(Loc, *D.getFirstToken());
60+
ASTGen::generateDeclAttributes(const Syntax &D, SourceLoc Loc,
61+
bool includeComments) {
62+
// Find the AST attribute-list from the lookup table.
63+
if (auto firstTok = D.getFirstToken()) {
64+
auto declLoc = advanceLocBegin(Loc, *firstTok);
65+
if (hasDeclAttributes(declLoc))
66+
return getDeclAttributes(declLoc);
7267
}
73-
if (hasDeclAttributes(attrsLoc))
74-
return getDeclAttributes(attrsLoc);
7568
return DeclAttributes();
7669
}
7770

@@ -111,8 +104,7 @@ TypeDecl *ASTGen::generate(const AssociatedtypeDeclSyntax &D,
111104
Identifier name;
112105
SourceLoc nameLoc = generateIdentifierDeclName(idToken, Loc, name);
113106

114-
DeclAttributes attrs =
115-
generateDeclAttributes(D, D.getAttributes(), D.getModifiers(), Loc, true);
107+
DeclAttributes attrs = generateDeclAttributes(D, Loc, true);
116108

117109
DebuggerContextChange DCC(P, name, DeclKind::AssociatedType);
118110

@@ -147,8 +139,7 @@ TypeDecl *ASTGen::generate(const TypealiasDeclSyntax &D, const SourceLoc Loc) {
147139
auto keywordLoc = advanceLocBegin(Loc, D.getTypealiasKeyword());
148140
Identifier name;
149141
SourceLoc nameLoc = generateIdentifierDeclName(idToken, Loc, name);
150-
auto attrs =
151-
generateDeclAttributes(D, D.getAttributes(), D.getModifiers(), Loc, true);
142+
auto attrs = generateDeclAttributes(D, Loc, true);
152143
SourceLoc equalLoc;
153144

154145
DebuggerContextChange DCC(P, name, DeclKind::TypeAlias);
@@ -986,14 +977,7 @@ GenericParamList *ASTGen::generate(const GenericParameterClauseSyntax &clause,
986977

987978
for (auto elem : clause.getGenericParameterList()) {
988979

989-
DeclAttributes attrs;
990-
if (auto attrsSyntax = elem.getAttributes()) {
991-
if (auto firstTok = attrsSyntax->getFirstToken()) {
992-
auto attrsLoc = advanceLocBegin(Loc, *firstTok);
993-
if (hasDeclAttributes(attrsLoc))
994-
attrs = getDeclAttributes(attrsLoc);
995-
}
996-
}
980+
DeclAttributes attrs = generateDeclAttributes(elem, Loc, false);
997981
Identifier name = Context.getIdentifier(elem.getName().getIdentifierText());
998982
SourceLoc nameLoc = advanceLocBegin(Loc, elem.getName());
999983

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,6 +2714,7 @@ Parser::parseDecl(ParseDeclOptions Flags,
27142714
StructureMarkerKind::Declaration);
27152715

27162716
// Parse attributes.
2717+
SourceLoc AttrsLoc = Tok.getLoc();
27172718
DeclAttributes Attributes;
27182719
if (Tok.hasComment())
27192720
Attributes.add(new (Context) RawDocCommentAttr(Tok.getCommentRange()));
@@ -2725,12 +2726,8 @@ Parser::parseDecl(ParseDeclOptions Flags,
27252726
StaticSpellingKind StaticSpelling = StaticSpellingKind::None;
27262727
parseDeclModifierList(Attributes, StaticLoc, StaticSpelling);
27272728

2728-
if (!Attributes.isEmpty()) {
2729-
auto startLoc = Attributes.getStartLoc();
2730-
if (startLoc.isInvalid())
2731-
startLoc = Tok.getLoc();
2732-
Generator.addDeclAttributes(Attributes, startLoc);
2733-
}
2729+
if (!Attributes.isEmpty())
2730+
Generator.addDeclAttributes(Attributes, AttrsLoc);
27342731

27352732
// We emit diagnostics for 'try let ...' in parseDeclVar().
27362733
SourceLoc tryLoc;

lib/Parse/ParseGeneric.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ Parser::parseGenericParameterClauseSyntax() {
7878
SyntaxParsingContext TmpCtxt(SyntaxContext);
7979
TmpCtxt.setTransparent();
8080

81+
auto AttrsLoc = Tok.getLoc();
8182
DeclAttributes attrsAST;
8283
parseDeclAttributeList(attrsAST);
8384
if (!attrsAST.isEmpty())
84-
Generator.addDeclAttributes(attrsAST, attrsAST.getStartLoc());
85+
Generator.addDeclAttributes(attrsAST, AttrsLoc);
8586
auto attrs = SyntaxContext->popIf<ParsedAttributeListSyntax>();
8687
if (attrs)
8788
paramBuilder.useAttributes(std::move(*attrs));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
@available(*, deprecated: 0.1) // expected-warning {{unexpected version number in 'available' attribute for non-specific platform '*'}}
4+
public typealias PublicAlias = Int
5+
public var value1: PublicAlias { return 1 }
6+
7+
@available(*, deprecated: 0.1) // expected-warning {{unexpected version number in 'available' attribute for non-specific platform '*'}}
8+
@available(iOS, deprecated: 99.9)
9+
private typealias PrivateAlias = Int // expected-note {{type declared here}}
10+
public var value2: PrivateAlias { return 1 } // expected-error {{variable cannot be declared public because its type uses a private type}}

0 commit comments

Comments
 (0)