Skip to content

Parse: Diagnose empty version numbers #72449

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 1 commit into from
Mar 20, 2024
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
11 changes: 11 additions & 0 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4089,6 +4089,11 @@ bool Parser::parseVersionTuple(llvm::VersionTuple &Version,
Version = llvm::VersionTuple(major);
Range = SourceRange(StartLoc, Tok.getLoc());
consumeToken();
if (Version.empty()) {
// Versions cannot be empty (e.g. "0").
diagnose(Range.Start, D).warnUntilSwiftVersion(6);
return true;
}
return false;
}

Expand Down Expand Up @@ -4125,6 +4130,12 @@ bool Parser::parseVersionTuple(llvm::VersionTuple &Version,
Version = llvm::VersionTuple(major, minor);
}

if (Version.empty()) {
// Versions cannot be empty (e.g. "0.0").
diagnose(Range.Start, D).warnUntilSwiftVersion(6);
return true;
}

return false;
}

Expand Down
6 changes: 6 additions & 0 deletions test/Parse/availability_query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ if #available(OSX { // expected-error {{expected version number}} expected-error
if #available(OSX) { // expected-error {{expected version number}}
}

if #available(OSX 0) { // expected-warning {{expected version number; this is an error in the Swift 6 language mode}}
}

if #available(OSX 0.0) { // expected-warning {{expected version number; this is an error in the Swift 6 language mode}}
}

if #available(OSX 10.51 { // expected-error {{expected ')'}} expected-note {{to match this opening '('}} expected-error {{must handle potential future platforms with '*'}} {{24-24=, *}}
}

Expand Down
2 changes: 1 addition & 1 deletion test/Parse/invalid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ let x: () = ()
// https://github.com/apple/swift/issues/50734

func f1_50734(@NSApplicationMain x: Int) {} // expected-error {{@NSApplicationMain may only be used on 'class' declarations}}
func f2_50734(@available(iOS, deprecated: 0) x: Int) {} // expected-error {{'@available' attribute cannot be applied to this declaration}}
func f2_50734(@available(iOS, deprecated: 1) x: Int) {} // expected-error {{'@available' attribute cannot be applied to this declaration}}
func f3_50734(@discardableResult x: Int) {} // expected-error {{'@discardableResult' attribute cannot be applied to this declaration}}
func f4_50734(@objcMembers x: String) {} // expected-error {{@objcMembers may only be used on 'class' declarations}}
func f5_50734(@weak x: String) {} // expected-error {{'weak' is a declaration modifier, not an attribute}} expected-error {{'weak' may only be used on 'var' declarations}}
Expand Down
6 changes: 6 additions & 0 deletions test/Sema/diag_originally_definedin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public class C {
@_originallyDefinedIn(module: "original", OSX 10.13)
public class D {}

@_originallyDefinedIn(module: "original", macOS) // expected-error {{expected version number in '@_originallyDefinedIn' attribute}}
public func missingVersion() {}

@_originallyDefinedIn(module: "original", macOS 0) // expected-warning {{expected version number in '@_originallyDefinedIn' attribute; this is an error in the Swift 6 language mode}}
public func versionZero() {}

@available(macOS 10.9, *)
@_originallyDefinedIn(module: "original", _myProject 2.0) // expected-error {{reference to undefined version '2.0' for availability macro '_myProject'}}
public func macroVersioned() {}
Expand Down
9 changes: 9 additions & 0 deletions test/attr/attr_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ let _: Int
@available(OSX, introduced: 1.0.0x4) // expected-error{{expected version number in 'available' attribute}}
let _: Int

@available(OSX, introduced: 0) // expected-warning{{expected version number in 'available' attribute; this is an error in the Swift 6 language mode}}
let _: Int

@available(OSX, introduced: 0.0) // expected-warning{{expected version number in 'available' attribute; this is an error in the Swift 6 language mode}}
let _: Int

@available(OSX, introduced: 0.0.0) // expected-warning{{expected version number in 'available' attribute; this is an error in the Swift 6 language mode}}
let _: Int

@available(*, renamed: "bad name") // expected-error{{'renamed' argument of 'available' attribute must be an operator, identifier, or full function name, optionally prefixed by a type name}}
let _: Int

Expand Down
3 changes: 3 additions & 0 deletions test/attr/attr_backDeployed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ public func missingVersionFunc2() {}
@backDeployed(before: macOS, iOS) // expected-error 2{{expected version number in '@backDeployed' attribute}}
public func missingVersionFunc3() {}

@backDeployed(before: macOS 0) // expected-warning {{expected version number in '@backDeployed' attribute; this is an error in the Swift 6 language mode}}
public func missingVersionFunc4() {}

@backDeployed(before: macOS 12.0, iOS 15.0,) // expected-error {{unexpected ',' separator}}
public func unexpectedSeparatorFunc() {}

Expand Down