Skip to content

Fixes Default Literals | Issue #368 - #3066

Open
IftekharUddin wants to merge 1 commit into
serde-rs:masterfrom
IftekharUddin:master
Open

IftekharUddin wants to merge 1 commit into
serde-rs:masterfrom
IftekharUddin:master

Conversation

@IftekharUddin

Copy link
Copy Markdown

Serde_Default_Expression.pdf

serde_default_expr_implementation_report.docx

Adds a new field-level attribute, default_expr, that allows an arbitrary
Rust expression to be used as a missing-field default during deserialization,
without requiring a dedicated helper function.

Motivation

The existing default = "path" form requires a named callable, forcing
boilerplate for trivial defaults like literals or simple constructors.
default_expr eliminates that overhead while keeping semantics explicit
and avoiding overload of the existing default attribute.

Changes

  • symbol.rs: introduce the DEFAULT_EXPR symbol
  • attr.rs: parse default_expr as syn::Expr; extend Default enum
    with Expr(syn::Expr); enforce single-default-source conflict rule
  • de.rs: emit expression directly in missing-field branches via
    quote_spanned!; no Default bound added for default_expr
  • de/struct_.rs: exhaustive match coverage for new Default::Expr variant

Tests

  • Runtime: test_default_expr_struct, test_default_expr_tuple,
    test_skip_with_expr
  • Compile-fail (nightly): conflict.rs, conflict_path.rs,
    type_mismatch.rs

More in depth documents have been attached. It goes over the game plan and then the implementation that was followed.

@IftekharUddin

Copy link
Copy Markdown
Author

hmmm i thought i checked all the tests, I'll look into that.

@IftekharUddin

IftekharUddin commented May 17, 2026

Copy link
Copy Markdown
Author

got tired, will pick it up later but I'm confused by the test. I'm like 70% sure the type mismatch file issue is that it needs to check that it doesn't auto convert? but this change in the default expression would, auto, convert? Too tired, i'm confusing myself.

Adds a new field-level attribute that embeds an arbitrary Rust expression
as the missing-field default during deserialization, eliminating the
boilerplate of writing a helper function for simple literals and
constructors. Type checking is delegated entirely to Rust; no implicit
coercions are performed.

- `Default::Expr(syn::Expr)` variant added to the internal default enum
- Attribute parsed in `Field::from_ast`; conflicts with `default` and
  `default = "path"` are detected and reported with a clear diagnostic
- `parse_lit_into_expr` uses `cx` for enriched error messages on
  malformed expressions
- Code generation in `expr_is_missing` / `expr_is_missing_seq` emits
  the expression with `quote_spanned!` so type errors point to the
  attribute site
- No `Default` bound is added for `default_expr`, matching the behavior
  of `default = "path"`
- Runtime tests cover named structs, tuple structs, and the
  `skip_deserializing` interaction
- UI compile-fail tests cover default conflicts (bare and path forms)
  and type mismatch; nightly required to bless type_mismatch.stderr
@IftekharUddin

Copy link
Copy Markdown
Author

#368

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant