Commit Graph

370 Commits

Author SHA1 Message Date
Suyash Srijan
15d2b4c2d4 [ast] [diag] [sil] add note for unreachable function body 2018-11-17 01:35:16 +00:00
Andrew Trick
2ecb48a89d Remove exclusivity support for Swift 3 mode.
Remove the compiler support for exclusivity warnings.

Leave runtime support for exclusivity warnings in non-release builds
only for unit testing convenience.

Remove a test case that checked the warning log output.

Modify test cases that relied on successful compilation in the
presence of exclusivity violations.

Fixes: <rdar://problem/45146046> Remaining -swift-version 3 tests for exclusivity
2018-10-12 09:08:42 -07:00
John McCall
b80618fc80 Replace materializeForSet with the modify coroutine.
Most of this patch is just removing special cases for materializeForSet
or other fairly mechanical replacements.  Unfortunately, the rest is
still a fairly big change, and not one that can be easily split apart
because of the quite reasonable reliance on metaprogramming throughout
the compiler.  And, of course, there are a bunch of test updates that
have to be sync'ed with the actual change to code-generation.

This is SR-7134.
2018-08-27 03:24:43 -04: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
Ravi Kandhadai
895c1f0355 [DefiniteInitialization] Check whether globals captured by top-level
defer statements are initialized.

