Commit Graph

56 Commits

Author SHA1 Message Date
Slava Pestov
e342a38b87 Sema: Convert TypeChecker::computeCaptures() into two requests
We now compute captures of functions and default arguments
lazily, instead of as a side effect of primary file checking.

Captures of closures are computed as part of the enclosing
context, not lazily, because the type checking of a single
closure body is not lazy.

This fixes a specific issue with the `-experimental-skip-*` flags,
where functions declared after a top-level `guard` statement are
considered to have local captures, but nothing was forcing these
captures to be computed.

Fixes rdar://problem/125981663.
2024-04-20 22:16:25 -04:00
Rick van Voorden
f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -08:00
Anthony Latsis
b7fcd2f434 Diag: Reword some errors for invalid usage of covariant Self 2021-03-12 15:54:21 +03:00
Anthony Latsis
06ceb19ccf Diag: Fix inaccuracy in dynamic_self_invalid 2021-02-11 03:22:20 +03:00
Luciano Almeida
3e386550bf Merge pull request #34883 from LucianoPAlmeida/SR-13899-coerce-to-checked
[SR-13899] [Sema] Adjustments on coerce to checked cast diagnostics
2020-12-01 21:30:28 -03:00
Luciano Almeida
c29fbb5528 [test] Adjusting test files where class syntax is used for protocol inheritance 2020-11-29 21:43:50 -03:00
Luciano Almeida
19727f2cc2 [Sema][test] Adjusting all missing downcast diagnostics failures 2020-11-28 18:17:18 -03:00
Pavel Yaskevich
f09b07be6c [Diagnostics] Extend use of argument mismatch fix to autoclosure parameters
When it comes to `@autoclosure` parameters we only detect and diagnose
mismatches related to invalid implicit conversions to pointer types. But
`@autoclosure` parameters just like regular ones can have type mismatches
as well which can be handled via recently introduced
`argument-to-parameter mismatch` fix.
2019-10-10 13:33:23 -07:00
Pavel Yaskevich
ec6a874ac8 [TypeChecker] NFC: Update test-cases improved by new missing arguments diagnostic 2019-09-25 10:47:26 -07:00
Slava Pestov
d3a4f3ded0 Sema: Diagnose captures of dynamic 'Self' type from default argument expressions
We could actually allow this for local functions, but it's not
worth implementing that until the more general ability of local
function default arguments to capture values is implemented.

