Commit Graph

74 Commits

Author SHA1 Message Date
stzn
d429a90ebc [Diagnostics] A self argument implicitly passed as an inout parameter is diagnosed as used before initialized 2025-01-06 05:05:26 +09:00
Doug Gregor
ac850bf86f Expand the tuple "hack" for initializable values to local tuples
Back off treating local lets of tuple type as "initializable", expanding
on the narrow carve-out from #74133. Without this, we would reject local
lets of tuple type that are initialized piecemeal.

Fixes rdar://135028163.
2024-09-05 10:57:02 -07:00
Doug Gregor
6a8eabf676 Tweak "initializable values" logic slightly to address source incompatibilities
Back of slightly on when we treat a "let" instance property as immutable
within an initializer, to deal with two newly-introduced source
incompatibilities.

Fixes rdar://129253556.
2024-06-04 17:33:26 -07:00
Doug Gregor
39f4e38027 Diagnose inout uses of 'lets' in constructors in the type checker.
Stored `let` properties of a struct, class, or actor permit
'inout' modification within the constructor body after they have been
initialized. Tentatively remove this rule, only allowing such `let`
properties to be initialized (assigned to) and not treated as `inout`.

Fixes rdar://127258363.
2024-05-14 15:59:50 -07:00
Anthony Latsis
7fb62f6d9c Gardening: Migrate test suite to GH issues: SILOptimizer (1/2) 2022-09-04 09:35:41 +03:00
Nate Chandler
bc268ddf34 [Test] Specified copy-prop and lex-lt behavior.
In preparation for changing the default, explicitly specify the behavior
of all tests that are affected by the choice of behavior for lexical
lifetimes and copy-propagation.
2021-12-15 21:37:00 -08:00
Andrew Trick
369ecb68f6 Disable weak reference lifetime warnings by default.
Only issue weak lifetime warnings for users who select object lifetime
optimization. The risk of spurious warnings outweighs the benefits.

Although the warnings are generally useful regardless of the level of
optimization, it isn't really critical to issue them unless the optimizer
aggressively shrinks reference lifetimes.

Fixes rdar://79146338 Xcode warns that "referenced object is
deallocated here" but that object was passed into a method that causes
strong retention)
2021-06-15 20:57:39 -07:00
Erik Eckstein
b12b4e95b9 Don't crash in RawSILInstLowering if DI produced an error.
In case DI doesn't classify assign_by_wrapper (because of another error), make sure that RawSILInstLowering can handle this.

rdar://75433096
2021-03-16 13:28:52 +01:00
Erik Eckstein
a17f8c2f3f SILOptimizer: add a diagnostics pass to warn about lifetime issues with weak references.
The DiagnoseLifetimeIssuesPass pass prints a warning if an object is stored to a weak property (or is weakly captured) and destroyed before the property (or captured reference) is ever used again.
This can happen if the programmer relies on the lexical scope to keep an object alive, but copy-propagation can shrink the object's lifetime to its last use.
For example:

  func test() {
    let k = Klass()
      // k is deallocated immediately after the closure capture (a store_weak).
      functionWithClosure({ [weak k] in
                            // crash!
                            k!.foo()
                          })
    }

Unfortunately this pass can only catch simple cases, but it's better than nothing.

rdar://73910632
2021-02-15 11:11:35 +01:00
Michael Gottesman
46432404f3 [ownership] Remove dead option: enable-ownership-stripping-after-serialization.
We always lower ownership now after the diagnostic passes (what this option
actually controlled). So remove it.

NFC.
2020-06-16 10:52:02 -07:00
Ravi Kandhadai
4e5389dacb [Definite Initialization] Improve diagnostics for delegating, failable
initializers that do not initialize along all paths.

<rdar://problem/62562254>
2020-05-04 22:00:18 -07:00
Michael Gottesman
f10b45b540 [ownership] Add an extra run of -Onone tests with diagnostics with -enable-ownership-stripping-after-serialization enabled.
Right now the stdlib/overlays can compile against -Onone tests with or without
-enable-ownership-stripping-after-serialization. This will help me to prevent
other work going on from breaking these properties.
2019-10-26 15:12:14 -07:00
Ravi Kandhadai
8d1d863f43 [Definite Initialization] Fix a bug that made the DI wrongly think
that an unused access to "self" in a delegating initializer is a use
of the form: `type(of:self)`

<rdar://51198592>
2019-06-12 14:53:01 -07:00
Michael Gottesman
3ebd8df493 [gardening] Remove unnecessary -enable-sil-ownership from tests that now just get it from their pattern.
This just eliminates -enable-sil-ownership from all target-swift-frontend and
target-swift-emit-silgen RUN lines. Both of those now include
enable-sil-ownership in their expansion.
2019-03-12 20:39:18 -07:00
Slava Pestov
a5f9619062 DI: Lower AssignInst in a post-processing pass
Compiler passes that intermingle analysis with mutation of the CFG
are fraught with danger. The bug here was that a single AssignInst
could appear twice in DI's Uses list, once as a store use and once
as a load use.

