Commit Graph

136 Commits

Author SHA1 Message Date
Doug Gregor
37959de29e Establish type refinement contexts for pattern binding decls directly
The type refinement context builder had a bunch of logic to try to
model type refinement contexts for the first variable declaration that
shows up within a pattern binding declaration. Instead, model this
more syntactically by creating a type refinement context for the
pattern binding declaration itself. This both addresses a regression
in the handling of `if #available` within a closure that's part of an
initializer, and fixes a bug in the same area where similar code has
explicit availability annotations.
2023-08-02 15:07:09 -07:00
Allan Shortlidge
9812c90023 Sema: Ban unavailable stored properties.
Marking a stored property as unavailable with `@available` should be banned,
just like it already is banned for potential unavailability. Unavailable code
is not supposed be reachable at runtime, but unavailable properties with
storage create a back door into executing arbitrary unavailable code at
runtime:

```
@available(*, unavailable)
struct Unavailable {
  init() {
    print("Unavailable.init()")
  }
}

struct S {
  @available(*, unavailable)
  var x = Unavailable()
}

_ = S() // prints "Unavailable.init()"
```

Resolves rdar://107449845
2023-04-04 08:52:39 -07:00
Allan Shortlidge
620462ddf6 Sema: Don't diagnose declarations more available than unavailable container.
It turns out that we must allow declarations with `introduced:` availability
nested inside of other declarations that are `unavailable` in order to
influence weak linking. Stop diagnosing declarations as being more available
than their unavailable containers and revert some changes to availability
inference that were designed to avoid creating these nestings but caused
regressions for declarations marked `@_spi_available.`

Reverts parts of https://github.com/apple/swift/pull/64310,
https://github.com/apple/swift/pull/64015, and
https://github.com/apple/swift/pull/62900.
2023-03-21 13:55:05 -07:00
Anthony Latsis
14b70f306b DiagnosticVerifier: Default expected fix-it start line to the diagnostic's 2023-03-08 12:10:27 +03:00
Allan Shortlidge
f796dc9417 Sema: Don't diagnose explicitly unavailable decls as "more available than unavailable scope."
Fixes a logic error introduced in https://github.com/apple/swift/pull/61242.

Resolves rdar://102877953
2022-12-09 17:33:58 -08:00
Allan Shortlidge
cebfe57cab Sema: Diagnose declarations that are annotated to be more available than their unavailable enclosing scope.
Resolves rdar://92552186
2022-09-21 18:00:43 -07:00
swift-ci
234ffb08d2 Merge remote-tracking branch 'origin/main' into rebranch 2022-07-10 22:13:32 -07:00
Allan Shortlidge
bd7a394cbb Sema: Allow explicitly available overrides to be as available as their context.
Previously, the following test case produced an erroneous diagnostic:

```
class A {
  init() {}
}

@available(macOS 12, *)
class B: A {
  @available(macOS 12, *)
  override init() { // error: overriding 'init' must be as available as declaration it overrides
    super.init()
  }
}
```

The overridden `init()` constructor is as available as it can possibly be. Removing the explicit `@available` annotation suppressed the diagnostic.

To fix this, we check to see if the override is as available as its self type and accept it if it is.

You may be wondering how this works when the `@available` annotation is removed from `override init()` in the example. It turns out that `AvailabilityInference::availableRange()` returns a result that is based only on the explicit availability of the decl in question without taking the availability of the context into account (except when the context is an extension). So with the explicit annotation gone, both the base `init()` and the override are both considered to be "always" available. This is pretty unintuitive and arguably wrong. However, it seems like a lot of existing code depends on this behavior so I've left it for now.

Resolves rdar://96253347
2022-07-01 17:53:47 -07:00
Ben Barham
f2bd6ce9cb [next] Remove subminor version from various tests
The "0" subminor is no longer included after
114b4d96e4 (which generally doesn't add a
subminor). Remove it from checks depending on it.
2022-05-11 17:06:29 -07:00
Robert Widmann
48849dc1b3 Shop Over-Availability Diagnostics
Adopt descriptive language and mention the actual availability bounds that are at issue when a declaration is more available than its enclosing scope.
2022-05-06 16:59:46 -07:00
Allan Shortlidge
c78090bc40 Sema: When computing potential unavailability of a decl, first check whether the decl is explicitly unavailable and the context is also unavailable. If those conditions are met, treat the decl as if it were always available since unavailable code is allowed to reference unavailable decls.
Previously, `TypeChecker::checkDeclarationAvailability()` would behave this way for most explicitly unavailable decls by _accident_. An explicitly unavailable decl has no introduction version, and the existing logic therefore would fail to find a lower bound on the availability of the decl. The edge case that exposed the fragility of this logic was an unavailable extension containing a member with it's own explicit availability. The unavailability of the extension ought to trump the availability of the member, but the existing logic couldn't detect that.

