Commit Graph

115 Commits

Author SHA1 Message Date
Allan Shortlidge
5f5cc9f00b Sema: Diagnose use of availability macros in conditional statements in @_backDeploy functions. Add a test verifying that expected diagnostics are emitted when referencing non-public declarations from a @_backDeploy function body.
Resolves rdar://90270100
2022-03-14 17:16:17 -07:00
Slava Pestov
ceb8675ad1 Sema: (Mostly) check conformance availability
If a conformance is defined in an extension, we now look for
references to the conformance in types and expressions and
respect's the extension's availability (or deprecation, etc).

The conformance checker itself still needs to check conformance
availability of associated conformances and the like; that will
be a separate change.

Note that conformances defined on types don't require any
special handling, since they are as available as the
intersection of the conforming type and the protocol.

By default, we diagnose conformance availability violations
where the OS version is not sufficiently new as warnings, to
avoid breaking source compatibility. Stricter behavior where
these violations are diagnosed as errors is enabled by passing
the -enable-conformance-availability-errors flag. There are
test cases that run both with and without this flag. In the
future, we hope to make the stricter behavior the default,
since after all, violations here can result in link errors and
runtime crashes.

Uses of completely unavailable conformances are still always
diagnosed as errors, even when this flag is not passed in.

Progress on <rdar://problem/35158274>.
2020-11-05 17:51:45 -05:00
Slava Pestov
fd38c4d18a Sema: Pass ExportContext by const reference where possible 2020-10-28 23:57:28 -04:00
Slava Pestov
5f5372a3fc Sema: Don't check SPI violations in diagnoseInlinableDeclRefAccess()
There's no need to check for that here, because we also run
diagnoseDeclRefExportability() on declarations referenced
from inlinable code.

This changes some diagnostics; we now produce the same diagnostic
for references to SPI types in declaration signatures and for
references to non-type SPI declarations in inlinable function bodies.

Also note that the old inlinable reference diagnostic no longer has
to handle the 'public' and 'open' access levels, which previously
happened for '@_spi'; so I changed those entries in the %select to
%error.
2020-10-22 01:11:46 -04:00
Slava Pestov
b3dadc8973 AST: Use VarDecl::isInitExposedToClients() from DeclContext::getFragileFunctionKind()
getFragileFunctionKind() would report that all initializers in
non-resilient public types were inlinable, including static
properties.

This was later patched by VarDecl::isInitExposedToClients(),
which was checked in diagnoseInlinableDeclRefAccess().
However, the latter function only looked at the innermost
DeclContexts, not all parent contexts, so it would incorrectly
diagnose code with a nested DeclContext inside of a static
property initializer.

Fix this by changing getFragileFunctionKind() to call
isInitExposedToClients() and simplifying
diagnoseInlinableDeclRefAccess().

This commit also introduces a new isLayoutExposedToClients()
method, which is similar to isInitExposedToClients(), except
it also returns 'true' if the property does not have an
initializer (and in fact the latter is implemented in terms
of the former).
2020-10-22 01:11:39 -04:00
Slava Pestov
0f27312790 Sema: Refactor TypeChecker::diagnoseInlinableDeclRef() 2020-10-22 01:11:02 -04:00
Slava Pestov
a824e5a33b Sema: Refactor getDisallowedOriginKind() to take an ExportContext
Converting the innermost DeclContext into a Decl won't work if
the innermost DeclContext is the parent context of a VarDecl.

Instead, use the relevant bit of state from the ExportContext,
which is computed correctly.

This fixes a regression caused by an earlier commit in this PR
which our test suite did not catch.
2020-10-20 01:02:31 -04:00
Slava Pestov
3044c057e8 Sema: Wrap up some availability-related state in a new ExportContext type
The ExportContext describes the restrictions, if any, on what
declarations can be uttered in a specific place in the
program. Here, the place is either the signature of a
declaration, or the body of a function.
2020-10-20 01:02:11 -04:00
Slava Pestov
c2ed347665 Sema: Pass ValueDecl instead of ConcreteDeclRef in a few places in availability checking
The substitution map is checked elsewhere.
2020-10-20 01:00:43 -04:00
Slava Pestov
0ea1367384 Sema: Refactoring in preparation for conformance availability checking
Today, we check conformance exportability in two places:

