Skip to content

Commit d96dd36

Browse files
committed
Make prefix negate invocable
BUG: Type inference cannot find `-` when it is declared static because it will thinks it needs to instantiate the enclosing nominal’s generic parameter.
1 parent 03881ae commit d96dd36

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

Sources/LLVM/Constant.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,6 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
126126
}
127127
}
128128

129-
/// Creates a constant negate operation to negate an integral value.
130-
///
131-
/// - parameter lhs: The operand to negate.
132-
///
133-
/// - returns: A constant value representing the negation of the given constant.
134-
public static prefix func -(lhs: Constant<Signed>) -> Constant<Signed> {
135-
precondition(lhs.repr == .signed, "Invalid representation")
136-
return Constant<Signed>(llvm: LLVMConstNeg(lhs.llvm))
137-
}
138-
139-
/// Creates a constant negate operation to negate a floating value.
140-
///
141-
/// - parameter lhs: The operand to negate.
142-
///
143-
/// - returns: A constant value representing the negation of the given constant.
144-
public static prefix func -(lhs: Constant<Floating>) -> Constant<Floating> {
145-
precondition(lhs.repr == .floating, "Invalid representation")
146-
return Constant<Floating>(llvm: LLVMConstFNeg(lhs.llvm))
147-
}
148-
149129
/// Creates a constant add operation to add two homogenous constants together.
150130
///
151131
/// - parameter lhs: The first summand value (the augend).
@@ -432,6 +412,26 @@ public struct Constant<Repr: ConstantRepresentation>: IRValue {
432412
}
433413
}
434414

415+
/// Creates a constant negate operation to negate an integral value.
416+
///
417+
/// - parameter lhs: The operand to negate.
418+
///
419+
/// - returns: A constant value representing the negation of the given constant.
420+
public prefix func -(lhs: Constant<Signed>) -> Constant<Signed> {
421+
precondition(lhs.repr == .signed, "Invalid representation")
422+
return Constant<Signed>(llvm: LLVMConstNeg(lhs.llvm))
423+
}
424+
425+
/// Creates a constant negate operation to negate a floating value.
426+
///
427+
/// - parameter lhs: The operand to negate.
428+
///
429+
/// - returns: A constant value representing the negation of the given constant.
430+
public prefix func -(lhs: Constant<Floating>) -> Constant<Floating> {
431+
precondition(lhs.repr == .floating, "Invalid representation")
432+
return Constant<Floating>(llvm: LLVMConstFNeg(lhs.llvm))
433+
}
434+
435435
extension Constant where Repr: IntegralConstantRepresentation {
436436
// MARK: Logical Operations
437437

Tests/LLVMTests/ConstantSpec.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ class ConstantSpec : XCTestCase {
2626
let val2 = builder.buildSub(constant - constant, constant / constant)
2727
// SIGNEDCONST-NOT: %{{[0-9]+}} = mul i64 %%{{[0-9]+}}, %%{{[0-9]+}}
2828
let val3 = builder.buildMul(val1, val2)
29+
// SIGNEDCONST-NOT: %{{[0-9]+}} = mul i64 %%{{[0-9]+}}, %%{{[0-9]+}}
30+
let val4 = builder.buildMul(val3, -constant)
2931

30-
// SIGNEDCONST-NEXT: ret i64 -1848
31-
builder.buildRet(val3)
32+
// SIGNEDCONST-NEXT: ret i64 77616
33+
builder.buildRet(val4)
3234
// SIGNEDCONST-NEXT: }
3335
module.dump()
3436
})

0 commit comments

Comments
 (0)