Skip to content

Commit 9043456

Browse files
Merge branch 'master' of https://github.com/apple/swift into SR-11295-warning-unecessary-casts
2 parents 922f372 + 88de305 commit 9043456

File tree

97 files changed

+1559
-1571
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1559
-1571
lines changed

CMakeLists.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ if(MSVC OR "${CMAKE_SIMULATE_ID}" STREQUAL MSVC)
455455
include(ClangClCompileRules)
456456
endif()
457457

458+
if(CMAKE_C_COMPILER_ID STREQUAL Clang)
459+
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-Werror=gnu>)
460+
endif()
461+
458462
if(CMAKE_SYSTEM_NAME STREQUAL Darwin OR
459463
EXISTS "${SWIFT_PATH_TO_LIBDISPATCH_SOURCE}")
460464
set(SWIFT_BUILD_SYNTAXPARSERLIB_default TRUE)
@@ -968,17 +972,14 @@ if(SWIFT_NEED_EXPLICIT_LIBDISPATCH)
968972
set(SWIFT_LIBDISPATCH_C_COMPILER ${CMAKE_C_COMPILER})
969973
set(SWIFT_LIBDISPATCH_CXX_COMPILER ${CMAKE_CXX_COMPILER})
970974
elseif(${CMAKE_SYSTEM_NAME} STREQUAL ${CMAKE_HOST_SYSTEM_NAME})
971-
get_target_property(CLANG_LOCATION clang LOCATION)
972-
get_filename_component(CLANG_LOCATION ${CLANG_LOCATION} DIRECTORY)
973-
974975
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
975976
set(SWIFT_LIBDISPATCH_C_COMPILER
976-
${CLANG_LOCATION}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
977+
$<TARGET_FILE_DIR:clang>/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
977978
set(SWIFT_LIBDISPATCH_CXX_COMPILER
978-
${CLANG_LOCATION}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
979+
$<TARGET_FILE_DIR:clang>/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
979980
else()
980-
set(SWIFT_LIBDISPATCH_C_COMPILER ${CLANG_LOCATION}/clang)
981-
set(SWIFT_LIBDISPATCH_CXX_COMPILER ${CLANG_LOCATION}/clang++)
981+
set(SWIFT_LIBDISPATCH_C_COMPILER $<TARGET_FILE_DIR:clang>/clang)
982+
set(SWIFT_LIBDISPATCH_CXX_COMPILER $<TARGET_FILE_DIR:clang>/clang++)
982983
endif()
983984
else()
984985
message(SEND_ERROR "libdispatch requires a newer clang compiler (${CMAKE_C_COMPILER_VERSION} < 3.9)")

cmake/modules/SwiftWindowsSupport.cmake

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,19 @@ endfunction()
8484
macro(swift_swap_compiler_if_needed target)
8585
if(NOT CMAKE_C_COMPILER_ID MATCHES Clang)
8686
if(CMAKE_SYSTEM_NAME STREQUAL CMAKE_HOST_SYSTEM_NAME)
87-
get_target_property(CLANG_LOCATION clang LOCATION)
88-
get_filename_component(CLANG_LOCATION ${CLANG_LOCATION} DIRECTORY)
89-
90-
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR
91-
"${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
92-
set(CMAKE_C_COMPILER
93-
${CLANG_LOCATION}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
94-
set(CMAKE_CXX_COMPILER
95-
${CLANG_LOCATION}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
87+
if(SWIFT_BUILT_STANDALONE)
88+
get_target_property(CLANG_LOCATION clang LOCATION)
89+
get_filename_component(CLANG_LOCATION ${CLANG_LOCATION} DIRECTORY)
9690
else()
97-
set(CMAKE_C_COMPILER
98-
${CLANG_LOCATION}/clang${CMAKE_EXECUTABLE_SUFFIX})
99-
set(CMAKE_CXX_COMPILER
100-
${CLANG_LOCATION}/clang++${CMAKE_EXECUTABLE_SUFFIX})
91+
set(CLANG_LOCATION ${LLVM_RUNTIME_OUTPUT_INTDIR})
92+
endif()
93+
94+
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
95+
set(CMAKE_C_COMPILER ${CLANG_LOCATION}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
96+
set(CMAKE_CXX_COMPILER ${CLANG_LOCATION}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
97+
else()
98+
set(CMAKE_C_COMPILER ${CLANG_LOCATION}/clang${CMAKE_EXECUTABLE_SUFFIX})
99+
set(CMAKE_CXX_COMPILER ${CLANG_LOCATION}/clang++${CMAKE_EXECUTABLE_SUFFIX})
101100
endif()
102101
else()
103102
message(SEND_ERROR "${target} requires a clang based compiler")

docs/proposals/DeclarationTypeChecker.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ This document describes some of the problems with our current "declaration" type
1313
Problems with the Current Approach
1414
----------------------------------
1515

16-
The current declaration type checker---in particular, ``validateDecl``, which assigns a type to a given declaration---is the source of a large number of Swift bugs, including crashes on both well-formed and ill-formed code, different behavior depending on the order of declarations within a file or across multiple files, infinite recursion, and broken ASTs. The main issues are:
16+
The current declaration type checker is the source of a large number of Swift bugs, including crashes on both well-formed and ill-formed code, different behavior depending on the order of declarations within a file or across multiple files, infinite recursion, and broken ASTs. The main issues are:
1717

1818
**Conceptual phases are tangled together**: We have a vague notion that there are phases within the compiler, e.g., extension binding occurs before name binding, which occurs before type checking. However, the implementations in the compiler don't respect phases: extension binding goes through type validation, which does both name binding and type checking. Name lookup attempts to do type checking so that it can establish whether one declaration shadows another.
1919

2020
**Unprincipled recursion**: Whenever type checking some particular declaration requires information about another declaration, it recurses to type-check that declaration. There are dozens of these recursion points scattered throughout the compiler, which makes it impossible to reason about the recursion or deal with, e.g., recursion that is too deep for the program stack.
2121

22-
**Ad hoc recursion breaking**: When we do encounter circular dependencies, we have scattered checks for recursion based on a number of separate bits stashed in the AST: ``BeingTypeChecked``, ``EarlyAttrValidation``, ``ValidatingGenericSignature``, etc. Adding these checks is unprincipled: adding a new check in the wrong place tends to break working code (because the dependency is something the compiler should be able to handle), while missing a check permits infinite recursion to continue.
22+
**Ad hoc recursion breaking**: When we do encounter circular dependencies, we have scattered checks for recursion based on a number of separate bits stashed in the AST: ``isComputingRequirementSignature()``, ``isComputingPatternBindingEntry()``, ``isComputingGenericSignature()/hasComputedGenericSignature()``, etc. Adding these checks is unprincipled: adding a new check in the wrong place tends to break working code (because the dependency is something the compiler should be able to handle), while missing a check permits infinite recursion to continue.
2323

2424
**Type checker does too much work**: validating a declaration is all-or-nothing. It includes computing its type, but also checking redeclarations and overrides, as well as numerous other aspects that a user of that declaration might not care about. Aside from the performance impact of checking too much, this can introduce false circularities in type-checking, because the user might only need some very basic information to continue.
2525

@@ -156,7 +156,7 @@ The proposed architecture is significantly different from the current type check
156156

157157
**Dependency graph and priority queue**: Extend the current-phase trait with an operation that enumerates the dependencies that need to be satisfied to bring a given AST node up to a particular phase. Start with ``TypeRepr`` nodes, and use the dependency graph and priority queue to satisfy all dependencies ahead of time, eliminating direct recursion from the type-resolution code path. Build circular-dependency detection within this test-bed.
158158

159-
**Incremental adoption of dependency graph**: Make other AST nodes (``Pattern``, ``VarDecl``, etc.) implement the phase-awareness trait, enumerating dependencies and updating their logic to perform minimal updates. Certain entry points that are used for ad hoc recursion (such as ``validateDecl``) can push/pop dependency graph and priority-queue instances, which leaves the existing ad hoc recursion checking in place but allows isolated subproblems to use the newer mechanisms.
159+
**Incremental adoption of dependency graph**: Make other AST nodes (``Pattern``, ``VarDecl``, etc.) implement the phase-awareness trait, enumerating dependencies and updating their logic to perform minimal updates. Certain entry points that are used for ad hoc recursion can push/pop dependency graph and priority-queue instances, which leaves the existing ad hoc recursion checking in place but allows isolated subproblems to use the newer mechanisms.
160160

161161
**Strengthen accessor assertions**: As ad hoc recursion gets eliminated from the type checker, strengthen assertions on the various AST nodes to make sure the AST node has been brought to the appropriate phase.
162162

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ DECL_ATTR(inline, Inline,
202202
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
203203
20)
204204
DECL_ATTR(_semantics, Semantics,
205-
OnAbstractFunction | OnSubscript |
205+
OnAbstractFunction | OnSubscript | OnNominalType |
206206
AllowMultipleAttributes | UserInaccessible |
207207
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
208208
21)

include/swift/AST/Decl.h

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,10 @@ bool conflicting(ASTContext &ctx,
282282

283283
/// Decl - Base class for all declarations in Swift.
284284
class alignas(1 << DeclAlignInBits) Decl {
285-
public:
286-
enum class ValidationState {
287-
Unchecked,
288-
Checking,
289-
Checked,
290-
};
291-
292285
protected:
293286
union { uint64_t OpaqueBits;
294287

295-
SWIFT_INLINE_BITFIELD_BASE(Decl, bitmax(NumDeclKindBits,8)+1+1+1+1+2+1,
288+
SWIFT_INLINE_BITFIELD_BASE(Decl, bitmax(NumDeclKindBits,8)+1+1+1+1+1,
296289
Kind : bitmax(NumDeclKindBits,8),
297290

298291
/// Whether this declaration is invalid.
@@ -307,9 +300,6 @@ class alignas(1 << DeclAlignInBits) Decl {
307300
/// Use getClangNode() to retrieve the corresponding Clang AST.
308301
FromClang : 1,
309302

310-
/// The validation state of this declaration.
311-
ValidationState : 2,
312-
313303
/// Whether this declaration was added to the surrounding
314304
/// DeclContext of an active #if config clause.
315305
EscapedFromIfConfig : 1
@@ -689,7 +679,6 @@ class alignas(1 << DeclAlignInBits) Decl {
689679
Bits.Decl.Invalid = false;
690680
Bits.Decl.Implicit = false;
691681
Bits.Decl.FromClang = false;
692-
Bits.Decl.ValidationState = unsigned(ValidationState::Unchecked);
693682
Bits.Decl.EscapedFromIfConfig = false;
694683
}
695684

@@ -830,37 +819,7 @@ class alignas(1 << DeclAlignInBits) Decl {
830819
/// Mark this declaration as implicit.
831820
void setImplicit(bool implicit = true) { Bits.Decl.Implicit = implicit; }
832821

833-
/// Get the validation state.
834-
ValidationState getValidationState() const {
835-
return ValidationState(Bits.Decl.ValidationState);
836-
}
837-
838-
private:
839-
friend class DeclValidationRAII;
840-
841-
/// Set the validation state.
842-
void setValidationState(ValidationState VS) {
843-
assert(VS > getValidationState() && "Validation is unidirectional");
844-
Bits.Decl.ValidationState = unsigned(VS);
845-
}
846-
847822
public:
848-
/// Whether the declaration is in the middle of validation or not.
849-
bool isBeingValidated() const {
850-
switch (getValidationState()) {
851-
case ValidationState::Unchecked:
852-
case ValidationState::Checked:
853-
return false;
854-
case ValidationState::Checking:
855-
return true;
856-
}
857-
llvm_unreachable("Unknown ValidationState");
858-
}
859-
860-
bool hasValidationStarted() const {
861-
return getValidationState() > ValidationState::Unchecked;
862-
}
863-
864823
bool escapedFromIfConfig() const {
865824
return Bits.Decl.EscapedFromIfConfig;
866825
}
@@ -982,25 +941,6 @@ class alignas(1 << DeclAlignInBits) Decl {
982941
}
983942
};
984943

985-
/// Use RAII to track Decl validation progress and non-reentrancy.
986-
class DeclValidationRAII {
987-
Decl *D;
988-
989-
public:
990-
DeclValidationRAII(const DeclValidationRAII &) = delete;
991-
DeclValidationRAII(DeclValidationRAII &&) = delete;
992-
void operator =(const DeclValidationRAII &) = delete;
993-
void operator =(DeclValidationRAII &&) = delete;
994-
995-
DeclValidationRAII(Decl *decl) : D(decl) {
996-
D->setValidationState(Decl::ValidationState::Checking);
997-
}
998-
999-
~DeclValidationRAII() {
1000-
D->setValidationState(Decl::ValidationState::Checked);
1001-
}
1002-
};
1003-
1004944
/// Allocates memory for a Decl with the given \p baseSize. If necessary,
1005945
/// it includes additional space immediately preceding the Decl for a ClangNode.
1006946
/// \note \p baseSize does not need to include space for a ClangNode if
@@ -2467,6 +2407,7 @@ class ValueDecl : public Decl {
24672407
friend class IsFinalRequest;
24682408
friend class IsDynamicRequest;
24692409
friend class IsImplicitlyUnwrappedOptionalRequest;
2410+
friend class InterfaceTypeRequest;
24702411
friend class Decl;
24712412
SourceLoc getLocFromSource() const { return NameLoc; }
24722413
protected:
@@ -3511,6 +3452,18 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
35113452
/// with placeholders for unimportable stored properties.
35123453
ArrayRef<Decl *> getStoredPropertiesAndMissingMemberPlaceholders() const;
35133454

3455+
/// Return the range of semantics attributes attached to this NominalTypeDecl.
3456+
auto getSemanticsAttrs() const
3457+
-> decltype(getAttrs().getAttributes<SemanticsAttr>()) {
3458+
return getAttrs().getAttributes<SemanticsAttr>();
3459+
}
3460+
3461+
bool hasSemanticsAttr(StringRef attrValue) const {
3462+
return llvm::any_of(getSemanticsAttrs(), [&](const SemanticsAttr *attr) {
3463+
return attrValue.equals(attr->Value);
3464+
});
3465+
}
3466+
35143467
// Implement isa/cast/dyncast/etc.
35153468
static bool classof(const Decl *D) {
35163469
return D->getKind() >= DeclKind::First_NominalTypeDecl &&

include/swift/AST/DiagnosticEngine.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,9 @@ namespace swift {
670670
/// Print diagnostic names after their messages
671671
bool printDiagnosticNames = false;
672672

673+
/// Use descriptive diagnostic style when available.
674+
bool useDescriptiveDiagnostics = false;
675+
673676
friend class InFlightDiagnostic;
674677
friend class DiagnosticTransaction;
675678
friend class CompoundDiagnosticTransaction;
@@ -713,6 +716,13 @@ namespace swift {
713716
return printDiagnosticNames;
714717
}
715718

719+
void setUseDescriptiveDiagnostics(bool val) {
720+
useDescriptiveDiagnostics = val;
721+
}
722+
bool getUseDescriptiveDiagnostics() const {
723+
return useDescriptiveDiagnostics;
724+
}
725+
716726
void ignoreDiagnostic(DiagID id) {
717727
state.setDiagnosticBehavior(id, DiagnosticState::Behavior::Ignore);
718728
}

include/swift/AST/DiagnosticsSema.def

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ NOTE(found_this_precedence_group,none,
883883
ERROR(unknown_precedence_group,none,
884884
"unknown precedence group %0", (Identifier))
885885
ERROR(precedence_group_cycle,none,
886+
"cycle in '%select{lowerThan|higherThan}0' relation", (bool))
887+
ERROR(higher_than_precedence_group_cycle,none,
886888
"cycle in higherThan relation: %0", (StringRef))
887889
ERROR(precedence_group_lower_within_module,none,
888890
"precedence group cannot be given lower precedence than group in same"
@@ -892,6 +894,8 @@ ERROR(precedence_group_redeclared,none,
892894
"precedence group redeclared", ())
893895
NOTE(previous_precedence_group_decl,none,
894896
"previous precedence group declaration here", ())
897+
NOTE(circular_reference_through_precedence_group, none,
898+
"through reference to precedence group %0 here", (Identifier))
895899

896900
//------------------------------------------------------------------------------
897901
// MARK: Expression Type Checking Errors
@@ -1126,13 +1130,20 @@ ERROR(extra_argument_named,none,
11261130
"extra argument %0 in call", (Identifier))
11271131
ERROR(extra_argument_positional,none,
11281132
"extra argument in call", ())
1133+
ERROR(extra_arguments_in_call,none,
1134+
"extra arguments at positions %0 in call", (StringRef))
11291135
ERROR(extra_argument_to_nullary_call,none,
11301136
"argument passed to call that takes no arguments", ())
11311137
ERROR(extra_trailing_closure_in_call,none,
11321138
"extra trailing closure passed in call", ())
11331139
ERROR(trailing_closure_bad_param,none,
11341140
"trailing closure passed to parameter of type %0 that does not "
11351141
"accept a closure", (Type))
1142+
NOTE(candidate_with_extraneous_args,none,
1143+
"candidate %0 requires %1 argument%s1, "
1144+
"but %2 %select{were|was}3 %select{provided|used in closure body}4",
1145+
(Type, unsigned, unsigned, bool, bool))
1146+
11361147
ERROR(no_accessible_initializers,none,
11371148
"%0 cannot be constructed because it has no accessible initializers",
11381149
(Type))
@@ -1720,12 +1731,18 @@ ERROR(type_does_not_conform_owner,none,
17201731
ERROR(type_does_not_conform_in_decl_ref,none,
17211732
"referencing %0 %1 on %2 requires that %3 conform to %4",
17221733
(DescriptiveDeclKind, DeclName, Type, Type, Type))
1734+
ERROR(type_does_not_conform_anyobject_in_decl_ref,none,
1735+
"referencing %0 %1 on %2 requires that %3 be a class type",
1736+
(DescriptiveDeclKind, DeclName, Type, Type, Type))
17231737
ERROR(type_does_not_conform_decl_owner,none,
17241738
"%0 %1 requires that %2 conform to %3",
17251739
(DescriptiveDeclKind, DeclName, Type, Type))
1726-
ERROR(type_does_not_conform_in_opaque_return,none,
1727-
"return type of %0 %1 requires that %2 conform to %3",
1740+
ERROR(type_does_not_conform_anyobject_decl_owner,none,
1741+
"%0 %1 requires that %2 be a class type",
17281742
(DescriptiveDeclKind, DeclName, Type, Type))
1743+
ERROR(type_does_not_conform_in_opaque_return,none,
1744+
"return type of %0 %1 requires that %2 %select{conform to %3|be a class type}4",
1745+
(DescriptiveDeclKind, DeclName, Type, Type, bool))
17291746
ERROR(types_not_equal_decl,none,
17301747
"%0 %1 requires the types %2 and %3 be equivalent",
17311748
(DescriptiveDeclKind, DeclName, Type, Type))
@@ -1744,6 +1761,8 @@ NOTE(where_requirement_failure_both_subst,none,
17441761
"where %0 = %1, %2 = %3", (Type, Type, Type, Type))
17451762
NOTE(requirement_implied_by_conditional_conformance,none,
17461763
"requirement from conditional conformance of %0 to %1", (Type, Type))
1764+
NOTE(wrapped_type_satisfies_requirement,none,
1765+
"wrapped type %0 satisfies this requirement; did you mean to unwrap?", (Type))
17471766
NOTE(candidate_types_conformance_requirement,none,
17481767
"candidate requires that %0 conform to %1 "
17491768
"(requirement specified as %2 == %3%4)",

include/swift/AST/IndexSubset.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,10 @@ class IndexSubset : public llvm::FoldingSetNode {
201201
id.AddInteger(index);
202202
}
203203

204-
void print(llvm::raw_ostream &s = llvm::outs()) const {
205-
s << '{';
206-
interleave(range(capacity), [this, &s](unsigned i) { s << contains(i); },
207-
[&s] { s << ", "; });
208-
s << '}';
209-
}
210-
211-
void dump(llvm::raw_ostream &s = llvm::errs()) const {
212-
s << "(index_subset capacity=" << capacity << " indices=(";
213-
interleave(getIndices(), [&s](unsigned i) { s << i; },
214-
[&s] { s << ", "; });
215-
s << "))";
216-
}
204+
void print(llvm::raw_ostream &s = llvm::outs()) const;
205+
LLVM_ATTRIBUTE_DEPRECATED(void dump(llvm::raw_ostream &s = llvm::errs())
206+
const LLVM_ATTRIBUTE_USED,
207+
"only for use within the debugger");
217208

218209
int findNext(int startIndex) const;
219210
int findFirst() const { return findNext(-1); }

include/swift/AST/LazyResolver.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ class LazyResolver {
5353
virtual void resolveWitness(const NormalProtocolConformance *conformance,
5454
ValueDecl *requirement) = 0;
5555

56-
/// Resolve the type and declaration attributes of a value.
57-
///
58-
/// This can be called when the type or signature of a value is needed.
59-
/// It does not perform full type-checking, only checks for basic
60-
/// consistency and provides the value a type.
61-
virtual void resolveDeclSignature(ValueDecl *VD) = 0;
62-
6356
/// Resolve any implicitly-declared constructors within the given nominal.
6457
virtual void resolveImplicitConstructors(NominalTypeDecl *nominal) = 0;
6558

0 commit comments

Comments
 (0)