Skip to content

Commit deefb44

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents 25081dc + 7016679 commit deefb44

18 files changed

+98
-29
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5216,6 +5216,9 @@ ERROR(pack_reference_outside_expansion,none,
52165216
ERROR(each_non_pack,none,
52175217
"'each' cannot be applied to non-pack type %0",
52185218
(Type))
5219+
ERROR(pack_expansion_missing_pack_reference,none,
5220+
"pack expansion %0 must specify a pack reference",
5221+
(TypeRepr*))
52195222
ERROR(tuple_duplicate_label,none,
52205223
"cannot create a tuple with a duplicate element label", ())
52215224
ERROR(multiple_ellipsis_in_tuple,none,

lib/IRGen/GenOpaque.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,25 +334,34 @@ Address irgen::slotForLoadOfOpaqueWitness(IRGenFunction &IGF,
334334
/// Load a specific witness from a known table. The result is
335335
/// always an i8*.
336336
llvm::Value *irgen::emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
337+
bool isProtocolWitness,
337338
llvm::Value *table,
338339
WitnessIndex index,
339340
llvm::Value **slotPtr) {
340-
auto isRelativeTable = IGF.IGM.IRGen.Opts.UseRelativeProtocolWitnessTables;
341+
// Is this is a load of a relative protocol witness table entry.
342+
auto isRelativeTable = IGF.IGM.IRGen.Opts.UseRelativeProtocolWitnessTables &&
343+
isProtocolWitness;
344+
341345
auto slot = slotForLoadOfOpaqueWitness(IGF, table, index, isRelativeTable);
342346
if (slotPtr) *slotPtr = slot.getAddress();
347+
343348
if (isRelativeTable) {
344349
return IGF.emitLoadOfRelativePointer(slot, false, IGF.IGM.Int8Ty);
345350
}
351+
346352
return IGF.emitInvariantLoad(slot);
347353
}
348354

349355
/// Load a specific witness from a known table. The result is
350356
/// always an i8*.
351357
llvm::Value *irgen::emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
358+
bool isProtocolWitness,
352359
llvm::Value *table,
353360
llvm::Value *index,
354361
llvm::Value **slotPtr) {
355362
assert(table->getType() == IGF.IGM.WitnessTablePtrTy);
363+
assert(!isProtocolWitness &&
364+
"This function does not yet support relative protocol witnesses");
356365

357366
// GEP to the appropriate index.
358367
llvm::Value *slot =
@@ -438,7 +447,8 @@ static FunctionPointer emitLoadOfValueWitnessFunction(IRGenFunction &IGF,
438447

439448
llvm::Value *slot;
440449
llvm::Value *witness =
441-
emitInvariantLoadOfOpaqueWitness(IGF, table, windex, &slot);
450+
emitInvariantLoadOfOpaqueWitness(IGF, /*isProtocolWitness*/false, table,
451+
windex, &slot);
442452
auto label = getValueWitnessLabel(index);
443453
auto signature = IGF.IGM.getValueWitnessSignature(index);
444454

lib/IRGen/GenOpaque.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ namespace irgen {
5252
/// The load is marked invariant. This should not be used in contexts where
5353
/// the referenced witness table is still undergoing initialization.
5454
llvm::Value *emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
55+
bool isProtocolWitness,
5556
llvm::Value *table,
5657
WitnessIndex index,
5758
llvm::Value **slot = nullptr);
@@ -62,6 +63,7 @@ namespace irgen {
6263
/// The load is marked invariant. This should not be used in contexts where
6364
/// the referenced witness table is still undergoing initialization.
6465
llvm::Value *emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
66+
bool isProtocolWitness,
6567
llvm::Value *table,
6668
llvm::Value *index,
6769
llvm::Value **slot = nullptr);

lib/IRGen/GenProto.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,7 +2634,8 @@ llvm::Value *irgen::loadParentProtocolWitnessTable(IRGenFunction &IGF,
26342634
auto &IGM = IGF.IGM;
26352635
if (!IGM.IRGen.Opts.UseRelativeProtocolWitnessTables) {
26362636
auto baseWTable =
2637-
emitInvariantLoadOfOpaqueWitness(IGF, wtable, index);
2637+
emitInvariantLoadOfOpaqueWitness(IGF,/*isProtocolWitness*/true, wtable,
2638+
index);
26382639
return baseWTable;
26392640
}
26402641
auto &Builder = IGF.Builder;
@@ -2659,7 +2660,8 @@ llvm::Value *irgen::loadParentProtocolWitnessTable(IRGenFunction &IGF,
26592660

26602661
Builder.emitBlock(isNotCondBB);
26612662
auto baseWTable2 =
2662-
emitInvariantLoadOfOpaqueWitness(IGF, wtable, index);
2663+
emitInvariantLoadOfOpaqueWitness(IGF,/*isProtocolWitness*/true, wtable,
2664+
index);
26632665
baseWTable2 = IGF.Builder.CreateBitCast(baseWTable2,
26642666
IGF.IGM.WitnessTablePtrTy);
26652667
Builder.CreateBr(endBB);
@@ -2677,7 +2679,8 @@ llvm::Value *irgen::loadConditionalConformance(IRGenFunction &IGF,
26772679

26782680
auto &IGM = IGF.IGM;
26792681
if (!IGM.IRGen.Opts.UseRelativeProtocolWitnessTables) {
2680-
return emitInvariantLoadOfOpaqueWitness(IGF, wtable, index);
2682+
return emitInvariantLoadOfOpaqueWitness(IGF, /*isProtocolWitness*/true,
2683+
wtable, index);
26812684
}
26822685

26832686
auto &Builder = IGF.Builder;

lib/Sema/TypeCheckType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3821,6 +3821,14 @@ TypeResolver::resolveDeclRefTypeRepr(DeclRefTypeRepr *repr,
38213821
// The base component uses unqualified lookup.
38223822
result = resolveUnqualifiedIdentTypeRepr(resolution.withOptions(options),
38233823
genericParams, identBase);
3824+
3825+
if (result && result->isParameterPack() &&
3826+
options.contains(TypeResolutionFlags::AllowPackReferences) &&
3827+
!options.contains(TypeResolutionFlags::FromPackReference)) {
3828+
diagnose(repr->getLoc(), diag::pack_expansion_missing_pack_reference,
3829+
repr);
3830+
return ErrorType::get(result);
3831+
}
38243832
} else {
38253833
result = resolveType(baseComp, options);
38263834
}
@@ -4294,6 +4302,7 @@ NeverNullType TypeResolver::resolvePackExpansionType(PackExpansionTypeRepr *repr
42944302
NeverNullType TypeResolver::resolvePackReference(PackReferenceTypeRepr *repr,
42954303
TypeResolutionOptions options) {
42964304
auto &ctx = getASTContext();
4305+
options |= TypeResolutionFlags::FromPackReference;
42974306
auto packReference = resolveType(repr->getPackType(), options);
42984307

42994308
// If we already failed, don't diagnose again.

lib/Sema/TypeCheckType.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ enum class TypeResolutionFlags : uint16_t {
7676
/// Pack references are only allowed inside pack expansions
7777
/// and in generic requirements.
7878
AllowPackReferences = 1 << 11,
79+
80+
/// Whether this is a resolution based on a pack reference.
81+
FromPackReference = 1 << 12,
7982
};
8083

8184
/// Type resolution contexts that require special handling.

test/Constraints/pack-expansion-expressions.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,19 @@ func sameShapeDiagnostics<T..., U...>(t: repeat each T, u: repeat each U) {
7777
_ = repeat Array<(each T, each U)>() // expected-error {{pack expansion requires that 'U' and 'T' have the same shape}}
7878
_ = repeat (Array<each T>(), each u) // expected-error {{pack expansion requires that 'U' and 'T' have the same shape}}
7979
}
80+
81+
func returnPackExpansionType<T...>(_ t: repeat each T) -> repeat each T { // expected-error {{variadic expansion 'T' cannot appear outside of a function parameter list, function result, tuple element or generic argument list}}
82+
fatalError()
83+
}
84+
85+
func returnEachPackReference<T...>(_ t: repeat each T) -> each T { // expected-error {{pack reference 'T' can only appear in pack expansion or generic requirement}}
86+
fatalError()
87+
}
88+
89+
func returnRepeatTuple<T...>(_ t: repeat each T) -> (repeat T) { // expected-error {{pack expansion 'T' must specify a pack reference}}
90+
fatalError()
91+
}
92+
93+
func paremeterAsPackTypeWithoutExpansion<T...>(_ t: T) -> repeat each T { // expected-error {{variadic expansion 'T' cannot appear outside of a function parameter list, function result, tuple element or generic argument list}}
94+
fatalError()
95+
}

test/Constraints/pack_expansion_types.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func patternInstantiationConcreteInvalid() {
257257
let _: (Array<Int>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{type of expression is ambiguous without more context}}
258258
}
259259

260-
func patternInstantiationGenericValid<T..., U...>(t: repeat each T, u: repeat each U) where (repeat (each T, each U)): Any, T: Hashable {
260+
func patternInstantiationGenericValid<T..., U...>(t: repeat each T, u: repeat each U) where (repeat (each T, each U)): Any, each T: Hashable {
261261
let _: (repeat Array<each T>) = patternInstantiationTupleTest1()
262262
let _: (repeat Array<each T>, Array<String>) = patternInstantiationTupleTest1()
263263
let _: (Array<String>, repeat Array<each T>) = patternInstantiationTupleTest1()
@@ -279,7 +279,7 @@ func patternInstantiationGenericValid<T..., U...>(t: repeat each T, u: repeat ea
279279
let _: (Dictionary<Int, String>, repeat Dictionary<each T, each U>, Dictionary<Double, Character>) -> () = patternInstantiationFunctionTest2()
280280
}
281281

282-
func patternInstantiationGenericInvalid<T...>(t: repeat each T) where T: Hashable {
282+
func patternInstantiationGenericInvalid<T...>(t: repeat each T) where each T: Hashable {
283283
let _: (repeat Set<each T>) = patternInstantiationTupleTest1() // expected-error {{cannot convert value of type '(repeat Array<each T>)' to specified type '(repeat Set<each T>)}}
284284
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
285285

test/Constraints/variadic_generic_constraints.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ takesAnyObject(C(), S(), C()) // expected-error {{type of expression is ambiguo
4747

4848
// Same-type requirements
4949

50-
func takesParallelSequences<T..., U...>(t: repeat each T, u: repeat each U) where T: Sequence, U: Sequence, T.Element == U.Element {}
50+
func takesParallelSequences<T..., U...>(t: repeat each T, u: repeat each U) where each T: Sequence, each U: Sequence, each T.Element == each U.Element {}
5151
// expected-note@-1 {{where 'T.Element' = 'String', 'U.Element' = 'Int'}}
5252

5353
takesParallelSequences() // ok

test/Constraints/variadic_generic_functions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
// REQUIRES: asserts
44

55
func debugPrint<T...>(_ items: repeat each T)
6-
where T: CustomDebugStringConvertible
6+
where each T: CustomDebugStringConvertible
77
{
88
/*for (item: T) in items {
99
stdout.write(item.debugDescription)
1010
}*/
1111
}
1212

1313
func max<T...>(_ values: repeat each T) -> T?
14-
where T: Comparable
14+
where each T: Comparable
1515
{
1616
return nil
1717
}

test/Constraints/variadic_generic_types.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: rdar104716322
2+
13
// RUN: %target-typecheck-verify-swift -enable-experimental-feature VariadicGenerics
24

35
// REQUIRES: asserts
@@ -9,17 +11,16 @@ func f<T...>(_: repeat each T) {
911
_ = G< >.self
1012
_ = G<Int>.self
1113
_ = G<Int, String>.self
12-
_ = G<repeat T>.self
13-
_ = G<Int, repeat Array<T>>.self
14+
_ = G<repeat each T>.self
15+
_ = G<Int, repeat Array<each T>>.self
1416
}
1517

1618
// Forming PackExpansionTypeReprs in simplifyTypeExpr()
1719
func g<T...>(_: repeat each T) {
18-
_ = (repeat T).self
19-
_ = (Int, repeat T).self
20-
_ = ((repeat T) -> ()).self
21-
_ = ((Int, repeat Array<T>) -> ()).self
20+
_ = (repeat each T).self
21+
_ = (Int, repeat each T).self
22+
_ = ((repeat each T) -> ()).self
23+
_ = ((Int, repeat Array<each T>) -> ()).self
2224

23-
_ = (repeat Int).self // expected-error {{variadic expansion 'Int' must contain at least one variadic generic parameter}}
25+
_ = (repeat each Int).self // expected-error {{variadic expansion 'Int' must contain at least one variadic generic parameter}}
2426
}
25-

test/Generics/pack-shape-requirements.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func inferSameShape<T..., U...>(ts t: repeat each T, us u: repeat each U) where
1313

1414
// CHECK-LABEL: desugarSameShape(ts:us:)
1515
// CHECK-NEXT: Generic signature: <T..., U... where T : P, ((T, U)...) : Any, U : P>
16-
func desugarSameShape<T..., U...>(ts t: repeat each T, us u: repeat each U) where T: P, U: P, (repeat (each T.A, each U.A)): Any {
16+
func desugarSameShape<T..., U...>(ts t: repeat each T, us u: repeat each U) where each T: P, each U: P, (repeat (each T.A, each U.A)): Any {
1717
}
1818

1919
// CHECK-LABEL: multipleSameShape1(ts:us:vs:)

test/Generics/tuple-conformances.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protocol P {
1111
func f()
1212
}
1313

14-
extension Builtin.TheTupleType: P where Elements: P {
14+
extension Builtin.TheTupleType: P where each Elements: P {
1515
typealias A = (repeat each Elements.A)
1616
typealias B = Float
1717
func f() {}

test/IRGen/relative_protocol_witness_table.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
// REQUIRES: CPU=x86_64 || CPU=arm64
44
// UNSUPPORTED: CPU=arm64e
55

6+
func testVWT<T>(_ t: T) {
7+
var local = t
8+
}
9+
610
protocol FuncOnly {
711
func a()
812
func b()
@@ -185,6 +189,18 @@ func instantiate_conditional_conformance_2nd<T>(_ t : T) where T: Sub, T.S == T
185189
// CHECK-SAME: i64 ptrtoint (i32* getelementptr inbounds ([4 x i32], [4 x i32]* @"$s1A17ConditionalStructVyxGAA20WithAssocConformanceA2A8FuncOnlyRzAA5InitPRzlWP", i32 0, i32 3) to i64)) to i32)
186190
// CHECK-SAME: ], align 8
187191

192+
// Make sure value witness table lookup is done right.
193+
194+
// CHECK: define{{.*}} swiftcc void @"$s1A7testVWTyyxlF"(%swift.opaque* {{.*}}, %swift.type* [[T:%.*]])
195+
// CHECK: [[T0:%.*]] = bitcast %swift.type* [[T]] to i8***
196+
// CHECK: [[VWT_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[T0]], i64 -1
197+
// CHECK: [[VWT_PTR:%.*]] = load i8**, i8*** [[VWT_ADDR]]
198+
// CHECK: [[T1:%.*]] = getelementptr inbounds i8*, i8** [[VWT_PTR]], i32 2
199+
// CHECK: [[T2:%.*]] = load i8*, i8** [[T1]]
200+
// CHECK: [[T3:%.*]] = bitcast i8* [[T2]] to %swift.opaque* (%swift.opaque*, %swift.opaque*, %swift.type*)*
201+
// CHECK: call %swift.opaque* [[T3]](%swift.opaque* {{.*}}, %swift.opaque* {{.*}}, %swift.type* [[T]])
202+
203+
188204
// Simple witness entry access.
189205

190206
// CHECK: define{{.*}} swiftcc void @"$s1A14requireWitnessyyxAA8FuncOnlyRzlF"(%swift.opaque* noalias nocapture {{%.*}}, %swift.type* {{%.*}}, i8** [[PWT:%.*]])

test/IRGen/variadic_generic_types.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: rdar104716322
2+
13
// RUN: %target-swift-frontend -emit-ir -primary-file %s -enable-experimental-feature VariadicGenerics | %FileCheck %s
24

35
// Because of -enable-experimental-feature VariadicGenerics
@@ -18,7 +20,7 @@ struct G<T...> {
1820
}
1921

2022
func makeTuple1() -> (repeat each T).Type {
21-
return (repeat T).self
23+
return (repeat each T).self
2224
}
2325

2426
func makeTuple2() -> (repeat Array<each T>).Type {

test/Interpreter/variadic_generic_types.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: rdar104716322
2+
13
// RUN: %target-run-simple-swift(-enable-experimental-feature VariadicGenerics) | %FileCheck %s
24

35
// REQUIRES: executable_test
@@ -7,7 +9,7 @@
79

810
struct G<T...> {
911
func makeTuple() {
10-
print((repeat (Array<T>)).self)
12+
print((repeat (Array<each T>)).self)
1113
}
1214
}
1315

test/SILGen/pack_expansion_type.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// REQUIRES: rdar104716322
2+
13
// RUN: %target-swift-emit-silgen %s -enable-experimental-feature VariadicGenerics | %FileCheck %s
24

35
// Experimental features require an asserts compiler
@@ -36,10 +38,10 @@ func variadicMetatypes<T...>(_: repeat each T) {
3638
_ = VariadicType< >.self
3739
_ = VariadicType<Int>.self
3840
_ = VariadicType<Int, String>.self
39-
_ = VariadicType<repeat T>.self
40-
_ = VariadicType<Int, repeat Array<T>>.self
41-
_ = (repeat T).self
42-
_ = (Int, repeat Array<T>).self
43-
_ = ((repeat T) -> ()).self
44-
_ = ((Int, repeat Array<T>) -> ()).self
41+
_ = VariadicType<repeat each T>.self
42+
_ = VariadicType<Int, repeat Array<each T>>.self
43+
_ = (repeat each T).self
44+
_ = (Int, repeat Array<each T>).self
45+
_ = ((repeat each T) -> ()).self
46+
_ = ((Int, repeat Array<each T>) -> ()).self
4547
}

test/type/pack_expansion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct Outer<T...> {
6161
}
6262

6363
struct AlsoGood<U...> {
64-
typealias Value = (repeat (T, E<repeat each U>))
64+
typealias Value = (repeat (each T, E<repeat each U>))
6565
}
6666
}
6767

0 commit comments

Comments
 (0)