The compiler also ought to diagnose the conflicting availability annotations but I'd like to address that separately.

Resolves rdar://92551870
2022-05-05 07:21:30 -07:00
Allan Shortlidge
6f8057d1e0 Sema: Avoid diagnosing potential unavailability of type components (extension nominal type, superclass, etc.) on declarations that are explicitly unavailable.
Resolves rdar://92179327
2022-04-26 16:03:10 -07:00
Allan Shortlidge
5091a7827b NFC: Use 'potentially unavailable' instead of 'unavailable' in the availability_versions.swift test to distinguish between test cases for types that are only available starting in a certain release versus types that are marked strictly unavailable. 2022-04-25 16:50:58 -07:00
Allan Shortlidge
e34196df0b Fix tests that depended on availability for lazy vars. 2022-02-04 10:28:11 -08:00
Slava Pestov
c508ef56ac Sema: Check conditional availability earlier in conformance checking
We would only check conditioanlavailability once we had committed
to a best witness. Instead, let's use this information to disambiguate
among multiple best witnesses, if we have them.

Access control and unconditional unavailability are still only
checked once the witness is chosen.

Fixes rdar://problem/77259046.
2021-07-09 18:03:38 -04:00
Slava Pestov
9d5baaae9b Merge pull request #36327 from slavapestov/fix-storage-availability
Fix availability checking for stored properties and enum cases with associated values
2021-03-06 09:23:01 -05:00
Slava Pestov
511ada4a50 Sema: Ban potential unavailability on enum cases with associated values
This cannot be supported for the same reasons struct and class stored
properties with potential unavailability cannot be supported.

It is possible that some users relied on this, because it worked in
some cases where the type metadata for the potentially unavailable
type was not actually needed for anything (eg, if the type was @frozen
and trivial, or a simple class reference). If this becomes an issue
we can downgrade this to a warning.

Note that just like the previous fix for stored properties, we do
still allow writing an '@available' attribute on the enum element if
the OS version is not any newer than the deployment target. This allows
annotating APIs with the OS version where they were introduced.
2021-03-05 18:03:05 -05:00
Slava Pestov
cb9c1f93ee Sema: Allow @available on stored properties as long as its as new as the deployment target 2021-03-05 18:03:05 -05:00
Slava Pestov
c346c4fd3b Sema: Relax witness availability check
We were checking that the witness is at least as available as
the intersection of the protocol and the conforming type.

In the case where the conformance is defined in an extension,
this should actually be the intersection of the protocol and
the _availability of the extension_.

Fixes <rdar://problem/74114880>.
2021-03-05 16:58:57 -05:00
Slava Pestov
8fd29a57cc Sema: Add some missing checks for where clauses on non-generic declarations
Access control, availability and exportability checking missed these
because of a baked-in assumption that getGenericParams() == nullptr
rules out the presence of a trailing where clause.
2020-10-16 00:34:56 -04:00
Slava Pestov
1888724166 Sema: Disallow protocols from refining less available protocols
Concrete types can conform to unavailable protocols because
the witness table for the conformance is not required for use
with the concrete type itself.

However, protocols cannot have unavailable base protocols.
I believe this was an oversight of the original implementation
here.
2020-10-16 00:27:26 -04:00
Alexis Laferrière
e509d6883a Revert "[Sema] Fix availability checking in inlinable code" 2020-10-14 10:52:11 -07:00
Alexis Laferrière
fb76ff1aea [Sema] Report non-constructor unavailable overrides as warnings
Report unavailable overrides as a warning to avoid breaking sources.
2020-10-12 19:13:55 -07:00
Alexis Laferrière
1ca852e77a [Sema] Accept unavailable constructors override in unavailable types
Preserve the old behavior of accepted unavailable override of
constructors as this change would be source breaking.
2020-10-09 10:57:01 -07:00
Alexis Laferrière
2f182c2b78 [Sema] Consider unavailable functions as being unreachable
This has the effect of rejecting unavailable overrides to available
methods in a similar way as overrides that are less available than the
introduction are rejected.
2020-10-09 10:40:21 -07: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
92ca9fc924 [Apple Silicon] Generalize tests for other macOS architectures
Most of the changes fall into a few categories:
* Replace explicit "x86_64" with %target-cpu in lit tests
* Cope with architecture differences in IR/asm/etc. macOS-specific tests
2020-07-02 16:27:46 -07:00
Slava Pestov
2ef101c815 Sema: Don't add local functions to TC.definedFunctions
Instead, check them and their error handling right away.

