Commit Graph

41 Commits

Author SHA1 Message Date
Allan Shortlidge
fbe686d37d SILGen: Fix an assert when emitting SIL for if #available.
Empty version numbers if availability specs are accepted by Sema prior to Swift
6, and when they occur in `if #available` queries they can make the query AST
node invalid without aborting compilation. Adjust an assertion in SILGen to
compensate.
2025-07-14 11:07:40 -07:00
Allan Shortlidge
272b2b43de AST: Accept AvailabilityDomains as diagnostic arguments.
This simplifies the code to emit availabilty diagnostics and ensures that they
display domain names consistently. While updating existing diagnostics, improve
consistency along other dimensions as well.
2025-02-28 09:18:38 -08:00
Allan Shortlidge
aaa0e0a7b3 AST/Parse: Parse custom availability domain specs in if #available(...).
Delay resolution of availability domain identifiers parsed in availability
specifications until type-checking. This allows custom domain specifications to
be written in `if #available` queries.
2025-02-27 23:07:55 -08:00
Allan Shortlidge
db29a1c6f4 Parse/Sema: Move #available query wildcard diagnostics to Sema.
In order to unblock resolution of availability domains during type-checking
instead of parsing, diagnostics about missing or superfluous wildcards in
availability specification lists need to move to Sema.
2025-02-23 22:09:33 -08:00
Allan Shortlidge
e90530990a Parse/Sema: Move some AvailabilitySpec diagnostics from Parse to Sema.
Eventually, querying the `AvailabilityDomain` associated with an
`AvailabilitySpec` will require invoking a request that takes a `DeclContext`.
This means that any diagnostics related to the domain identified by an
`AvailabilitySpec` need to be emitted during type-checking rather than parsing.
This change migrates several `AvailabilitySpec` diagnostics from Parse to Sema
to unblock further work.
2025-02-19 11:40:56 -08:00
Ben Langmuir
fd1875dcfb [test] Move availability tests to later fake OS versions
10.50 was once greater than any real macOS version, but now it compares
less than real released versions, which makes these tests depend on the
deployment target unnecessarily. Update these tests to use even larger
numbers to hopefully keep them independent a little longer.
2024-08-21 11:38:54 -07:00
Allan Shortlidge
0096904caa Parse: Diagnose empty version numbers.
The compiler treats version tuples that are all zeros as empty, or the same as
not having a version. Diagnose attempts to specify all-zeroes versions in
attributes and availability queries to prevent surprising behavior.

