Commit Graph

57 Commits

Author SHA1 Message Date
Slava Pestov
1957bd6065 Sema: Reword diagnostics to say 'without a type annotation' instead of 'without more context' 2023-06-09 17:44:42 -04:00
Anthony Latsis
f76f6acbf0 Revert "Sema: Emit diagnostics when walking into collection literals with defaulted types"
This reverts commit f608802cfe.
2023-04-20 21:55:40 +03:00
Anthony Latsis
593c6a3887 Merge pull request #60592 from AnthonyLatsis/migrate-test-suite-to-gh-issues-5
Gardening: Migrate test suite to GH issues p. 5
2022-08-20 04:53:48 +03:00
Anthony Latsis
da8d035b0b Gardening: Migrate test suite to GH issues: Constraints (2/5) 2022-08-19 06:43:11 +03:00
Sima Nerush
f608802cfe Sema: Emit diagnostics when walking into collection literals with defaulted types
Resolves https://github.com/apple/swift/issues/60011

Gardening: clean up `checkTypeDefaultedCollectionExpr` function
2022-08-15 22:45:21 -06:00
Hamish Knight
4aaec65780 [CS] Warn on mismatched tuple labels for subtyping
This is something that we'd like to fix to bring
in line with tuple conversion, so start warning on
cases where it occurs.
2021-11-17 17:06:21 +00:00
Hamish Knight
da36a2cb88 [CS] Restore a type variable for compatibility with rdar://85263844
Despite being otherwise disconnected from the
constraint system, it's possible for it to affect
how we type-check tuple matches in certain cases.

This is due to the fact that:
- It can have a lower type variable ID than an
opened generic parameter type, so becomes the
representative when merged with it. And because it
has a different locator, this can influence
binding prioritization.
- Tuple subtyping is broken, as it's currently a
*weaker* relationship than conversion.

Therefore, temporarily restore this bit of logic
for language versions < 6. If possible, we should
try and fix tuple subtying in Swift 6 mode to not
accept label mismatches, so that it's not more
permissive than tuple conversion.

rdar://85263844
2021-11-17 17:06:19 +00:00
Pavel Yaskevich
86c33e3449 [TypeChecker] NFC: Add a test-case for rdar://85263844 regression 2021-11-10 16:43:00 -08:00
Abdul Ajetunmobi
f8c737b1ec SR-13976: Improve compiler error message: "partial application of ‘mutating’ method is not allowed” 2021-10-06 19:59:09 +01:00
Pavel Yaskevich
2b207e8f07 [Diagnostics] Augment "expected parameter" note with an argument type
Currently ambiguity notes attached to a candidate only mention
expected type and its position. To improve clarify of such notes
it's useful to print argument type as well since it's not always
clear what it is at the first glance at the code.

Resolves: SR-14634
Resolves: rdar://78224323
2021-05-26 12:58:35 -07:00
Frederick Kellison-Linn
5b5e30b6d7 Implement implicit member chains 2020-08-26 22:42:29 -04:00
Pavel Yaskevich
d726c90326 [CSBindings] Limit optionality hack only to direct protocol requirements
In situations like:

$T0 subtype $T1
$T1 literal conforms to ExpressibleByArrayLiteral
$T0 conv [<Type>]?

We have to ensure that $T0 gets to maintain its optionality
because $T1 then could strip optionality later based on a
typing rule where `T <: T?`
2020-07-15 20:50:50 -07:00
Pavel Yaskevich
20fc51d4f4 [CSBindings] Open collection before binding parameter only if original argument type failed
Instead of always opening argument type represented by a collection
without type variables (to support subtyping when element is a labeled tuple),
let's try original type first and if that fails use a slower path with
indirection which attempts `array upcast`. Doing it this way helps to
propagate contextual information faster which fixes a performance regression.