When processing Uses, we would lower AssignInsts on the fly. We would
take care to erase the instruction pointer from the current Use, but
if a subsequent Use *also* referenced the now-deleted AssignInst, we
would crash.

Handle this in the standard way: instead of lowering assignments
right away, just build a list of assignments that we're planning on
lowering and process them all at the very end.

This has the added benefit of simplifying the code, because we no
longer have to work as hard to keep the state of the Uses list
consistent while lowering AssignInsts. The rest of DI should not
care that the lowering got postponed either, since it was already
expected to handle any ordering of elements in the Uses list, so
it could not assume that any particular AssignInst has been lowered.

Fixes <https://bugs.swift.org/browse/SR-9451>.
2018-12-10 00:03:08 -05:00
Andrew Trick
ba03bef27e Fix test cases for SILGen after removing critical edges. 2018-10-19 23:14:17 -07:00
Joe Groff
91bf80fdf4 Update tests based on review feedback. 2018-09-13 12:31:27 -07:00
Joe Groff
77a0923ca6 SILGen: Emit convenience initializers as allocating entry points.
And only dispatch designated inits by their allocating entry points. rdar://problem/29634243
2018-09-13 12:31:23 -07:00
Robert Widmann
e12fbc2aa2 Reject empty associated value lists
SE-0155 makes an empty associated value list in an enum element
declaration illegal.  Warn about this in legacy Swift mode, and reject
it otherwise.  This offers two fixes:
1) Remove the empty associated value list
2) Insert a 'Void' associated value
2018-08-24 10:50:23 -07:00
John McCall
b2f20c063f Fix and improve DI diagnostics around inout uses of immutable constants.
The only real bug here is that we were looking specifically for `apply`
instructions, so we failed to diagnose `try_apply` calls to mutating
throwing functions on immutable existentials.  Fixing this is a
source-compatibility break, but it's clearly in the "obvious bug" category
rather than something we need to emulate.  (I found this bug because DI
stopped diagnosing a modification of a property of a `let` existential
when it started being done with `modify`, which of course is called with
`begin_apply`.)
2018-08-20 02:51:44 -04:00
Slava Pestov
c7fff66c5d DI: Don't forget to check inout uses for try_apply too
Fixes <https://bugs.swift.org/browse/SR-8368>, <rdar://problem/42597950>.
2018-07-27 17:40:29 -07:00
Ben Cohen
345879429b [stdlib] Take several underscored stdlib functions private (#18134)
* Make _sanityCheck internal

* Make _debugPrecondition internal

* Make Optional._unsafelyUnwrappedUnchecked internal.

* Make _precondition internal

* Switch Foundation _sanityChecks to assertions

* Update file check tests

* Remove one more _debugPrecondition

* Update Optimization-with-check tests
2018-07-24 18:26:19 -07:00
Ravi Kandhadai
e35da021d8 [DefiniteInitialization] Detect overwriting of immutable optionals
through optional chaining.

<rdar://38624845>
SR-7226
2018-07-03 09:22:10 -07:00
Ravi Kandhadai
964397c9a8 Revert "[DefiniteInitialization] Fix the bug that allows overwriting immutable"
This reverts commit 82abf974f2 as it breaks
compatability of some existing benchmarks.

<rdar://41501480>
2018-06-27 11:45:58 -07:00
Ravi Kandhadai
82abf974f2 [DefiniteInitialization] Fix the bug that allows overwriting immutable
variables of optional type (SR-7226, rdar://38624845)
2018-06-22 14:55:21 -07:00
Joe Groff
711984234d Sema: Pass the useDC down to getTypeOfReference.
This fixes a subtle issue where, during single-expression closure type inference, we would ask for the settability of local variables from the outer function's context, leading us to mistakenly consider them mutable inside single-expression closure contexts. DI would catch some but not all violations of the expected semantics here.
2018-06-11 10:53:00 -07:00
David Zarzycki
e326954bd0 [Diag] QoI: Add ReferenceOwnership to DiagnosticArgumentKind
By formalizing ReferenceOwnership as a diagnostic argument kind, we get
less boilerplate, better type safety, better output consistency, and
last but not least: future proofing.
2018-04-20 08:32:43 -04:00
Hamish
48f08bdbc7 [Sema] Diagnose immediate deallocation of instances assigned to non-owning variables
When performing a binding/assignment to a weak or unowned variable/property from an initialiser call, emit a warning that the instance will be immediately deallocated.
2018-03-08 16:32:35 +00:00
Michael Gottesman
2914c6b0f5 [di] Once we have exclusively borrowed self, if we go down the non-formal evaluation path, copy instead of asserting.
We can only do this for two reasons:

1. There is a code path that should have gone through the non-exclusively
borrowed self entrypoints, but they were not implemented.
2. We are trying to access self for an argument.

By copying the value, we preserve invariants around ownership and also make it
easy for DI to catch 2 and not blow up in the case of 1. It is better to error
in DI incorrectly, than to hit an unreachable (especially since in non-assert
builds, we don't trap at unreachables and just continue to the next function in
the text segment).

SR-5682
rdar://35402738
2017-11-15 21:38:21 -08:00
Slava Pestov
4c9d736ea0 DI: Use new 'self initialized' analysis in LifetimeChecker::handleStoreUse() 2017-10-20 16:10:32 -07:00
Slava Pestov
b5eeae7446 DI: All enum initializers should be delegating
Again, since there's no distinction between an enum initializer that
delegates to 'self.init' from one that assigns to 'self', we can remove
the special handling of enum initializers in the 'root self' case.

Now, 'root self' is only used for designated initializers in classes
with no superclass, and struct initializers that perform memberwise
initialization of stored properties.

This regresses some diagnostics, because the logic for delegating
init diagnostics is missing some heuristics present in the root self
case. I will fix this in a subsequent patch.
2017-10-13 23:51:15 -07:00
Slava Pestov
0c16aedb60 DI: All protocol extension initializers should be delegating
Previously protocol extension initializers which called 'self.init' were
considered 'delegating', and ones that assign to 'self' were considered
'root'.

Both have the same SIL lowering so the distinction is not useful, and
removing it simplifies some code.
2017-10-13 23:51:14 -07:00
Slava Pestov
e51556345d DI: Consolidate and clean up some diagnostics 2017-10-13 23:51:14 -07:00
Graydon Hoare
d8094e52b4 [Diagnostics] s/may/must/ in 'may not be passed inout' 2017-09-29 16:09:23 -04:00
Michael Gottesman
4c087097f3 [di] Update definite_initialization for ownership.
rdar://31521023
2017-09-07 15:23:22 -07:00
Michael Gottesman
2cacbc6403 [di] Enable SIL Ownership verification on all definite init tests.
*NOTE* DefiniteInit is still running /after/ ownership is stripped. This is just
making sure that the code we are producing can actually pass the verifier.

rdar://31521023
2017-09-06 16:31:20 -07:00
Michael Gottesman
8492ef1185 [di] Expand a test to use non-trivial types.
This ensures that DI handles the more difficult non-trivial diagnostic case.

rdar://31521023
2017-09-05 09:32:22 -07:00
Michael Ilseman
a7b299b51b [tests] Update tests as String.CharacterView is deprecated 2017-08-11 12:32:39 -07:00
Slava Pestov
2119ab2782 Sema: A call of a closure literal is noescape
We cannot model a type variable bound to the ExtInfo of a function
type in the constraint solver, which means we have a hard time
propagating noescape-ness in some cases.

Fixes <rdar://problem/31910280> and <rdar://problem/32409133>.
2017-05-31 17:31:36 -07:00
Brent Royal-Gordon
b60b11a4f6 [SIL][DI] Add test for fixed diagnostics crash 2017-03-27 06:41:35 -07:00
Michael Gottesman
92cb1f3014 [di] Handle escapes from the new upcast+borrow+downcast pattern.
As a bonus, now DI properly errors on:

super.init(self)
2017-02-23 08:48:58 -08:00
swift-ci
e5b66c0a70 Merge pull request #6891 from adrian-prantl/cleanup 2017-02-06 12:10:45 -08:00
Adrian Prantl
4d1ae142c6 Remove the redundant DeclCtx field in SILFunction.
In all cases the DeclCtx field was supposed to be initialized from the
SILLocation of the function, so we can save one pointer per
SILFunction.

There is one test case change where a different (more precise)
diagnostic is being generated after this change.
2017-02-06 11:07:50 -08:00
Brian King
e4ca85f417 Fix test failures from a change in warning strings 2017-02-02 11:54:36 -05:00
Michael Gottesman
20dd563efb [semantic-arc] Update tests for qualified/unqualified ownership and SILGen emission of copy_value, destroy_value. 2016-10-29 20:11:09 -07:00
Mark Lacey
401ca24532 Emit a warning when optionals are coerced to Any.
Emit a warning for optionals that are implicitly converted to Any, and
add fixits giving options to:
- Add '??' with a default value after
- Force-unwrap the optional with '!'
- Explicitly cast to 'as Any' to silence the warning

This covers diagnostics aspect of SE-0140.

rdar://problem/28196843
2016-09-20 22:33:45 -07:00
Slava Pestov
960b8e9cd0 DI: New way of modeling factory initializers
Previously, we were only able to detect factory initializers
dispatched through class_method. This didn't work for
factory initializers defined in protocol extensions.

The end result would be that we would strong_release an
uninitialized class instance, which could cause crashes.

Fix DI to correctly release the old instance using
dealloc_partial_ref instead.

Fixes <rdar://problem/27713221>.
2016-08-13 01:51:45 -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
Alex Hoppen
2108a235df [Tests] Update existing tests for the upcoming implementation of SE-0036 2016-06-02 17:50:55 +02:00
Chris Lattner
8c3bcddb92 Merge pull request #2540 from nubbel/pr/sr-1469
[SilOptimizer] SR-1469: Issue diagnostic for `init?` at early return statement
2016-05-19 22:11:29 -07:00