<rdar://30720636>
2018-07-02 11:40:21 -07:00
Ravi Kandhadai
193407bf23 [SIL Diagnostics] Warn when assigning an integer literal to a
variable of floating-point type results in loss of precision.
<rdar://15224041>
2018-06-21 10:56:19 -07:00
Ravi Kandhadai
addae9bae4 [SIL Diagnostics] Add warnings for imprecision during assignment of
floating-point literals to variables of floating-point type.
2018-06-18 12:02:23 -07:00
Ravi Kandhadai
9846342444 [SIL Diagnostics & Tests] Re-add diagnostics for detecting invalid conversions from
floating point literals to integers (<rdar://39730762>).

Add test cases for checking the correctness of the diagnostics.
Contains tests specific to x86 and non-x86 architectures.
2018-05-15 18:08:08 -07:00
Ravi Kandhadai
95ff425389 Revert "[Diagnostics & Tests] Add diagnostics for detecting invalid conversions form Floating point to integers"
This reverts commit 957dc37615.
Reason: Float80 doesn't exist on all non-x86 architectures.
2018-05-14 11:10:08 -07:00
Ravi Kandhadai
957dc37615 [Diagnostics & Tests] Add diagnostics for detecting invalid conversions from
floating point literals to integers (<rdar://39730762>).

Add test cases for checking the correctness of the diagnostics.
2018-05-08 15:30:04 -07:00
Joe Groff
62771a0c9d stdlib: Add withUnsafeBytes(of:) and withUnsafePointer(to:) for immutable arguments.
Since the functions produce pointers with tightly-scoped lifetimes there's no formal reason these have to only work on `inout` things.  Now that arguments can be +0, we can even do this without copying values that we already have at +0.
2018-04-18 09:13:45 -07:00
Robert Widmann
5c7b79072b Detect and diagnose infinitely-recursive code
Add a new warning that detects when a function will call itself
recursively on all code paths.  Attempts to invoke functions like this
may cause unbounded stack growth at least or undefined behavior in the
worst cases.

The detection code is implemented as DFS for a reachable exit path in
a given SILFunction.
2018-02-26 16:27:32 -05:00
Slava Pestov
f5f715f8ff SILGen: Support multiple-entry case blocks with address-only bindings
Fixes <rdar://problem/30870493>, <https://bugs.swift.org/browse/SR-4163>.
2018-01-17 21:02:08 -08:00
Pavel Yaskevich
7034ef92b6 Revert "Stricter enforcement of the "large space" heuristic" 2018-01-14 19:37:44 -08:00
Robert Widmann
43f06afef2 Defer analysis of large switch bodies
This presents a regression in diagnostic quality that is definitely
worth it not to lie to SILGen about whether a switch is covered or not.

At the same time, disable SIL’s unreachable diagnostic for ‘default’
clauses which would previously cause a warning to be emitted if the
default was proven to be unreachable.  This analysis is incomplete
anyways and can be done by Sema in the future if we desire.
2017-12-13 16:22:48 -05:00
Jordan Rose
8f8f00012a Merge pull request #12834 from jrose-apple/restrict-cross-module-struct-initializers-2
Implementation of SE-0189 "Restrict cross-module struct initializers to be delegating"

rdar://problem/34777878
2017-11-30 13:32:45 -08:00
Devin Coughlin
cf9a09e18d [Exclusivity] Diagnose when noescape closure is passed via reabstraction thunk
This fixes a serious false negative in static exclusivity enforcement when
a noescape closure performs an access that conflicts with an in-progress access
but is not reported because the closure is passed via a reabstraction thunk.

When diagnosing at a call site, the enforcement now looks through partial
applies that are passed as noescape arguments. If the partial apply takes
an argument that is itself a noescape partial apply, it recursively checks
the captures for that partial apply for conflicts and diagnoses if it finds one.

This means, for example, that the compiler will now diagnose when there is a
conflict involving a closure with a concrete return type that is passed to a
function that expects a closure returning a generic type. To preserve source
compatibility these diagnostics are emitted as warnings for now. The intent is
that they will be upgraded to errors in a future version of Swift.

rdar://problem/35215926, SR-6103
2017-11-16 16:01:08 -08:00
Jordan Rose
1f12e72071 DI: Suggest "self.init()" for extension initializers of C structs
...unless the struct contains a field that cannot be zero-initialized,
such as a non-nullable pointer.

This suggestion is only made for C structs because 'init()' may not be
the right choice for other structs.
2017-11-09 14:28:38 -08:00
Jordan Rose
1598a21e43 DI: Warn on non-delegating cross-module struct initializers
...as detected by initializing an individual field without having
initialized the whole object (via `self = value`).

This only applies in pre-Swift-5 mode because the next commit will
treat all cross-module struct initializers as delegating in Swift 5.
2017-11-09 11:24:28 -08:00
swift-ci
b006d07342 Merge pull request #12294 from anemet/opt-remarks 2017-10-20 20:45:50 -07:00
Adam Nemet
9b9805420d Add optimization remarks
This allows reporting successful and unsuccessful optimizations similar to
clang/llvm.

This first patch adds support for the
options -Rpass=<pass-name-regex> -Rpass-missed=<pass-name-regex>.  These allow
reporting successful/unsuccessful optimization on the compiler output for passes
specified by the regex.  I've also added one missed and one passed remark type
to the inliner to test the infrastructure.

Clang also has the option of collecting these records in an external YAML data
file.  This will be added in a later patch.

A few notes:
* The goal is to use this facility for both user-lever "performance" warnings
and expert-level performance analysis.  There will probably be a flag in the
future differentiating the verbosity.

* The intent is match clang/llvm as much as it makes sense.  On the other hand I
did make some changes.  Unlike in llvm, the emitter is not a pass which
simplifies things.  Also the remark class hierarchy is greatly simplified since
we don't derive from DiagnosticInfo.  We also don't derive from Diagnostic to
support the streaming API for arbitrary named-value pairs.

* Currently function names are printed mangled which should be fixed.
2017-10-20 12:41:37 -07:00
Slava Pestov
e51556345d DI: Consolidate and clean up some diagnostics 2017-10-13 23:51:14 -07:00
Graydon Hoare
3eed841f51 [Diagnostics] s/may/must/ in 'may not be assigned to' 2017-09-29 16:09:23 -04:00
Graydon Hoare
d8094e52b4 [Diagnostics] s/may/must/ in 'may not be passed inout' 2017-09-29 16:09:23 -04:00
Graydon Hoare
324471a2bc [Diagnostics] s/may/must/ in 'may not fall through' 2017-09-29 16:09:23 -04:00
Devin Coughlin
6dcc8ce9b2 [Exclusivity] Don't suggest copying to a local when swapAt() is appropriate
Update static exclusivity diagnostics to not suggest copying to a local
when a call to swapAt() should be used instead. We already emit a FixIt in
this case, so don't suggest an an helpful fix in the diagnostic.

rdar://problem/32296784
2017-07-16 13:48:52 -07:00
Devin Coughlin
f6df5c79b2 [Exclusivity] Update static diagnostic text for "simultaneous" accesses
Remove the descriptive decl kind (since with subpaths it is not correct and
cannot represent a tuple element) and change "simultaneous" to "overlapping"
in order to lower the register slightly and avoid connoting threading.

For example, for the following:

   takesTwoInouts(&x.f, &x.f)

the diagnostic will change from

"simultaneous accesses to var 'x.f', but modification requires exclusive access;
consider copying to a local variable"

to

"overlapping accesses to 'x.f', but modification requires exclusive access;
consider copying to a local variable"
2017-06-13 20:08:48 -07:00
Devin Coughlin
d76ec6a8ff [Exclusivity] Relax enforcement for separate struct stored properties
Relax the static enforcement of exclusive access so that we no longer diagnose
on accesses to separate stored structs of the same property:

takesInout(&s.f1, &s.f2) // no-warning

And perform the analogous relaxation for tuple elements.

To do this, track for each begin_access the projection path from that
access and record the read and write-like modifications on a per-subpath
basis.

We still warn if the there are conflicting accesses on subpaths where one is
the prefix of another.

This commit leaves the diagnostic text in a not-so-good shape since we refer
to the DescriptiveDeclKind of the access even describing a subpath.

I'll fix that up in a later commit that changes only diagnostic text.

https://bugs.swift.org/browse/SR-5119
rdar://problem/31909639
2017-06-13 19:15:38 -07:00
Alex Hoppen
d922944af0 Merge pull request #9976 from ahoppen/pdm-diagnostics2
Migration to DeclBaseName in diagnostic definitions
2017-06-13 12:55:16 +02:00
Michael Gottesman
bf71ec8599 [silgen] Error nicely on multiple pattern cases with address only patterns.
We silently miscompiled previously the following code:

     protocol Gesture {}
     struct Foo {}
     struct Bar {}
     enum FooOrBar {
       case foo(Foo)
       case bar(Bar)
     }

     func main(_ f : FooOrBar) {
       switch f {
       case .foo(let data as Gesture),
            .bar(let data as Gesture):
         ...
       }
       ...
     }

This was because the multiple pattern per case code never implemented support
for address only types.

Now instead of miscompiling such programs, we do the following:

1. We emit an error.
2. When we construct the arguments for the named bindings, we just skip the
address only types. Everything else is normal.
3. In the case block, we use a SILUndef for the address only value.

This ensures that we preserve as many other diagnostics as possible.

rdar://32525952

P.S. As an additional benefit, this eliminates a source of address phi nodes
from SILGen.
2017-06-02 20:35:09 -07:00
Alex Hoppen
faa1720c48 [Diagnostics] Adjustments for DeclBaseName
Adjust the definition of some diagnostics that are already called with
DeclBaseNames so that the implicit conversion from DeclBaseName to
Identifier is no longer needed.

Adjust the call side of diagnostics which don't have to deal with
special names to pass an Identifier to the diagnostic.
2017-05-31 15:58:46 +02:00
Devin Coughlin
cedfa6d37c [Exclusivity] Update static diagnostic to suggest adding a local
Update the static diagnostics when enforcing exclusive access to suggest adding
a local variable. The new diagnostic text is:

  "simultaneous accesses to var 'a', but modification requires exclusive
  access; consider copying to a local variable"
2017-05-09 16:09:10 -07:00
Devin Coughlin
0777cccb71 [Exclusivity] Make static access conflict an error in Swift 4 mode.
When static enforcement of exclusive access is enabled, make access conflicts an
error in Swift 4 mode. They will remain a warning in Swift 3 mode.
2017-05-06 19:13:53 -07:00
Devin Coughlin
9cdff5fe00 [Exclusivity] Improve static enforcement diagnostics
Static diagnostics now refer to the identifier for the variable requiring
exclusive diagnostics. Additionally, when two accesses conflict we now always
emit the main diagnostic on the first modifying access and the note on either
the second modifying access or the read.

The diagnostics also now highlight the source range for the expression
beginning the access.
2017-04-23 11:39:25 -07:00
Devin Coughlin
4cb51f77fd [SILDiagnostics] Demote exclusivity violation error to warning.
Change the error emitted when diagnosing an exclusivity violation to an warning.
This will make it easier to evaluate on large projects while staging. The
intent it to turn it back into an error before the diagnosis is turned on
by default.
2017-04-14 16:26:50 -07:00
Devin Coughlin
bee17f7838 [SILDiagnostics] Add static enforcement of Law of Exclusivity (#8560)
Add a diagnostic pass that emits errors when a violation of the "Law of
Exclusivity" is detected at compile time. The Law of Exclusivity requires
that the access duration of any access to an address not overlap
with an access to the same address unless both accesses are reads.

This pass relies on 'begin_access' and 'end_access' SIL instruction
markers inserted by SILGen to determine when an access to an address begins and
ends. It models the in-progress accesses with a map from storage locations to
the counts of read and write-like accesses in progress for that location.
2017-04-09 21:41:13 -07:00
Xi Ge
b4cf37bf7d Sema: several improvements on missing switch cases diagnostics. (#8026)
1. Make sure the actions taken by fixits are reflected in diagnostics messages.
2. Issue missing cases diagnostics at the start of the switch statement instead of its end.
3. Use <#code#> instead of <#Code#> in the stub.
2017-03-10 19:32:37 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
practicalswift
cc852042c9 [gardening] Fix accidental trailing whitespace. 2016-10-29 10:22:58 +02:00
David Farler
d5f4ef7fc0 Don't suggest 'break' for empty guard bodies
That's not valid code.

rdar://problem/28944787
2016-10-25 14:51:36 -07:00
Tim Bodeit
9ce9dced91 [SILOptimizer] Fix let constant of non-class protocol being mutable
Extend the checks in `LifetimeChecker` in
`SILOptimizer/Mandatory/DefiniteInitialization.cpp`
to catch when the memory object corresponding to a let constant is used
as an inout parameter in a protocol witness method.

Initializing a let constant separate from its declaration requires write
access. Therefore it is treated as `@lvalue TestProtocol` in the AST for
a certain scope.
Checking that the constant is not written to after being initialized is
supposed to happen in the Mandatory SILOptimizer phase instead.

On loads, the Optimizer checks, that a variable is fully initialized,
but perviously did not validate what happens to it after it is loaded.
This allowed loading the memory object through an open_existential_addr
instruction and applying the result as an Operand to a mutating witness
method.
2016-09-01 22:01:23 +02:00
Robert Widmann
5a2479cb2a Expand the no-return diagnostic
Turn on the noreturn diagnostic for cases where a reachable unreachable
could be encountered.  Previously, the diagnostic would not fire if the
function was marked noreturn and any of its reachable unreachable calls
were around.  While this makes sense from a SILGen perspective (it Just
Crashes tm), it is still wrong.  We need to diagnose *everything* that
has reachable unreachables.
2016-08-18 20:39:24 -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
practicalswift
b704ff612a [gardening] Remove unused diagnostic: bridging_objcbridgeable_missing_conformance 2016-03-12 09:57:27 +01:00
Doug Gregor
725c610714 [SILGen] Use _ObjectiveCBridgeable._bridgeToObjectiveC for String -> NSString bridging.
Provide a general mechanism for bridging from a Swift value type to
its corresponding Objective-C class type through the
_bridgeToObjectiveC witness of the appropriate _ObjectiveCBridgeable
protocol conformance. Only enable this new code for bridging String ->
NSString and work through the issues that crop up.

We cannot actually *delete* the _convertStringtoNSString entrypoint
yet, because there is some code that is depending on it indirectly;
I'll address that separately as part of the continued generalization
of the _ObjectiveCBridgeable mechanism.
2016-03-11 20:04:34 -08:00
Max Moiseev
3a3984877a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-15 15:43:34 -08:00
Daniel Duan
5242b67476 [Sema] implement better type circularity check
Certain type circularities weren't being checked until this point. Such as

```
struct X<T> { let a: X<X> }
struct Y<T> { let a: (Int, Y<Y>) }
enum Z<T> { case A(Optional<Z<Z>>) }
```

We introduce a more comprehensive approach to detect these in type checker.
After name lookup, exam each value field in declared structs and enums for
self-reference types that creates inifinite sizes.
2016-02-07 22:10:42 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
Doug Gregor
f7407f6a4d [SILGen] SE-0022: emit SIL for #selector expressions.
This completes the core of the #selector feature, which can now be
used to reference the selectors of @objc methods and initializers.
2016-01-27 13:57:40 -08:00