Skip to content

[Type checker] Request nominal layout for all potential metadata sources #18805

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
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
22 changes: 15 additions & 7 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,20 @@ static bool validateParameterType(ParamDecl *decl, DeclContext *DC,
return hadError;
}

/// Given a type of a function parameter, request layout for any
/// nominal types that IRGen could use as metadata sources.
static void requestLayoutForMetadataSources(TypeChecker &tc, Type type) {
type->getCanonicalType().visit([&tc](CanType type) {
// Generic types are sources for typemetadata and conformances. If a
// parameter is of dependent type then the body of a function with said
// parameter could potentially require the generic type's layout to
// recover them.
if (auto *nominalDecl = type->getAnyNominal()) {
tc.requestNominalLayout(nominalDecl);
}
});
}

/// Request nominal layout for any types that could be sources of type metadata
/// or conformances.
void TypeChecker::requestRequiredNominalTypeLayoutForParameters(
Expand All @@ -800,13 +814,7 @@ void TypeChecker::requestRequiredNominalTypeLayoutForParameters(
if (!param->hasInterfaceType())
continue;

// Generic types are sources for typemetadata and conformances. If a
// parameter is of dependent type then the body of a function with said
// parameter could potentially require the generic type's layout to
// recover them.
if (auto *nominalDecl = param->getInterfaceType()->getAnyNominal()) {
requestNominalLayout(nominalDecl);
}
requestLayoutForMetadataSources(*this, param->getInterfaceType());
}
}

Expand Down
4 changes: 2 additions & 2 deletions test/multifile/Inputs/require-layout-generic-class.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class Base<T> {
public class Sub<T> : Base<T> {
}

public func requestTypeThrough<T>(closure: (Sub<T>) -> (), arg: T) {
closure(Sub(arg))
public func requestTypeThrough<T>(closure: ((Sub<T>, Int)) -> (), arg: T) {
closure((Sub(arg), 0))
}
4 changes: 2 additions & 2 deletions test/multifile/require-layout-generic-arg-closure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

// The offset of the typemetadata in the class typemetadata must match.

// FILE1-LABEL: define internal swiftcc void @"$S4test12requestType21xyx_tlFyAA3SubCyxGXEfU_"(%T4test3SubC*)
// FILE1-LABEL: define internal swiftcc void @"$S4test12requestType21xyx_tlFyAA3SubCyxG_Sit_tXEfU_
// FILE1: entry:
// FILE1: [[T1:%.*]] = bitcast %T4test3SubC* %0 to %swift.type**
// FILE1: [[TYPEMETADATA:%.*]] = load %swift.type*, %swift.type** [[T1]]
// FILE1: [[T2:%.*]] = bitcast %swift.type* [[TYPEMETADATA]] to %swift.type**
// FILE1: [[T_PTR:%.*]] = getelementptr inbounds %swift.type*, %swift.type** [[T2]], i64 16
// FILE1: [[T:%.*]] = load %swift.type*, %swift.type** [[T_PTR]]
// FILE1: call swiftcc %swift.metadata_response @"$S4test3SubCMa"(i64 0, %swift.type* [[T]])
// FILE1: call swiftcc %swift.metadata_response @"$S4test3SubCMa"(i64 255, %swift.type* [[T]])

public func requestType2<T>(x: T) {
requestTypeThrough(closure: { x in print(x) }, arg: x)
Expand Down