Skip to content

Commit 4e62b21

Browse files
authored
Merge pull request #72204 from kavon/ncgenerics-fixes-3-8-2024
NCGenerics: More Builtin fixes and casting test fix
2 parents 547eb9c + 28dd909 commit 4e62b21

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/AST/Builtins.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,11 @@ namespace {
725725
InterfaceResult = generator.build(*this);
726726
}
727727

728+
template <class G>
729+
void addConformanceRequirement(const G &generator, KnownProtocolKind kp) {
730+
addConformanceRequirement(generator, Context.getProtocol(kp));
731+
}
732+
728733
template <class G>
729734
void addConformanceRequirement(const G &generator, ProtocolDecl *proto) {
730735
assert(proto && "missing protocol");
@@ -1194,14 +1199,23 @@ static ValueDecl *getNativeObjectCast(ASTContext &Context, Identifier Id,
11941199
}
11951200

11961201
BuiltinFunctionBuilder builder(Context);
1202+
1203+
auto genParam = makeGenericParam();
1204+
1205+
// Add safety, unless requested.
1206+
if (BV != BuiltinValueKind::UnsafeCastToNativeObject) {
1207+
builder.addConformanceRequirement(genParam, KnownProtocolKind::Copyable);
1208+
builder.addConformanceRequirement(genParam, KnownProtocolKind::Escapable);
1209+
}
1210+
11971211
if (BV == BuiltinValueKind::CastToNativeObject ||
11981212
BV == BuiltinValueKind::UnsafeCastToNativeObject ||
11991213
BV == BuiltinValueKind::BridgeToRawPointer) {
1200-
builder.addParameter(makeGenericParam(), ownership);
1214+
builder.addParameter(genParam, ownership);
12011215
builder.setResult(makeConcrete(builtinTy));
12021216
} else {
12031217
builder.addParameter(makeConcrete(builtinTy), ownership);
1204-
builder.setResult(makeGenericParam());
1218+
builder.setResult(genParam);
12051219
}
12061220
return builder.build(Id);
12071221
}
@@ -1303,7 +1317,9 @@ static ValueDecl *getZeroInitializerOperation(ASTContext &Context,
13031317
Identifier Id) {
13041318
// <T> () -> T
13051319
BuiltinFunctionBuilder builder(Context);
1306-
builder.setResult(makeGenericParam());
1320+
auto genParam = makeGenericParam();
1321+
builder.addConformanceRequirement(genParam, KnownProtocolKind::Escapable);
1322+
builder.setResult(genParam);
13071323
return builder.build(Id);
13081324
}
13091325

test/Interpreter/moveonly_generics_casting.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ func main() {
4747
// CHECK: hello
4848
attemptCall(Cat<Noncopyable, Ordinary>())
4949

50-
// FIXME: this should succeeed!!
51-
// CHECK: failed to cast (test_radar124171788)
50+
// CHECK: cast succeeded
5251
test_radar124171788(.nothing)
5352
}
5453

@@ -58,8 +57,8 @@ enum Maybe<Wrapped: ~Copyable>: ~Copyable {
5857
case nothing
5958
}
6059
extension Maybe: Copyable {}
61-
extension Maybe: CustomStringConvertible {
62-
var description: String {
60+
extension Maybe: CustomDebugStringConvertible {
61+
var debugDescription: String {
6362
"cast succeeded"
6463
}
6564
}

test/SILOptimizer/moveonly_builtins.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ func checkArrayBuiltins(_ dest: Builtin.RawPointer, src: Builtin.RawPointer, cou
3131
Builtin.assignCopyArrayBackToFront(NC.self, dest, src, count) // expected-illegal-error {{noncopyable type 'NC' cannot be substituted for copyable generic parameter 'T' in 'assignCopyArrayBackToFront'}}
3232
#endif
3333
}
34+
35+
public func checkIllegal() {
36+
#if ILLEGAL
37+
_ = Builtin.unsafeCastToNativeObject(NC())
38+
_ = Builtin.castToNativeObject(NC()) // expected-illegal-error {{noncopyable type 'NC' cannot be substituted for copyable generic parameter 'T' in 'castToNativeObject'}}
39+
let _: NC = Builtin.zeroInitializer()
40+
#endif
41+
}

0 commit comments

Comments
 (0)