Commit Graph

27 Commits

Author SHA1 Message Date
Anthony Latsis
d113ca6bd2 CSApply: Fix several issues with non-failable to failable/throwing initializer delegation/chaining diagnostics
* Delegated-to `Optional` ctors were always handled as if they were failable, resulting in false-positive delegation errors.
* Delegations via `try?` were not diagnosed if the called ctor had IUO failability on top of being `throws`. SILGen would then handle the `try?` as if it was a `try!`.
* Delegations via `try?` were diagnosed with the wrong message if the `try?` was nested inside a `try!`, e.g. `try! try self.init(nonFailable:)`
* If there are issues with both `try?` and failability of the called initializer, diagnose both.
2022-06-01 00:11:45 +03:00
Pavel Yaskevich
202234f325 [CSDiagnostics] Diagnose invalid optional unwrap via fixes
Detect and fix situations when (force) unwrap is used on
a non-optional type, this helps to diagnose invalid unwraps
precisely and provide fix-its.

Resolves: [SR-8977](https://bugs.swift.org/browse/SR-8977)
Resolves: rdar://problem/45218255
2018-12-05 08:10:07 -08:00
Doug Gregor
945c09b1cc [Type checker] Improve diagnostics when an optional value is not unwrapped.
When we determine that an optional value needs to be unwrapped to make
an expression type check, use notes to provide several different
Fix-It options (with descriptions) rather than always pushing users
toward '!'. Specifically, the errors + Fix-Its now looks like this:

    error: value of optional type 'X?' must be unwrapped to a value of
        type 'X'
      f(x)
        ^
    note: coalesce using '??' to provide a default when the optional
        value contains 'nil'
      f(x)
        ^
          ?? <#default value#>
    note: force-unwrap using '!' to abort execution if the optional
        value contains 'nil'
      f(x)
         ^
         !

Fixes rdar://problem/42081852.
2018-07-13 11:02:04 -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
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
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Doug Gregor
86b7322e87 [Sema -> AST] Refactor "is representable in Objective-C?" checking.
Migrate the check for whether a given type is representable in
Objective-C, which is currently used to verify when @objc can be
inferred or verify that an explicitly-written @objc is well-formed,
from Sema into a set of queries on the Type within the AST library, so
it can be used in other parts of the compiler.

As part of this refactoring, clean up and improve a number of aspects
of this code:

* Unify the "trivially representable" and "representable" code paths
  into a single code path that covers these cases. Clarify the
  different levels of "representable" we have in both the code and
  in comments.

* Distinguish between representation in C vs. representation in
  Objective-C. While we aren't using this now, I'm anticipating it
  being useful to allow exporting C interfaces via @_cdecl (or
  similar).

* Eliminate the special cases for bridging String/Array/Dictionary/Set
  with their Foundation counterparts; we now consult
  _ObjectiveCBridgeable conformances exclusively to get this
  information.

* Cache foreign-representation information on the ASTContext in a
  manner that will let us more easily get the right answer across
  different contexts while providing more sharing than the TypeChecker
  version.

Annoyingly, this only seemed to fix a small class of error where we
were permitting Unsafe(Mutable)Pointer<T> to be representable in
Objective-C when T was representable but not trivially representable,
e.g., T=String or T=AnyObject.Type.
2016-03-16 23:53:48 -07:00
Chris Lattner
f8c6e46e03 remove the Failure::IsNotOptional failure mode, as CSDiags does a better job
of providing contextual diagnostics (e.g. producing the warning in 
Constraints/dynamic_lookup.swift).  This drops a specific diagnostic about
force casting the result of as! which was added in the Swift 1.2 timeframe
to explain the change in cast semantics.  Now that as! has been around for
a long time, it is more confusing than helpful.



Swift SVN r31887
2015-09-11 04:40:13 +00:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
Jordan Rose
3524fdaccd Fix placement of '!' in fix-it for chaining to a failable initializer.
Swift SVN r30858
2015-07-31 18:41:06 +00:00
Doug Gregor
82ea28943e Add some tests for forcing self.init/super.init calls to failable initializers.
Swift SVN r30274
2015-07-16 21:30:41 +00:00
Doug Gregor
90e5fbad47 Start allowing self.init/super.init calls to be forced with '!'.
Adjust our placement of RebindSelfInConstructorExpr to fall outside of
a force-value expression (!), allowing, e.g.,

  self.init(somethingThatCanFail: x)!

and

  super.init(somehtingThatCanFail: x)!

Start suggesting '!' when chaining/delegating to a failable
initializer from a non-failable one, in addition to suggesting that
the enclosing initializer become failable.

Note that DI cannot handle this yet, so this is only the Sema part of
rdar://problem/18497407.

Swift SVN r30219
2015-07-15 17:35:11 +00:00
Chris Lattner
7059871abf Convert some 'var' bindings to 'let' when they are not mutated, some
var/let bindings to _ when they are never used, and use some values that
are only written.  This is a testsuite cleanup, NFC. More to come.


Swift SVN r28406
2015-05-11 00:20:55 +00:00
Doug Gregor
2bf69a0ea0 Require witnesses for @objc requirements to be @objc.
Previously, we attempted to infer @objc-ness based on conformance, but
doing so is fraught with ordering dependencies, and just doesn't work
in the general case. Among other crimes, this allowed us to
retroactively mark a non-@objc method from an imported module as
@objc... even though nobody would ever then emit the @objc entry
points for it.

Fixes the rest of rdar://problem/18383574.

Swift SVN r24831
2015-01-29 22:53:53 +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
Doug Gregor
c1c51b4e8b Move checking of non-failable-to-failable initializer calls into constraint application.
This makes sure we get the same checking for initializer delegation in
structs/enums as we do for classes, fixing rdar://problem/18458622.

Swift SVN r23128
2014-11-06 06:29:35 +00:00
Doug Gregor
92aae25a7b Allow a failabile initializer spelled init! to satisfy a non-failable initializer requirement.
This is useful when making an Objective-C class conform to a Swift
protocol. Note that we can only implement this safely for non-@objc
protocols, where we have a witnes function that can perform the
checked unwrapping.

Swift SVN r21782
2014-09-08 19:58:36 +00:00
Doug Gregor
ebf4c1e5ca Allow "return nil" in a failable initializer to initiate failure.
Basic semantic analysis only; AST to follow.

Swift SVN r21309
2014-08-20 17:22:34 +00:00
Doug Gregor
38bc16f698 A non-failable initializer requirement cannot be satisfied by a failable initializer.
Swift SVN r21308
2014-08-20 16:34:41 +00:00
Doug Gregor
9c26f899cb QoI: Provide a specific diagnostic when a failable initializer overrides a non-failable one.
Swift SVN r21299
2014-08-19 23:22:04 +00:00
Doug Gregor
ce9ce59e97 Diagnose delegating from a non-failable initializer to a failable one.
Swift SVN r21298
2014-08-19 22:34:28 +00:00
Doug Gregor
a1d27734f3 Diagnose attempts to chain to a failable superclass initializer from a non-failable one.
Only failable initializers spelled with "?" trigger the error; for the
"!" case, we will trap if the error occurs.




Swift SVN r21296
2014-08-19 22:04:43 +00:00
Doug Gregor
9be2802761 Calls to failable initializers produce the appropriate optional type.
Swift SVN r21295
2014-08-19 21:08:17 +00:00
Doug Gregor
42dc448e7e Reflect failability of an initializer in the optionality of its result type.
Don't verify this for the Optional and ImplicitlyUnwrappedOptional types.


Swift SVN r21293
2014-08-19 20:42:06 +00:00
Doug Gregor
c06e22d406 Revert r21291; it's breaking the standard library build.
Swift SVN r21292
2014-08-19 20:37:05 +00:00
Doug Gregor
e6f051fcd2 Reflect failability of an initializer in the optionality of its result type.
Swift SVN r21291
2014-08-19 20:22:25 +00:00
Doug Gregor
64bd844e0e Parse failable initializers.
Parsing, representation, (de-)serialization, printing, and dumping for
failable initializers.


Swift SVN r21290
2014-08-19 18:41:30 +00:00