In addition to fixing the crash in the radar, this also causes
us to emit unused variable warnings in functions containing
local functions.

Eventually, TC.definedFunctions should go away altogether.

Fixes <rdar://problem/53956342>.
2019-08-07 00:37:21 -04:00
Brent Royal-Gordon
3494c0bd88 [TypeChecker] Rephrase platforms in availability diagnostics
This does several different things to improve how platforms are described in availability diagnostics:

• Mentions the platform in diagnostics for platform-specific @available(unavailable) attributes.
• Replaces “OS X” with “macOS”.
• Replaces “fooOS application extension” with “application extensions for fooOS”.
• Replaces “on fooOS” with “in fooOS”.

Fixes <rdar://problem/49963341>.
2019-04-30 16:32:43 -07:00
Nate Chandler
c21678d34a Corrected tests by removing implicit returns. 2019-04-24 10:04:20 -07:00
Pavel Yaskevich
63b802ca88 [AST/Printing] Don't omit empty labels in special names
This makes diagnostics more verbose and accurate, because
it's possible to distinguish how many parameters there are
based on the message itself.

Also there are multiple diagnostic messages in a format of
`<descriptive-kind> <decl-name> ...` that get printed as
e.g. `subscript 'subscript'` if empty labels are omitted.
2018-09-24 18:36:53 -07:00
Slava Pestov
463a467046 Merge pull request #18026 from slavapestov/inherited-availability-attribute-part-2
Sema: Fix availability of inherited designated initializers, part 2
2018-07-30 18:40:11 -07:00
Jordan Rose
6290d9be60 Introduce DescriptiveDeclKind::Property (#18183)
...and collapse StaticVar/ClassVar and StaticLet/ClassLet into
StaticProperty/ClassProperty.

"var" and "let" aren't great nouns to use in diagnostics to begin with,
especially alongside semantic terms like "instance method". Focus on
the type vs. non-type aspect instead with "property", which better
matches how people talk about member vars (and lets) anyway.
2018-07-30 09:23:59 -07:00
Slava Pestov
336f51e5bc Sema: Fix availability of inherited designated initializers, part 2
We need to explicitly handle nested types here.

Fixes the (re-opened) <rdar://problem/40853731>,
<https://bugs.swift.org/browse/SR-7881>.
2018-07-24 16:13:45 -07:00
Slava Pestov
cc06543e87 Sema: Remove third pass over parameter list when validating generic functions and subscripts
The code for performing the third pass using the GenericToArchetypeResolver
is still there, but it only runs for non-generic declarations.

The next step is to consolidate this path with the generic path.
2018-07-18 01:54:24 -07:00
Slava Pestov
ae6c75ab5d Sema: Fix availability of inherited designated initializers
Inherited designated initializers got the same availability
as the corresponding initialier in the superclass.

However if the superclass was more available than the subclass,
we would generate a diagnostic that a member cannot be more
available than its containing type.

This diagnostic had an unknown source location, since the
location was for a synthesized declaration, causing confusion.

Fixes <https://bugs.swift.org/browse/SR-7881> aka
<rdar://problem/40853731>.
2018-06-08 23:23:43 -07:00
Jordan Rose
6bd7e5e5b4 Make sure protocol witness errors don't leave the conformance context
That is, if there's a problem with a witness, and the witness comes
from a different extension from the conformance (or the original type,
when the conformance is on an extension), put the main diagnostic on
the conformance, with a note on the witness. This involves some
shuffling and rephrasing of existing diagnostics too.

There's a few reasons for this change:

- More context. It may not be obvious why a declaration in file
  A.swift needs to be marked 'public' if you can't see the conformance
  in B.swift.

- Better locations for imported declarations. If you're checking a
  conformance in a source file but the witness came from an imported
  module, it's better to put the diagnostic on the part you have
  control over. (This is especially true in Xcode, which can't display
  diagnostics on imported declarations in the source editor.)

- Plays better with batch mode. Without this change, you can have
  diagnostics being reported in file A.swift that are tied to a
  conformance declared in file B.swift. Of course the contents of
  A.swift also affect the diagnostic, but compiling A.swift on its
  own wouldn't produce the diagnostic, and so putting it there is
  problematic.

The change does in some cases make for a worse user experience,
though; if you just want to apply the changes and move on, the main
diagnostic isn't in the "right place". It's the note that has the info
and possible fix-it. It's also a slightly more complicated
implementation.
2018-05-10 19:31:12 -07:00
Doug Gregor
58356f9bf5 Update test case now that we produce fewer redundant diagnostics. 2017-09-24 23:08:17 -07:00
Joe Shajrawi
00f44ce24a Revert "Create fewer generic signature builders" 2017-09-22 21:57:53 -07:00
Doug Gregor
a9873b4d15 Update test case now that we produce fewer redundant diagnostics. 2017-09-22 12:37:49 -07:00
Robert Widmann
39494b2ba2 Rearrange test code for exhaustiveness 2017-04-28 02:06:39 -04:00
Doug Gregor
7dd1c87dd3 [SE-0160] Warn about uses of @objc declarations that used deprecated @objc inference.
When in Swift 3 compatibility mode without
`-warn-swift3-objc-inference`, warn on the *uses* of declarations that
depend on the Objective-C runtime that became `@objc` due to the
deprecated inference rule. This far more directly captures important
uses of the deprecated Objective-C entrypoints. We diagnose:

* `#selector` expressions that refer to one of these `@objc` members
* `#keyPath` expressions that refer to one of these `@objc` members
* Dynamic lookup (i.e., member access via `AnyObject`) that refers to
  one of these `@objc` members.
2017-03-31 21:22:15 -07:00
Paweł Szot
e935700c25 [SR-4087] fix "add @available attribute" location (#7864) 2017-03-03 10:51:02 -08: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
38e6b2808e Do even less availability checking under -disable-availability-checking. (#4214)
...and make sure we're in that mode for SIL inputs and for sil-opt and
sil-extract, even when working with AST types and declarations rather
than SIL types.

Without this, we get zillions of deprecation warnings coming out of
the validation tests SIL/parse_stdlib_*.sil, which dump the standard
library and then attempt to re-parse it. This has been causing the
"long" tests to take, well, too long.
2016-08-11 10:19:46 -07:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
David Farler
7bfaeb57f1 [SE-0081] Warn on deprecated where clause inside angle brackets
and provide a fix-it to move it to the new location as referenced
in SE-0081.

Fix up a few stray places in the standard library that is still using
the old syntax.

Update any ./test files that aren't expecting the new warning/fix-it
in -verify mode.

While investigating what I thought was a new crash due to this new
diagnostic, I discovered two sources of quite a few compiler crashers
related to unterminated generic parameter lists, where the right
angle bracket source location was getting unconditionally set to
the current token, even though it wasn't actually a '>'.
2016-07-26 01:41:10 -07:00
Slava Pestov
ddc51c5917 AST: Implement SE-0102, introducing new semantics for Never alongside @noreturn
No migrator support yet, and the code for @noreturn is still in
place.
2016-07-22 14:56:39 -07:00
Devin Coughlin
c553de4478 [Sema] Remove 'deployment target ensures guard will always be true'
Remove the diagnostic that warns when an availability check is unnecessary
because the minimum deployment target ensures it will always be true. This
diagnostic is valuable (it tells users that they have dead fallback code) but
can also be super annoying when a source file is shared between projects with
different deployment targets. There is not currently a good mechanism to
suppress these warnings (for example, by expressing a "compatibility version" in
the source or as a build setting) and so it is better to turn the diagnostic
off.

rdar://problem/22337402
2016-07-10 19:00:47 -04:00
Chris Lattner
3549ec5404 [QoI] make several improvements to the unused expression diagnostics, to go
along with recent policy changes:

- For expression types that are not specifically handled, make sure to
  produce a general "unused value" warning, catching a bunch of unused
  values in the testsuite.

- For unused operator results, diagnose them as uses of the operator
  instead of "calls".

- For calls, mutter the type of the result for greater specificity.

- For initializers, mutter the type of the initialized value.

- Look through OpenExistentialExpr's so we can handle protocol member
  references propertly.

- Look through several other expressions so we handle @discardableResult
  better.
2016-05-16 23:26:07 -07:00