Fixes <rdar://problem/55119566>.
2019-09-18 14:26:01 -04:00
Slava Pestov
bf8e50c3f5 Sema: Consolidate diagnostics for invalid usage of DynamicSelfType 2019-06-26 01:12:27 -04:00
John Holdsworth
b158163d71 Update tests 2019-06-26 01:10:11 -04:00
Sam Lazarus
de7851b0e9 Test: Update tests to reflect change to generic mismatch note locations 2019-06-14 12:35:32 -04:00
Sam Lazarus
81dc5460c9 Sema / Test: Fix tests broken by introduction of GenericArgumentsMismatchFailure
Additionally, fixed a crash caused by the change relating to opaque types.
2019-06-14 12:35:31 -04:00
Slava Pestov
744850749b Sema: Fix 'for ... in ...' over a sequence of DynamicSelfType 2019-05-15 14:34:10 -07:00
Slava Pestov
1889d5aa45 Sema: Correctly simplify member types with a DynamicSelfType base
Fixes <https://bugs.swift.org/browse/SR-10434>, <rdar://problem/49779783>.
2019-04-12 01:00:19 -04:00
John Holdsworth
dbe99d771e Make Self available to member functions (SE-0068?) (#22863)
* Make Self available to instance member functions (SE-0068?)

* Works for value types and static functions.

* Further experiments with TypeExpr

* Move Self processing off diagnostic path

* diagnostic instead of assertion fail

* TypeExpr of DynamicSelfType now working.

* Update tests for availability of Self

* Cast to Self fixed!

* Self not available as type in classes except for return type

* Could it be this simple?

* Nearly there

* Fix function decls using Self inside methods.

* Fix validation-test/compiler_crashers_2_fixed/0164-sr7989.swift

* Fix of ./validation-test/compiler_crashers_2_fixed/0179-rdar44963974.swift

* "Un-fix" validation-test/compiler_crashers_2_fixed/0164-sr7989.swift

* CHANGELOG entry

* Update CHANGELOG.md

Co-Authored-By: johnno1962 <github@johnholdsworth.com>

* Update CHANGELOG.md
2019-03-15 23:23:19 -04:00
Slava Pestov
feb140129c Sema: Add test for operator returning Self
We didn't have a test for it, as I discovered while adding some assertions.
2019-02-07 23:46:31 -05:00
David Zarzycki
995dec5d82 [Sema] Error if ObjC interop is needed when disabled 2018-05-07 14:43:04 -04:00
John McCall
7815892a76 Add unique typo corrections to the main diagnostic with a fix-it.
Continue to emit notes for the candidates, but use different text.
Note that we can emit a typo correction fix-it even if there are
multiple candidates with the same name.

Also, disable typo correction in the migrator, since the operation
is quite expensive, the notes are never presented to the user, and
the fix-its can interfere with the migrator's own edits.

Our general guidance is that fix-its should be added on the main
diagnostic only when the fix-it is highly likely to be correct.
The exact threshold is debateable.  Typo correction is certainly
capable of making mistakes, but most of its edits are right, and
when it's wrong it's usually obviously wrong.  On balance, I think
this is the right thing to do.  For what it's worth, it's also
what we do in Clang.
2018-04-07 16:01:39 -04:00
Mark Lacey
4c682d52c8 Support functions that return an IUO Self.
We were only looking through plain Optional TypeReprs in decl
checking.
2018-01-11 23:15:03 -08:00
Slava Pestov
6f6974654a Sema: Fix type safety hole with convenience initializers which delegate to protocol extension initializers
A protocol extension initializer creates a new instance of the
static type of Self at the call site.

However a convenience initializer in a class is expected to
initialize an instance of the dynamic type of the 'self' value,
because convenience initializers can be inherited by subclasses.

This means that when a convenience initializer delegates to a
protocol extension initializer, we have to substitute the
'Self' type in the protocol extension generic signature with
the DynamicSelfType, and not the static type.

Since the substitution is formed from the type of the 'self'
parameter in the class convenience initializer, the solution is
to change the type of 'self' in a class convenience initializer
to DynamicSelfType, just like we do for methods that return
'Self'.

This fixes cases where we allowed code to type check that
should not type check (if the protocol extension initializer
has 'Self' in contravariant position, and we pass in an
instance of the static type).

It also fixes a miscompile with valid code -- if the protocol
extension initializer was implemented by calling 'Self()',
it would again use the static type and not the dynamic type.

Note that the SILGen change is necessary because Sema now creates
CovariantReturnExprs that convert a static class type to
DynamicSelfType, but the latter lowers to the former at the
SIL level, so we have to peephole away unnecessary unchecked_ref_cast
instructions in this case.

Because this change breaks source compatibility, it is guarded
by a '-swift-version 5' check.
2017-08-26 01:18:35 -07:00
Slava Pestov
07c189558c Sema: Improve the fixit for 'Self' in invalid places
In an extension of a nested type, the extended type must be
fully qualified.

Also clean up the diagnostic logic a little bit and centralize
it in diagnoseUnknownType().

Fixes <https://bugs.swift.org/browse/SR-4379>.
2017-04-23 01:10:40 -07:00
Slava Pestov
700be61438 Sema: Fix crash with metatype construction -vs- dynamic Self
Fixes <rdar://problem/31297864>.
2017-04-17 19:57:12 -07:00
Slava Pestov
7e1bc3c980 AST: Fix calls to protocol extension methods from class methods returning 'Self'
Here, the 'self' value has a dynamic Self type, which we must strip
off when performing the conformance lookup.

Fixes <https://bugs.swift.org/browse/SR-2696>.
2017-01-03 20:13:43 -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
Robert Widmann
4f465224ea Polish off uses of dynamicType in tests 2016-07-29 16:59:14 -07:00
Robert Widmann
38df1c0537 Include parent contexts in search for protocol extension contexts.
The previous check excluded nested functions that took and returned
static Self.  e.g.

protocol P {}
extension P {
  func foo() -> Self {
    func bar() -> Self {
      return self
    }
    return bar()
  }
}

Instead, search up the decl context chain until we find a protocol
extension context.
2016-07-07 10:23:00 -07:00
John McCall
3fc2291733 Add basic typo correction for unqualified lookup.
There's a lot of room for better QoI / performance here.
2016-05-20 11:04:58 -07: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
David Farler
93b6962478 Warn when using 'var' bindings in function parameters
These will no longer be allowed in a future Swift release.

rdar://problem/23172698
2015-11-03 17:24:20 -08:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
Chris Lattner
1a0a0315fe wordsmith a diagnostic, NFC otherwise.
Swift SVN r30731
2015-07-28 23:35:25 +00:00
Chris Lattner
922a7f53b3 consolidate the diagnostics produced by the "Failure" case and the expr walker in CSDiags to
get the same wording, fixing <rdar://problem/21964599> Different diagnostics for the same issue

While I'm in the area, remove some dead code.



Swift SVN r30713
2015-07-28 04:43:37 +00:00
Chris Lattner
06cc05daa9 reword a diagnostic, as suggested by Jordan
Swift SVN r30712
2015-07-28 04:03:25 +00:00
Chris Lattner
8c3e62d7c5 Provide contextually sensitive conversion failure messages for situations in
which we have a contextual type that was the failure reason.  These are a bit
longer but also more explicit than the previous diagnostics.



Swift SVN r30669
2015-07-26 23:06:40 +00:00
Chris Lattner
eb01d5de9d word-smith a diagnostic in preparation for it becoming a family.
Swift SVN r30668
2015-07-26 22:42:32 +00:00
Chris Lattner
a8d9aec957 revert r30641, it wasn't correct. We produce better diagnostics for the
cases I was worried about anyway now.


Swift SVN r30651
2015-07-26 05:33:33 +00:00
Chris Lattner
babcbe0c76 fix a bug in the type checker handling references to instance methods with metatype
bases.  Consider this example (reduced from NameBinding/name_lookup.swift):

class ThisBase1 {
  func baseFunc0() {}
}

class ThisDerived1 : ThisBase1 {
  class func staticTestSelf1(a : ThisBase1) {
    let x = self.baseFunc0
    x(a)()
 }
}

The type checker was incorrectly blasting over the self type of the 'self.baseFunc0'
reference, giving 'x' a type of "ThisDerived -> () -> ()" instead of the correct
type of "ThisBase -> () -> ()" and rejecting the testcase.

I'm not confident that this is the right fix, review greatly appreciated!




Swift SVN r30641
2015-07-25 23:27:43 +00:00
Chris Lattner
f25e14ecc9 fix <rdar://problem/14096697> QoI: Diagnostics for trying to return values from void functions
by propagating the 'is return expr' bit more carefully in sequence folding, and by
adding another path for handling the return diagnostics better.

This probably improves a number of cases where we complain about "this argument list 
is invalid" when the call is in the context of a return.



Swift SVN r29565
2015-06-23 05:53:24 +00:00
Joe Groff
d7b9ae72aa Sema: Require '.init' when constructing from a dynamic metatype.
This makes it clearer that expressions like "foo.myType.init()" are creating new objects, instead of invoking a weird-looking method. The last part of rdar://problem/21375845.

Swift SVN r29375
2015-06-14 19:50:06 +00:00
Chris Lattner
e4b6afb9ae Start moving the testsuite to the "_ = foo()" idiom for evaluating an
expression but ignoring its value.  This is the right canonical way to do
this.  NFC, just testsuite changes.



Swift SVN r28638
2015-05-15 20:15:54 +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
Chris Lattner
e31b915f54 fix <rdar://problem/20800015> Fix error message for invalid if-let
Emitting an error message about a pattern the user didn't write isn't awesome,
complain about the type requirements of an if/let binding specifically.


Swift SVN r28119
2015-05-04 16:36:13 +00:00
Chris Lattner
31c01eab73 Change the meaning of "if let x = foo()" back to Xcode 6.4 semantics. The compiler
includes a number of QoI things to help people write the correct code.  I will commit
the testcase for it as the next patch.

The bulk of this patch is moving the stdlib, testsuite and validation testsuite to
the new syntax.  I moved a few uses of "as" patterns back to as? expressions in the 
stdlib as well.



Swift SVN r27959
2015-04-30 04:38:13 +00:00
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Joe Pamer
0562411bb2 Improve support for diagnosing errors that result from contextual or conversion type mismatches. Doing so allows us to improve our diagnostics for a few important cases:
- Situations where the type of a return statement's result expression doesn't line up with the function's type annotation.
- Situations where the type of an initializer expression doesn't line up with its declaration's type pattern.
- Situations where we assume a conversion to a built-in protocol must take place, such as in if-statement conditionals.

(Addresses rdar://problem/19224776, rdar://problem/19422107, rdar://problem/19422156, rdar://problem/19547806 and lots of other dupes.)

Swift SVN r24853
2015-01-30 19:32:20 +00:00
Chris Willmore
6c21a6414a <rdar://problem/19421148> Calling init with a missing label doesn't provide a descriptive error when overloaded inits differ only by label
Swift SVN r24624
2015-01-22 01:12:45 +00:00
Doug Gregor
b6e3e1d97e Improve the diagnostic when Self is used in an unsupported position.
"class method" != "method in a class". Fixes rdar://problem/18111507.

Swift SVN r24558
2015-01-20 19:27:35 +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