Commit Graph

48 Commits

Author SHA1 Message Date
Pavel Yaskevich
4132aa04f9 [Tests] NFC: Update all of the test-cases improved by changes to generic argument mismatch handling 2025-06-03 00:49:06 -07:00
Anthony Latsis
14b70f306b DiagnosticVerifier: Default expected fix-it start line to the diagnostic's 2023-03-08 12:10:27 +03:00
Luciano Almeida
19727f2cc2 [Sema][test] Adjusting all missing downcast diagnostics failures 2020-11-28 18:17:18 -03:00
Pavel Yaskevich
fb0a484a08 [ConstraintSystem] Let try? infer type from sub-expression even with contextual mismatch
This helps to diagnose contextual mismatches like `Int? vs. Bool`
instead of suggesting to unwrap the optional which would still
produce an incorrect type.
2020-02-05 12:11:01 -08:00
Pavel Yaskevich
0103041a30 [ConstraintSystem] Tidy up restriction handling in contextual failures
Delay "fixing" contextual conversion failures until restriction is applied
this helps to tidy up logic for superclass and existential conversions.

Too bad we have to "fix" in `simplifyRestrictedConstraintImpl` now but
we can't really do much about that because diagnostics need both top-level
types to be useful.
2020-02-05 12:11:01 -08:00
Pavel Yaskevich
b79341538f [TypeChecker] NFC: Adjust test-cases improved by optional unwrap fix 2019-11-19 15:15:30 -08:00
Slava Pestov
48e89f40d7 Sema: Fix applying solution for OptionalTryExpr
In Swift 5 mode, CSGen generates a conversion constraint from
the type of OptionalTryExpr's subexpression, call it T, and
Optional<$tv> for a new type variable $tv.

When applying the solution, we would coerce the sub-expression
to T? if T was not optional, or T otherwise. This was wrong
because there's no reason that $tv is actually equal to T.

Instead, we must coerce the sub-expression to Optional<$tv>,
which is the type of the overall OptionalTryExpr.

Fixes <rdar://problem/46742002>.
2018-12-15 01:29:07 -05:00
BJ Homer
510cbd2d00 Rename try_swift4.swift back to try.swift.
It makes it clearer what has changed.
2018-11-14 13:34:30 -07:00
BJ Homer
9aaeaec61f Add test validating that we preserve existing Swift 4 'try?' behavior. 2018-11-08 00:12:38 -07:00
BJ Homer
1789d44d6c Don't consider fixups that won't do anything.
Since 'try?' no longer unconditionally adds a layer of optional, converting it
to 'try!' will no longer unconditionally remove a layer of optional. So let's not
suggest it. This leads to better diagnostics anyway.
2018-11-06 23:31:02 -07:00
BJ Homer
94229861e1 Add a bunch more 'try' tests.
Some of these are currently failing. For example:

```swift
// expected-error {{cannot convert value of type 'Int??' to specified type 'Int'}}
let _: Int = try? producer.produceDoubleOptionalInt()
```

This actually produces this warning:

```
value of optional type 'Int?' not unwrapped; did you mean to use 'try!' or chain with '?'?
```

