Skip to content

[Parse] Warn if the same argument is specified more than once in @available #20997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,8 @@ ERROR(attr_availability_unavailable_deprecated,none,
"'%0' attribute cannot be both unconditionally 'unavailable' and "
"'deprecated'", (StringRef))

WARNING(attr_availability_invalid_duplicate,none,
"'%0' argument has already been specified", (StringRef))
WARNING(attr_availability_unknown_platform,none,
"unknown platform '%0' for attribute '%1'", (StringRef, StringRef))
ERROR(attr_availability_invalid_renamed,none,
Expand Down
16 changes: 14 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
while (HasUpcomingEntry) {
SyntaxParsingContext EntryContext(SyntaxContext,
SyntaxKind::AvailabilityArgument);
auto ArgumentLoc = Tok.getLoc();
AnyAnnotations = true;
StringRef ArgumentKindStr = Tok.getText();
ParamIndex++;
Expand All @@ -382,8 +383,8 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
}

if (ArgumentKind == IsInvalid) {
diagnose(Tok.getLoc(), diag::attr_availability_expected_option, AttrName)
.highlight(SourceRange(Tok.getLoc()));
diagnose(ArgumentLoc, diag::attr_availability_expected_option, AttrName)
.highlight(SourceRange(ArgumentLoc));
if (Tok.is(tok::code_complete) && CodeCompletion) {
CodeCompletion->completeDeclAttrParam(DAK_Available, ParamIndex);
consumeToken(tok::code_complete);
Expand All @@ -395,6 +396,13 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(

consumeToken();

auto diagnoseDuplicate = [&](bool WasEmpty) {
if (!WasEmpty) {
diagnose(ArgumentLoc, diag::attr_availability_invalid_duplicate,
ArgumentKindStr);
}
};

switch (ArgumentKind) {
case IsMessage:
case IsRenamed: {
Expand Down Expand Up @@ -427,6 +435,7 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
}

if (ArgumentKind == IsMessage) {
diagnoseDuplicate(Message.empty());
Message = Value.getValue();
} else {
ParsedDeclName parsedName = parseDeclName(Value.getValue());
Expand All @@ -435,6 +444,7 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
AnyArgumentInvalid = true;
break;
}
diagnoseDuplicate(Renamed.empty());
Renamed = Value.getValue();
}

Expand Down Expand Up @@ -476,6 +486,7 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
? Introduced
: (ArgumentKind == IsDeprecated) ? Deprecated : Obsoleted;

bool VerArgWasEmpty = VerArg.empty();
if (parseVersionTuple(
VerArg.Version, VerArg.Range,
Diagnostic(diag::attr_availability_expected_version, AttrName))) {
Expand All @@ -484,6 +495,7 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
consumeToken();
}
VerArg.DelimiterLoc = DelimiterLoc;
diagnoseDuplicate(VerArgWasEmpty);

SyntaxContext->createNodeInPlace(SyntaxKind::AvailabilityLabeledArgument);

Expand Down
20 changes: 20 additions & 0 deletions test/attr/attr_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,23 @@ struct SR8634_Struct: Equatable {
@available(*, deprecated, message: "I must not be raised in synthesized code", renamed: "x")
let a: Int
}

@available(*, deprecated, message: "This is a message", message: "This is another message")
// expected-warning@-1 {{'message' argument has already been specified}}
func rdar46348825_message() {}

@available(*, deprecated, renamed: "rdar46348825_message", renamed: "unavailable_func_with_message")
// expected-warning@-1 {{'renamed' argument has already been specified}}
func rdar46348825_renamed() {}

@available(swift, introduced: 4.0, introduced: 4.0)
// expected-warning@-1 {{'introduced' argument has already been specified}}
func rdar46348825_introduced() {}

@available(swift, deprecated: 4.0, deprecated: 4.0)
// expected-warning@-1 {{'deprecated' argument has already been specified}}
func rdar46348825_deprecated() {}

@available(swift, obsoleted: 4.0, obsoleted: 4.0)
// expected-warning@-1 {{'obsoleted' argument has already been specified}}
func rdar46348825_obsoleted() {}