Skip to content

Commit c557cc2

Browse files
committed
Add warning on some cases of integer literal coercion
Fix misisng warnings on: int x3 = 18446744073709551615; int x4 = 0xFFFFFFFFFFFFFFFF; Add test cases for the integer literal coercion
1 parent 6f56b47 commit c557cc2

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

source/slang/slang-parser.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -6897,7 +6897,7 @@ static IntegerLiteralValue _fixIntegerLiteral(
68976897
// If the masked value is 0 or equal to the mask, we 'assume' no information is
68986898
// lost
68996899
// This allows for example -1u, to give 0xffffffff
6900-
// It also means 0xfffffffffffffffffu will give 0xffffffff, without a warning.
6900+
// It also means 0xffffffffffffffffu will give 0xffffffff, without a warning.
69016901
if ((!(maskedValue == 0 || maskedValue == mask)) && sink && token)
69026902
{
69036903
// Output a warning that number has been altered
@@ -6954,8 +6954,11 @@ static BaseType _determineNonSuffixedIntegerLiteralType(
69546954
{
69556955
baseType = BaseType::UInt64;
69566956

6957-
if (isDecimalBase)
6957+
// Emit warning if the value is too large for signed 64-bit, regardless of base
6958+
// This fixes the inconsistency between decimal and hex literals
6959+
if (sink && token)
69586960
{
6961+
69596962
// There is an edge case here where 9223372036854775808 or INT64_MAX + 1
69606963
// brings us here, but the complete literal is -9223372036854775808 or INT64_MIN and is
69616964
// valid. Unfortunately because the lexer handles the negative(-) part of the literal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//TEST:SIMPLE(filecheck=CHECK): -target dxbc-assembly
2+
3+
// Test that UINT64_MAX literals produce appropriate warnings
4+
// This test verifies the fix from commit 0a3c1c8dbd6aa9501ea94a8de69d1a5967034b2c
5+
// which ensures literals equivalent to UINT64_MAX produce appropriate warnings
6+
7+
void main()
8+
{
9+
// Decimal form of UINT64_MAX
10+
// CHECK: warning 39999: integer literal is too large to be represented in a signed integer type, interpreting as unsigned
11+
uint64_t a = 18446744073709551615;
12+
13+
// Hex form of UINT64_MAX
14+
// CHECK: warning 39999: integer literal is too large to be represented in a signed integer type, interpreting as unsigned
15+
uint64_t b = 0xFFFFFFFFFFFFFFFF;
16+
17+
// Assigning UINT64_MAX to int - should also warn about size
18+
// CHECK: warning 39999: integer literal is too large to be represented in a signed integer type, interpreting as unsigned
19+
int c = 18446744073709551615;
20+
21+
// Also hex form to int
22+
// CHECK: warning 39999: integer literal is too large to be represented in a signed integer type, interpreting as unsigned
23+
int d = 0xFFFFFFFFFFFFFFFF;
24+
}

tests/slang-extension/atomic-int64-byte-address-buffer.slang

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
3232
// Bit logical
3333
outputBuffer.InterlockedOrU64((idx << 3), (uint64_t(2) << 32) | (tid << 4));
3434
outputBuffer.InterlockedXorU64((idx << 3), tid << 8);
35-
outputBuffer.InterlockedAndU64((idx << 3), (uint64_t(tid | 2) << 32) | 0xffffffffffffffff);
35+
outputBuffer.InterlockedAndU64((idx << 3), (uint64_t(tid | 2) << 32) | 0xffffffffffffffffULL);
3636
}
3737

0 commit comments

Comments
 (0)