Resolves rdar://124661151
2024-03-19 18:17:56 -07:00
Luciano Almeida
52e2b3dc24 [Parse] Suggest platform correction based on known platforms edit distance 2022-09-26 19:44:04 -03:00
Allan Shortlidge
f1ecb4c215 Parse: Offer a fix-it when platform is specified with the wrong case in availability attributes and queries.
Resolves rdar://100195680
2022-09-21 16:21:36 -07:00
Alex Hoppen
bfc68f48e4 [Parser] When recovering from expression parsing don't stop at '{'
When recovering from a parser error in an expression, we resumed parsing at a '{'. I assume this was because we wanted to continue inside e.g. an if-body if parsing the condition failed, but it's actually causing more issue because when parsing e.g.

```swift
expr + has - error +

functionTakesClosure {
}
```

we continue parsing at the `{` of the trailing closure, which is a completely garbage location to continue parsing.

The motivating example for this change was (in a result builder)
```swift
Text("\(island.#^COMPLETE^#)")
takeTrailingClosure {}
```

Here `Text(…)` has an error (because it contains a code completion token) and thus we skip `takeTrailingClosure`, effectively parsing
```swift
Text(….) {}
```

which the type checker wasn’t very happy with and thus refused to provide code completion. With this change, we completely drop `takeTrailingClosure {}`. The type checker is a lot happier with that.
2022-04-07 09:19:22 +02:00
Bruno Rocha
1fe3857735 [SE-0290] Add #unavailable 2021-07-02 13:35:11 +02:00
Nathan Hawes
9bcb54910e [AST] Prefer the 'macOS' spelling over 'OSX' when printing the platform kind.
This affects module interfaces, interface generation in sourcekitd, and
diagnostics. Also fixes a fixit that was assuming the 'OSX' spelling when
computing the source range to replace.

Resolves rdar://problem/64667960
2020-07-08 13:51:25 -07:00
mishal_shah
1e38fc3030 Update master to build with Xcode 11 beta, macOS 10.15, iOS 13, tvOS 13, and watchOS 6 SDKs 2019-06-03 22:50:02 -07:00
Timothy J. Wood
03d6f25c45 Fix crash parsing #available with missing version.
In a multipart #available spec list, don't attempt to look at the SpecResult for the previous part if there wasn't one (a missing version number for example).

Added a test case that asserted before this change.
2017-10-21 12:37:58 -07:00
Jacob Bandes-Storch
c98e515734 [QoI] Improvements to function call & closure diagnostics (#7224) 2017-02-07 17:36:11 -08:00
Brian King
0c57aebfea Fix unit tests 2017-01-26 10:04:41 -05:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Jordan Rose
1cffaed907 [Parse] 'where' to ',' fix-it should remove a preceding space. (#4944)
rdar://problem/27709302
2016-09-23 10:54:17 -07:00
Jordan Rose
77bb14bc0d SE-0106: Add "macOS" as an alias for "OSX" in #available.
...and @available.

Later commits will switch which one is the default.
2016-07-07 14:57:56 -07:00
Chris Lattner
45f2cfaaa0 Implement SE-0099, but where the migration diagnostics are left as warnings
for now.  I'll upgrade them to errors in a week or two to give downstream
projects a chance to update.
2016-07-02 15:44:57 -07:00
Chris Lattner
0d57fe5b34 Fix <rdar://problem/24467411> QoI: Using "&& #available" should fixit to comma
This tweet: https://twitter.com/radexp/status/694561060230184960 pointed out
the sad truth that most people don't know that stmt-condition can contain
(including a fixit) when they try to use && instead of commas between clauses.

Before:

t.swift:4:16: error: #available may only be used as condition of an 'if', 'guard' or 'while' statement
  if x == y && #available(iOS 9, *) { }
               ^
t.swift:5:27: error: expected '{' after 'if' condition
  if #available(iOS 9, *) && x == y {}
                          ^
t.swift:5:37: error: braced block of statements is an unused closure
  if #available(iOS 9, *) && x == y {}
                                    ^
t.swift:5:37: error: expression resolves to an unused function
  if #available(iOS 9, *) && x == y {}
                                    ^~

After:

t.swift:4:13: error: expected ',' joining parts of a multi-clause condition
  if x == y && #available(iOS 9, *) { }
            ^~
            ,
t.swift:5:27: error: expected ',' joining parts of a multi-clause condition
  if #available(iOS 9, *) && x == y {}
                          ^~
                          ,
2016-02-02 22:20:20 -08:00
Slava Pestov
79b24fa7d1 Update availability tests to be independent of the actual deployment target
Use fictional version numbers in the 10.50..10.99 range.
2016-01-13 19:27:26 -08:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
John McCall
bc3b47b98a Infer the return type of a closure to be () if it contains no
return statements, or a return statement with no operand.

Also, fix a special-case diagnostic about converting a return
expression to (1) only apply to converting the actual return
expression, not an arbitrary sub-expression, and (2) use the
actual operand and return types, not the drilled-down types
that caused the failure.

Swift SVN r30420
2015-07-20 21:52:18 +00:00
Chris Lattner
3ad108b0be Reapply r29419:
Enhance fixItRemove() to be a bit more careful about what whitespace it leaves around: if the thing it is removing has leading and trailing whitespace already, this nukes an extra space to avoid leaving double spaces or incorrectly indented results.  

This includes an extra fix for looking off the start of a buffer, which extractText doesn't and can't handle.

This fixes <rdar://problem/21045509> Fixit deletes 'let' from non-binding 'if case let' statements, but leaves an extra space




Swift SVN r29449
2015-06-17 16:31:26 +00:00
Ted Kremenek
d13549e607 Revert "enhance fixItRemove() to be a bit more careful about what whitespace it leaves around:"
This was breaking the bots.

Swift SVN r29432
2015-06-17 02:20:52 +00:00
Chris Lattner
6b3167ab36 enhance fixItRemove() to be a bit more careful about what whitespace it leaves around:
if the thing it is removing has leading and trailing whitespace already, this nukes
an extra space to avoid leaving double spaces or incorrectly indented results.  This
fixes <rdar://problem/21045509> Fixit deletes 'let' from non-binding 'if case let' statements, but leaves an extra space



Swift SVN r29419
2015-06-17 00:55:59 +00:00
Devin Coughlin
9f4233bdc1 Move validation of #available() from Sema to Parse
In anticipation of adding short-form @available() annotations, move validation
of #available() platform/version lists out of Sema and into the parser. This
also enables slightly more graceful recovery from errors.

Swift SVN r28637
2015-05-15 20:08:25 +00:00
Chris Lattner
8a7b3f414e Revise the parser and AST representation of #available to be part of StmtCondition
instead of being an expression.

To the user, this has a couple of behavior changes, stemming from its non-expression-likeness.
 - #available cannot be parenthesized anymore
 - #available is in its own clause, not used in a 'where' clause of if/let.

Also, the implementation in the compiler is simpler and fits the model better.  This
fixes:
<rdar://problem/20904820> Following a "let" condition with #available is incorrectly rejected



Swift SVN r28521
2015-05-13 19:00:40 +00:00
Devin Coughlin
d04cc58c6c Sema: Refine availability context in IfStmt guard conditions following #available()
Loosen restrictions on where #available() can appear in IfStmt guards and refine the
context for guard StmtConditionElements following an availability check.

This enables #available() to be combined with if let optional binding:

if #available(iOS 8.0, *),
   let x = someIOS8API() {
  // Do more iOS 8 stuff
}

and

if let x = someIOS7API() where #available(iOS 8.0, *),
   let y = someIOS8API() {
  // Do more iOS 8 stuff
}

Swift SVN r28096
2015-05-03 20:27:29 +00:00
Devin Coughlin
37088d4fb9 Parse: Update parsing of #available(...) to no longer user >= version comparison
Change the syntax of availability queries from #available(iOS >= 8.0, OSX >= 10.10, *) to

This change reflects the fact that now that we spell the query '#available()' rather than
'#os()', the specification is about availability of the APIs introduced in a particular OS
release rather than an explicit range of OS versions on which the developer expects the
code to run.

There is a Fix-It to remove '>=' to ease adopting the new syntax.

Swift SVN r28025
2015-05-01 05:34:10 +00:00
Devin Coughlin
d08b98b1ca Sema: Turn on availability checking by default
Enable checking for uses of potentially unavailable APIs. There is
a frontend option to disable it: -disable-availability-checking.

This commit updates the SDK overlays with @availability() annotations for the
declarations where the overlay refers to potentially unavailable APIs. It also changes
several tests that refer to potentially unavailable APIs to use either #available()
or @availability annotations.

Swift SVN r27272
2015-04-14 06:44:01 +00:00
Devin Coughlin
8b5f6fec60 Rename '#os' to '#available'
The API review list found it confusing that if #os() and #if os() looked so similar, so
change the availability checking query to be spelled #available:

if #available(iOS >= 9.0, *) {
  ...
}

Swift SVN r26995
2015-04-04 23:33:13 +00:00
Devin Coughlin
a3c4a8cd50 Add '*' wildcard to #os()
On platforms that are not explicitly mentioned in the #os() guard, this new '*'
availability check generates a version comparison against the minimum deployment target.

This construct, based on feedback from API review, is designed to ease porting
to new platforms. Because new platforms typically branch from
existing platforms, the wildcard allows an API availability check to do the "right"
thing (executing the guarded branch accessing newer APIs) on the new platform without
requiring a modification to every availability guard in the program.

So, if the programmer writes:

  if #os(OSX >= 10.10, *) {
  . . .
  }

and then ports the code to iOS, the body will execute.

We still do compile-time availability checking with '*', so the compiler will
emit errors for references to potentially unavailable symbols in the body when compiled
for iOS.

We require a '*' clause on all #os() guards to force developers to
"future proof" their availability checks against the introduction of new a platform.

Swift SVN r26988
2015-04-04 21:03:20 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +00:00
Graham Batty
83f27a8af7 Revert "Mark tests that don't pass on linux as XFAIL."
This reverts commit 2711ca86de7bf6a7885ccea24219a48a590b1e95.

Swift SVN r23577
2014-11-24 17:42:13 +00:00
Graham Batty
198402dcfe Mark tests that don't pass on linux as XFAIL.
Swift SVN r23573
2014-11-24 17:40:37 +00:00
Devin Coughlin
8f275da82a [Parser] Update #os(...) parse tests for r22720.
Swift SVN r22722
2014-10-14 03:56:28 +00:00
Devin Coughlin
d9c10b1915 Extend parsing of #os(...) to allow queries for multiple platforms.
This patch extends the AST and parsing of #os(...) queries to permit queries for
multiple platforms, e.g., #os(OSX >= 10.10, iOS >= 8.0). It also improves
parsing error recovery.


Swift SVN r22154
2014-09-21 01:27:53 +00:00
Devin Coughlin
d33876ec5c Add option to enable experimental API availability checking
Swift SVN r21884
2014-09-11 18:46:56 +00:00
Devin Coughlin
0d7996f4ee Add parsing of availability query expressions (#os(...))
This patch adds a new 'pound_os' token, a new case for it in parseExprPostfix, and parsing of platform version constraints, e.g., OSX >= 10.10. 

It also adds enough type checking and SILGen to get the parsing tests to run without triggering "Unimplemented" assertions.


Swift SVN r21865
2014-09-11 02:59:05 +00:00