Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add support for parsing Rust-style string continuation escapes ([#585](https://github.com/ron-rs/ron/issues/585))

### Bug Fixes

- Fixed round-tripping of compact ranges whose start bound is `inf` or `NaN` ([#605](https://github.com/ron-rs/ron/pull/605))

## [0.12.2] - 2026-06-22

### Format Changes
Expand Down
75 changes: 31 additions & 44 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,56 +783,43 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
|| (name == "RangeInclusive" && matches!(fields, ["start", "end" | "last"]))
{
let end_field = fields[1];
if let Some(c) = self.parser.peek_char() {
if self.parser.is_number_start(c) {
let start = self.parser.any_number()?;

let inclusive = if self.parser.consume_str("..=") {
true
} else if self.parser.consume_str("..") {
false
} else {
return Err(Error::ExpectedRangeSyntax);
};

if inclusive && name == "Range" {
return Err(Error::Message(String::from(
"expected `..` for `Range`, found `..=`",
)));
}
if !inclusive && name == "RangeInclusive" {
return Err(Error::Message(String::from(
"expected `..=` for `RangeInclusive`, found `..`",
)));
}
if self.parser.check_any_number_start() {
let start = self.parser.any_number()?;

let end = self.parser.any_number()?;
return visitor.visit_map(RangeMapAccess::new(start, end, end_field));
let inclusive = if self.parser.consume_str("..=") {
true
} else if self.parser.consume_str("..") {
false
} else {
return Err(Error::ExpectedRangeSyntax);
};

if inclusive && name == "Range" {
return Err(Error::Message(String::from(
"expected `..` for `Range`, found `..=`",
)));
}
if !inclusive && name == "RangeInclusive" {
return Err(Error::Message(String::from(
"expected `..=` for `RangeInclusive`, found `..`",
)));
}

let end = self.parser.any_number()?;
return visitor.visit_map(RangeMapAccess::new(start, end, end_field));
}
}

if fields == ["start"] && name == "RangeFrom" {
if let Some(c) = self.parser.peek_char() {
if self.parser.is_number_start(c)
|| self.parser.check_ident("inf")
|| self.parser.check_ident("inff32")
|| self.parser.check_ident("inff64")
|| self.parser.check_ident("NaN")
|| self.parser.check_ident("NaNf32")
|| self.parser.check_ident("NaNf64")
{
let start = self.parser.any_number()?;
if self.parser.consume_str("..=") {
return Err(Error::Message(String::from(
"expected `..` for `RangeFrom`, found `..=`",
)));
} else if !self.parser.consume_str("..") {
return Err(Error::ExpectedRangeSyntax);
}
return visitor.visit_map(RangeFromMapAccess::new(start));
}
if fields == ["start"] && name == "RangeFrom" && self.parser.check_any_number_start() {
let start = self.parser.any_number()?;
if self.parser.consume_str("..=") {
return Err(Error::Message(String::from(
"expected `..` for `RangeFrom`, found `..=`",
)));
} else if !self.parser.consume_str("..") {
return Err(Error::ExpectedRangeSyntax);
}
return visitor.visit_map(RangeFromMapAccess::new(start));
}

if fields == ["end"]
Expand Down
18 changes: 18 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,24 @@ impl<'a> Parser<'a> {
T::parse(self, sign, base)
}

/// Check whether [`Parser::any_number`] has a number literal to parse at
/// the cursor. The `inf` and `NaN` literals start with a letter, so they
/// are not covered by [`Parser::is_number_start`].
pub fn check_any_number_start(&mut self) -> bool {
match self.peek_char() {
None => false,
Some(c) => {
self.is_number_start(c)
|| self.check_ident("inf")
|| self.check_ident("inff32")
|| self.check_ident("inff64")
|| self.check_ident("NaN")
|| self.check_ident("NaNf32")
|| self.check_ident("NaNf64")
}
}
}

pub fn any_number(&mut self) -> Result<Number> {
if self.consume_ident("inf") || self.consume_ident("inff32") {
return Ok(Number::F32(crate::value::F32(core::f32::INFINITY)));
Expand Down
50 changes: 50 additions & 0 deletions tests/601_support_for_number_ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,53 @@ fn test_range_full_whitespace_lookahead() {
MaybeRange::RangeFull(std::ops::RangeFull)
);
}

#[test]
fn test_range_inf_nan_start_bound_roundtrips() {
// Regression: the compact-range serializer emits `inf`/`NaN` start bounds
// (e.g. `inf..2.0`, `NaN..=2.0`), but the `Range`/`RangeInclusive`
// deserializers only entered the compact-range path when the first byte
// was recognised by `is_number_start` (digit / `+` / `-` / `.` / `b`).
// Since `inf` and `NaN` start with a letter, those inputs failed to
// round-trip with `ExpectedNamedStructLike`, even though `RangeFrom`
// (e.g. `inf..`) round-tripped fine.
let compact = ron::ser::PrettyConfig::new().compact_ranges(true);

// Range<f64> with +inf start
let r = f64::INFINITY..2.0;
let s = ron::ser::to_string_pretty(&r, compact.clone()).unwrap();
assert_eq!(s, "inf..2.0");
let de: std::ops::Range<f64> = ron::from_str(&s).unwrap();
assert!(de.start.is_infinite() && de.start.is_sign_positive());
assert_eq!(de.end, 2.0);

// Range<f64> with NaN start
let r = f64::NAN..2.0;
let s = ron::ser::to_string_pretty(&r, compact.clone()).unwrap();
assert_eq!(s, "NaN..2.0");
let de: std::ops::Range<f64> = ron::from_str(&s).unwrap();
assert!(de.start.is_nan());
assert_eq!(de.end, 2.0);

// RangeInclusive<f64> with +inf start
let r = f64::INFINITY..=2.0;
let s = ron::ser::to_string_pretty(&r, compact.clone()).unwrap();
assert_eq!(s, "inf..=2.0");
let de: std::ops::RangeInclusive<f64> = ron::from_str(&s).unwrap();
assert!(de.start().is_infinite() && de.start().is_sign_positive());
assert_eq!(*de.end(), 2.0);

// RangeInclusive<f64> with NaN start
let r = f64::NAN..=2.0;
let s = ron::ser::to_string_pretty(&r, compact.clone()).unwrap();
assert_eq!(s, "NaN..=2.0");
let de: std::ops::RangeInclusive<f64> = ron::from_str(&s).unwrap();
assert!(de.start().is_nan());
assert_eq!(*de.end(), 2.0);

// Direct parse of the serializer output (independent of the serializer).
let de: std::ops::Range<f64> = ron::from_str("inf..2.0").unwrap();
assert!(de.start.is_infinite());
let de: std::ops::Range<f64> = ron::from_str("NaN..2.0").unwrap();
assert!(de.start.is_nan());
}
Loading