Resolves: rdar://problem/54580247
2020-02-27 16:26:13 -08:00
Pavel Yaskevich
1c803a7cac [CSFix] Account for special closure handling in invalid trailing closure fix
Since closures don't get the type assigned right away anymore we
have to fetch it before any of the internal type variables could
be marked as holes.
2020-01-14 00:09:32 -08:00
Pavel Yaskevich
5cacd1bb36 [ConstraintSystem] Fix situations when contextual base type can't be inferred
It might be either impossible to infer the base because there is
no contextual information e.g. `_ = .foo` or there is something
else wrong in the expression which disconnects member reference
from its context.
2019-12-03 12:07:16 -08:00
Pavel Yaskevich
58329e0c27 Revert "[Diagnostics][Qol] SR-11295 Emit diagnostics for same type coercion. " 2019-10-25 01:05:07 -07:00
Luciano Almeida
86ca3454d6 Fixing warning UnnecessaryCoercion tests 2019-10-21 23:11:21 -03:00
Hamish Knight
e2096ae34d [CSDiagnostics] Tweak candidate note text for arg mismatch
Number the parameters starting at 1 in order to
match other diagnostics such as
diag::missing_argument_positional, and change the
text to make it explicit that we're referring to
the parameter position (rather than argument
position).
2019-10-03 15:26:31 -07:00
Pavel Yaskevich
001f46231c [Diagnostics] Fix a typo in argument mismatch diagnostic note 2019-09-13 22:35:52 -07:00
Pavel Yaskevich
a1643d94f7 [Diagnostics] NFC: Update all of the improved test-cases 2019-09-13 22:35:52 -07:00
Greg Titus
f444b4d44b Calls with unexpected trailing closures weren't getting passed on to ArgumentMatcher. 2019-08-05 06:50:44 -07:00
Pavel Yaskevich
d86ccc9741 [ConstraintSystem] Fix regression in partial application of initializers
Correct a regression related to use of partially applied initializers,
which should be rejected only if it happens in a delegation chain.