Note that the value in question is not an `Int?`, so a suggestion of `try!` here is inappropriate.
(And, due to the change in semantics of 'try?', wouldn't change anything anyway.)
2018-11-06 23:31:02 -07:00
BJ Homer
ba03d126c0 Add more 'try?' tests. 2018-11-06 23:31:02 -07:00
BJ Homer
8b9ac0ad7f Run tests for the new 'try?' behavior with '-swift-version 5' 2018-11-06 23:31:02 -07:00
BJ Homer
3ae807d4bf Make 'try?' flatten optional chaining like optional-chaining does.
If the sub-expression of the 'try?' is optional, the result will be the same level of optional-ness.
If the sub-expression is non-optional, the result is optional.

Thus, the following lines all end up with the same type of 'Int?'
 - let x = try? 3 as Int
 - let x = try? 3 as? Int
 - let x = try? 3 as Int?
2018-11-06 23:31:02 -07:00
Brent Royal-Gordon
9bd1a26089 Implementation for SE-0228: Fix ExpressibleByStringInterpolation (#20214)
* [CodeCompletion] Restrict ancestor search to brace

This change allows ExprParentFinder to restrict certain searches for parents to just AST nodes within the nearest surrounding BraceStmt. In the string interpolation rework, BraceStmts can appear in new places in the AST; this keeps code completion from looking at irrelevant context.

NFC in this commit, but keeps code completion from crashing once TapExpr is introduced.

* Remove test relying on ExpressibleByStringInterpolation being deprecated

Since soon enough, it won’t be anymore.

* [AST] Introduce TapExpr

TapExpr allows a block of code to to be inserted between two expressions, accessing and potentially mutating the result of its subexpression before giving it to its parent expression. It’s roughly equivalent to this function:

  func _tap<T>(_ value: T, do body: (inout T) throws -> Void) rethrows -> T {
    var copy = value
    try body(&copy)
    return copy
  }

Except that it doesn’t use a closure, so no variables are captured and no call frame is (even notionally) added.

This commit does not include tests because nothing in it actually uses TapExpr yet. It will be used by string interpolation.

* SE-0228: Fix ExpressibleByStringInterpolation

This is the bulk of the implementation of the string interpolation rework. It includes a redesigned AST node, new parsing logic, new constraints and post-typechecking code generation, and new standard library types and members.

* [Sema] Rip out typeCheckExpressionShallow()

With new string interpolation in place, it is no longer used by anything in the compiler.

* [Sema] Diagnose invalid StringInterpolationProtocols

StringInterpolationProtocol informally requires conforming types to provide at least one method with the base name “appendInterpolation” with no (or a discardable) return value and visibility at least as broad as the conforming type’s. This change diagnoses an error when a conforming type does not have a method that meets those criteria.

* [Stdlib] Fix map(String.init) source break

Some users, including some in the source compatibility suite, accidentally used init(stringInterpolationSegment:) by writing code like `map(String.init)`. Now that these intializers have been removed, the remaining initializers often end up tying during overload resolution. This change adds several overloads of `String.init(describing:)` which will break these ties in cases where the compiler previously selected `String.init(stringInterpolationSegment:)`.

* [Sema] Make callWitness() take non-mutable arrays

It doesn’t actually need to mutate them.

* [Stdlib] Improve floating-point interpolation performance

This change avoids constructing a String when interpolating a Float, Double, or Float80. Instead, we write the characters to a fixed-size buffer and then append them directly to the string’s storage.

This seems to improve performance for all three types, but especially for Double and Float80, which cannot always fit into a small string when stringified.

* [NameLookup] Improve MemberLookupTable invalidation

In rare cases usually involving generated code, an overload added by an extension in the middle of a file would not be visible below it if the type had lazy members and the same base name had already been referenced above the extension. This change essentially dirties a type’s member lookup table whenever an extension is added to it, ensuring the entries in it will be updated.

This change also includes some debugging improvements for NameLookup.

* [SILOptimizer] XFAIL dead object removal failure

The DeadObjectRemoval pass in SILOptimizer does not currently remove reworked string interpolations as well as the old design because their effects cannot be described by @_effects(readonly). That causes a test failure on Linux. This change temporarily silences that test. The SILOptimizer issue has been filed as SR-9008.

* Confess string interpolation’s source stability sins

* [Parser] Parse empty interpolations

Previously, the parser had an odd asymmetry which caused the same function to accept foo(), but reject “\()”. This change fixes the issue.

Already tested by test/Parse/try.swift, which uses this construct in one of its throwing interpolation tests.

* [Sema] Fix batch-mode-only lazy var bug

The temporary variable used by string interpolation needs to be recontextualized when it’s inserted into a synthesized getter. Fixes a compilation failure in Alamofire.

I’ll probably follow up on this bug a bit more after merging.
2018-11-02 19:16:03 -07:00
fischertony
ba1bbd028d updated missed tests 2018-04-22 20:48:24 +03:00
Rintaro Ishizaki
6d402888ad [Parse] Don't swallow 'try' at declaration member position (#15733)
Previously, this used to be accepted:
  class Foo {
    try func bar() { }
  }
2018-04-05 07:59:53 +09:00
Pavel Yaskevich
b7ab7491e6 [QoI] Provide fix-it for missing "try" when calling throwing function
When calling a throwing function without 'try', let's suggest multiple
possibilities of note + fix-it for user to choose from.

Resolves: rdar://problem/33040113
2017-07-09 19:22:43 -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
practicalswift
ef8e43b519 [gardening] Increase consistency with regards to spacing after colons 2016-09-22 16:28:57 +02:00
John McCall
c8c41b385c Implement SE-0077: precedence group declarations.
What I've implemented here deviates from the current proposal text
in the following ways:

- I had to introduce a FunctionArrowPrecedence to capture the parsing
  of -> in expression contexts.

- I found it convenient to continue to model the assignment property
  explicitly.

- The comparison and casting operators have historically been
  non-associative; I have chosen to preserve that, since I don't
  think this proposal intended to change it.

- This uses the precedence group names and higherThan/lowerThan
  as agreed in discussion.
2016-07-26 14:04:57 -07:00
Jacob Bandes-Storch
ebabfe6b1c [stdlib] Remove optional comparison operators (SE-0121) 2016-07-23 22:30:09 -07:00
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
Alex Hoppen
b956dcd5ad [SR-1752] Fix warning about unused result if return type is Void? 2016-07-06 07:51:40 +02:00
Chris Lattner
3549ec5404 [QoI] make several improvements to the unused expression diagnostics, to go
along with recent policy changes:

- For expression types that are not specifically handled, make sure to
  produce a general "unused value" warning, catching a bunch of unused
  values in the testsuite.

- For unused operator results, diagnose them as uses of the operator
  instead of "calls".

- For calls, mutter the type of the result for greater specificity.

- For initializers, mutter the type of the initialized value.

- Look through OpenExistentialExpr's so we can handle protocol member
  references propertly.

- Look through several other expressions so we handle @discardableResult
  better.
2016-05-16 23:26:07 -07:00
Trent Nadeau
0cc851568a Updated tests to use @discardableResult and _ = . 2016-05-11 22:53:38 -04:00
Chris Lattner
2c81c8a114 add some parens to the testsuite, NFC. 2016-05-05 23:19:08 -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
Max Moiseev
a49dab6bf8 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-29 12:08:52 -08:00
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
ken0nek
3ac60b13f5 Add spaces before and after closure arrow in test 2015-12-23 04:38:46 +09:00
Dmitri Gribenko
feacbc4433 Rename ErrorType to ErrorProtocol 2015-12-09 17:12:19 -08:00
Jordan Rose
77d8a079af Special-case trying to force a 'try?' expression.
...both in the message and in the fix-it, which offers to change it to
'try!'.

More rdar://problem/22259867.

Swift SVN r31201
2015-08-13 03:08:18 +00:00
Jordan Rose
a690885f5c Properly parenthesize 'try?' in fix-its.
It's not /really/ an infix operator, but it behaves like a very low
precedence prefix operator. On the other hand, 'try' and 'try!' can
freely move in and out of all the operations we add in fix-its, so
don't bother.

rdar://problem/22259867

Swift SVN r31200
2015-08-13 03:08:17 +00:00
Jordan Rose
a26716290c When adding " != nil" to an optional in an 'if', we may need parens.
...such as with 'try?'.

rdar://problem/22259712

Swift SVN r31199
2015-08-13 03:08:15 +00:00
Jordan Rose
410083dfc9 Warn on unused results in single-expression closures coerced to Void.
...just like an unused result anywhere else.

Swift SVN r31091
2015-08-08 00:23:05 +00:00
Jordan Rose
bd031d1515 Always warn when 'try?' is discarded.
rdar://problem/22195906

Swift SVN r31090
2015-08-08 00:23:00 +00:00
Jordan Rose
185326755c 'try?' is supposed to stack optionals, not merge them.
More rdar://problem/21692467

Swift SVN r31059
2015-08-06 21:02:40 +00:00
Jordan Rose
2801d47e59 Add Parse and Sema support for 'try?'.
rdar://problem/21692467

Swift SVN r31030
2015-08-05 22:17:25 +00:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
Chris Lattner
9d9b8aaf35 move protocol conformance errors away from being diagnosed as a Failure, instead
putting it into the expr diagnostics path, allowing more contextual messages.


Swift SVN r30920
2015-08-01 18:31:59 +00:00
Chris Lattner
40d2b99aa4 fix:
<rdar://problem/21414023> Assertion failure when compiling function that takes throwing functions and rethrows
<rdar://problem/21432429> Calling rethrows from rethrows crashes Swift compiler
<rdar://problem/21427855> Swift 2: Omitting try from call to throwing closure in rethrowing function crashes compiler



Swift SVN r29580
2015-06-23 21:48:58 +00:00
Chris Lattner
9df3cccb06 make the error message and diagnostic location for
throwing operators not marked "try" more specific.


Swift SVN r29368
2015-06-12 18:33:56 +00:00
Jordan Rose
0c77785020 [Parser] Explicitly reject "try" before a statement with a specific diagnostic.
And for "try return", "try throw", and "try let", get even more specific,
with a fix-it to suggest moving the "try" onto the expression.

rdar://problem/21043120

Swift SVN r28862
2015-05-21 00:12:36 +00:00
John McCall
5c171fd448 Parsing, type-checking, SILGen, and IRGen for try!.
Swift SVN r28085
2015-05-02 08:03:15 +00:00
John McCall
fdcecfcfb7 Move error-handling diagnostics to Sema and check try coverage.
Needs better test-case coverage.

Swift SVN r27898
2015-04-29 00:49:40 +00:00
John McCall
dd48c25d3f Parse 'try' expressions.
We parse 'try' as if it were a unary operator allowed on an
arbitrary element of an expr-sequence, but sequence-folding
constrains it to never appear on the RHS of most operators.

We do allow it on the RHS of an assignment or conditional
operator, but not if there's anything to the right which
was not parsed within the RHS.

We do this for assignments so that
  var x = try whatever
and
  x = try whatever
both work as you might expect.

We do this for conditionals because it feels natural to
allow 'try' in the center operand, and then disallowing it
in the right operand feels very strange.

In both case, this works largely because these operators are
assumed to be very low-precedence; there are no standard
operators which would parse outside the RHS.  But if you
create one and use 'try' before it, we'll diagnose it.

Swift SVN r26052
2015-03-12 18:59:21 +00:00