Skip to content

Commit 363d45c

Browse files
Kenoclaude
andauthored
Reject decimal points after hex float literals (#60199)
Hex float literals like `0x1p3 `should not be allowed to be followed by decimal digits or decimal points, as this creates confusing implicit multiplication that looks like a malformed literal. This change makes the parser reject expressions like 0x1p3.2 (previously parsed as 0x1p3 * 0.2) as invalid numeric constants, consistent with the existing restriction on juxtaposing hex integer literals. Fixes #60189. Written by Claude (obviously - since it's set as the author - trying out the new web thing). I figure this situation is rare enough and bad enough that we should just change this unconditionally across syntax versions, but if preferred, we could make use of syntax evolution for this (although we'd have to start versioning the lexer). Co-authored-by: Claude <[email protected]>
1 parent 22cfa2b commit 363d45c

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

JuliaSyntax/src/julia/tokenize.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,14 @@ function lex_digit(l::Lexer, kind)
10571057
if !accept_number(l, isdigit) || !had_digits
10581058
return emit(l, K"ErrorInvalidNumericConstant") # `0x1p` `0x.p0`
10591059
end
1060+
# Check for invalid trailing decimal point
1061+
# https://github.com/JuliaLang/julia/issues/60189
1062+
pc = peekchar(l)
1063+
if pc == '.'
1064+
accept_batch(l, c->(c == '.' || isdigit(c)))
1065+
# `0x1p3.` `0x1p3.2` `0x1.5p2.3`
1066+
return emit(l, K"ErrorInvalidNumericConstant")
1067+
end
10601068
elseif isfloat
10611069
return emit(l, K"ErrorHexFloatMustContainP") # `0x.` `0x1.0`
10621070
end

JuliaSyntax/test/tokenize.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ end
632632
@test onlytok("0x.p0") == K"ErrorInvalidNumericConstant"
633633
@test onlytok("0x.") == K"ErrorHexFloatMustContainP"
634634
@test onlytok("0x1.0") == K"ErrorHexFloatMustContainP"
635+
# https://github.com/JuliaLang/julia/issues/60189
636+
@test onlytok("0x1p3.") == K"ErrorInvalidNumericConstant"
637+
@test onlytok("0x1p3.2") == K"ErrorInvalidNumericConstant"
638+
@test onlytok("0x1.5p2.3") == K"ErrorInvalidNumericConstant"
635639
end
636640

637641
@testset "binary literals" begin

test/syntax.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,9 @@ end
16841684
# #16356
16851685
@test_parseerror "0xapi"
16861686

1687+
# #60189
1688+
@test_parseerror "0x1p3.2"
1689+
16871690
# #22523 #22712
16881691
@test_parseerror "a?b:c"
16891692
@test_parseerror "a ?b:c"

0 commit comments

Comments
 (0)