Skip to content

Commit eaa781e

Browse files
authored
Merge pull request #4068 from swiftwasm/main
[pull] swiftwasm from main
2 parents 404cce3 + 43324e8 commit eaa781e

File tree

108 files changed

+2760
-532
lines changed

Some content is hidden

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

108 files changed

+2760
-532
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ CHANGELOG
33

44
_**Note:** This is in reverse chronological order, so newer entries are added to the top._
55

6+
## Swift Next
7+
8+
* [SE-0328][]:
9+
10+
Opaque types (expressed with 'some') can now be used in structural positions
11+
within a result type, including having multiple opaque types in the same
12+
result. For example:
13+
14+
```
15+
func getSomeDictionary() -> [some Hashable: some Codable] {
16+
return [ 1: "One", 2: "Two" ]
17+
}
18+
```
619
Swift 5.6
720
---------
821

@@ -8761,6 +8774,7 @@ Swift 1.0
87618774
[SE-0316]: <https://github.com/apple/swift-evolution/blob/main/proposals/0316-global-actors.md>
87628775
[SE-0324]: <https://github.com/apple/swift-evolution/blob/main/proposals/0324-c-lang-pointer-arg-conversion.md>
87638776
[SE-0323]: <https://github.com/apple/swift-evolution/blob/main/proposals/0323-async-main-semantics.md>
8777+
[SE-0328]: <https://github.com/apple/swift-evolution/blob/main/proposals/0328-structural-opaque-result-types.md>
87648778

87658779
[SR-75]: <https://bugs.swift.org/browse/SR-75>
87668780
[SR-106]: <https://bugs.swift.org/browse/SR-106>

include/swift/AST/ASTContext.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ class ASTContext final {
357357
DelayedPatternContexts;
358358

359359
/// Cache of module names that fail the 'canImport' test in this context.
360-
mutable llvm::SmallPtrSet<Identifier, 8> FailedModuleImportNames;
361-
360+
mutable llvm::StringSet<> FailedModuleImportNames;
361+
362362
/// Set if a `-module-alias` was passed. Used to store mapping between module aliases and
363363
/// their corresponding real names, and vice versa for a reverse lookup, which is needed to check
364364
/// if the module names appearing in source files are aliases or real names.
@@ -980,10 +980,10 @@ class ASTContext final {
980980
///
981981
/// Note that even if this check succeeds, errors may still occur if the
982982
/// module is loaded in full.
983-
bool canImportModuleImpl(ImportPath::Element ModulePath,
984-
llvm::VersionTuple version,
985-
bool underlyingVersion,
983+
bool canImportModuleImpl(ImportPath::Module ModulePath,
984+
llvm::VersionTuple version, bool underlyingVersion,
986985
bool updateFailingList) const;
986+
987987
public:
988988
namelookup::ImportCache &getImportCache() const;
989989

@@ -1012,10 +1012,10 @@ class ASTContext final {
10121012
///
10131013
/// Note that even if this check succeeds, errors may still occur if the
10141014
/// module is loaded in full.
1015-
bool canImportModule(ImportPath::Element ModulePath,
1015+
bool canImportModule(ImportPath::Module ModulePath,
10161016
llvm::VersionTuple version = llvm::VersionTuple(),
10171017
bool underlyingVersion = false);
1018-
bool canImportModule(ImportPath::Element ModulePath,
1018+
bool canImportModule(ImportPath::Module ModulePath,
10191019
llvm::VersionTuple version = llvm::VersionTuple(),
10201020
bool underlyingVersion = false) const;
10211021

@@ -1160,6 +1160,9 @@ class ASTContext final {
11601160
InheritedProtocolConformance *
11611161
getInheritedConformance(Type type, ProtocolConformance *inherited);
11621162

1163+
/// Check if \p decl is included in LazyContexts.
1164+
bool isLazyContext(const DeclContext *decl);
1165+
11631166
/// Get the lazy data for the given declaration.
11641167
///
11651168
/// \param lazyLoader If non-null, the lazy loader to use when creating the

include/swift/AST/ASTMangler.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ class ASTMangler : public Mangler {
339339
}
340340

341341
void appendBoundGenericArgs(Type type, GenericSignature sig,
342-
bool &isFirstArgList);
342+
bool &isFirstArgList,
343+
const ValueDecl *forDecl = nullptr);
343344

344345
/// Append the bound generics arguments for the given declaration context
345346
/// based on a complete substitution map.
@@ -349,18 +350,21 @@ class ASTMangler : public Mangler {
349350
unsigned appendBoundGenericArgs(DeclContext *dc,
350351
GenericSignature sig,
351352
SubstitutionMap subs,
352-
bool &isFirstArgList);
353+
bool &isFirstArgList,
354+
const ValueDecl *forDecl = nullptr);
353355

354356
/// Append the bound generic arguments as a flat list, disregarding depth.
355357
void appendFlatGenericArgs(SubstitutionMap subs,
356-
GenericSignature sig);
358+
GenericSignature sig,
359+
const ValueDecl *forDecl = nullptr);
357360

358361
/// Append any retroactive conformances.
359362
void appendRetroactiveConformances(Type type, GenericSignature sig);
360363
void appendRetroactiveConformances(SubstitutionMap subMap,
361364
GenericSignature sig,
362365
ModuleDecl *fromModule);
363-
void appendImplFunctionType(SILFunctionType *fn, GenericSignature sig);
366+
void appendImplFunctionType(SILFunctionType *fn, GenericSignature sig,
367+
const ValueDecl *forDecl = nullptr);
364368

365369
void appendContextOf(const ValueDecl *decl);
366370

include/swift/AST/ClangModuleLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ class ClangModuleLoader : public ModuleLoader {
182182
/// Imports a clang decl directly, rather than looking up its name.
183183
virtual Decl *importDeclDirectly(const clang::NamedDecl *decl) = 0;
184184

185+
/// Emits any import diagnostics associated with the provided decl.
186+
virtual void diagnoseDeclDirectly(const clang::NamedDecl *decl) = 0;
187+
185188
/// Instantiate and import class template using given arguments.
186189
///
187190
/// This method will find the clang::ClassTemplateSpecialization decl if

include/swift/AST/Decl.h

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,45 +2699,63 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
26992699
/// The declared type uses a special kind of archetype type to represent
27002700
/// abstracted types, e.g. `(some P, some Q)` becomes `((opaque archetype 0),
27012701
/// (opaque archetype 1))`.
2702-
class OpaqueTypeDecl : public GenericTypeDecl {
2702+
class OpaqueTypeDecl final :
2703+
public GenericTypeDecl,
2704+
private llvm::TrailingObjects<OpaqueTypeDecl, OpaqueReturnTypeRepr *> {
2705+
friend TrailingObjects;
2706+
27032707
/// The original declaration that "names" the opaque type. Although a specific
27042708
/// opaque type cannot be explicitly named, oapque types can propagate
27052709
/// arbitrarily through expressions, so we need to know *which* opaque type is
27062710
/// propagated.
2707-
ValueDecl *NamingDecl;
2711+
///
2712+
/// The bit indicates whether there are any trailing
2713+
/// OpaqueReturnTypeReprs.
2714+
llvm::PointerIntPair<ValueDecl *, 1>
2715+
NamingDeclAndHasOpaqueReturnTypeRepr;
27082716

27092717
/// The generic signature of the opaque interface to the type. This is the
27102718
/// outer generic signature with added generic parameters representing the
27112719
/// abstracted underlying types.
27122720
GenericSignature OpaqueInterfaceGenericSignature;
27132721

2714-
/// The type repr of the underlying type. Might be null if no source location
2715-
/// is availble, e.g. if this decl was loaded from a serialized module.
2716-
OpaqueReturnTypeRepr *UnderlyingInterfaceRepr;
2717-
2718-
/// The generic parameter that represents the underlying type.
2719-
GenericTypeParamType *UnderlyingInterfaceType;
2720-
27212722
/// If known, the underlying type and conformances of the opaque type,
27222723
/// expressed as a SubstitutionMap for the opaque interface generic signature.
27232724
/// This maps types in the interface generic signature to the outer generic
27242725
/// signature of the original declaration.
27252726
Optional<SubstitutionMap> UnderlyingTypeSubstitutions;
27262727

27272728
mutable Identifier OpaqueReturnTypeIdentifier;
2728-
2729-
public:
2729+
27302730
OpaqueTypeDecl(ValueDecl *NamingDecl, GenericParamList *GenericParams,
27312731
DeclContext *DC,
27322732
GenericSignature OpaqueInterfaceGenericSignature,
2733-
OpaqueReturnTypeRepr *UnderlyingInterfaceRepr,
2734-
GenericTypeParamType *UnderlyingInterfaceType);
2733+
ArrayRef<OpaqueReturnTypeRepr *> OpaqueReturnTypeReprs);
2734+
2735+
unsigned getNumOpaqueReturnTypeReprs() const {
2736+
return NamingDeclAndHasOpaqueReturnTypeRepr.getInt()
2737+
? getOpaqueGenericParams().size()
2738+
: 0;
2739+
}
2740+
2741+
size_t numTrailingObjects(OverloadToken<OpaqueReturnTypeRepr *>) const {
2742+
return getNumOpaqueReturnTypeReprs();
2743+
}
2744+
2745+
public:
2746+
static OpaqueTypeDecl *get(
2747+
ValueDecl *NamingDecl, GenericParamList *GenericParams,
2748+
DeclContext *DC,
2749+
GenericSignature OpaqueInterfaceGenericSignature,
2750+
ArrayRef<OpaqueReturnTypeRepr *> OpaqueReturnTypeReprs);
27352751

2736-
ValueDecl *getNamingDecl() const { return NamingDecl; }
2752+
ValueDecl *getNamingDecl() const {
2753+
return NamingDeclAndHasOpaqueReturnTypeRepr.getPointer();
2754+
}
27372755

27382756
void setNamingDecl(ValueDecl *D) {
2739-
assert(!NamingDecl && "already have naming decl");
2740-
NamingDecl = D;
2757+
assert(!getNamingDecl() && "already have naming decl");
2758+
NamingDeclAndHasOpaqueReturnTypeRepr.setPointer(D);
27412759
}
27422760

27432761
/// Is this opaque type the opaque return type of the given function?
@@ -2754,11 +2772,24 @@ class OpaqueTypeDecl : public GenericTypeDecl {
27542772
GenericSignature getOpaqueInterfaceGenericSignature() const {
27552773
return OpaqueInterfaceGenericSignature;
27562774
}
2757-
2758-
GenericTypeParamType *getUnderlyingInterfaceType() const {
2759-
return UnderlyingInterfaceType;
2775+
2776+
/// Retrieve the generic parameters that represent the opaque types described by this opaque
2777+
/// type declaration.
2778+
TypeArrayView<GenericTypeParamType> getOpaqueGenericParams() const {
2779+
return OpaqueInterfaceGenericSignature.getInnermostGenericParams();
27602780
}
2761-
2781+
2782+
/// Retrieve the buffer containing the opaque return type
2783+
/// representations that correspond to the opaque generic parameters.
2784+
ArrayRef<OpaqueReturnTypeRepr *> getOpaqueReturnTypeReprs() const {
2785+
return {
2786+
getTrailingObjects<OpaqueReturnTypeRepr *>(),
2787+
getNumOpaqueReturnTypeReprs()
2788+
};
2789+
}
2790+
2791+
/// The substitutions that map the generic parameters of the opaque type to
2792+
/// their underlying types, when that information is known.
27622793
Optional<SubstitutionMap> getUnderlyingTypeSubstitutions() const {
27632794
return UnderlyingTypeSubstitutions;
27642795
}

include/swift/AST/DiagnosticEngine.h

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "llvm/Support/SaveAndRestore.h"
3434
#include "llvm/Support/VersionTuple.h"
3535

36+
namespace clang {
37+
class NamedDecl;
38+
}
39+
3640
namespace swift {
3741
class Decl;
3842
class DeclAttribute;
@@ -116,7 +120,8 @@ namespace swift {
116120
VersionTuple,
117121
LayoutConstraint,
118122
ActorIsolation,
119-
Diagnostic
123+
Diagnostic,
124+
ClangDecl
120125
};
121126

122127
namespace diag {
@@ -149,6 +154,7 @@ namespace swift {
149154
LayoutConstraint LayoutConstraintVal;
150155
ActorIsolation ActorIsolationVal;
151156
DiagnosticInfo *DiagnosticVal;
157+
const clang::NamedDecl *ClangDecl;
152158
};
153159

154160
public:
@@ -251,6 +257,9 @@ namespace swift {
251257
DiagnosticVal(D) {
252258
}
253259

260+
DiagnosticArgument(const clang::NamedDecl *ND)
261+
: Kind(DiagnosticArgumentKind::ClangDecl), ClangDecl(ND) {}
262+
254263
/// Initializes a diagnostic argument using the underlying type of the
255264
/// given enum.
256265
template<
@@ -356,6 +365,11 @@ namespace swift {
356365
assert(Kind == DiagnosticArgumentKind::Diagnostic);
357366
return DiagnosticVal;
358367
}
368+
369+
const clang::NamedDecl *getAsClangDecl() const {
370+
assert(Kind == DiagnosticArgumentKind::ClangDecl);
371+
return ClangDecl;
372+
}
359373
};
360374

361375
/// Describes the current behavior to take with a diagnostic.
@@ -1327,18 +1341,20 @@ namespace swift {
13271341
builder();
13281342
}
13291343

1330-
/// Temporary on-stack storage and unescaping for encoded diagnostic
1331-
/// messages.
1332-
class EncodedDiagnosticMessage {
1333-
llvm::SmallString<128> Buf;
1344+
void printClangDeclName(const clang::NamedDecl *ND, llvm::raw_ostream &os);
13341345

1335-
public:
1336-
/// \param S A string with an encoded message
1337-
EncodedDiagnosticMessage(StringRef S);
1346+
/// Temporary on-stack storage and unescaping for encoded diagnostic
1347+
/// messages.
1348+
class EncodedDiagnosticMessage {
1349+
llvm::SmallString<128> Buf;
13381350

1339-
/// The unescaped message to display to the user.
1340-
const StringRef Message;
1341-
};
1351+
public:
1352+
/// \param S A string with an encoded message
1353+
EncodedDiagnosticMessage(StringRef S);
1354+
1355+
/// The unescaped message to display to the user.
1356+
const StringRef Message;
1357+
};
13421358

13431359
/// Returns a value that can be used to select between accessor kinds in
13441360
/// diagnostics.

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,30 @@ WARNING(import_multiple_mainactor_attr,none,
100100

101101
ERROR(module_map_not_found, none, "module map file '%0' not found", (StringRef))
102102

103+
NOTE(macro_not_imported_unsupported_operator, none, "operator not supported in macro arithmetic", ())
104+
NOTE(macro_not_imported_unsupported_named_operator, none, "operator '%0' not supported in macro arithmetic", (StringRef))
105+
NOTE(macro_not_imported_invalid_string_literal, none, "invalid string literal", ())
106+
NOTE(macro_not_imported_invalid_numeric_literal, none, "invalid numeric literal", ())
107+
NOTE(macro_not_imported_unsupported_literal, none, "only numeric and string macro literals supported", ())
108+
NOTE(macro_not_imported_nested_cast, none, "non-null nested casts not supported", ())
109+
110+
ERROR(macro_not_imported_function_like, none, "macro '%0' not imported: function like macros not supported", (StringRef))
111+
ERROR(macro_not_imported_unsupported_structure, none, "macro '%0' not imported: structure not supported", (StringRef))
112+
ERROR(macro_not_imported, none, "macro '%0' not imported", (StringRef))
113+
114+
NOTE(return_type_not_imported, none, "return type not imported", ())
115+
NOTE(parameter_type_not_imported, none, "parameter %0 not imported", (const clang::NamedDecl*))
116+
NOTE(incomplete_interface, none, "interface %0 is incomplete", (const clang::NamedDecl*))
117+
NOTE(incomplete_protocol, none, "protocol %0 is incomplete", (const clang::NamedDecl*))
118+
NOTE(unsupported_builtin_type, none, "built-in type '%0' not supported", (StringRef))
119+
120+
WARNING(record_field_not_imported, none, "field %0 not imported", (const clang::NamedDecl*))
121+
WARNING(invoked_func_not_imported, none, "function %0 not imported", (const clang::NamedDecl*))
122+
WARNING(record_method_not_imported, none, "method %0 not imported", (const clang::NamedDecl*))
123+
WARNING(objc_property_not_imported, none, "property %0 not imported", (const clang::NamedDecl*))
124+
125+
NOTE(forward_declared_interface_label, none, "interface %0 forward declared here", (const clang::NamedDecl*))
126+
NOTE(forward_declared_protocol_label, none, "protocol %0 forward declared here", (const clang::NamedDecl*))
127+
103128
#define UNDEFINE_DIAGNOSTIC_MACROS
104129
#include "DefineDiagnosticMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,9 +1876,6 @@ ERROR(opaque_type_invalid_constraint,none,
18761876
"and/or a base class", ())
18771877
NOTE(opaque_of_optional_rewrite,none,
18781878
"did you mean to write an optional of an 'opaque' type?", ())
1879-
ERROR(more_than_one_opaque_type,none,
1880-
"%0 contains multiple 'opaque' types, but only one 'opaque' type is "
1881-
"supported", (TypeRepr*))
18821879
ERROR(inferred_opaque_type,none,
18831880
"property definition has inferred type %0, involving the 'some' "
18841881
"return type of another declaration", (Type))
@@ -4135,8 +4132,8 @@ ERROR(opaque_type_no_underlying_type_candidates,none,
41354132
"function declares an opaque return type, but has no return statements "
41364133
"in its body from which to infer an underlying type", ())
41374134
ERROR(opaque_type_mismatched_underlying_type_candidates,none,
4138-
"function declares an opaque return type, but the return statements "
4139-
"in its body do not have matching underlying types", ())
4135+
"function declares an opaque return type %0, but the return statements "
4136+
"in its body do not have matching underlying types", (TypeRepr *))
41404137
NOTE(opaque_type_underlying_type_candidate_here,none,
41414138
"return statement has underlying type %0", (Type))
41424139
ERROR(opaque_type_self_referential_underlying_type,none,

include/swift/AST/Expr.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,8 +3018,13 @@ class LinearToDifferentiableFunctionExpr : public ImplicitConversionExpr {
30183018
/// (opaque type)" and "S<T> ---> S<(opaque type)>".
30193019
class UnderlyingToOpaqueExpr : public ImplicitConversionExpr {
30203020
public:
3021-
UnderlyingToOpaqueExpr(Expr *subExpr, Type ty)
3022-
: ImplicitConversionExpr(ExprKind::UnderlyingToOpaque, subExpr, ty) {}
3021+
/// The substitutions to be applied to the opaque type declaration to
3022+
/// produce the resulting type.
3023+
const SubstitutionMap substitutions;
3024+
3025+
UnderlyingToOpaqueExpr(Expr *subExpr, Type ty, SubstitutionMap substitutions)
3026+
: ImplicitConversionExpr(ExprKind::UnderlyingToOpaque, subExpr, ty),
3027+
substitutions(substitutions) {}
30233028

30243029
static bool classof(const Expr *E) {
30253030
return E->getKind() == ExprKind::UnderlyingToOpaque;

0 commit comments

Comments
 (0)