Commit Graph

82 Commits

Author SHA1 Message Date
Pavel Yaskevich
dde501485e [CSBinding] Prefer array literal type over a disjunction in certain cases
If array literal type is not delayed and doesn't have any type variables
associated with it, let's prefer it over a disjunction to facilitate
type propagation through its `Element` type to element expressions.

Resolves: rdar://118993030
2023-11-30 17:43:13 -08:00
Pavel Yaskevich
863bb7c39c [TypeChecker] Make couldDynamicallyConformToProtocol more leanient
Current check is too strict for stdlib collection types because
it checks conditional conformances which leads to erroneous warnings.

To determine whether a type dynamically conforms to a protocol,
for stdlib collection types, the method is employing
`Module::lookupConformance` check which has `allowMissing` conformances
set to `false` by default. That directly contradicts the comment
which states that the conditional conformances are to be skipped
and doesn't fit well with `TypeChecker::conformsToProtocol` check
below it that defaults `allowMissing` to `true`.

Resolves: rdar://115603144
2023-10-26 13:59:16 -07:00
Pavel Yaskevich
0f194f4fcd [CSBindings] Don't priorize variables without direct bindings over disjunctions
It's possible that a disjunction could introduce new bindings
to the set.
2023-10-23 09:43:43 -07:00
Danilo Camarotto
8a84fdd79c improving tests readability 2023-10-11 23:32:15 +01:00
Danilo Camarotto
6142a44996 making improvements requested in code review 2023-10-11 20:17:14 +01:00
Danilo Camarotto
7eac14b783 changing diagnostics err to use ValueDecl and created note 2023-10-10 10:39:49 +01:00
Danilo Camarotto
4a90f729aa fixing unit tests 2023-10-07 15:59:01 +01:00
Danilo Camarotto
439ae8051f changing message and fixing tests 2023-10-05 02:14:13 +01:00
Danilo Camarotto
a1e8d29fa9 implementing changes and fixing unit tests 2023-10-03 15:37:03 +01:00
Pavel Yaskevich
5b5b3dd692 [TypeChecker] NFC: Add a test-case for rdar://109381194 2023-05-16 14:48:18 -07: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
dd627b4e54 Merge pull request #60641 from AnthonyLatsis/migrate-test-suite-to-gh-issues-8 2022-08-26 14:36:57 +03:00
Anthony Latsis
e51ef47b37 Gardening: Migrate test suite to GH issues: Constraints (5/5) 2022-08-19 06:35:23 +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
Luciano Almeida
d602a255d3 [Sema] Do not emit cast to unrelated warning from existential to Anyhashable 2022-06-21 07:15:41 -03:00
Anthony Latsis
b2e2bab7e0 DiagnosticEngine: Print any in 'aka' types 2022-05-29 21:55:31 +03:00
Slava Pestov
dac8d666ee Stop passing -requirement-machine-{abstract,inferred,protocol}-signatures flags in tests
These flags are now no-ops.
2022-05-10 12:56:17 -04:00
Slava Pestov
02bfb79935 RequirementMachine: Concrete contraction discards errors from inferred requirements 2022-04-01 23:55:19 -04:00
Luciano Almeida
de1bd2d478 [SR-16058][Sema] Consider wrapping type variable layers of optionality when warning about checked casts 2022-03-27 19:14:43 -03:00
Holly Borla
12459cff80 [Diagnostics] Print 'any' in diagnostic arguments. 2022-03-05 14:26:45 -08:00
Hamish Knight
63c094c28b [CS] Don't apply compatibility logic to collection literals
We currently permit (but warn on) coercions of
collections with unrelated element types in
certain cases. This is done in an effort to preserve
compatibility with pre-5.3 compilers that may have
allowed such code to compile due to dropping
constraints while solving.

This is limited to collection coercions as we
emit somewhat reasonable code for them that
performs a force cast of the elements at runtime.
However, it turns out that in the case of collection
literals, we peephole the element conversions in
CSApply, leading to codegen crashes. As such,
narrow down the condition of this compatibility
logic to not apply to collection literals, as this
never would have compiled properly.

