From 1b4fc1425769f45ae5ab20491647e50bb50ad456 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Thu, 4 Apr 2024 06:40:23 -0400 Subject: [PATCH 1/2] TOML: Improve type-stability of BigInt/UInt support From a type-stability perspective, this restores a lot of our behavior before #47903. As it turns out, 10 of the 11 uses of `parse_int` introduced in that PR are unnecessary since the TOML format already requires the parsed value to be within a very limited range. Note that this change does not actually revert any functionality (in contrast to #49576) --- base/toml_parser.jl | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/base/toml_parser.jl b/base/toml_parser.jl index 23fc14a894c06..d32d629b6e2ff 100644 --- a/base/toml_parser.jl +++ b/base/toml_parser.jl @@ -854,7 +854,7 @@ function parse_number_or_date_start(l::Parser) ate, contains_underscore = @try accept_batch_underscore(l, isdigit, readed_zero) read_underscore |= contains_underscore if (read_digit || ate) && ok_end_value(peek(l)) - return parse_int(l, contains_underscore) + return parse_integer(l, contains_underscore) end # Done with integers here @@ -904,7 +904,18 @@ function parse_float(l::Parser, contains_underscore)::Err{Float64} return v end -for (name, T1, T2, n1, n2) in (("int", Int64, Int128, 17, 33), +function parse_int(l::Parser, contains_underscore, base=nothing)::Err{Int64} + s = take_string_or_substring(l, contains_underscore) + v = try + Base.parse(Int64, s; base=base) + catch e + e isa Base.OverflowError && return(ParserError(ErrOverflowError)) + error("internal parser error: did not correctly discredit $(repr(s)) as an int") + end + return v +end + +for (name, T1, T2, n1, n2) in (("integer", Int64, Int128, 17, 33), ("hex", UInt64, UInt128, 18, 34), ("oct", UInt64, UInt128, 24, 45), ("bin", UInt64, UInt128, 66, 130), From 2bbecbae9dcddc2e2b8de802bc5d817b13dff0d6 Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Thu, 4 Apr 2024 09:38:29 -0400 Subject: [PATCH 2/2] TOML: Add type-assertion for `parse_local_time` --- base/toml_parser.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/toml_parser.jl b/base/toml_parser.jl index d32d629b6e2ff..b31e256517a1e 100644 --- a/base/toml_parser.jl +++ b/base/toml_parser.jl @@ -1114,7 +1114,7 @@ function _parse_local_time(l::Parser, skip_hour=false)::Err{NTuple{4, Int64}} end # DateTime in base only manages 3 significant digits in fractional # second - fractional_second = parse_int(l, false) + fractional_second = parse_int(l, false)::Int64 # Truncate off the rest eventual digits accept_batch(l, isdigit) end