Skip to content

Commit 2b00d3f

Browse files
committed
Correctly sext instead of zext for negative integer types
1 parent cfd0d6a commit 2b00d3f

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/IRGen/MetadataRequest.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,8 +3592,15 @@ IRGenFunction::emitTypeMetadataRefForLayout(SILType ty,
35923592

35933593
llvm::Value *IRGenFunction::emitValueGenericRef(CanType type) {
35943594
if (auto integer = type->getAs<IntegerType>()) {
3595-
return llvm::ConstantInt::get(IGM.SizeTy,
3596-
integer->getValue().zextOrTrunc(IGM.SizeTy->getBitWidth()));
3595+
auto value = integer->getValue();
3596+
3597+
if (integer->isNegative()) {
3598+
value = value.sextOrTrunc(IGM.SizeTy->getBitWidth());
3599+
} else {
3600+
value = value.zextOrTrunc(IGM.SizeTy->getBitWidth());
3601+
}
3602+
3603+
return llvm::ConstantInt::get(IGM.SizeTy, value);
35973604
}
35983605

35993606
return tryGetLocalTypeData(type, LocalTypeDataKind::forValue());

test/Interpreter/value_generics.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
// REQUIRES: executable_test
77

8-
struct A<let N: Int, let M: Int> {}
8+
struct A<let N: Int, let M: Int> {
9+
func foo() {
10+
print("Lower: \(N), Upper: \(M)")
11+
}
12+
}
913

1014
extension A where N == 2 {
1115
struct B {}
@@ -36,3 +40,24 @@ let x: A<-5, -5> = getA()
3640

3741
// CHECK: main.A<-5, -5>
3842
print(_typeName(type(of: x), qualified: true))
43+
44+
// CHECK: Lower: 1, Upper: 8
45+
A<1, 8>().foo()
46+
47+
// CHECK: Lower: 0, Upper: 8
48+
A<0, 8>().foo()
49+
50+
// CHECK: Lower: -1, Upper: 8
51+
A< -1, 8>().foo()
52+
53+
// CHECK: Lower: -2, Upper: 8
54+
A< -2, 8>().foo()
55+
56+
// CHECK: Lower: -3, Upper: 8
57+
A< -3, 8>().foo()
58+
59+
// CHECK: Lower: -4, Upper: 8
60+
A< -4, 8>().foo()
61+
62+
// CHECK: Lower: -5, Upper: 8
63+
A< -5, 8>().foo()

0 commit comments

Comments
 (0)