rdar://88334481
2022-02-10 19:06:48 +00:00
LucianoAlmeida
85246bcc55 [test] Add regression test for SR-15562 2021-12-09 00:00:35 -03:00
LucianoAlmeida
b517ed3593 [Sema] Suggest for unwrap fix-it instead of coercion if a subtype relation exists with from and to types 2021-10-09 14:26:21 -03:00
LucianoAlmeida
8c424383a6 [Sema] Account for value to optional restriction when recording runtime cast warnings 2021-10-09 14:26:21 -03:00
LucianoAlmeida
3c6027042c [tests] Add regression tests for SR-15038 2021-08-16 21:34:44 -03:00
LucianoAlmeida
434ff12062 [Sema] Check for extra optionals injected on checked cast subexprs when recording extraneous cast warnings 2021-08-07 17:27:31 -03:00
Luciano Almeida
b851974424 [Sema] Do not attempt warn extraneous checked cast for placeholder types 2021-04-24 23:52:58 -03:00
Doug Gregor
6efaf7ac0f Introduce -warn-concurrency flag to warn about concurrency issues.
To help support incremental adoption of the concurrency model, a number
of concurrency-related diagnostics are enabled only in "new" code that
takes advantage of concurrency features---async, @concurrent functions,
actors, etc. This warning flag opts into additional warnings that better
approximate the eventual concurrency model, and which will become
errors a future Swift version, allowing one to both experiment with
the full concurrency model and also properly prepare for it.
2021-03-05 10:58:54 -08:00
Doug Gregor
2f213d5f7b Update a few tests for the "final class" change. 2021-03-02 22:45:54 -08:00
Luciano Almeida
d55eed46e8 [Sema][CSApply] Moving some checked cast diagnostics to a fix format (#34980)
* [Sema] Implementing is runtime check always true diagnostic as a fix

* [AST] Implement getWithoutThrows on function type

* [CSSimplify] Detect that checked cast types conversion is always true and record warning fix

* [test] Some additional test cases for SR-13789

* [Sema] Fixing typo on fix name

* [Sema] Move and adjust the ConditionalCast diagnostics to the fix format

* [Sema] Remove some checked cast diagnostics from check constraints and move to fix

* [Sema] Renaming checked cast coercible types fix

* [Sema] Some adjustments and rewrite on the logic for downcast record fix

* [Sema] Move logic of runtime function type to AllowUnsupportedRuntimeCheckedCast::attempt

* [Sema] Abstract checked cast fix logic to static function and minor adjustments

* [Sema] Renamings from review
2021-02-10 08:31:06 -03:00
Luciano Almeida
19727f2cc2 [Sema][test] Adjusting all missing downcast diagnostics failures 2020-11-28 18:17:18 -03:00
tbkka
82c43a8ac7 [Dynamic Casting] Allow casts to "more optional" types (#33684)
Generally, casting consistency demands that we be able
to extract anything from an existential that can be put
into that existential.  (Which is why the casting spec
requires that casting permit arbitrary injection and
projection of optionals.)

This particular diagnostic prevented optionals from being
projected back out of existentials:
   let i: Int?
   let a: Any = i  // Inject Int? into Any
   // Error prevents projecting Int? back out of Any
   a as? Int?

This also broke certain uses of Mirror (weak variables get reflected as
optionals stored in Any existentials).
2020-08-31 17:52:22 -07:00
Luciano Almeida
8207abd245 [tests] Adding regression tests for SR-7187 and SR-6192 2020-07-05 22:55:56 -03:00
Luciano Almeida
e23a36a952 [TypeConstraints] Warning cast to unrelated of protocol composition to no existential type 2020-07-05 19:34:25 -03:00
Luciano Almeida
28fb66cbaf [SR-13088] Fix false positive downcast unrelated of types that cannot be statically known (#32592)
* [TypeCheckConstraints] Adjusting cases where checked casts that cannot be determined statically were producing misleading warnings

* [tests] Adding regression tests for SR-13088

* [TypeCheckConstraints] Adjusting comment and adding an extra test case for SR13035

* [TypeCheckConstraints] Fixing typos in comments

* [AST] Moving implementation of isCollection from ConstraintSystem to AST TypeBase

* [TypeCheckConstraints] Adjusting logic to verify specific conformance to stdlib collection type before emit an downcast warning

* [TypeCheckConstraints] Creating new CheckedCastContextKind::CollectionElement to be able to verify special cases within typeCheckCheckedCast for collection elements

* [TypeCheckConstraints] Adjusting logic around generic substitution to check both subtype and supertype

* [Sema] Adding isKnownStdlibCollectionType and replacing all usages contraint system method

* [TypeChecker] Reverting fixes around array element types

* [TypeChecker] Abstract logic of check for conditional requirements on TypeChecker::couldDynamicallyConformToProtocol

* [TypeChecker] Ajdustinc can conformDynamically conform and adjust review comments

* [TypeChecker] Ajusting comments and fixing typos

* [TypeChecker] Adjusting existential and archetype logic to check inside couldDynamicConform

* [TypeChecker] Adjusting minor and adding existential check into couldDynamically conform.

* [TypeChecker] Adjusting comments
2020-07-02 22:06:29 -03:00
Hamish Knight
d69a42d656 [CS] Preserve compatibility for collection coercions
Previously we could allow some invalid coercions to
sneak past Sema. In most cases these would either
cause crashes later down the pipeline or
miscompiles. However, for coercions between
collections, we emitted somewhat reasonable code
that performed a force cast.

This commit aims to preserve compatibility with
those collection coercions that previously
compiled, and emits a warning telling the user to
use either 'as?' or 'as!' instead.
2020-04-10 10:16:07 -07:00
Hamish Knight
47541d86f7 [CS] Visit all fixed bindings for constraint re-activation
Start visiting transitive fixed bindings for type
variables, and stop visiting adjacencies for
`gatherConstraint`'s `AllMentions` mode.

This improves performance and fixes a correctness
issue with the old implementation where we could
fail to re-activate a coercion constraint, and
then let invalid code get past Sema, causing
either miscompiles or crashes later down the
pipeline.

Unfortunately this change requires us to
temporarily drop the non-ephemeral fix for a couple
of fairly obscure cases where the overload hasn't
yet been resolved. The logic was previously relying
on stale adjacency state in order to re-activate
the fix when the overload is bound, but it's not
connected on the constraint graph. We need to find
a way to connect constraints to unresolved
overloads they depend on.

Resolves SR-12369.
2020-04-10 10:16:07 -07:00
Pavel Yaskevich
465b0e193a [ConstraintSystem] Diagnose more cases of invalid nil use during constraint generation
Move check for `nil` destination of conditional casts to `visitNilLiteral` and
add support for `nil?` which wasn't previously detected.
2020-03-24 14:57:27 -07:00
Pavel Yaskevich
d7b12a6de7 [TypeChecker] Treat tuples specially while validating checked casts
Based on the checked cast behavior don't warn about unrelated
casts between tuples unless their sizes or labels differ.

Resolves: rdar://problem/56436235
2019-12-20 14:02:32 -08:00
Holly Borla
b1638ce1ab [CSGen] Add a tailored diagnostic for conditional casting from a nil literal,
and diagnose in CSGen.
2019-11-04 20:49:36 -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
Pavel Yaskevich
ec6a874ac8 [TypeChecker] NFC: Update test-cases improved by new missing arguments diagnostic 2019-09-25 10:47:26 -07:00
Suyash Srijan
e446ae726b [typechecker] if we're casting to a nominal type that's a protocol, then skip the check 2019-03-21 00:57:04 +00:00
Suyash Srijan
f7837c1694 [test] update cast diagnostics for existing tests 2019-03-21 00:22:51 +00:00
David Zarzycki
995dec5d82 [Sema] Error if ObjC interop is needed when disabled 2018-05-07 14:43:04 -04:00
Maxim Moiseev
128092a7d6 Rename filterMap to compactMap 2017-12-18 09:22:41 -08:00
Max Moiseev
96d3439669 Update tests after deprecating flatMap variant 2017-12-18 09:16:37 -08:00
Slava Pestov
65c3565c2d Sema: Fix failure to produce diagnostics when 'is' casts are involved
When re-typechecking an expression during diagnostics, we begin by
erasing all the types in the expression. However, any expressions
created as part of applying the solution will remain.

CSGen was doing the wrong thing when it encountered EnumIsCaseExpr,
which can only appear in already-type checked ASTs.

Returning expr->getType() is not correct, because the type is not
yet set; returning a null type is intended to signal that a
diagnostic was already emitted, which causes Sema to stop
type checking.

The end result is that certain combinations of invalid code with
an 'is' cast nested inside would crash either the AST verifier
(with asserts on) or in SILGen (with asserts off), because we
would stop trying to diagnose the issue prematurely, and possibly
not emit a diagnostic at all.

Fixes <https://bugs.swift.org/browse/SR-5050> and
<rdar://problem/32487948>.
2017-06-13 20:19:43 -07:00
Doug Gregor
ffa901cb9f [Type checker] Don't infer type variable bindings from checked casts (as?/as!).
Checked casts are dependent on run-time queries; we should not attempt
to infer type variable bindings from them, because doing so produces
unreasonable bindings. Fixes rdar://problem/29894174.
2017-01-10 11:03:54 -08:00