Skip to content

Commit 758cdae

Browse files
authored
Merge branch 'main' into se-pointer-convenience
2 parents bb05e85 + 10f3828 commit 758cdae

File tree

211 files changed

+13834
-8807
lines changed

Some content is hidden

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

211 files changed

+13834
-8807
lines changed

CHANGELOG.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,48 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
55

66
## Swift 5.7
77

8+
* The compiler now emits a warning when a non-final class conforms to a protocol that imposes a same-type requirement between `Self` and an associated type. This is because such a requirement makes the conformance unsound for subclasses.
9+
10+
For example, Swift 5.6 would allow the following code, which at runtime would construct an instanec of `C` and not `SubC` as expected:
11+
12+
```swift
13+
protocol P {
14+
associatedtype A : Q where Self == Self.A.B
15+
}
16+
17+
protocol Q {
18+
associatedtype B
19+
20+
static func getB() -> B
21+
}
22+
23+
class C : P {
24+
typealias A = D
25+
}
26+
27+
class D : Q {
28+
typealias B = C
29+
30+
static func getB() -> C { return C() }
31+
}
32+
33+
extension P {
34+
static func getAB() -> Self {
35+
// This is well-typed, because `Self.A.getB()` returns
36+
// `Self.A.B`, which is equivalent to `Self`.
37+
return Self.A.getB()
38+
}
39+
}
40+
41+
class SubC : C {}
42+
43+
// P.getAB() declares a return type of `Self`, so it should
44+
// return `SubC`, but it actually returns a `C`.
45+
print(SubC.getAB())
46+
```
47+
48+
To make the above example correct, either the class `C` needs to become `final` (in which case `SubC` cannot be declared) or protocol `P` needs to be re-designed to not include the same-type requirement `Self == Self.A.B`.
49+
850
* [SE-0341][]:
951

1052
Opaque types can now be used in the parameters of functions and subscripts, wher they provide a shorthand syntax for the introduction of a generic parameter. For example, the following:

include/swift/APIDigester/ModuleAnalyzerNodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ int dumpSDKContent(const CompilerInvocation &InitInvok,
816816
const llvm::StringSet<> &ModuleNames,
817817
StringRef OutputFile, CheckerOptions Opts);
818818

819-
void dumpModuleContent(ModuleDecl *MD, StringRef OutputFile, bool ABI);
819+
void dumpModuleContent(ModuleDecl *MD, StringRef OutputFile, bool ABI, bool Empty);
820820

821821
/// Mostly for testing purposes, this function de-serializes the SDK dump in
822822
/// dumpPath and re-serialize them to OutputPath. If the tool performs correctly,

