Commit Graph

591 Commits

Author SHA1 Message Date
Slava Pestov
ec12e974ee Sema: More consistent enforcement of '.self' on metatype values in Swift 4 mode
In Swift 3, we required that '.self' be specified on TypeExprs
and DeclRefExprs that reference types.

However, types referenced as member lookups, such as 'Foo.Bar',
did not get this treatment, and '.self' was not required.

Fix this by emitting warnings in the cases that Swift 3 did not
diagnose, and producing errors in Swift 4 mode where we want
strict enforcement.
2017-05-21 18:14:55 -07:00
Rintaro Ishizaki
6fa84150c5 [AST] Eliminate IfConfigStmt
Resolves: https://bugs.swift.org/browse/SR-4426

* Make IfConfigDecl be able to hold ASTNodes
* Parse #if as IfConfigDecl
* Stop enclosing toplevel #if into TopLevelCodeDecl.
* Eliminate IfConfigStmt
2017-05-16 12:19:54 +09:00
practicalswift
492f5cd35a [gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
Replace `NameOfType foo = dyn_cast<NameOfType>(bar)` with DRY version `auto foo = dyn_cast<NameOfType>(bar)`.

The DRY auto version is by far the dominant form already used in the repo, so this PR merely brings the exceptional cases (redundant repetition form) in line with the dominant form (auto form).

See the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names) for a general discussion on why to use `auto` to avoid redundant repetition of type names.
2017-05-05 09:45:53 +02:00
Slava Pestov
214c7f8e5a Sema: Update getDefaultGenericArgumentsString() for primitive AnyObject 2017-05-01 19:30:43 -07:00
Slava Pestov
f4b91cd118 AST: Remove unused 'resolver' argument from TypeBase::getSuperclass() 2017-04-20 00:37:38 -07:00
practicalswift
65bcc8ff84 [gardening] Use o && isa<T>(o) instead of dyn_cast_or_null<T>(o) when result is unused 2017-04-14 17:33:24 +02:00
Slava Pestov
d58f049608 AST: Introduce ASTContext::getAnyObjectType()
This replaces a number of usages of KnownProtocolKind::AnyObject,
which is soon going away.
2017-04-13 21:17:05 -07:00
David Hart
13edc600f0 [SR-2421] Remove variable_never_used fixit to work with two-stage let initialization
The variable_never_used fixit transforms into invalid code in the case of two-stage let initialization. I introduced a new diagnostic that does not fixit and suggests removing the value.
2017-03-18 00:21:18 +01:00
Doug Gregor
a9daae2fea [Type checker] Don’t print an erroneous superclass in a diagnostic. 2017-02-07 15:22:50 -08:00
Jordan Rose
37774eb97e Fix the TypeChecker's omitNeedlessWords to use interface types. (#7193)
...avoiding a crash when trying to detect near misses of protocol
requirements. Unfortunately I can't come up with a test case for the
VarDecl changes; everything I try seems to already work. But using
interface types is more correct anyway.

https://bugs.swift.org/browse/SR-3812
2017-02-02 14:26:29 -08:00
Slava Pestov
71cf245701 Merge pull request #7023 from KingOfBrian/bugfix/SR-2115
Generate unused variable warnings in top level statements
2017-01-29 20:25:46 -08:00
Slava Pestov
974e2e1e9e Sema: Fix @warn_unqualified_access crash 2017-01-28 18:35:24 -08:00
Brian King
e9e237871c Fix a crash with a nil pattern returned from StmtConditionElement 2017-01-27 15:37:19 -05:00
Brian King
3c4fec8d87 Make sure braces are consistently cuddled 2017-01-26 21:52:19 -05:00
Brian King
b389fc3ef5 Code review feedback 2017-01-26 18:00:00 -05:00
Brian King
d835167485 Flag global variables declared in guard constructs 2017-01-26 09:59:51 -05:00
Brian King
36e36a1551 Align TLCD checking better with existing approach 2017-01-26 09:59:27 -05:00
Brian King
6091cda408 Run the VarDeclUsageChecker on top level declarations 2017-01-24 22:50:28 -05:00
Slava Pestov
10e292bfdf Sema: Make it explicit that the 'tuple splat' diagnostic is only for Swift 3 compatibility mode 2017-01-19 20:07:06 -08:00
Rintaro Ishizaki
3952e0043c [Diag] Don't attempt 'fixOverrideDeclarationTypes' when indices count doesn't match for subscript
Fixes a crasher
2017-01-16 22:51:35 +09:00
Slava Pestov
aa63f83f51 Sema: Extract availability code into TypeCheckAvailability.cpp 2017-01-06 18:59:34 -08:00
Joe Groff
720f496b2e Merge pull request #6605 from jckarter/unsafe-bitcast-warnings
Sema: Warn about some common classes of `unsafeBitCast` misuse.
2017-01-06 09:13:56 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Joe Groff
d0787fb6b5 Sema: Warn about some common classes of unsafeBitCast misuse.
- Most immediately, we now have `withoutActuallyEscaping` as a supported way to temporarily reference a nonescaping closure as if it were escapable, and we plan to break the ABI for escaping and nonescaping closures so that the old `unsafeBitCast` workaround no longer works.
- `unsafeBitCast` is also commonly used to kludge pointers into different types, but we have more semantically meaningful APIs for type punning now. Guide users towards those APIs.
- Suggest more specific and type-safe operations, like `bitPattern:` initializers or `unsafeDowncast`, for the situations where `unsafeBitCast` is being used to avoid dynamic type checks or reinterpret numerical bits.
2017-01-05 21:19:02 -08:00
Slava Pestov
87e9b1b34e Sema: Fix a warning 2017-01-04 15:48:38 -08:00
Slava Pestov
18adb53226 Sema: Tighten up name lookup routines to not look through metatypes/lvalues/etc
Previously all of the following would strip off varying amounts of
MetatypeType, LValueType, InOutType, DynamicSelfType, etc:

- ConstraintSystem::performMemberLookup()
- ConstraintSystem::lookupMember()
- TypeChecker::lookupMember()
- DeclContext::lookupQualified()
- Type::getContextSubstitutions()

The problem is that the higher level methods that took a lookup type
would call the lower level methods, and post-process the result using
the given lookup type. Since different levels of sugar were stripped,
it made the code hard to reason about and opened up edge cases, eg
if a DynamicSelfType or InOutType appears where we didn't expect it.

Since filtering out static/instance and mutating/nonmutating members
is done at higher levels, there's no reason for these name lookup
operations to accept anything other than nominal types, existentials
and archetypes.

Make this so with assertions, and deal with the fallout.
2017-01-04 01:40:19 -08:00
Slava Pestov
7258861d73 Merge pull request #6515 from xedin/crasher-28588
[QoI] While merging equivalence classes don't forget to merge fixed types (if any).
2017-01-03 19:35:47 -08:00
Joe Groff
796df2dc44 Merge pull request #6429 from jckarter/type-of-by-overload-resolution
`withoutActuallyEscaping`
2017-01-03 18:47:29 -08:00
Robert Widmann
3a4eb3f8ac Validate an assumption in 'lookupConstructors'
Invalid ASTs like the one in 28625 cause Parse to generate an AST that
looks a heck of a lot like a constructor call, however this makes no
sense as the type in question is a TupleType and lookup asserts in such
cases.  Guard against this so we don't crash diagnosing.
2017-01-03 19:03:23 -07:00
Robert Widmann
51dc142fca [28419] Extend nested init-chaining diagnostics to defer statements
This used to cause SILGen to capture and subsequently load `self` as a
constant.  Then, when the super call was SILGen’d, it assumed that
`self` would be loaded Boxed.  Diagnose before hitting SILGen so we
don’t have to pollute Lowering with code that handles `self` in this
odd position.
2017-01-03 18:53:06 -07:00
Pavel Yaskevich
fac59ce4ee [Diagnostics] Improve diagnostics of self assignment of the anonymous closure parameters 2016-12-31 19:00:30 -08:00
Joe Groff
0c9297862f Sema: Handle type-checking for withoutActuallyEscaping.
withoutActuallyEscaping has a signature like `<T..., U, V, W> (@nonescaping (T...) throws<U> -> V, (@escaping (T...) throws<U> -> V) -> W) -> W, but our type system for functions unfortunately isn't quite that expressive yet, so we need to special-case it. Set up the necessary type system when resolving an overload set to reference withoutActuallyEscaping, and if a type check succeeds, build a MakeTemporarilyEscapableExpr to represent it in the type-checked AST.
2016-12-22 17:51:26 -08:00
Joe Groff
1889fde228 Resolve type(of:) by overload resolution rather than parse hackery.
`type(of:)` has behavior whose type isn't directly representable in Swift's type system, since it produces both concrete and existential metatypes. In Swift 3 we put in a parser hack to turn `type(of: <expr>)` into a DynamicTypeExpr, but this effectively made `type(of:)` a reserved name. It's a bit more principled to put `Swift.type(of:)` on the same level as other declarations, even with its special-case type system behavior, and we can do this by special-casing the type system we produce during overload resolution if `Swift.type(of:)` shows up in an overload set. This also lays groundwork for handling other declarations we want to ostensibly behave like normal declarations but with otherwise inexpressible types, viz. `withoutActuallyEscaping` from SE-0110.
2016-12-22 16:28:31 -08:00
practicalswift
ce7a10474f [gardening] Fix accidental double and triple spaces. 2016-12-21 22:13:56 +01:00
Slava Pestov
fb0f372e94 AST: Move mapType{In,OutOf}Context() out of ArchetypeBuilder and clean up headers
- The DeclContext versions of these methods have equivalents
  on the DeclContext class; use them instead.

- The GenericEnvironment versions of these methods are now
  static methods on the GenericEnvironment class. Note that
  these are not made redundant by the instance methods on
  GenericEnvironment, since the static methods can also be
  called with a null GenericEnvironment, in which case they
  just assert that the type is fully concrete.

- Remove some unnecessary #includes of ArchetypeBuilder.h
  and GenericEnvironment.h. Now changes to these files
  result in a lot less recompilation.
2016-12-18 19:55:41 -08:00
Michael Gottesman
96c63e9e76 Merge pull request #6324 from practicalswift/cpp-gardening
[gardening] C++ gardening: Terminate namespaces, fix argument names, …
2016-12-16 23:30:33 -08:00
practicalswift
38be6125e5 [gardening] C++ gardening: Terminate namespaces, fix argument names, ...
Changes:
* Terminate all namespaces with the correct closing comment.
* Make sure argument names in comments match the corresponding parameter name.
* Remove redundant get() calls on smart pointers.
* Prefer using "override" or "final" instead of "virtual". Remove "virtual" where appropriate.
2016-12-17 00:32:42 +01:00
Doug Gregor
0c0517f997 [Type checker] Don't drop "delayed" conformance diagnostics after a source file.
The protocol conformance checker tries to delay the emission of
diagnostics related to the failure of a type to conform to a protocol
until the source file that contains the conformance is encountered, to
provide redundant diagnostics. However, if a file produced only such
delayed diagnostics, such that all diagnostics were suppressed,
invalid ASTs could slip through to later stages in the pipeline where
they would cause verification errors and crashes. This happens
generally with whole-module-optimization builds, where we are re-using
an ASTContext when typing multiple source files.

This is a narrow-ish fix to stop dropping diagnostics from one source
file to the next in whole-module-optimization builds. Part of
rdar://problem/29689007.
2016-12-16 15:15:17 -08:00
Slava Pestov
2c6b9f71b6 AST: Change TypeAliasDecls to store an interface type as their underlying type
- TypeAliasDecl::getAliasType() is gone. Now, getDeclaredInterfaceType()
  always returns the NameAliasType.

- NameAliasTypes now always desugar to the underlying type as an
  interface type.

- The NameAliasType of a generic type alias no longer desugars to an
  UnboundGenericType; call TypeAliasDecl::getUnboundGenericType() if you
  want that.

- The "lazy mapTypeOutOfContext()" hack for deserialized TypeAliasDecls
  is gone.

- The process of constructing a synthesized TypeAliasDecl is much simpler
  now; instead of calling computeType(), setInterfaceType() and then
  setting the recursive properties in the right order, just call
  setUnderlyingType(), passing it either an interface type or a
  contextual type.

  In particular, many places weren't setting the recursive properties,
  such as the ClangImporter and deserialization. This meant that queries
  such as hasArchetype() or hasTypeParameter() would return incorrect
  results on NameAliasTypes, which caused various subtle problems.

- Finally, add some more tests for generic typealiases, most of which
  fail because they're still pretty broken.
2016-12-15 22:46:15 -08:00
Slava Pestov
a384b2a677 Don't call VarDecl::getType() on deserialized VarDecls 2016-12-15 22:46:15 -08:00
Slava Pestov
4b0a9e1d1e Sema: Ban existentials with associated types from appearing in 'switch' patterns
This was allowed on accident in Swift 3, and caused a build regression
with the JaySON project (https://github.com/DanToml/Jay).
2016-12-12 18:10:03 -08:00
Slava Pestov
b4d11338ec AST: Push ValueDecl::{has,get,set}Type() down to VarDecl
After recent changes, this asserts on all decls that are not VarDecls,
so we can just enforce that statically now. Interestingly, this turns
up some dead code which would have asserted immediately if called.

Also, replace AnyFunctionRef::getType() with
AnyFunctionRef::getInterfaceType(), since the old
AnyFunctionRef::getType() would just assert when called on
a Decl.
2016-12-01 19:28:13 -08:00
Slava Pestov
8bdbe774e0 AST: Don't call hasType()/getType()/setType() on SubscriptDecls 2016-12-01 13:00:17 -08:00
Slava Pestov
044034cae5 Sema: hasType() => hasInterfaceType() 2016-11-29 03:05:31 -07:00
Slava Pestov
7cfe0e6401 Sema: getType() => getInterfaceType() 2016-11-29 03:05:27 -07:00
Slava Pestov
f803d8f12c AST: Remove AbstractFunctionDecl::getBodyResultType()
This is made redundant by the existing getResultInterfaceType()
accessor.
2016-11-29 03:05:23 -07:00
Slava Pestov
f6e692198c AST: Remove FuncDecl::getResultType() 2016-11-29 03:05:23 -07:00
Rintaro Ishizaki
cdcbc2d2e0 [QoI] When adding '.self' for metatype expressions, enclose with parenthesis if the typerepr is not Simple 2016-11-29 15:28:48 +09:00
Rintaro Ishizaki
af19a0bd50 [Diag] Suggest adding '()' only if it has accessible initializers 2016-11-29 15:28:48 +09:00
Graydon Hoare
7c1dc18b64 Revert "Give all declarations an explicit interface type" 2016-11-24 09:55:27 -08:00