1) For inlinable function bodies: in availability checking
2) For types in inlinable declaration signatures: in availability checking
3) For types in non-inlinable declaration signatures: in ExportabilityChecker

I want to merge 2) and 3) together, and also generalize the conformance
exportability check to check conformance availability as well.

This is a preliminary refactoring towards achieving this goal.
2020-10-20 01:00:43 -04:00
Alexis Laferrière
dac3922090 [Sema] Accept use of SPI in inlinable SPI declarations
rdar://63978500
rdar://63189125
2020-09-16 11:20:56 -07:00
Alexis Laferrière
3acbd0917e [Sema] Type-check exportability of SPI conformances
Report the use of conformances declared as SPI in public declarations
and inlinable code.

rdar://problem/66659715
2020-09-01 11:11:26 -07:00
Arnold Schwaighofer
825a2a259b Mark non-foreign entry points of @objc dynamic methods in generic classes dynamically_replaceable
```
class Generic<T> {
  @objc dynamic func method() {}
}

extension Generic {
  @_dynamicReplacement(for:method())
  func replacement() {}
}
```

The standard mechanism of using Objective-C categories for dynamically
replacing @objc methods in generic classes does not work.

Instead we mark the native entry point as replaceable.

Because this affects all @objc methods in generic classes (whether there
is a replacement or not) by making the native entry point
`[dynamically_replaceable]` (regardless of optimization mode) we guard this by
the -enable-implicit-dynamic flag because we are late in the release cycle.

* Replace isNativeDynamic and isObjcDynamic by calls to shouldUse*Dispatch and
  shouldUse*Replacement
  This disambiguates between which dispatch method we should use at call
  sites and how these methods should  implement dynamic function
  replacement.

* Don't emit the method entry for @_dynamicReplacement(for:) of generic class
  methods
  There is not way to call this entry point since we can't generate an
  objective-c category for generic classes.

rdar://63679357
2020-06-09 09:23:29 -07:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Slava Pestov
e2349bdf67 AST: Move TypeChecker::getFragileFunctionKind() to a method on DeclContext
We had some duplicated logic between getResilienceExpansion() and
getFragileFunctionKind(). Clean this up by moving the latter into
AST, and re-implementing the former in terms of the latter.

This also fixes a crash in at least one case where these two
implementations had previously diverged.

Fixes <rdar://problem/60605117>.
2020-04-16 14:09:04 -04:00
Alexis Laferrière
26c6a18345 [Sema] Improve type-checking of the use and exposability of SPIs 2020-03-04 16:43:05 -08:00
Alexis Laferrière
2e73cb44fd [Sema] Type-check the use and exposability of SPI decls 2020-02-19 14:17:14 -08:00
Robert Widmann
f27916b76c Inline TypeChecker::diagnoseInlinableLocalType 2019-11-05 22:52:28 -08:00
Robert Widmann
56b6e53dae Remove raw references to PatternBindingEntry APIs
Switch most callers to explicit indices.  The exceptions lie in things that needs to manipulate the parsed output directly including the Parser and components of the ASTScope.  These are included as friend class exceptions.
2019-10-17 13:31:14 -07:00
Slava Pestov
d2c579d5f7 Sema: Move some checks from validateType() down to resolveType()
validateType() is just the TypeLoc-based wrapper; it should not do
any checks that resolveType() itself does not do.
2019-10-10 13:53:40 -04:00
Slava Pestov
bbce38409c Sema: Decouple availability checking from the TypeChecker 2019-09-24 22:51:23 -04:00
Jordan Rose
8d7f1b7c5d [AST] Separate SourceFile from FileUnit.h
Like the last commit, SourceFile is used a lot by Parse and Sema, but
less so by the ClangImporter and (de)Serialization. Split it out to
cut down on recompilation times when something changes.

