Skip to content

YAML error source locations, sequence aggregation, NoDuplicateKeys, alias guards, and SourceMap - #40

Closed
clockblocker wants to merge 5 commits into
mainfrom
yaml-error-locations-and-validation
Closed

clockblocker wants to merge 5 commits into
mainfrom
yaml-error-locations-and-validation

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

Improves YAML deserialization diagnostics and safety while remaining backwards-compatible with existing callers and zero-overhead for non-location formats (such as JSON). Structured as 5 independent semantic commits (matching a clean upstream split order):

  1. b1dc17c0 — Guard rfl::yaml::Reader against recursive aliases and out-of-bounds literal marks
    • Tracks active ancestor YAML::Nodes via a thread-local RAII ActiveNodeGuard (in src/rfl/yaml/Reader.cpp), rejecting cyclic aliases via rfl::Error instead of stack overflow while keeping const rfl::yaml::Reader thread-safe and allowing non-cyclic (DAG) alias reuse.
    • Bounds-checks YAML::Mark::pos before indexing yaml_str_ when trimming literal block scalars.
  2. fdea5d61 — Add optional rfl::SourceLocation to rfl::ErrorDetail and populate it in YAML
    • Extends rfl::ErrorDetail with std::optional<rfl::SourceLocation> location = std::nullopt (line, column, exact, 1-based), populated via if constexpr (requires { _r.source_location(...); }) only for readers that support it.
    • Short-circuits attach_source_location() via Error::has_all_source_locations() to avoid O(depth) copies/allocations on nested error unwinding.
    • Preserves Error::what() verbatim.
  3. af788eb3 — Collect all element errors in YAML sequences (honoring rfl::FailFast)
    • Collects all failing element errors in rfl::yaml::Reader::read_array (stopping at the first error under rfl::FailFast).
    • Tracks num_failed_ only on the [[unlikely]] error path in VectorReader (zero extra instructions on the std::vector happy path), guards std::inplace_vector against emitting duplicate capacity errors on excess elements, and tracks num_failed_ alongside num_set_ in ArrayReader and TupleReader.
  4. 002eeac7 — Add opt-in rfl::NoDuplicateKeys processor for YAML duplicate mapping keys
    • Keeps default duplicate-key behavior unchanged for backwards compatibility.
    • Adds opt-in rfl::NoDuplicateKeys processor to reject repeated mapping keys with Duplicate key '<k>'. at the offending key's source location, aggregated alongside other field errors (and honoring rfl::FailFast).
  5. 264b5939 — Add optional rfl::SourceMap support to rfl::yaml::read and rfl::yaml::load
    • Adds opt-in rfl::SourceMap mapping ErrorDetail::PathSegment paths to value/key SourceLocations (plus find_nearest fallback with exact = false).
    • Keeps the non-template implementation out-of-line in src/rfl/SourceMap.cpp (via Pimpl) and keeps SourceMap.hpp out of umbrella include/rfl.hpp so non-YAML TUs pay no compile-time cost.
    • Uses a bounds-safe SourceMap::Builder + opaque SourceMap::NodeId and RFL_API rfl::yaml::build_source_map(const YAML::Node&) reusing Reader::ActiveNodeGuard.

Verification

  • Full CMake test suite (-DREFLECTCPP_JSON=ON -DREFLECTCPP_YAML=ON -DREFLECTCPP_STRICT_WARNINGS=ON): 630/630 passed (generic, json, json_c_arrays_and_inheritance, cli, env, yaml).
  • Shared library build (-DREFLECTCPP_BUILD_SHARED=ON with -fvisibility=hidden): 85/85 YAML tests passed.
  • Self-contained header checks (-Wall -Werror -fsyntax-only) on all 26 modified/added headers in both C++20 and C++23 (-DREFLECTCPP_USE_STD_EXPECTED), plus C++23 compile checks on all 4 new YAML test files.
  • Relative-include check verified zero #include <rfl/...> inside include/rfl/.

@clockblocker clockblocker changed the title YAML reader: source locations in Error::details(), all errors in sequences, duplicate-key & cycle detection YAML reader: source locations in Error::details(), SourceMap, all errors in sequences, duplicate-key & cycle detection Nov 25, 2026
@clockblocker
clockblocker force-pushed the yaml-error-locations-and-validation branch from 8af794b to 503dcc5 Compare November 25, 2026 10:58
…iteral marks

Track ancestor YAML nodes via a thread-local RAII guard (ActiveNodeGuard in
src/rfl/yaml/Reader.cpp) across read_object and read_array so cyclic YAML
aliases return an rfl::Error instead of overflowing the stack, while keeping
const Reader instances thread-safe and allowing non-cyclic (DAG) alias reuse.

