@@ -1924,6 +1924,10 @@ namespace {
19241924 return diags.diagnose (std::forward<ArgTypes>(Args)...);
19251925 }
19261926
1927+ NeverNullType resolveOpenedExistentialArchetype (
1928+ TypeAttributes &attrs, TypeRepr *repr,
1929+ TypeResolutionOptions options);
1930+
19271931 NeverNullType resolveAttributedType (AttributedTypeRepr *repr,
19281932 TypeResolutionOptions options);
19291933 NeverNullType resolveAttributedType (TypeAttributes &attrs, TypeRepr *repr,
@@ -2307,6 +2311,67 @@ TypeResolver::resolveAttributedType(AttributedTypeRepr *repr,
23072311 return resolveAttributedType (attrs, repr->getTypeRepr (), options);
23082312}
23092313
2314+ // / In SIL, handle '@opened(UUID, constraintType) interfaceType',
2315+ // / which creates an opened archetype.
2316+ NeverNullType
2317+ TypeResolver::resolveOpenedExistentialArchetype (
2318+ TypeAttributes &attrs, TypeRepr *repr,
2319+ TypeResolutionOptions options) {
2320+ options.setContext (None);
2321+
2322+ auto *dc = getDeclContext ();
2323+ auto &ctx = dc->getASTContext ();
2324+
2325+ // The interface type is the type wrapped by the attribute. Resolve it
2326+ // with the fake <Self> generic parameter list uniquely stored in the
2327+ // ASTContext, and use structural resolution to avoid querying the
2328+ // DeclContext's generic signature, which is not the right signature
2329+ // for this.
2330+ auto structuralResolution = TypeResolution::forStructural (
2331+ dc, options,
2332+ /* unboundTyOpener*/ nullptr ,
2333+ /* placeholderHandler*/ nullptr );
2334+ TypeResolver interfaceTypeResolver (structuralResolution,
2335+ ctx.getSelfGenericParamList (dc));
2336+ auto interfaceType = interfaceTypeResolver.resolveType (repr, options);
2337+
2338+ // The constraint type is stored inside the attribute. It is resolved
2339+ // normally, as if it were written in the current context.
2340+ auto constraintType = resolveType (attrs.getConstraintType (), options);
2341+
2342+ Type archetypeType;
2343+ if (!constraintType->isExistentialType ()) {
2344+ diagnoseInvalid (repr, attrs.getLoc (TAK_opened),
2345+ diag::opened_bad_constraint_type,
2346+ constraintType);
2347+
2348+ archetypeType = ErrorType::get (constraintType->getASTContext ());
2349+ } else if (!interfaceType->isTypeParameter ()) {
2350+ diagnoseInvalid (repr, attrs.getLoc (TAK_opened),
2351+ diag::opened_bad_interface_type,
2352+ interfaceType);
2353+
2354+ archetypeType = ErrorType::get (interfaceType->getASTContext ());
2355+ } {
2356+ // The constraint type is written with respect to the surrounding
2357+ // generic environment.
2358+ constraintType = GenericEnvironment::mapTypeIntoContext (
2359+ resolution.getGenericSignature ().getGenericEnvironment (),
2360+ constraintType);
2361+
2362+ // The opened existential type is formed by mapping the interface type
2363+ // into a new opened generic environment.
2364+ archetypeType = OpenedArchetypeType::get (constraintType->getCanonicalType (),
2365+ interfaceType,
2366+ GenericSignature (),
2367+ attrs.getOpenedID ());
2368+ }
2369+
2370+ attrs.clearAttribute (TAK_opened);
2371+
2372+ return archetypeType;
2373+ }
2374+
23102375NeverNullType
23112376TypeResolver::resolveAttributedType (TypeAttributes &attrs, TypeRepr *repr,
23122377 TypeResolutionOptions options) {
@@ -2715,6 +2780,10 @@ TypeResolver::resolveAttributedType(TypeAttributes &attrs, TypeRepr *repr,
27152780 attrs.clearAttribute (TAK_unchecked);
27162781 }
27172782
2783+ if (attrs.has (TAK_opened)) {
2784+ ty = resolveOpenedExistentialArchetype (attrs, repr, options);
2785+ }
2786+
27182787 auto instanceOptions = options;
27192788 instanceOptions.setContext (None);
27202789
@@ -2834,21 +2903,6 @@ TypeResolver::resolveAttributedType(TypeAttributes &attrs, TypeRepr *repr,
28342903 attrs.clearAttribute (TAK_noDerivative);
28352904 }
28362905
2837- // In SIL, handle @opened (n), which creates an existential archetype.
2838- if (attrs.has (TAK_opened)) {
2839- if (!ty->isExistentialType ()) {
2840- diagnoseInvalid (repr, attrs.getLoc (TAK_opened), diag::opened_non_protocol,
2841- ty);
2842- } else {
2843- ty = GenericEnvironment::mapTypeIntoContext (
2844- resolution.getGenericSignature ().getGenericEnvironment (), ty);
2845- ty = OpenedArchetypeType::get (ty->getCanonicalType (),
2846- resolution.getGenericSignature (),
2847- attrs.OpenedID );
2848- }
2849- attrs.clearAttribute (TAK_opened);
2850- }
2851-
28522906 // In SIL files *only*, permit @weak and @unowned to apply directly to types.
28532907 if (attrs.hasOwnership ()) {
28542908 if (auto SF = getDeclContext ()->getParentSourceFile ()) {
0 commit comments