include/swift/AST/Decl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5504,6 +5504,7 @@ enum class ParamSpecifier : uint8_t {
55045504
class ParamDecl : public VarDecl {
55055505
friend class DefaultArgumentInitContextRequest;
55065506
friend class DefaultArgumentExprRequest;
5507+
friend class DefaultArgumentTypeRequest;
55075508

55085509
enum class ArgumentNameFlags : uint8_t {
55095510
/// Whether or not this parameter is destructed.
@@ -5524,6 +5525,9 @@ class ParamDecl : public VarDecl {
55245525
struct alignas(1 << StoredDefaultArgumentAlignInBits) StoredDefaultArgument {
55255526
PointerUnion<Expr *, VarDecl *> DefaultArg;
55265527

5528+
/// The type of the default argument expression.
5529+
Type ExprType;
5530+
55275531
/// Stores the context for the default argument as well as a bit to
55285532
/// indicate whether the default expression has been type-checked.
55295533
llvm::PointerIntPair<Initializer *, 1, bool> InitContextAndIsTypeChecked;
@@ -5641,6 +5645,10 @@ class ParamDecl : public VarDecl {
56415645
return nullptr;
56425646
}
56435647

5648+
/// Retrieve the type of the default expression (if any) associated with
5649+
/// this parameter declaration.
5650+
Type getTypeOfDefaultExpr() const;
5651+
56445652
VarDecl *getStoredProperty() const {
56455653
if (auto stored = DefaultValueAndFlags.getPointer())
56465654
return stored->DefaultArg.dyn_cast<VarDecl *>();
@@ -5655,6 +5663,10 @@ class ParamDecl : public VarDecl {
56555663
/// parameter's fully type-checked default argument.
56565664
void setDefaultExpr(Expr *E, bool isTypeChecked);
56575665

5666+
/// Sets a type of default expression associated with this parameter.
5667+
/// This should only be called by deserialization.
5668+
void setDefaultExprType(Type type);
5669+
56585670
void setStoredProperty(VarDecl *var);
56595671

56605672
/// Retrieve the initializer context for the parameter's default argument.

include/swift/AST/DiagnosticsSema.def

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ NOTE(extended_type_declared_here,none,
3838
"extended type declared here", ())
3939
NOTE(opaque_return_type_declared_here,none,
4040
"opaque return type declared here", ())
41+
NOTE(default_value_declared_here,none,
42+
"default value declared here", ())
4143

4244
//------------------------------------------------------------------------------
4345
// MARK: Constraint solver diagnostics
@@ -140,8 +142,8 @@ ERROR(could_not_use_instance_member_on_type,none,
140142
"%select{| instance of nested}3 type %0",
141143
(Type, DeclNameRef, Type, bool))
142144
ERROR(could_not_use_member_on_existential,none,
143-
"member %1 cannot be used on value of protocol type %0; use a generic"
144-
" constraint instead",
145+
"member %1 cannot be used on value of protocol type %0; consider using a"
146+
" generic constraint instead",
145147
(Type, DeclNameRef))
146148
FIXIT(replace_with_type,"%0",(Type))
147149
FIXIT(insert_type_qualification,"%0.",(Type))
@@ -1942,6 +1944,11 @@ ERROR(type_does_not_conform,none,
19421944
WARNING(type_does_not_conform_swiftui_warning,none,
19431945
"type %0 does not conform to protocol %1", (Type, Type))
19441946

1947+
ERROR(non_final_class_cannot_conform_to_self_same_type,none,
1948+
"non-final class %0 cannot safely conform to protocol %1, "
1949+
"which requires that 'Self' is exactly equal to %2",
1950+
(Type, Type, Type))
1951+
19451952
ERROR(cannot_use_nil_with_this_type,none,
19461953
"'nil' cannot be used in context expecting type %0", (Type))
19471954

@@ -2784,9 +2791,11 @@ WARNING(anyobject_class_inheritance_deprecated,Deprecation,
27842791
ERROR(multiple_inheritance,none,
27852792
"multiple inheritance from classes %0 and %1", (Type, Type))
27862793
ERROR(inheritance_from_non_protocol_or_class,none,
2787-
"inheritance from non-protocol, non-class type %0", (Type))
2794+
"inheritance from non-protocol, non-class type '%0'", (StringRef))
27882795
ERROR(inheritance_from_non_protocol,none,
2789-
"inheritance from non-protocol type %0", (Type))
2796+
"inheritance from non-protocol type '%0'", (StringRef))
2797+
ERROR(inheritance_from_anyobject,none,
2798+
"only protocols can inherit from 'AnyObject'", ())
27902799
ERROR(inheritance_from_parameterized_protocol,none,
27912800
"cannot inherit from protocol type with generic argument %0", (Type))
27922801
ERROR(superclass_not_first,none,
@@ -6238,5 +6247,28 @@ ERROR(type_sequence_on_non_generic_param, none,
62386247
"'@_typeSequence' must appear on a generic parameter",
62396248
())
62406249

6250+
//------------------------------------------------------------------------------
6251+
// MARK: Type inference from default expressions
6252+
//------------------------------------------------------------------------------
6253+
6254+
ERROR(cannot_default_generic_parameter_inferrable_from_another_parameter, none,
6255+
"cannot use default expression for inference of %0 because it "
6256+
"is inferrable from parameters %1",
6257+
(Type, StringRef))
6258+
6259+
ERROR(cannot_default_generic_parameter_inferrable_through_same_type, none,
6260+
"cannot use default expression for inference of %0 because it "
6261+
"is inferrable through same-type requirement: '%1'",
6262+
(Type, StringRef))
6263+
6264+
ERROR(cannot_default_generic_parameter_invalid_requirement, none,
6265+
"cannot use default expression for inference of %0 because "
6266+
"requirement '%1' refers to other generic parameters",
6267+
(Type, StringRef))
6268+
6269+
ERROR(cannot_convert_default_value_type_to_argument_type, none,
6270+
"cannot convert default value of type %0 to expected argument type %1 for parameter #%2",
6271+
(Type, Type, unsigned))
6272+
62416273
#define UNDEFINE_DIAGNOSTIC_MACROS
62426274
#include "DefineDiagnosticMacros.h"

include/swift/AST/Expr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
491491
return getSemanticsProvidingExpr()->getKind() == ExprKind::InOut;
492492
}
493493

494+
bool printConstExprValue(llvm::raw_ostream *OS) const;
494495
bool isSemanticallyConstExpr() const;
495496

496497
/// Returns false if this expression needs to be wrapped in parens when

include/swift/AST/SearchPathOptions.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ enum class ModuleSearchPathKind {
3636

3737
/// A single module search path that can come from different sources, e.g.
3838
/// framework search paths, import search path etc.
39-
struct ModuleSearchPath {
40-
/// The actual path of the module search path. References a search path string
41-
/// stored inside \c SearchPathOptions, which must outlive this reference.
42-
StringRef Path;
39+
class ModuleSearchPath : public llvm::RefCountedBase<ModuleSearchPath> {
40+
/// The actual path of the module search path.
41+
std::string Path;
4342

4443
/// The kind of the search path.
4544
ModuleSearchPathKind Kind;
@@ -52,6 +51,18 @@ struct ModuleSearchPath {
5251
/// different file names in \c searchPathsContainingFile.
5352
unsigned Index;
5453

54+
public:
55+
ModuleSearchPath(StringRef Path, ModuleSearchPathKind Kind, bool IsSystem,
56+
unsigned Index)
57+
: Path(Path), Kind(Kind), IsSystem(IsSystem), Index(Index) {}
58+
59+
StringRef getPath() const { return Path; }
60+
ModuleSearchPathKind getKind() const { return Kind; }
61+
62+
bool isSystem() const { return IsSystem; }
63+
64+
unsigned getIndex() const { return Index; }
65+
5566
bool operator<(const ModuleSearchPath &Other) const {
5667
if (this->Kind == Other.Kind) {
5768
return this->Index < Other.Index;
@@ -61,6 +72,8 @@ struct ModuleSearchPath {
6172
}
6273
};
6374

75+
using ModuleSearchPathPtr = llvm::IntrusiveRefCntPtr<ModuleSearchPath>;
76+
6477
class SearchPathOptions;
6578

6679
/// Maintains a mapping of filenames to search paths that contain a file with
@@ -97,7 +110,7 @@ class ModuleSearchPathLookup {
97110
const SearchPathOptions *Opts;
98111
} State;
99112

100-
llvm::StringMap<SmallVector<ModuleSearchPath, 4>> LookupTable;
113+
llvm::StringMap<SmallVector<ModuleSearchPathPtr, 4>> LookupTable;
101114

102115
/// Scan the directory at \p SearchPath for files and add those files to the
103116
/// lookup table. \p Kind specifies the search path kind and \p Index the

include/swift/AST/TypeCheckRequests.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AccessorDecl;
4141
enum class AccessorKind;
4242
class ContextualPattern;
4343
class DefaultArgumentExpr;
44+
class DefaultArgumentType;
4445
class ClosureExpr;
4546
class GenericParamList;
4647
class PrecedenceGroupDecl;
@@ -2665,6 +2666,26 @@ class DefaultArgumentExprRequest
26652666
void cacheResult(Expr *expr) const;
26662667
};
26672668

2669+
/// Computes the type of the default expression for a given parameter.
2670+
class DefaultArgumentTypeRequest
2671+
: public SimpleRequest<DefaultArgumentTypeRequest, Type(ParamDecl *),
2672+
RequestFlags::SeparatelyCached> {
2673+
public:
2674+
using SimpleRequest::SimpleRequest;
2675+
2676+
private:
2677+
friend SimpleRequest;
2678+
2679+
// Evaluation.
2680+
Type evaluate(Evaluator &evaluator, ParamDecl *param) const;
2681+
2682+
public:
2683+
// Separate caching.
2684+
bool isCached() const { return true; }
2685+
Optional<Type> getCachedResult() const;
2686+
void cacheResult(Type type) const;
2687+
};
2688+
26682689
/// Computes the fully type-checked caller-side default argument within the
26692690
/// context of the call site that it will be inserted into.
26702691
class CallerSideDefaultArgExprRequest

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ SWIFT_REQUEST(TypeChecker, CustomAttrTypeRequest,
5959
SeparatelyCached, NoLocationInfo)
6060
SWIFT_REQUEST(TypeChecker, DefaultArgumentExprRequest,
6161
Expr *(ParamDecl *), SeparatelyCached, NoLocationInfo)
62+
SWIFT_REQUEST(TypeChecker, DefaultArgumentTypeRequest,
63+
Type(ParamDecl *), SeparatelyCached, NoLocationInfo)
6264
SWIFT_REQUEST(TypeChecker, DefaultArgumentInitContextRequest,
6365
Initializer *(ParamDecl *), SeparatelyCached, NoLocationInfo)
6466
SWIFT_REQUEST(TypeChecker, DefaultDefinitionTypeRequest,

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
696696
///
697697
/// "Unresolved" dependent member types have no known associated type,
698698
/// and are only used transiently in the type checker.
699-
const DependentMemberType *findUnresolvedDependentMemberType();
699+
DependentMemberType *findUnresolvedDependentMemberType();
700700

701701
/// Return the root generic parameter of this type parameter type.
702702
GenericTypeParamType *getRootGenericParam();

include/swift/Basic/LangOptions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,11 @@ namespace swift {
530530
RequirementMachineMode RequirementMachineInferredSignatures =
531531
RequirementMachineMode::Disabled;
532532

533+
/// Disable preprocessing pass to eliminate conformance requirements
534+
/// on generic parameters which are made concrete. Usually you want this
535+
/// enabled. It can be disabled for debugging and testing.
536+
bool EnableRequirementMachineConcreteContraction = true;
537+
533538
/// Enables dumping type witness systems from associated type inference.
534539
bool DumpTypeWitnessSystems = false;
535540

@@ -718,6 +723,10 @@ namespace swift {
718723
/// closures.
719724
bool EnableMultiStatementClosureInference = false;
720725

726+
/// Enable experimental support for generic parameter inference in
727+
/// parameter positions from associated default expressions.
728+
bool EnableTypeInferenceFromDefaultArguments = false;
729+
721730
/// See \ref FrontendOptions.PrintFullConvention
722731
bool PrintFullConvention = false;
723732
};

0 commit comments

Comments
 (0)