Also bounds-check YAML::Mark::pos before indexing yaml_str_ when trimming
trailing newlines on literal block scalars.
…in YAML

Extend rfl::ErrorDetail with an optional rfl::SourceLocation{line, column, exact}
(1-based), filled in by readers that provide source_location(...) and left
std::nullopt by other readers with zero hot-path overhead.

attach_source_location() short-circuits via Error::has_all_source_locations()
so unwinding deeply nested errors does not allocate or copy once leaf locations
are populated. Wrapping preserves leaf locations and Error::what() stays
unchanged.
Update rfl::yaml::Reader::read_array to continue past failing elements and
aggregate all element errors via to_single_error(), matching object field
error aggregation while stopping at the first error under rfl::FailFast.

Track num_failed_ only on the error path in VectorReader (keeping zero extra
instructions on the std::vector happy path for JSON and binary formats),
guard std::inplace_vector against emitting duplicate capacity errors on
excess elements, and track num_failed_ alongside num_set_ in ArrayReader
and TupleReader so indices stay accurate and partial destruction remains
safe.
…keys

Keep existing default duplicate-key behavior intact for backwards
compatibility, and add an opt-in rfl::NoDuplicateKeys processor (matching
rfl::NoExtraFields and rfl::EnumNamesOnly).

When rfl::NoDuplicateKeys is passed, rfl::yaml::Reader::read_object tracks
seen keys per mapping and reports each repeated key as
"Duplicate key '<k>'." at the repeated key's source location, aggregated
alongside other field errors via add_error() (and honoring rfl::FailFast).
…:load

Add an opt-in rfl::SourceMap type mapping ErrorDetail::PathSegment paths to
rfl::SourceLocation entries (value and mapping key locations, plus
find_nearest fallback to the deepest enclosing ancestor with exact = false).

- Move the non-template SourceMap implementation out of the header into
  src/rfl/SourceMap.cpp (via Pimpl) and keep SourceMap.hpp out of the
  umbrella rfl.hpp header so non-YAML TUs pay no compile-time cost.
- Replace raw node-index mutators with a bounds-safe SourceMap::Builder
  and opaque SourceMap::NodeId.
- Expose RFL_API rfl::yaml::build_source_map(const YAML::Node&) in
  src/rfl/yaml/Reader.cpp, reusing Reader::ActiveNodeGuard for cycle safety.
- Add SourceMap* overloads for rfl::yaml::read and rfl::yaml::load that
  clear the map up front and populate it on success.
@clockblocker clockblocker changed the title YAML reader: source locations in Error::details(), SourceMap, all errors in sequences, duplicate-key & cycle detection YAML error source locations, sequence aggregation, NoDuplicateKeys, alias guards, and SourceMap Nov 25, 2026
@clockblocker
clockblocker force-pushed the yaml-error-locations-and-validation branch from 503dcc5 to 264b593 Compare November 25, 2026 11:17
@clockblocker

Copy link
Copy Markdown
Owner Author

Split into 5 stacked PRs for easier review:

  1. Guard rfl::yaml::Reader against recursive aliases and out-of-bounds literal marks #41 — Recursive YAML alias guard + literal-mark bounds check
  2. YAML recursive alias guard and rfl::SourceLocation on ErrorDetail #42 — Optional rfl::SourceLocation on ErrorDetail + YAML locations
  3. Collect all element errors in YAML sequences (honoring rfl::FailFast) #43 — Sequence error aggregation + FailFast
  4. Add opt-in rfl::NoDuplicateKeys processor for YAML duplicate mapping keys #44 — Opt-in rfl::NoDuplicateKeys processor
  5. Add optional rfl::SourceMap support to rfl::yaml::read and rfl::yaml::load #45 — Opt-in rfl::SourceMap + read/load overloads

@clockblocker

Copy link
Copy Markdown
Owner Author

Update: combined #41 and #42 into a single PR (#42, 2 semantic commits off main). Final 4-PR stack:

  1. YAML recursive alias guard and rfl::SourceLocation on ErrorDetail #42 (main ← yaml-error-source-locations)
  2. Collect all element errors in YAML sequences (honoring rfl::FailFast) #43 (yaml-error-source-locations ← yaml-sequence-error-aggregation)
  3. Add opt-in rfl::NoDuplicateKeys processor for YAML duplicate mapping keys #44 (yaml-sequence-error-aggregation ← yaml-no-duplicate-keys)
  4. Add optional rfl::SourceMap support to rfl::yaml::read and rfl::yaml::load #45 (yaml-no-duplicate-keys ← yaml-source-map)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant