Skip to content

[ConstraintSystem] Always choose an available type eraser type. #79406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/DeclAttr.def
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ SIMPLE_DECL_ATTR(_inheritsConvenienceInitializers, InheritsConvenienceInitialize
OnClass | UserInaccessible | NotSerialized | APIStableToAdd | ABIStableToAdd | APIBreakingToRemove | ABIBreakingToRemove,
93)
DECL_ATTR(_typeEraser, TypeEraser,
OnProtocol | UserInaccessible | ABIStableToAdd | ABIBreakingToRemove | APIStableToAdd | APIBreakingToRemove,
OnProtocol | UserInaccessible | ABIStableToAdd | ABIBreakingToRemove | APIStableToAdd | APIBreakingToRemove | AllowMultipleAttributes,
94)
SIMPLE_DECL_ATTR(IBSegueAction, IBSegueAction,
OnFunc | ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
Expand Down
32 changes: 27 additions & 5 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4553,6 +4553,8 @@ Expr *ConstraintSystem::buildAutoClosureExpr(Expr *expr,
Expr *ConstraintSystem::buildTypeErasedExpr(Expr *expr, DeclContext *dc,
Type contextualType,
ContextualTypePurpose purpose) {
auto &ctx = dc->getASTContext();

if (purpose != CTP_ReturnStmt)
return expr;

Expand All @@ -4571,13 +4573,33 @@ Expr *ConstraintSystem::buildTypeErasedExpr(Expr *expr, DeclContext *dc,
return expr;

auto *PD = protocols.front();
auto *attr = PD->getAttrs().getAttribute<TypeEraserAttr>();
if (!attr)

auto contextAvailability =
TypeChecker::availabilityAtLocation(expr->getLoc(), dc);
auto refinedAvailability =
AvailabilityContext::forPlatformRange(
AvailabilityRange::alwaysAvailable(), ctx);

// The least available type eraser for the enclosing context.
Type typeEraser;
for (auto *attr : PD->getAttrs().getAttributes<TypeEraserAttr>()) {
auto eraser = attr->getResolvedType(PD);
assert(eraser && "Failed to resolve eraser type!");

auto *nominal = eraser->getAnyNominal();
auto nominalAvailability =
TypeChecker::availabilityForDeclSignature(nominal);

if (contextAvailability.isContainedIn(nominalAvailability) &&
nominalAvailability.isContainedIn(refinedAvailability)) {
refinedAvailability = nominalAvailability;
typeEraser = eraser;
}
}

if (!typeEraser)
return expr;

auto typeEraser = attr->getResolvedType(PD);
assert(typeEraser && "Failed to resolve eraser type!");
auto &ctx = dc->getASTContext();
auto *argList = ArgumentList::forImplicitSingle(ctx, ctx.Id_erasing, expr);
return CallExpr::createImplicit(
ctx, TypeExpr::createImplicit(typeEraser, ctx), argList);
Expand Down
63 changes: 62 additions & 1 deletion test/Sema/type_eraser.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// RUN: %target-swift-frontend -typecheck -disable-availability-checking -dump-ast %s | %FileCheck %s
// RUN: %target-swift-frontend -target %target-cpu-apple-macosx10.15 -typecheck -dump-ast %s | %FileCheck %s

// REQUIRES: OS=macosx

class AnyP: P {
init<T: P>(erasing: T) {}
}

@_typeEraser(AnyP)
@_typeEraser(NewAnyP)
protocol P {}

struct ConcreteP: P, Hashable {}
Expand Down Expand Up @@ -119,3 +122,61 @@ extension DynamicReplacement {
return ConcreteP()
}
}

@available(macOS 100, *)
struct NewAnyP: P {
init(erasing: some P) {}
}

@available(macOS 100, *)
struct NewConcreteP: P {}

// CHECK-LABEL: var_decl{{.*}}x0
dynamic var x0: some P {
// CHECK: return_stmt
// CHECK-NEXT: underlying_to_opaque_expr implicit type="some P"
// CHECK-NEXT: call_expr implicit type="AnyP"
ConcreteP()
}

// CHECK-LABEL: var_decl{{.*}}x1
@available(macOS 99, *)
dynamic var x1: some P {
// CHECK: return_stmt
// CHECK-NEXT: underlying_to_opaque_expr implicit type="some P"
// CHECK-NEXT: call_expr implicit type="AnyP"
ConcreteP()
}

// CHECK-LABEL: var_decl{{.*}}x2
@available(macOS 100, *)
dynamic var x2: some P {
// CHECK: return_stmt
// CHECK-NEXT: underlying_to_opaque_expr implicit type="some P"
// CHECK-NEXT: call_expr implicit type="NewAnyP"
ConcreteP()
}

// CHECK-LABEL: var_decl{{.*}}x3
@available(macOS 101, *)
dynamic var x3: some P {
// CHECK: return_stmt
// CHECK-NEXT: underlying_to_opaque_expr implicit type="some P"
// CHECK-NEXT: call_expr implicit type="NewAnyP"
ConcreteP()
}

// CHECK-LABEL: var_decl{{.*}}x4
dynamic var x4: some P {
if #available(macOS 101, *) {
// CHECK: return_stmt
// CHECK-NEXT: underlying_to_opaque_expr implicit type="some P"
// CHECK-NEXT: call_expr implicit type="NewAnyP"
return NewConcreteP()
} else {
// CHECK: return_stmt
// CHECK-NEXT: underlying_to_opaque_expr implicit type="some P"
// CHECK-NEXT: call_expr implicit type="AnyP"
return ConcreteP()
}
}