Skip to content

Commit 89052a7

Browse files
authored
Merge pull request #84655 from Azoy/negative-nancy
[IRGen] Correctly sext instead of zext for negative integer types
2 parents 615b299 + bb5dfd3 commit 89052a7

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/IRGen/MetadataRequest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3590,8 +3590,8 @@ IRGenFunction::emitTypeMetadataRefForLayout(SILType ty,
35903590

35913591
llvm::Value *IRGenFunction::emitValueGenericRef(CanType type) {
35923592
if (auto integer = type->getAs<IntegerType>()) {
3593-
return llvm::ConstantInt::get(IGM.SizeTy,
3594-
integer->getValue().zextOrTrunc(IGM.SizeTy->getBitWidth()));
3593+
auto value = integer->getValue().sextOrTrunc(IGM.SizeTy->getBitWidth());
3594+
return llvm::ConstantInt::get(IGM.SizeTy, value);
35953595
}
35963596

35973597
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)