This commit does /not/ split the implementation of SourceFile out of
Module.cpp, which is where most of it lives. That might also be a
reasonable change, but the reason I was reluctant to is because a
number of SourceFile members correspond to the entry points in
ModuleDecl. Someone else can pick this up later if they decide it's a
good idea.

No functionality change.
2019-09-17 17:54:41 -07:00
Jordan Rose
853caa66d4 [AST] Split FileUnit and its subclasses out of Module.h
Most of AST, Parse, and Sema deal with FileUnits regularly, but SIL
and IRGen certainly don't. Split FileUnit out into its own header to
cut down on recompilation times when something changes.

No functionality change.
2019-09-17 17:54:41 -07:00
Robert Widmann
30f4df3b00 Make validateType a utility method
Move around some code so that TypeChecker::validateType is now a utility
method of the type checker.  This cleans up at least one request that no
longer needs to grab the type checker from the AST context.
2019-09-13 15:16:14 -07:00
Slava Pestov
bdaeaf1e5f Don't pass SubstitutionMap by const reference 2019-07-24 12:43:36 -04:00
Jordan Rose
3b4bb1960d @_implementationOnly: fix over-eager checking for vars in extensions (#24629)
The logic I had here checked whether an extension used an
implementation-only type whenever there was a declaration within that
extension that needed checking...but unfortunately that included not
just PatternBindingDecls (whose access is filtered at a later step)
but things like IfConfigDecls (#if). Change this to only check
signatures of extensions with ABI-public members, something that is
tested once when visiting an ExtensionDecl.

Additionally, skip AccessorDecls entirely, since they'll be tested
as part of the corresponding subscript or var. This saves a duplicate
diagnostic.

rdar://problem/50541589
2019-05-16 13:13:48 -07:00
Jordan Rose
778f0f82f4 Separate TypeDeclFinder from AccessScopeChecker (#23957)
...and move the latter into lib/Sema as TypeAccessScopeChecker, since
it's not interesting outside of Sema anyway.

No functionality change.
2019-04-18 20:14:21 -07:00
Jordan Rose
326fa14f8b Don't bother checking implementation-only import use for accessors
Their associated AbstractStorageDecl is already being checked, and
will produce a better error.
2019-04-11 16:33:23 -07:00
Jordan Rose
a4606111af Adjust the implementation-only diagnostics to sound more similar
John and I had slightly different styles when adding these; better
to keep them in harmony.
2019-04-11 16:33:23 -07:00
Jordan Rose
7006aa0b9f Check for use of implementation-only conformances in inlinable code
This includes both the types and the values (generic functions, etc)
used in the inlinable code. We get some effectively duplicate
diagnostics at this point because of this, but we can deal with that
at a future date.

Last part of rdar://problem/48991061
2019-04-10 11:09:07 -07:00
Jordan Rose
590551781a Pass a ConcreteDeclRef through for availability checking where present
This will be used in the next commit for checking conformance use in
inlinable function bodies, but for now it's No Functionality Change.
2019-04-10 11:09:07 -07:00
Jordan Rose
a15dec8774 Diagnose typealiases of non-public types in inlinable functions (#23912)
...as a warning, since we didn't check this correctly in Swift 5.
For parseable interfaces, these typealiases won't work at all.
2019-04-10 09:21:44 -07:00
John McCall
ae6561c32c Checks for implementation-only imports in @inlinable code.
Part of rdar://48991061
2019-04-05 16:30:25 -04:00
Slava Pestov
a2049972ca AST: Add ModuleDecl::isResilient()
This cleans up some code. I'm keeping the ResilienceStrategy enum around
though, in case we want to use it to version the ABI in the future.
2019-03-26 18:42:59 -04:00
Slava Pestov
bd6490b391 SIL: Introduce '@_alwaysEmitIntoClient' attribute for use by standard library
This is like '@inlinable', except that the symbol does not have a public
entry point in the generated binary at all; it is deserialized and a copy
is always emitted into the client binary, with shared linkage.

Just like '@inlinable', if you apply this to an internal declaration it
becomes '@usableFromInline' automatically.

This uses the same mechanism as default arguments ever since Swift 4, so
it should work reasonably well, but there are rough edges with diagnostics
and such. Don't use this if you are not the standard library.

Fixes <rdar://problem/33767512>, <https://bugs.swift.org/browse/SR-5646>.
2019-02-18 17:10:57 -05:00
Harlan Haskins
1abeeb8b92 [Sema] Diagnose internal(set) from @inlinable functions
This patch mainly consolidates the functions used to check accessors vs.
other decls, and makes sure we check setter access as well as regular
decl access.

rdar://45217648
2018-12-07 12:46:19 -08:00
Slava Pestov
3e864b26aa AST: @inline(__always) no longer implies @inlinable
Fixes <rdar://problem/44657000>.
2018-11-12 21:00:15 -05:00
Arnold Schwaighofer
c158106329 Allow dynamic without @objc in -swift-version 5
Dynamic functions will allow replacement of their implementation at
runtime.
2018-11-06 09:53:21 -08:00
Harlan Haskins
5f9ff021d4 [Sema] Allow non-public static property initializers in @_fixed_layout types
Static properties are not subject to the same restrictions as properties
whose initializers are exposed to clients in @_fixed_layout types.

rdar://45080912
2018-10-08 18:02:30 -07:00
Jordan Rose
df1ef05c4c Remove unwanted exceptions from @inlinable checking
- Treat protocol requirements just like associated types in how they
  inherit usable-from-inline-ness, rather than special-casing them in
  the check for inlinable code.

- Enum elements are already treated this way, so we can remove a
  redundant check for that.

- Finally, start enforcing that 'dynamic' declarations need to be
  '@usableFromInline' to be used in inlinable functions...in Swift 5
  mode or resilient code. I didn't even add a warning in Swift 4/4.2
  because it was illegal to use '@usableFromInline' on a 'dynamic'
  declaration in Swift 4.2. (Oops.)
2018-10-05 12:21:01 -07:00
Slava Pestov
2b75d7f1b0 Sema: Don't complain about internal type aliases referenced from @inlinable functions in Swift < 4.2 2018-06-25 17:00:47 -07:00
Slava Pestov
b137b08822 Sema: Type aliases referenced from @inlinable functions must be public or @usableFromInline
This is a source breaking change, so emit a warning in Swift 4 mode,
and an error in Swift 5 mode.
2018-06-15 14:16:05 -07:00
Slava Pestov
96901f1983 SE-0193: Public default argument generators cannot reference declarations that are @usableFromInline 2018-04-05 16:56:34 -07:00
Slava Pestov
4af8ff6872 Sema: TypeChecker::getFragileFunctionKind() returns if @usableFromInline references are allowed
For now, always true, soon, default arguments of public functions
will return false here.
2018-04-05 16:47:26 -07:00
Slava Pestov
b280f61f83 Sema: Resilience diagnostics pre-compute a few things 2018-04-05 16:42:45 -07:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Slava Pestov
a293790e7d Fix a few unused variable warnings 2018-01-14 21:39:53 -08:00
John McCall
52bb547a7e Merge pull request #13866 from rjmccall/accessor-decl
Split AccessorDecl out from FuncDecl.  NFC.
2018-01-12 17:02:35 -05:00
John McCall
7f0f8830cd Split AccessorDecl out from FuncDecl. NFC.
This has three principal advantages:

- It gives some additional type-safety when working
  with known accessors.

- It makes it significantly easier to test whether a declaration
  is an accessor and encourages the use of a common idiom.

- It saves a small amount of memory in both FuncDecl and its
  serialized form.
2018-01-12 14:20:27 -05:00
Slava Pestov
6ac2dde97e Sema: Enforce inlinable restrictions for stored property initializers in fixed layout types
We want stored property initializers of fixed layout structs to be
inlinable, so that inlinable initializers can be fully eliminated
by the optimizer.

This is the first step in fixing <rdar://problem/36454839>.
2018-01-12 00:06:03 -08:00