Skip to content

Commit 3dca923

Browse files
committed
Add a fix-it and rephrase some warning messages.
1 parent b790eb5 commit 3dca923

File tree

3 files changed

+56
-41
lines changed

3 files changed

+56
-41
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,19 +1328,19 @@ ERROR(attr_availability_expected_equal,none,
13281328
ERROR(attr_availability_expected_version,none,
13291329
"expected version number in '%0' attribute", (StringRef))
13301330

1331-
WARNING(attr_availability_swift_platform_expected_option,none,
1332-
"expected '%0' option with a version number for swift platform, "
1333-
"such as 'introduced', 'deprecated', or 'obsoleted'", (StringRef))
1334-
WARNING(attr_availability_swift_platform_deprecated,none,
1335-
"'deprecated' must have a version number for swift platform in '%0' "
1336-
"attribute", (StringRef))
1337-
WARNING(attr_availability_swift_platform_infeasible,none,
1338-
"argument '%0' is infeasible for swift platform in '%1' attribute",
1331+
WARNING(attr_availability_swift_expected_option,none,
1332+
"expected 'introduced', 'deprecated', or 'obsoleted' in '%0' attribute "
1333+
"for platform 'swift'", (StringRef))
1334+
WARNING(attr_availability_swift_expected_deprecated_version,none,
1335+
"expected version number with 'deprecated' in '%0' attribute for "
1336+
"platform 'swift'", (StringRef))
1337+
WARNING(attr_availability_swift_infeasible_option,none,
1338+
"'%0' cannot be used in '%1' attribute for platform 'swift'",
13391339
(StringRef, StringRef))
13401340

13411341
WARNING(attr_availability_nonspecific_platform_unexpected_version,none,
1342-
"unexpected version number for non-specific platform '*' in '%0' "
1343-
"attribute", (StringRef))
1342+
"unexpected version number in '%0' attribute for non-specific platform "
1343+
"'*'", (StringRef))
13441344

13451345
// autoclosure
13461346
ERROR(attr_autoclosure_expected_r_paren,PointsToFirstBadToken,

lib/Parse/ParseDecl.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,19 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
315315
return true;
316316
};
317317

318+
struct VersionArg {
319+
llvm::VersionTuple Version;
320+
SourceRange Range;
321+
SourceLoc DelimiterLoc;
322+
bool empty() const {
323+
return Version.empty();
324+
}
325+
};
326+
318327
StringRef Platform = Tok.getText();
319328

320329
StringRef Message, Renamed;
321-
llvm::VersionTuple Introduced, Deprecated, Obsoleted;
322-
SourceRange IntroducedRange, DeprecatedRange, ObsoletedRange;
330+
VersionArg Introduced, Deprecated, Obsoleted;
323331
auto PlatformAgnostic = PlatformAgnosticAvailabilityKind::None;
324332

325333
SyntaxParsingContext AvailabilitySpecContext(
@@ -443,7 +451,9 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
443451
case IsIntroduced:
444452
case IsObsoleted: {
445453
// Items with version arguments.
454+
SourceLoc DelimiterLoc;
446455
if (findAttrValueDelimiter()) {
456+
DelimiterLoc = Tok.getLoc();
447457
consumeToken();
448458
} else {
449459
diagnose(Tok, diag::attr_availability_expected_equal, AttrName,
@@ -454,24 +464,19 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
454464
break;
455465
}
456466

457-
auto &VersionArg =
467+
auto &VerArg =
458468
(ArgumentKind == IsIntroduced)
459469
? Introduced
460470
: (ArgumentKind == IsDeprecated) ? Deprecated : Obsoleted;
461471

462-
auto &VersionRange = (ArgumentKind == IsIntroduced)
463-
? IntroducedRange
464-
: (ArgumentKind == IsDeprecated)
465-
? DeprecatedRange
466-
: ObsoletedRange;
467-
468472
if (parseVersionTuple(
469-
VersionArg, VersionRange,
473+
VerArg.Version, VerArg.Range,
470474
Diagnostic(diag::attr_availability_expected_version, AttrName))) {
471475
AnyArgumentInvalid = true;
472476
if (peekToken().isAny(tok::r_paren, tok::comma))
473477
consumeToken();
474478
}
479+
VerArg.DelimiterLoc = DelimiterLoc;
475480

476481
SyntaxContext->createNodeInPlace(SyntaxKind::AvailabilityLabeledArgument);
477482

@@ -514,22 +519,19 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
514519
if (!PlatformKind.hasValue() && Platform == "swift") {
515520
if (PlatformAgnostic == PlatformAgnosticAvailabilityKind::Deprecated) {
516521
diagnose(AttrLoc,
517-
diag::attr_availability_swift_platform_deprecated,
522+
diag::attr_availability_swift_expected_deprecated_version,
518523
AttrName);
519524
return nullptr;
520525
}
521526
if (PlatformAgnostic == PlatformAgnosticAvailabilityKind::Unavailable) {
522-
diagnose(AttrLoc,
523-
diag::attr_availability_swift_platform_infeasible,
524-
"unavailable",
525-
AttrName);
527+
diagnose(AttrLoc, diag::attr_availability_swift_infeasible_option,
528+
"unavailable", AttrName);
526529
return nullptr;
527530
}
528531
assert(PlatformAgnostic == PlatformAgnosticAvailabilityKind::None);
529532

530533
if (!SomeVersion) {
531-
diagnose(AttrLoc,
532-
diag::attr_availability_swift_platform_expected_option,
534+
diagnose(AttrLoc, diag::attr_availability_swift_expected_option,
533535
AttrName);
534536
return nullptr;
535537
}
@@ -547,21 +549,30 @@ ParserResult<AvailableAttr> Parser::parseExtendedAvailabilitySpecList(
547549
return nullptr;
548550
}
549551

550-
// Warn if any version is specified for non-specific '*' platforms.
552+
// Warn if any version is specified for non-specific platform '*'.
551553
if (Platform == "*" && SomeVersion) {
552-
diagnose(AttrLoc,
553-
diag::attr_availability_nonspecific_platform_unexpected_version,
554-
AttrName);
554+
auto diag = diagnose(AttrLoc,
555+
diag::attr_availability_nonspecific_platform_unexpected_version,
556+
AttrName);
557+
if (!Introduced.empty())
558+
diag.fixItRemove(SourceRange(Introduced.DelimiterLoc,
559+
Introduced.Range.End));
560+
if (!Deprecated.empty())
561+
diag.fixItRemove(SourceRange(Deprecated.DelimiterLoc,
562+
Deprecated.Range.End));
563+
if (!Obsoleted.empty())
564+
diag.fixItRemove(SourceRange(Obsoleted.DelimiterLoc,
565+
Obsoleted.Range.End));
555566
return nullptr;
556567
}
557568

558569
auto Attr = new (Context)
559570
AvailableAttr(AtLoc, SourceRange(AttrLoc, Tok.getLoc()),
560571
PlatformKind.getValue(),
561572
Message, Renamed,
562-
Introduced, IntroducedRange,
563-
Deprecated, DeprecatedRange,
564-
Obsoleted, ObsoletedRange,
573+
Introduced.Version, Introduced.Range,
574+
Deprecated.Version, Deprecated.Range,
575+
Obsoleted.Version, Obsoleted.Range,
565576
PlatformAgnostic,
566577
/*Implicit=*/false);
567578
return makeParserResult(Attr);

test/Parse/diagnose_availability.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,34 @@ func twoShorthandsFollowedByDeprecated() {}
3131
// SR-8598: Missing/wrong warning message for '*' or 'swift' platform.
3232

3333
@available(*, deprecated: 4.2)
34-
// expected-warning@-1 {{unexpected version number for non-specific platform '*' in 'available' attribute}}
34+
// expected-warning@-1 {{unexpected version number in 'available' attribute for non-specific platform '*'}} {{25-30=}}
3535
func allPlatformsDeprecatedVersion() {}
3636

3737
@available(*, deprecated, obsoleted: 4.2)
38-
// expected-warning@-1 {{unexpected version number for non-specific platform '*' in 'available' attribute}}
38+
// expected-warning@-1 {{unexpected version number in 'available' attribute for non-specific platform '*'}} {{36-41=}}
3939
func allPlatformsDeprecatedAndObsoleted() {}
4040

41+
@available(*, introduced: 4.0, deprecated: 4.1, obsoleted: 4.2)
42+
// expected-warning@-1 {{unexpected version number in 'available' attribute for non-specific platform '*'}} {{25-30=}} {{42-47=}} {{58-63=}}
43+
func allPlatformsDeprecatedAndObsoleted2() {}
44+
4145
@available(swift, unavailable)
42-
// expected-warning@-1 {{argument 'unavailable' is infeasible for swift platform in 'available' attribute}}
46+
// expected-warning@-1 {{'unavailable' cannot be used in 'available' attribute for platform 'swift'}}
4347
func swiftUnavailable() {}
4448

4549
@available(swift, unavailable, introduced: 4.2)
46-
// expected-warning@-1 {{argument 'unavailable' is infeasible for swift platform in 'available' attribute}}
50+
// expected-warning@-1 {{'unavailable' cannot be used in 'available' attribute for platform 'swift'}}
4751
func swiftUnavailableIntroduced() {}
4852

4953
@available(swift, deprecated)
50-
// expected-warning@-1 {{'deprecated' must have a version number for swift platform in 'available' attribute}}
54+
// expected-warning@-1 {{expected version number with 'deprecated' in 'available' attribute for platform 'swift'}}
5155
func swiftDeprecated() {}
5256

5357
@available(swift, deprecated, obsoleted: 4.2)
54-
// expected-warning@-1 {{'deprecated' must have a version number for swift platform in 'available' attribute}}
58+
// expected-warning@-1 {{expected version number with 'deprecated' in 'available' attribute for platform 'swift'}}
5559
func swiftDeprecatedObsoleted() {}
5660

5761
@available(swift, message: "missing valid option")
58-
// expected-warning@-1 {{expected 'available' option with a version number for swift platform, such as 'introduced', 'deprecated', or 'obsoleted'}}
62+
// expected-warning@-1 {{expected 'introduced', 'deprecated', or 'obsoleted' in 'available' attribute for platform 'swift'}}
5963
func swiftMessage() {}
6064

0 commit comments

Comments
 (0)