Resolves: [SR-10837](https://bugs.swift.org/browse/SR-10837)
Resolves: rdar://problem/51442825
2019-06-05 12:13:50 -07:00
Pavel Yaskevich
31d001cc67 [CSDiag] Adjust assert to account for changes in filterContextualMemberList.
Originally `filterContextualMemberList` would only return a limited
set of closeness kinds `CC_GeneralMismatch`, `CC_Argument{Label, Count}Mismatch`,
and unavailable/inaccessible. At some point later it also started
matching almost everything besides `CC_SelfMismatch` and logic in
`visitUnresolvedMemberExpr` needs to get adjusted to account for that.

Resolves: rdar://problem/50668864
2019-05-20 13:39:39 -07:00
Pavel Yaskevich
688042becf [Diagnostics] Add inaccessible member diagnostic 2019-03-18 13:48:08 -07:00
Pavel Yaskevich
1d42e16ad2 [ConstraintSystem] Detect invalid implicit ref to initializer on non-const metatype
Situations like:

```swift
struct S {}
func foo(_ s: S.Type) {
  _ = s()
}
```

Used to be diagnosed in solution application phase, which means that
solver was allowed to formed an incorrect solution.
2019-02-07 00:17:07 -08:00
Brent Royal-Gordon
a72be0fb7d Sort overloads in suggestPotentialOverloads()
When the compiler fails to find an overload with suitable parameter or return types, it often attaches a note listing the available overloads so that users can find the one they meant to use. The overloads are currently ordered in a way that depends on the order they were declared, so swift-evolve would sometimes cause tests involving these diagnostics to fail.

This change emits the list in a textually-sorted order instead. The names were already being sorted as they were inserted into a std::set, so this shouldn’t significantly slow down the diagnostic.
2018-12-12 11:58:57 -08:00
Pavel Yaskevich
4e7a089717 [ConstraintSolver] Fix handling of defaultable constraint in getPotentialBindings
While trying to find a fixed type for a given type variable, check if
it has representative and if it does, reflect that in the returned type.

Resolves: rdar://problem/34670592
2017-09-29 00:08:07 -07:00
Pavel Yaskevich
d793744931 [Diagnostics] Add test-case for SR-5245 2017-06-17 12:42:02 -07:00
Doug Gregor
91951c8068 [Type checker] Delete unnecessary, bogus optimization for initializers.
An early optimization in constraint generation attempted to simplify
type construction (e.g., X(...)) when the type in question has no
failable initializers. However, the optimization didn't appropriately
clear out the cached bit when new information became available (e.g.,
new conformances, new extensions), and didn't seem to help anything
performance-wise (type-checking times didn't increase at all when I
turned this off).

Fixes rdar://problem/30588177.
2017-05-10 17:06:55 -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
Jordan Rose
caeed32302 Add a fix-it for missing generic parameters on construction.
For example, if someone tries to use the newly-generic type Cache,
from Foundation:

  var cache = Cache()

they'll now get a fix-it to substitute the default generic parameters:

  var cache = Cache<AnyObject, AnyObject>()

The rules for choosing this placeholder type are based on constraints
and won't be right 100% of the time, but they should be reasonable.
(In particular, constraints on associated types are ignored.)
In cases where there's no one concrete type that will work, an Xcode-
style placeholder is inserted instead.

- An unconstrained generic parameter defaults to 'Any'.
- A superclass-constrained parameter defaults to that class,
  e.g. 'UIView'.
- A parameter constrained to a single @objc protocol (or to AnyObject)
  defaults to that protocol, e.g. 'NSCoding'.
- Anything else gets a placeholder using the generic parameter's name
  and protocol composition syntax.

rdar://problem/27087345
2016-09-21 18:04:14 -06: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
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
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
Chris Lattner
f102876943 Improve diagnostics for unbound archetypes coming from a type, to indicate
information about where the archetype was defined.  Before:

t.swift:6:17: error: generic parameter 'T' could not be inferred
var a : Int = A.foo()
                ^

After:

t.swift:6:17: error: generic parameter 'T' could not be inferred
var a : Int = A.foo()
                ^
t.swift:2:8: note: 'T' declared as parameter to type 'A'
struct A<T> {
       ^
2016-01-21 17:37:39 -08:00
Chris Lattner
b5500b8600 Generalize the conditions in which we'll accept an ambiguous solution to
a constraint system in "allowFreeTypeVariables" mode.  Previously, we
only allowed a few specific constraints, now we allow any relational and
member constraints.  The later one is a big deal because it means that we
can allow ".Foo" expressions as ambiguous solutions, which CSDiags can
handle well.

This unblocks solving 23942743 and enables some minor improvements across
the board, including diagnosing things like this better:
  Optional(.none)  // now: generic parameter 'T' could not be inferred

That said, it also just permutes some non-awesome diagnostics.
2016-01-11 17:04:46 -08:00
Joe Pamer
828eb68e72 Commit DaveA's API changes to 'print', along with the compiler changes necessary to support them.
There's still work left to do. In terms of next steps, there's still rdar://problem/22126141, which covers removing the 'workaround' overloads for print (that prevent bogus overload resolution failures), as well as providing a decent diagnostic when users invoke print with 'appendNewline'.

Swift SVN r30976
2015-08-04 01:57:11 +00:00
Chris Lattner
ede0c50856 Revamp how value & type member constraint failures are diagnosed, eliminating the
"unavoidable failure" path, along with Failure::DoesNotHaveNonMutatingMember and
just doing some basic disambiguation in CSDiags.

This provides some benefits:
 - Allows us to plug in much more specific diagnostics for the existing "only has 
   mutating members" diagnostic, including producing notes for why the base expr
   isn't mutable (see e.g. test/Sema/immutability.swift diffs).
 - Corrects issues where we'd drop full decl name info for selector references.
 - Wordsmiths diagnostics to not complain about "values of type Foo.Type" instead 
   complaining about "type Foo"
 - Where before we would diagnose all failures with "has no member named", we now
   distinguish between when there is no member, and when you can't use it.  When you
   can't use it, you get a vauge "cannot use it" diagnostic, but...
 - This provides an infrastructure for diagnosing other kinds of problems (e.g. 
   trying to use a private member or a static member from an instance).
 - Improves a number of cases where failed type member constraints would produce uglier
   diagnostics than a different constraint failure would.
 - Resolves a number of rdars, e.g. (and probably others):
   <rdar://problem/20294245> QoI: Error message mentions value rather than key for subscript



Swift SVN r30715
2015-07-28 07:04:22 +00:00
Chris Lattner
dff93b512b Now that we have the notion of an uncurry level, we can do a lot more
detailed analysis of callees, which give us overload sets in more cases,
producing notes more consistently, and producing much better diagnostics
for the curried cases in test/Constraints/diagnostics.swift.

This also allows us to eliminate getCalleeName, which simplifies things
in CSDiags.


Swift SVN r30491
2015-07-22 05:37:39 +00:00
Chris Lattner
a32947cbb2 Introduce the notion of an uncurry level to CalleeCandidateInfo to start
rationalizing how it handles members and curried functions, also paving
the way for future improvements.  This implements the infrastructure but
keeps the functionality the same (the only functionality change is that
it works a bit better with vardecls of function type).



Swift SVN r30464
2015-07-21 21:59:47 +00:00
Chris Lattner
f2a40da3ed fix FailureDiagnosis::collectCalleeCandidateInfo to look at the entire
disjunction candidate set for a constraint that fails to match, not just
a single Bind within it.  This eliminates the arbitrary nature of picking
one match, allowing us to diagnose the entire candidate set.

This exposed that we were trying to do argument matching of 'self' against
the partially curried arguments.  Adjust the hack we have for that a bit to
make things work, but there are bigger problems for argument matching that
will need to be addressed.


Swift SVN r30064
2015-07-10 06:03:37 +00:00
Chris Lattner
63f99a486c Move CallExpr diagnostics over to the same overload candidate diagnosis
facilities used by operators etc.  This required a bunch of changes to make
the diagnostics changes strictly an improvement:

  - Teach the new path about calls to TypeExprs.
  - Teach evaluateCloseness some simple things about varargs.
  - Make the generic diagnosis logic produce a better error when there is 
    exactly one match.

Overall, the resultant diagnostics are a step forward: we now produce candidate
set notes more uniformly, and the messages about some existing ones are 
more specific.  This is just another stepping stone towards progress though.



Swift SVN r30057
2015-07-10 04:26:42 +00:00
Joe Groff
9e0b290f81 Parser: Always parse '.foo' as expr-postfix when possible.
When a line begins with '.', it's almost always due to a method chain, not an attempt to start an expression with a contextual member lookup. This is a more principled grammar rule than the long tail of hacks we've been putting up to try to accommodate "builder pattern" usages. Fixes rdar://problem/20238557.

Swift SVN r29606
2015-06-24 14:39:34 +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
Joe Groff
bebfa969bd Sema: Allow 'x.init' references on metatype expressions.
If 'x.init' appears as a member reference other than 'self.init' or 'super.init' within an initializer, treat it as a regular static member lookup for 'init' members. This allows a more explicit syntax for dynamic initializations; 'self.someMetatype()' looks too much like it's invoking a method. It also allows for partial applications of initializers using 'someMetatype.init' (though this needs some SILGen fixes, coming up next). While we're in the neighborhood, do some other correctness and QoI fixes:

- Only lookup initializers as members of metatypes, not instances, and add a fixit (instead of crashing) to insert '.dynamicType' if the initializer is found on an instance.
- Make it so that constructing a class-constrained archetype type correctly requires a 'required' or protocol initializer.
- Warn on unused initializer results. This seems to me like just the right thing to do, but is also a small guard against the fact that 'self.init' is now valid in a static method, but produces a newly-constructed value instead of delegating initialization (and evaluating to void).

Swift SVN r29344
2015-06-08 04:11:28 +00:00
Dmitri Hrybenko
f46f16ae82 stdlib: implement new print() API
rdar://20775683

Swift SVN r28309
2015-05-08 01:37:59 +00:00
Joe Pamer
24eb7a2d60 Add tests for rdar://problem/19459079 and rdar://problem/19005271.
Swift SVN r24694
2015-01-23 23:32:46 +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
66d5f425bd Add test case from rdar://problem/19254404
Swift SVN r24539
2015-01-20 00:07:06 +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